1 local ltjb = luatexja.base
2 local ltjc = luatexja.charrange
3 local ltjs = luatexja.stack
4 local ltjj = luatexja.jfmglue
6 local node_type = node.type
7 local node_new = node.new
8 local node_prev = node.prev
9 local node_next = node.next
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_spec = node.id('glue_spec')
19 local id_glue = node.id('glue')
20 local id_kern = node.id('kern')
21 local id_hlist = node.id('hlist')
22 local id_vlist = node.id('vlist')
23 local id_rule = node.id('rule')
24 local id_math = node.id('math')
25 local id_whatsit = node.id('whatsit')
26 local sid_user = node.subtype('user_defined')
28 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
29 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
30 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
31 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
34 local lang_ja_token = token.create('ltj@japanese')
35 local lang_ja = lang_ja_token[2]
38 local ljfm_find_char_class = ltj.int_find_char_class
51 ------------------------------------------------------------------------
53 -- ltj.ext_... : called from \directlua{}
54 -- ltj.int_... : called from other Lua codes, but not from \directlua{}
55 -- (other) : only called from this file
58 function ltj.error(s,t)
59 tex.error('LuaTeX-ja error: ' .. s ,t)
62 -- Three aux. functions, bollowed from tex.web
64 local function print_scaled(s)
70 out=out..tostring(math.floor(s/unity)) .. '.'
73 if delta>unity then s=s+32768-50000 end
74 out=out .. tostring(math.floor(s/unity))
81 local function print_glue(d,order)
82 local out=print_scaled(d)
86 out=out..'l'; order=order-1
94 local function print_spec(p)
95 local out=print_scaled(p.width)..'pt'
97 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
100 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
105 -- return true if and only if p is a Japanese character node
106 local function is_japanese_glyph_node(p)
107 return p and (p.id==id_glyph)
108 and (p.font==has_attr(p,attr_curjfnt))
111 function math.two_add(a,b) return a+b end
112 function math.two_average(a,b) return (a+b)/2 end
114 ---- table: charprop_stack_table [stack_level][chr_code].{pre|post|xsp}
116 ------------------------------------------------------------------------
117 -- CODE FOR GETTING/SETTING PARAMETERS
118 ------------------------------------------------------------------------
120 -- EXT: print parameters that don't need arguments
121 function ltj.ext_get_parameter_unary(k)
122 if k == 'yalbaselineshift' then
123 tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
124 elseif k == 'yjabaselineshift' then
125 tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
126 elseif k == 'kanjiskip' then
127 tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
128 elseif k == 'xkanjiskip' then
129 tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
130 elseif k == 'jcharwidowpenalty' then
131 tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
132 elseif k == 'autospacing' then
133 tex.write(tex.getattribute('ltj@autospc'))
134 elseif k == 'autoxspacing' then
135 tex.write(tex.getattribute('ltj@autoxspc'))
136 elseif k == 'differentjfm' then
137 if luatexja.jfmglue.diffmet_rule == math.max then
139 elseif luatexja.jfmglue.diffmet_rule == math.min then
141 elseif luatexja.jfmglue.diffmet_rule == math.two_average then
143 elseif luatexja.jfmglue.diffmet_rule == math.two_add then
145 else -- This can't happen.
151 -- EXT: print parameters that need arguments
152 function ltj.ext_get_parameter_binary(k,c)
153 if k == 'jacharrange' then
155 ltjb.package_error('luatexja',
156 'invalid character range number (' .. c .. ')',
157 'A character range number should be in the range 0..216,\n'..
158 'So I changed this one to zero.')
161 tex.write(ltjc.get_range_setting(c))
163 if c<0 or c>0x10FFFF then
164 ltjb.package_error('luatexja',
165 'bad character code (' .. c .. ')',
166 'A character number must be between -1 and 0x10ffff.\n'..
167 "(-1 is used for denoting `math boundary')\n"..
168 'So I changed this one to zero.')
171 if k == 'prebreakpenalty' then
172 tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
173 elseif k == 'postbreakpenalty' then
174 tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
175 elseif k == 'kcatcode' then
176 tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
177 elseif k == 'chartorange' then
178 tex.write(ltjc.char_to_range(c))
179 elseif k == 'jaxspmode' or k == 'alxspmode' then
180 tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
185 -- EXT: print \global if necessary
186 function ltj.ext_print_global()
187 if ltj.isglobal=='global' then tex.sprint('\\global') end
191 ------------------------------------------------------------------------
192 -- MAIN PROCESS STEP 1: replace fonts (prefix: main1)
193 ------------------------------------------------------------------------
194 ltj.box_stack_level = 0
195 -- This is used in Step 2 (JFM glue/kern) and Step 3 (\[x]kanjiskip).
197 local function main1_suppress_hyphenate_ja(head)
198 for p in node.traverse_id(id_glyph, head) do
199 if ltjc.is_ucs_in_japanese_char(p) then
200 local v = has_attr(p, attr_curjfnt)
203 node.set_attribute(p, attr_jchar_class,
204 ljfm_find_char_class(p.char, ltj.font_metric_table[v].jfm))
206 v = has_attr(p, attr_ykblshift)
208 node.set_attribute(p, attr_yablshift, v)
210 node.unset_attribute(p, attr_yablshift)
219 -- mode: true iff this function is called from hpack_filter
220 local function main1_set_box_stack_level(head, mode)
221 local box_set = false
223 local cl = tex.currentgrouplevel + 1
225 if p.id==id_whatsit and p.subtype==sid_user and p.user_id==30112 then
227 if mode and g.value==cl then box_set = true end
228 head, p = node.remove(head, g)
229 else p = node_next(p)
233 ltj.box_stack_level = tex.getcount('ltj@@stack') + 1
235 ltj.box_stack_level = tex.getcount('ltj@@stack')
237 if not head then -- prevent that the list is null
238 head = node_new(id_kern); head.kern = 0; head.subtype = 1
244 luatexbase.add_to_callback('hpack_filter',
246 return main1_set_box_stack_level(head, true)
247 end,'ltj.hpack_filter_pre',1)
248 luatexbase.add_to_callback('pre_linebreak_filter',
250 return main1_set_box_stack_level(head, false)
251 end,'ltj.pre_linebreak_filter_pre',1)
252 luatexbase.add_to_callback('hyphenate',
254 return main1_suppress_hyphenate_ja(head)
258 ------------------------------------------------------------------------
259 -- MAIN PROCESS STEP 4: width of japanese chars (prefix: main4)
260 ------------------------------------------------------------------------
263 local function main4_get_hss()
264 local hss = node_new(id_glue)
265 local fil_spec = node_new(id_glue_spec)
267 fil_spec.stretch = 65536
268 fil_spec.stretch_order = 2
269 fil_spec.shrink = 65536
270 fil_spec.shrink_order = 2
275 local function main4_set_ja_width(head, dir)
277 local met_tb, t, s, g, q, a, h
278 local m = false -- is in math mode?
280 local v=has_attr(p,attr_yablshift) or 0
281 if p.id==id_glyph then
282 p.yoffset = p.yoffset-v
283 if is_japanese_glyph_node(p) then
284 met_tb = ltj.font_metric_table[p.font]
285 t = ltj.metrics[met_tb.jfm]
286 s = t.char_type[has_attr(p,attr_jchar_class)]
287 if s.width ~= 'prop' and
288 not(s.left==0.0 and s.down==0.0 and s.align=='left'
289 and round(s.width*met_tb.size)==p.width) then
290 -- must be encapsuled by a \hbox
291 head, q = node.remove(head, p)
292 local full_width = round(s.width*met_tb.size)
293 p.yoffset=p.yoffset-round(met_tb.size*s.down)
294 p.xoffset=p.xoffset-round(met_tb.size*s.left)
296 -- The next if block sets h:=(head of a new list)
297 if s.align=='left' then
298 h = node_new(id_kern); h.subtype = 0
299 h.kern = full_width - p.width
301 elseif s.align=='right' then
302 h = node_new(id_kern); h.subtype = 0
303 h.kern = full_width - p.width
304 p.next = nil; h.next = p
305 elseif s.align=='middle' then
306 local total = full_width - p.width
307 h = node_new(id_kern); h.subtype = 0
308 h.kern = round(total/2); p.next = h
309 h = node_new(id_kern); h.subtype = 0
310 h.kern = total - round(total/2); h.next = p
312 g = node_new(id_hlist); g.width = full_width
313 g.height = round(met_tb.size*s.height)
314 g.depth = round(met_tb.size*s.depth)
315 g.glue_set = 0; g.glue_order = 0; g.head = h
316 g.shift = 0; g.dir = dir or 'TLT'
317 node.set_attribute(g, attr_icflag, PACKED)
319 head = node_insert_before(head, q, g)
321 head = node_insert_after(head, node.tail(head), g)
328 elseif p.id==id_math then
329 m=(p.subtype==0); p=node_next(p)
332 if p.id==id_hlist or p.id==id_vlist then
334 elseif p.id==id_rule then
335 p.height=p.height-v; p.depth=p.depth+v
345 -- mode = true iff main_process is called from pre_linebreak_filter
346 local function main_process(head, mode, dir)
348 p = ltjj.main(p,mode)
349 p = main4_set_ja_width(p, dir)
356 function ltj.ext_show_node_list(head,depth,print_fn)
360 debug_show_node_X(head, print_fn); head = node_next(head)
363 print_fn(debug_depth .. ' (null list)')
366 function ltj.ext_show_node(head,depth,print_fn)
369 debug_show_node_X(head, print_fn)
371 print_fn(debug_depth .. ' (null list)')
374 function debug_show_node_X(p,print_fn)
375 local k = debug_depth
377 local pt=node_type(p.id)
378 local base = debug_depth .. string.format('%X', has_attr(p,attr_icflag) or 0)
379 .. ' ' .. pt .. ' ' .. p.subtype
380 if pt == 'glyph' then
381 print_fn(base, utf.char(p.char), p.font)
382 elseif pt=='hlist' then
383 s = base .. '(' .. print_scaled(p.height) .. '+'
384 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
385 if p.glue_sign >= 1 then
386 s = s .. ' glue set '
387 if p.glue_sign == 2 then s = s .. '-' end
388 s = s .. tostring(math.floor(p.glue_set*10000)/10000)
389 if p.glue_order == 0 then
393 for i = 2, p.glue_order do s = s .. 'l' end
396 if has_attr(p, attr_icflag, PACKED) then
401 debug_depth=debug_depth.. '.'
403 debug_show_node_X(q, print_fn); q = node_next(q)
406 elseif pt == 'glue' then
407 s = base .. ' ' .. print_spec(p.spec)
408 if has_attr(p, attr_icflag)==FROM_JFM then
409 s = s .. ' (from JFM)'
410 elseif has_attr(p, attr_icflag)==KANJI_SKIP then
411 s = s .. ' (kanjiskip)'
412 elseif has_attr(p, attr_icflag)==XKANJI_SKIP then
413 s = s .. ' (xkanjiskip)'
416 elseif pt == 'kern' then
417 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
419 s = s .. ' (for accent)'
420 elseif has_attr(p, attr_icflag)==ITALIC then
421 s = s .. ' (italic correction)'
422 elseif has_attr(p, attr_icflag)==FROM_JFM then
423 s = s .. ' (from JFM)'
424 elseif has_attr(p, attr_icflag)==LINE_END then
425 s = s .. " (from 'lineend' in JFM)"
428 elseif pt == 'penalty' then
429 s = base .. ' ' .. tostring(p.penalty)
430 if has_attr(p, attr_icflag)==KINSOKU then
431 s = s .. ' (for kinsoku)'
434 elseif pt == 'whatsit' then
435 s = base .. ' subtype: ' .. tostring(p.subtype)
436 if p.subtype==sid_user then
437 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
439 s = s .. node.subtype(p.subtype)
450 luatexbase.add_to_callback('pre_linebreak_filter',
451 function (head,groupcode)
452 return main_process(head, true, tex.textdir)
453 end,'ltj.pre_linebreak_filter',2)
454 luatexbase.add_to_callback('hpack_filter',
455 function (head,groupcode,size,packtype, dir)
456 return main_process(head, false, dir)
457 end,'ltj.hpack_filter',2)