1 ------------------------------------------------------------------------
2 -- MAIN PROCESS STEP 3: insert \xkanjiskip (prefix: none)
3 ------------------------------------------------------------------------
5 local node_type = node.type
6 local node_new = node.new
7 local node_prev = node.prev
8 local node_next = node.next
9 local node_copy = node.copy
10 local has_attr = node.has_attribute
11 local node_insert_before = node.insert_before
12 local node_insert_after = node.insert_after
13 local node_hpack = node.hpack
14 local round = tex.round
16 local id_penalty = node.id('penalty')
17 local id_glyph = node.id('glyph')
18 local id_glue = node.id('glue')
19 local id_glue_spec = node.id('glue_spec')
20 local id_kern = node.id('kern')
21 local id_hlist = node.id('hlist')
22 local id_ins = node.id('ins')
23 local id_mark = node.id('mark')
24 local id_adjust = node.id('adjust')
25 local id_math = node.id('math')
26 local id_whatsit = node.id('whatsit')
28 local attr_icflag = luatexbase.attributes['ltj@icflag']
29 local attr_autospc = luatexbase.attributes['ltj@autospc']
30 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
31 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
32 local max_dimen = 1073741823
43 -- (glyph_node nr) ... (node nq) <GLUE> ,,, (node np)
47 local after_wchar = 2 -- nr is a Japanese glyph_node
48 local insert_skip = no_skip
51 local cstb_get_inhibit_xsp_table = ltj.int_get_inhibit_xsp_table
53 local function is_japanese_glyph_node(p)
54 return p and (p.id==id_glyph)
55 and (p.font==has_attr(p,attr_curjfnt))
58 local function get_zero_glue()
59 local g = node_new(id_glue_spec)
60 g.width = 0; g.stretch_order = 0; g.stretch = 0
61 g.shrink_order = 0; g.shrink = 0
65 -- the following 2 functions are the lowest part.
66 -- cx: the Kanji code of np
67 local function insert_ascii_kanji_xkskip(q, cx)
68 if cstb_get_inhibit_xsp_table(cx)<=1 then return end
69 local g = node_new(id_glue)
70 g.subtype = 0; g.spec = node.copy(xkanji_skip)
71 node_insert_after(head, q, g)
74 local function insert_kanji_ascii_xkskip(q, p)
77 while p.components and p.subtype
78 and math.floor(p.subtype/2)%2==1 do
79 p = p.components; c = p.char
81 if cstb_get_inhibit_xsp_table(c)%2 == 1 then
82 if cstb_get_inhibit_xsp_table(nrc)%2 == 0 then g = false end
87 g.subtype = 0; g.spec = node.copy(xkanji_skip)
88 node_insert_after(head, q, g)
93 local function set_insert_skip_after_achar(p)
95 while p.components and p.subtype
96 and math.floor(p.subtype/2)%2 == 1 do
97 p=node.tail(p.components); c = p.char
99 if cstb_get_inhibit_xsp_table(c)>=2 then
100 insert_skip = after_schar
102 insert_skip = no_skip
106 local function get_kanji_skip_from_jfm(p)
107 local px = { ltj.font_metric_table[p.font].size,
108 ltj.font_metric_table[p.font].jfm }
109 local i = ltj.metrics[px[2]].kanjiskip
110 print(p.font, px[1], px[2], ltj.metrics[px[2]].dir, i)
112 return { round(i[1]*px[1]), round(i[2]*px[1]), round(i[3]*px[1]) }
117 local function insert_kanji_skip()
118 print('a', utf.char(np.char))
119 local g = node_new(id_glue); g.subtype=0
120 if kanji_skip.width==max_dimen then -- use kanjiskip from JFM
121 local gx = node_new(id_glue_spec);
122 gx.stretch_order = 0; gx.shrink_order = 0
123 local bk = nil -- incomplete
124 local ak = get_kanji_skip_from_jfm(np)
127 gx.width = round(ltj.ja_diffmet_rule(bk[1], ak[1]))
128 gx.stretch = round(ltj.ja_diffmet_rule(bk[2], ak[2]))
129 gx.shrink = -round(ltj.ja_diffmet_rule(-bk[3], -ak[3]))
131 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
134 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
136 gx = get_zero_glue()-- ???
140 g.spec=node.copy(kanji_skip)
142 local h = node_prev(np)
143 if h and has_attr(h, attr_icflag)==TEMPORARY then
144 if h.id==id_kern then
145 g.spec.width = g.spec.width + h.kern
146 head = node.remove(head, h)
147 node_insert_before(head, np, g)
149 h.spec.width = g.spec.width + h.spec.width
150 h.spec.stretch = g.spec.stretch + h.spec.stretch
151 h.spec.shrink = g.spec.shrink + h.spec.shrink
154 node_insert_before(head, np, g)
156 print('b', utf.char(np.char))
159 -- When p is a glyph_node ...
160 local function insks_around_char()
161 if is_japanese_glyph_node(np) then
162 if insert_skip==after_wchar then
164 elseif insert_skip==after_schar then
165 insert_ascii_kanji_xkskip(nq, np.char)
167 insert_skip=after_wchar; nrc = np.char
169 if insert_skip==after_wchar then
170 insert_kanji_ascii_xkskip(nq, np)
172 set_insert_skip_after_achar(np)
177 -- Return first and last glyph nodes in a hbox
178 local first_char = nil
179 local last_char = nil
180 local find_first_char = nil
181 local function check_box(box_ptr)
182 local p = box_ptr; local found_visible_node = false
184 if p.id==id_glyph then
186 if find_first_char then
187 first_char = p; find_first_char = false
189 last_char = p; found_visible_node = true; p=node_next(p)
190 if not p then return found_visible_node end
193 if p.id==id_hlist then
194 found_visible_node = true
196 if check_box(p.head) then found_visible_node = true end
197 else if find_first_char then
198 find_first_char = false
203 elseif p.id==id_ins or p.id==id_mark
204 or p.id==id_adjust or p.id==id_whatsit
205 or p.id==id_penalty then
208 found_visible_node = true
209 if find_first_char then
210 find_first_char = false
217 return found_visible_node
220 -- When np is a hlist_node ...
221 local function insks_around_hbox()
223 find_first_char = true; first_char = nil; last_char = nil
224 if check_box(np.head) then
226 if is_japanese_glyph_node(first_char) then
227 nrc = first_char.char
228 if insert_skip==after_schar then
229 insert_ascii_kanji_xkskip(nq, first_char.char)
230 elseif insert_skip==after_wchar then
233 insert_skip = after_wchar
234 elseif first_char then
235 if insert_skip==after_wchar then
236 insert_kanji_ascii_xkskip(nq, first_char)
238 set_insert_skip_after_achar(first_char)
241 if is_japanese_glyph_node(last_char) then
242 if is_japanese_glyph_node(node_next(np)) then
243 local g = node_new(id_glue)
244 g.subtype = 0; g.spec = node.copy(kanji_skip)
245 node_insert_after(head, np, g)
247 insert_skip = after_wchar; nrc = last_char.char
248 elseif last_char then
249 set_insert_skip_after_achar(last_char)
250 else insert_skip = no_skip
252 else insert_skip = no_skip
254 else insert_skip = no_skip
259 -- When np is a penalty ...
260 local function insks_around_penalty()
264 -- When np is a kern ...
266 local function insks_around_kern()
267 if np.subtype==1 then -- \kern or \/
268 local i = has_attr(np, attr_icflag)
269 if not i or i==FROM_JFM then -- \kern
270 insert_skip = no_skip
271 elseif i==ITALIC or i==LINE_END or i==TEMPORARY then
274 elseif np.subtype==2 then
275 -- (np = kern from \accent) .. (accent char) .. (kern from \accent) .. (glyph)
276 np = node_next(node_next(np))
280 -- When np is a math_node ...
281 local function insks_around_math()
282 local g = { char = -1 }
283 if (np.subtype==0) and (insert_skip==after_wchar) then
284 insert_kanji_ascii_xkskip(nq, g)
285 insert_skip = no_skip
287 nq = np; set_insert_skip_after_achar(g)
291 function ltj.int_insert_kanji_skip(ahead)
292 kanji_skip=tex.skip['kanjiskip']
293 xkanji_skip=tex.skip['xkanjiskip']
295 np = head; nq = nil; insert_skip = no_skip
297 if np.id==id_glyph then
299 insks_around_char(); np=node_next(np)
300 until (not np) or np.id~=id_glyph
302 if np.id==id_hlist then
304 elseif np.id==id_penalty then
305 insks_around_penalty()
306 elseif np.id==id_kern then
308 elseif np.id==id_math then
310 elseif np.id==id_ins or np.id==id_mark
311 or np.id==id_adjust or np.id==id_whatsit then
315 -- rule, disc, glue, margin_kern
316 insert_skip = no_skip