2 local floor = math.floor
6 ------------------------------------------------------------------------
8 -- ext_... : called from \directlua{}
9 -- int_... : called from other Lua codes, but not from \directlua{}
10 -- (other) : only called from this file
11 function luatexja.error(s,t)
12 tex.error('LuaTeX-ja error: ' .. s ,t)
14 function luatexja.load_module(name)
15 if not package.loaded['luatexja.' .. name] then
16 local fn = 'ltj-' .. name .. '.lua'
17 local found = kpse.find_file(fn, 'tex')
19 luatexja.error("File `" .. fn .. "' not found",
20 {'This file ' .. fn .. ' is required for LuaTeX-ja.', 'Please check your installation.'})
22 texio.write_nl('(' .. found .. ')')
27 function luatexja.load_lua(fn)
28 local found = kpse.find_file(fn, 'tex')
30 error("File `" .. fn .. "' not found")
32 texio.write_nl('(' .. found .. ')')
38 local icflag_table = {}
39 luatexja.icflag_table = icflag_table
40 icflag_table.ITALIC = 1
41 icflag_table.PACKED = 2
42 icflag_table.KINSOKU = 3
43 icflag_table.FROM_JFM = 6
44 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高
46 icflag_table.KANJI_SKIP = 9
47 icflag_table.XKANJI_SKIP = 10
48 icflag_table.PROCESSED = 11
49 icflag_table.IC_PROCESSED = 12
50 icflag_table.BOXBDD = 15
51 icflag_table.PROCESSED_BEGIN_FLAG = 32
53 local stack_table_index = {}
54 luatexja.stack_table_index = stack_table_index
55 stack_table_index.PRE = 0x200000 -- characterごと
56 stack_table_index.POST = 0x400000 -- characterごと
57 stack_table_index.KCAT = 0x600000 -- characterごと
58 stack_table_index.XSP = 0x800000 -- characterごと
59 stack_table_index.JWP = 0 -- 0のみ
60 stack_table_index.MJT = 0x100 -- 0--255
61 stack_table_index.MJS = 0x200 -- 0--255
62 stack_table_index.MJSS = 0x300 -- 0--255
63 stack_table_index.KSJ = 0x400 -- 0--9
65 local userid_table = {}
66 luatexja.userid_table = userid_table
67 userid_table.IHB = 30111 -- \inhibitglue
68 userid_table.STCK = 30112 -- スタック管理
69 userid_table.OTF = 30113 -- luatexja-otf
70 userid_table.BPAR = 30114 -- 「段落始め」
75 local load_module = luatexja.load_module
76 load_module('base'); local ltjb = luatexja.base
77 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
78 load_module('charrange'); local ltjc = luatexja.charrange
79 load_module('jfont'); local ltjf = luatexja.jfont
80 load_module('inputbuf'); local ltji = luatexja.inputbuf
81 load_module('stack'); local ltjs = luatexja.stack
82 load_module('pretreat'); local ltjp = luatexja.pretreat
83 load_module('jfmglue'); local ltjj = luatexja.jfmglue
84 load_module('setwidth'); local ltjw = luatexja.setwidth
85 load_module('math'); local ltjm = luatexja.math
86 load_module('tangle'); local ltjb = luatexja.base
89 local node_type = node.type
90 local node_new = node.new
91 local node_prev = node.prev
92 local node_next = node.next
93 local has_attr = node.has_attribute
94 local node_insert_before = node.insert_before
95 local node_insert_after = node.insert_after
96 local node_hpack = node.hpack
98 local id_penalty = node.id('penalty')
99 local id_glyph = node.id('glyph')
100 local id_glue_spec = node.id('glue_spec')
101 local id_glue = node.id('glue')
102 local id_kern = node.id('kern')
103 local id_hlist = node.id('hlist')
104 local id_vlist = node.id('vlist')
105 local id_rule = node.id('rule')
106 local id_math = node.id('math')
107 local id_whatsit = node.id('whatsit')
108 local sid_user = node.subtype('user_defined')
110 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
111 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
112 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
113 local attr_icflag = luatexbase.attributes['ltj@icflag']
114 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
115 local cat_lp = luatexbase.catcodetables['latex-package']
117 -- Three aux. functions, bollowed from tex.web
119 local function print_scaled(s)
125 out=out..tostring(floor(s/unity)) .. '.'
128 if delta>unity then s=s+32768-50000 end
129 out=out .. tostring(floor(s/unity))
136 local function print_glue(d,order)
137 local out=print_scaled(d)
141 out=out..'l'; order=order-1
149 function print_spec(p)
150 local out=print_scaled(p.width)..'pt'
152 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
155 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
161 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
163 ------------------------------------------------------------------------
164 -- CODE FOR GETTING/SETTING PARAMETERS
165 ------------------------------------------------------------------------
167 -- EXT: print parameters that don't need arguments
168 function luatexja.ext_get_parameter_unary(k)
169 local t = tex.getcount('ltj@@stack')
170 if k == 'yalbaselineshift' then
171 tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
172 elseif k == 'yjabaselineshift' then
173 tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
174 elseif k == 'kanjiskip' then
175 tex.write(print_spec(ltjs.get_skip_table('kanjiskip', t)))
176 elseif k == 'xkanjiskip' then
177 tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', t)))
178 elseif k == 'jcharwidowpenalty' then
179 tex.write(ltjs.get_penalty_table(stack_table_index.JWP, 0, t))
180 elseif k == 'autospacing' then
181 tex.write(tex.getattribute('ltj@autospc'))
182 elseif k == 'autoxspacing' then
183 tex.write(tex.getattribute('ltj@autoxspc'))
184 elseif k == 'differentjfm' then
185 if luatexja.jfmglue.diffmet_rule == math.max then
187 elseif luatexja.jfmglue.diffmet_rule == math.min then
189 elseif luatexja.jfmglue.diffmet_rule == math.two_average then
191 elseif luatexja.jfmglue.diffmet_rule == math.two_paverage then
192 tex.write('paverage')
193 elseif luatexja.jfmglue.diffmet_rule == math.two_pleft then
195 elseif luatexja.jfmglue.diffmet_rule == math.two_pright then
197 elseif luatexja.jfmglue.diffmet_rule == math.two_add then
199 else -- This can't happen.
206 -- EXT: print parameters that need arguments
207 function luatexja.ext_get_parameter_binary(k,c)
208 local t = tex.getcount('ltj@@stack')
209 if type(c)~='number' then
210 ltjb.package_error('luatexja',
211 'invalid the second argument (' .. tostring(c) .. ')',
212 'I changed this one to zero.')
215 if k == 'jacharrange' then
216 if c>=7*ltjc.ATTR_RANGE then
217 ltjb.package_error('luatexja',
218 'invalid character range number (' .. c .. ')',
219 'A character range number should be in the range 0..'
220 .. 7+ATTR_RANGE-1 .. ",\n"..
221 'So I changed this one to zero.')
224 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
225 tex.write( (c<0) and -1 or ltjc.get_range_setting(c))
227 if c<0 or c>0x10FFFF then
228 ltjb.package_error('luatexja',
229 'bad character code (' .. c .. ')',
230 'A character number must be between -1 and 0x10ffff.\n'..
231 "(-1 is used for denoting `math boundary')\n"..
232 'So I changed this one to zero.')
235 if k == 'prebreakpenalty' then
236 tex.write(ltjs.get_penalty_table(stack_table_index.PRE + c, 0, t))
237 elseif k == 'postbreakpenalty' then
238 tex.write(ltjs.get_penalty_table(stack_table_index.POST+ c, 0, t))
239 elseif k == 'kcatcode' then
240 tex.write(ltjs.get_penalty_table(stack_table_index.KCAT+ c, 0, t))
241 elseif k == 'chartorange' then
242 tex.write(ltjc.char_to_range(c))
243 elseif k == 'jaxspmode' or k == 'alxspmode' then
244 tex.write(ltjs.get_penalty_table(stack_table_index.XSP + c, 3, t))
249 -- EXT: print \global if necessary
250 function luatexja.ext_print_global()
251 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
255 -- mode = true iff main_process is called from pre_linebreak_filter
256 local function main_process(head, mode, dir)
258 p = ltjj.main(p,mode)
259 if p then p = ltjw.set_ja_width(p, dir) end
265 luatexbase.add_to_callback('pre_linebreak_filter',
266 function (head,groupcode)
267 return main_process(head, true, tex.textdir)
268 end,'ltj.pre_linebreak_filter',
269 luatexbase.priority_in_callback('pre_linebreak_filter',
270 'luaotfload.pre_linebreak_filter') + 1)
271 luatexbase.add_to_callback('hpack_filter',
272 function (head,groupcode,size,packtype, dir)
273 return main_process(head, false, dir)
274 end,'ltj.hpack_filter',
275 luatexbase.priority_in_callback('hpack_filter',
276 'luaotfload.hpack_filter') + 1)
279 local function get_attr_icflag(p)
280 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
285 local function debug_show_node_X(p,print_fn)
286 local k = debug_depth
288 local pt=node_type(p.id)
289 local base = debug_depth .. string.format('%X', get_attr_icflag(p))
290 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
291 if pt == 'glyph' then
292 s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
293 .. ' (' .. print_scaled(p.height) .. '+'
294 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
296 elseif pt=='hlist' or pt=='vlist' then
297 s = base .. '(' .. print_scaled(p.height) .. '+'
298 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
300 s = s .. ', shifted ' .. print_scaled(p.shift)
302 if p.glue_sign >= 1 then
303 s = s .. ' glue set '
304 if p.glue_sign == 2 then s = s .. '-' end
305 s = s .. tostring(floor(p.glue_set*10000)/10000)
306 if p.glue_order == 0 then
310 for i = 2, p.glue_order do s = s .. 'l' end
313 if get_attr_icflag(p) == icflag_table.PACKED then
318 debug_depth=debug_depth.. '.'
320 debug_show_node_X(q, print_fn); q = node_next(q)
323 elseif pt == 'glue' then
324 s = base .. ' ' .. print_spec(p.spec)
325 if get_attr_icflag(p)>icflag_table.KINSOKU
326 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
327 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
328 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
329 s = s .. ' (kanjiskip)'
330 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
331 s = s .. ' (xkanjiskip)'
334 elseif pt == 'kern' then
335 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
337 s = s .. ' (for accent)'
338 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
339 s = s .. ' (italic correction)'
340 -- elseif get_attr_icflag(p)==ITALIC then
341 -- s = s .. ' (italic correction)'
342 elseif get_attr_icflag(p)>icflag_table.KINSOKU
343 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
344 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
347 elseif pt == 'penalty' then
348 s = base .. ' ' .. tostring(p.penalty)
349 if get_attr_icflag(p)==icflag_table.KINSOKU then
350 s = s .. ' (for kinsoku)'
353 elseif pt == 'whatsit' then
354 s = base .. ' subtype: ' .. tostring(p.subtype)
355 if p.subtype==sid_user then
356 if p.type ~= 110 then
357 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
360 s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
363 debug_depth=debug_depth.. '.'
365 debug_show_node_X(q, print_fn); q = node_next(q)
370 s = s .. node.subtype(p.subtype); print_fn(s)
372 -------- math node --------
373 elseif pt=='noad' then
374 s = base ; print_fn(s)
376 debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
379 debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
382 debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
385 elseif pt=='math_char' then
386 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
388 elseif pt=='sub_box' then
391 debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn);
398 function luatexja.ext_show_node_list(head,depth,print_fn)
402 debug_show_node_X(head, print_fn); head = node_next(head)
405 print_fn(debug_depth .. ' (null list)')
408 function luatexja.ext_show_node(head,depth,print_fn)
411 debug_show_node_X(head, print_fn)
413 print_fn(debug_depth .. ' (null list)')