1 local ltjb = luatexja.base
2 local ltjc = luatexja.charrange
3 local ltjs = luatexja.stack
4 local ltjj = luatexja.jfmglue
5 local ltjf = luatexja.jfont
7 local node_type = node.type
8 local node_new = node.new
9 local node_prev = node.prev
10 local node_next = node.next
11 local has_attr = node.has_attribute
12 local node_insert_before = node.insert_before
13 local node_insert_after = node.insert_after
14 local node_hpack = node.hpack
15 local round = tex.round
17 local id_penalty = node.id('penalty')
18 local id_glyph = node.id('glyph')
19 local id_glue_spec = node.id('glue_spec')
20 local id_glue = node.id('glue')
21 local id_kern = node.id('kern')
22 local id_hlist = node.id('hlist')
23 local id_vlist = node.id('vlist')
24 local id_rule = node.id('rule')
25 local id_math = node.id('math')
26 local id_whatsit = node.id('whatsit')
27 local sid_user = node.subtype('user_defined')
29 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
30 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
31 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
42 local IC_PROCESSED = 9
45 ------------------------------------------------------------------------
47 -- ltj.ext_... : called from \directlua{}
48 -- ltj.int_... : called from other Lua codes, but not from \directlua{}
49 -- (other) : only called from this file
52 function ltj.error(s,t)
53 tex.error('LuaTeX-ja error: ' .. s ,t)
56 -- Three aux. functions, bollowed from tex.web
58 local function print_scaled(s)
64 out=out..tostring(math.floor(s/unity)) .. '.'
67 if delta>unity then s=s+32768-50000 end
68 out=out .. tostring(math.floor(s/unity))
75 local function print_glue(d,order)
76 local out=print_scaled(d)
80 out=out..'l'; order=order-1
88 local function print_spec(p)
89 local out=print_scaled(p.width)..'pt'
91 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
94 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
99 -- return true if and only if p is a Japanese character node
100 local function is_japanese_glyph_node(p)
101 return p and (p.id==id_glyph)
102 and (p.font==has_attr(p,attr_curjfnt))
105 function math.two_add(a,b) return a+b end
106 function math.two_average(a,b) return (a+b)/2 end
108 ---- table: charprop_stack_table [stack_level][chr_code].{pre|post|xsp}
110 ------------------------------------------------------------------------
111 -- CODE FOR GETTING/SETTING PARAMETERS
112 ------------------------------------------------------------------------
114 -- EXT: print parameters that don't need arguments
115 function ltj.ext_get_parameter_unary(k)
116 if k == 'yalbaselineshift' then
117 tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
118 elseif k == 'yjabaselineshift' then
119 tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
120 elseif k == 'kanjiskip' then
121 tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
122 elseif k == 'xkanjiskip' then
123 tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
124 elseif k == 'jcharwidowpenalty' then
125 tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
126 elseif k == 'autospacing' then
127 tex.write(tex.getattribute('ltj@autospc'))
128 elseif k == 'autoxspacing' then
129 tex.write(tex.getattribute('ltj@autoxspc'))
130 elseif k == 'differentjfm' then
131 if luatexja.jfmglue.diffmet_rule == math.max then
133 elseif luatexja.jfmglue.diffmet_rule == math.min then
135 elseif luatexja.jfmglue.diffmet_rule == math.two_average then
137 elseif luatexja.jfmglue.diffmet_rule == math.two_add then
139 else -- This can't happen.
145 -- EXT: print parameters that need arguments
146 function ltj.ext_get_parameter_binary(k,c)
147 if k == 'jacharrange' then
149 ltjb.package_error('luatexja',
150 'invalid character range number (' .. c .. ')',
151 'A character range number should be in the range 0..216,\n'..
152 'So I changed this one to zero.')
155 tex.write(ltjc.get_range_setting(c))
157 if c<0 or c>0x10FFFF then
158 ltjb.package_error('luatexja',
159 'bad character code (' .. c .. ')',
160 'A character number must be between -1 and 0x10ffff.\n'..
161 "(-1 is used for denoting `math boundary')\n"..
162 'So I changed this one to zero.')
165 if k == 'prebreakpenalty' then
166 tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
167 elseif k == 'postbreakpenalty' then
168 tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
169 elseif k == 'kcatcode' then
170 tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
171 elseif k == 'chartorange' then
172 tex.write(ltjc.char_to_range(c))
173 elseif k == 'jaxspmode' or k == 'alxspmode' then
174 tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
179 -- EXT: print \global if necessary
180 function ltj.ext_print_global()
181 if ltj.isglobal=='global' then tex.sprint('\\global') end
185 ------------------------------------------------------------------------
186 -- MAIN PROCESS STEP 4: width of japanese chars (prefix: main4)
187 ------------------------------------------------------------------------
190 local function main4_get_hss()
191 local hss = node_new(id_glue)
192 local fil_spec = node_new(id_glue_spec)
194 fil_spec.stretch = 65536
195 fil_spec.stretch_order = 2
196 fil_spec.shrink = 65536
197 fil_spec.shrink_order = 2
202 local function main4_set_ja_width(head, dir)
204 local met_tb, t, s, g, q, a, h
205 local m = false -- is in math mode?
207 local v=has_attr(p,attr_yablshift) or 0
208 if p.id==id_glyph then
209 p.yoffset = p.yoffset-v
210 if is_japanese_glyph_node(p) then
211 met_tb = ltjf.font_metric_table[p.font]
212 t = ltjf.metrics[met_tb.jfm]
213 s = t.char_type[has_attr(p,attr_jchar_class)]
214 if s.width ~= 'prop' and
215 not(s.left==0.0 and s.down==0.0 and s.align=='left'
216 and round(s.width*met_tb.size)==p.width) then
217 -- must be encapsuled by a \hbox
218 head, q = node.remove(head, p)
219 local full_width = round(s.width*met_tb.size)
220 p.yoffset=p.yoffset-round(met_tb.size*s.down)
221 p.xoffset=p.xoffset-round(met_tb.size*s.left)
223 -- The next if block sets h:=(head of a new list)
224 if s.align=='left' then
225 h = node_new(id_kern); h.subtype = 0
226 h.kern = full_width - p.width
228 elseif s.align=='right' then
229 h = node_new(id_kern); h.subtype = 0
230 h.kern = full_width - p.width
231 p.next = nil; h.next = p
232 elseif s.align=='middle' then
233 local total = full_width - p.width
234 h = node_new(id_kern); h.subtype = 0
235 h.kern = round(total/2); p.next = h
236 h = node_new(id_kern); h.subtype = 0
237 h.kern = total - round(total/2); h.next = p
239 g = node_new(id_hlist); g.width = full_width
240 g.height = round(met_tb.size*s.height)
241 g.depth = round(met_tb.size*s.depth)
242 g.glue_set = 0; g.glue_order = 0; g.head = h
243 g.shift = 0; g.dir = dir or 'TLT'
244 node.set_attribute(g, attr_icflag, PACKED)
246 head = node_insert_before(head, q, g)
248 head = node_insert_after(head, node.tail(head), g)
255 elseif p.id==id_math then
256 m=(p.subtype==0); p=node_next(p)
259 if p.id==id_hlist or p.id==id_vlist then
261 elseif p.id==id_rule then
262 p.height=p.height-v; p.depth=p.depth+v
272 -- mode = true iff main_process is called from pre_linebreak_filter
273 local function main_process(head, mode, dir)
275 p = ltjj.main(p,mode)
276 p = main4_set_ja_width(p, dir)
283 function ltj.ext_show_node_list(head,depth,print_fn)
287 debug_show_node_X(head, print_fn); head = node_next(head)
290 print_fn(debug_depth .. ' (null list)')
293 function ltj.ext_show_node(head,depth,print_fn)
296 debug_show_node_X(head, print_fn)
298 print_fn(debug_depth .. ' (null list)')
301 function debug_show_node_X(p,print_fn)
302 local k = debug_depth
304 local pt=node_type(p.id)
305 local base = debug_depth .. string.format('%X', has_attr(p,attr_icflag) or 0)
306 .. ' ' .. pt .. ' ' .. p.subtype
307 if pt == 'glyph' then
308 print_fn(base, utf.char(p.char), p.font)
309 elseif pt=='hlist' then
310 s = base .. '(' .. print_scaled(p.height) .. '+'
311 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
312 if p.glue_sign >= 1 then
313 s = s .. ' glue set '
314 if p.glue_sign == 2 then s = s .. '-' end
315 s = s .. tostring(math.floor(p.glue_set*10000)/10000)
316 if p.glue_order == 0 then
320 for i = 2, p.glue_order do s = s .. 'l' end
323 if has_attr(p, attr_icflag, PACKED) then
328 debug_depth=debug_depth.. '.'
330 debug_show_node_X(q, print_fn); q = node_next(q)
333 elseif pt == 'glue' then
334 s = base .. ' ' .. print_spec(p.spec)
335 if has_attr(p, attr_icflag)==FROM_JFM then
336 s = s .. ' (from JFM)'
337 elseif has_attr(p, attr_icflag)==KANJI_SKIP then
338 s = s .. ' (kanjiskip)'
339 elseif has_attr(p, attr_icflag)==XKANJI_SKIP then
340 s = s .. ' (xkanjiskip)'
343 elseif pt == 'kern' then
344 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
346 s = s .. ' (for accent)'
347 elseif has_attr(p, attr_icflag)==IC_PROCESSED then
348 s = s .. ' (italic correction)'
349 -- elseif has_attr(p, attr_icflag)==ITALIC then
350 -- s = s .. ' (italic correction)'
351 elseif has_attr(p, attr_icflag)==FROM_JFM then
352 s = s .. ' (from JFM)'
353 elseif has_attr(p, attr_icflag)==LINE_END then
354 s = s .. " (from 'lineend' in JFM)"
357 elseif pt == 'penalty' then
358 s = base .. ' ' .. tostring(p.penalty)
359 if has_attr(p, attr_icflag)==KINSOKU then
360 s = s .. ' (for kinsoku)'
363 elseif pt == 'whatsit' then
364 s = base .. ' subtype: ' .. tostring(p.subtype)
365 if p.subtype==sid_user then
366 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
368 s = s .. node.subtype(p.subtype)
380 luatexbase.add_to_callback('pre_linebreak_filter',
381 function (head,groupcode)
382 return main_process(head, true, tex.textdir)
383 end,'ltj.pre_linebreak_filter',
384 luatexbase.priority_in_callback('pre_linebreak_filter',
385 'luaotfload.pre_linebreak_filter') + 1)
386 luatexbase.add_to_callback('hpack_filter',
387 function (head,groupcode,size,packtype, dir)
388 return main_process(head, false, dir)
389 end,'ltj.hpack_filter',
390 luatexbase.priority_in_callback('hpack_filter',
391 'luaotfload.hpack_filter') + 1)