4 ------------------------------------------------------------------------
6 -- ext_... : called from \directlua{}
7 -- int_... : called from other Lua codes, but not from \directlua{}
8 -- (other) : only called from this file
9 function luatexja.load_module(name)
10 require('ltj-' .. name.. '.lua')
12 function luatexja.load_lua(fn)
13 local found = kpse.find_file(fn, 'tex')
15 tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
17 texio.write_nl('(' .. found .. ')')
23 local icflag_table = {}
24 luatexja.icflag_table = icflag_table
25 icflag_table.ITALIC = 1
26 icflag_table.PACKED = 2
27 icflag_table.KINSOKU = 3
28 icflag_table.FROM_JFM = 6
29 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高
31 icflag_table.KANJI_SKIP = 9
32 icflag_table.XKANJI_SKIP = 10
33 icflag_table.PROCESSED = 11
34 icflag_table.IC_PROCESSED = 12
35 icflag_table.BOXBDD = 15
36 icflag_table.PROCESSED_BEGIN_FLAG = 32
38 local stack_table_index = {}
39 luatexja.stack_table_index = stack_table_index
40 stack_table_index.PRE = 0x200000 -- characterごと
41 stack_table_index.POST = 0x400000 -- characterごと
42 stack_table_index.KCAT = 0x600000 -- characterごと
43 stack_table_index.XSP = 0x800000 -- characterごと
44 stack_table_index.JWP = 0 -- 0のみ
45 stack_table_index.MJT = 0x100 -- 0--255
46 stack_table_index.MJS = 0x200 -- 0--255
47 stack_table_index.MJSS = 0x300 -- 0--255
48 stack_table_index.KSJ = 0x400 -- 0--9
50 local userid_table = {}
51 luatexja.userid_table = userid_table
52 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
53 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
54 userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid', 'luatexja') -- luatexja-otf
55 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
59 local load_module = luatexja.load_module
60 load_module('base'); local ltjb = luatexja.base
61 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
62 load_module('charrange'); local ltjc = luatexja.charrange
63 load_module('jfont'); local ltjf = luatexja.jfont
64 load_module('inputbuf'); local ltji = luatexja.inputbuf
65 load_module('stack'); local ltjs = luatexja.stack
66 load_module('pretreat'); local ltjp = luatexja.pretreat
67 load_module('jfmglue'); local ltjj = luatexja.jfmglue
68 load_module('setwidth'); local ltjw = luatexja.setwidth
69 load_module('math'); local ltjm = luatexja.math
70 load_module('tangle'); local ltjb = luatexja.base
72 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
73 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
74 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
75 local attr_icflag = luatexbase.attributes['ltj@icflag']
76 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
77 local cat_lp = luatexbase.catcodetables['latex-package']
80 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
83 -- Three aux. functions, bollowed from tex.web
86 local floor = math.floor
88 local function print_scaled(s)
94 out=out..tostring(floor(s/unity)) .. '.'
97 if delta>unity then s=s+32768-50000 end
98 out=out .. tostring(floor(s/unity))
105 local function print_glue(d,order)
106 local out=print_scaled(d)
110 out=out..'l'; order=order-1
118 local function print_spec(p)
119 local out=print_scaled(p.width)..'pt'
121 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
124 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
130 ------------------------------------------------------------------------
131 -- CODE FOR GETTING/SETTING PARAMETERS
132 ------------------------------------------------------------------------
134 -- EXT: print parameters that don't need arguments
136 luatexja.unary_pars = {
137 yalbaselineshift = function(t)
138 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
140 yjabaselineshift = function(t)
141 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
143 kanjiskip = function(t)
144 return print_spec(ltjs.get_skip_table('kanjiskip', t))
146 xkanjiskip = function(t)
147 return print_spec(ltjs.get_skip_table('xkanjiskip', t))
149 jcharwidowpenalty = function(t)
150 return ltjs.get_penalty_table(stack_table_index.JWP, 0, t)
152 autospacing = function(t)
153 return tex.getattribute('ltj@autospc')
155 autoxspacing = function(t)
156 return tex.getattribute('ltj@autoxspc')
158 differentjfm = function(t)
159 local f, r = luatexja.jfmglue.diffmet_rule, '???'
160 if f == math.max then r = 'large'
161 elseif f == math.min then r = 'small'
162 elseif f == math.two_average then r = 'average'
163 elseif f == math.two_paverage then r = 'paverage'
164 elseif f == math.two_pleft then r = 'pleft'
165 elseif f == math.two_pright then r = 'pright'
166 elseif f == math.two_add then r = 'both'
172 local unary_pars = luatexja.unary_pars
173 function luatexja.ext_get_parameter_unary(k)
174 local t = tex.getcount('ltj@@stack')
175 if unary_pars[k] then
176 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
182 -- EXT: print parameters that need arguments
183 function luatexja.ext_get_parameter_binary(k,c)
184 local t = tex.getcount('ltj@@stack')
185 if type(c)~='number' then
186 ltjb.package_error('luatexja',
187 'invalid the second argument (' .. tostring(c) .. ')',
188 'I changed this one to zero.')
191 if k == 'jacharrange' then
192 if c<=0 or c>31*ltjc.ATTR_RANGE then
193 ltjb.package_error('luatexja',
194 'invalid character range number (' .. c .. ')',
195 'A character range number should be in the range 1..'
196 .. 31*ltjc.ATTR_RANGE .. ",\n"..
197 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
198 c=0 -- external range 217 == internal range 0
199 elseif c==31*ltjc.ATTR_RANGE then c=0
201 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
202 tex.write( (c<0) and 1 or ltjc.get_range_setting(c))
204 if c<0 or c>0x10FFFF then
205 ltjb.package_error('luatexja',
206 'bad character code (' .. c .. ')',
207 'A character number must be between -1 and 0x10ffff.\n'..
208 "(-1 is used for denoting `math boundary')\n"..
209 'So I changed this one to zero.')
212 if k == 'prebreakpenalty' then
213 tex.write(ltjs.get_penalty_table(stack_table_index.PRE + c, 0, t))
214 elseif k == 'postbreakpenalty' then
215 tex.write(ltjs.get_penalty_table(stack_table_index.POST+ c, 0, t))
216 elseif k == 'kcatcode' then
217 tex.write(ltjs.get_penalty_table(stack_table_index.KCAT+ c, 0, t))
218 elseif k == 'chartorange' then
219 tex.write(ltjc.char_to_range(c))
220 elseif k == 'jaxspmode' or k == 'alxspmode' then
221 tex.write(ltjs.get_penalty_table(stack_table_index.XSP + c, 3, t))
226 -- EXT: print \global if necessary
227 function luatexja.ext_print_global()
228 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
233 -- mode = true iff main_process is called from pre_linebreak_filter
234 local function main_process(head, mode, dir)
236 p = ltjj.main(p,mode)
237 if p then p = ltjw.set_ja_width(p, dir) end
243 luatexbase.add_to_callback(
244 'pre_linebreak_filter',
245 function (head,groupcode)
246 return main_process(head, true, tex.textdir)
247 end,'ltj.pre_linebreak_filter',
248 luatexbase.priority_in_callback('pre_linebreak_filter',
249 'luaotfload.node_processor') + 1)
250 luatexbase.add_to_callback(
252 function (head,groupcode,size,packtype, dir)
253 return main_process(head, false, dir)
254 end,'ltj.hpack_filter',
255 luatexbase.priority_in_callback('hpack_filter',
256 'luaotfload.node_processor') + 1)
261 local otfl_fdr = fonts.definers.read
262 local ltjr_font_callback = ltjr.font_callback
263 function luatexja.font_callback(name, size, id)
264 return ltjf.font_callback(
266 function (name, size, id) return ltjr_font_callback(name, size, id, otfl_fdr) end
269 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
279 local node_type = node.type
280 local node_next = node.next
281 local has_attr = node.has_attribute
283 local id_penalty = node.id('penalty')
284 local id_glyph = node.id('glyph')
285 local id_glue_spec = node.id('glue_spec')
286 local id_glue = node.id('glue')
287 local id_kern = node.id('kern')
288 local id_hlist = node.id('hlist')
289 local id_vlist = node.id('vlist')
290 local id_rule = node.id('rule')
291 local id_math = node.id('math')
292 local id_whatsit = node.id('whatsit')
293 local sid_user = node.subtype('user_defined')
295 local function get_attr_icflag(p)
296 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
301 local 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', get_attr_icflag(p))
306 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
307 if pt == 'glyph' then
308 s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
309 .. ' (' .. print_scaled(p.height) .. '+'
310 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
312 elseif pt=='hlist' or pt=='vlist' then
313 s = base .. '(' .. print_scaled(p.height) .. '+'
314 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
316 s = s .. ', shifted ' .. print_scaled(p.shift)
318 if p.glue_sign >= 1 then
319 s = s .. ' glue set '
320 if p.glue_sign == 2 then s = s .. '-' end
321 s = s .. tostring(floor(p.glue_set*10000)/10000)
322 if p.glue_order == 0 then
326 for i = 2, p.glue_order do s = s .. 'l' end
329 if get_attr_icflag(p) == icflag_table.PACKED then
334 debug_depth=debug_depth.. '.'
336 debug_show_node_X(q, print_fn); q = node_next(q)
339 elseif pt == 'glue' then
340 s = base .. ' ' .. print_spec(p.spec)
341 if get_attr_icflag(p)>icflag_table.KINSOKU
342 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
343 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
344 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
345 s = s .. ' (kanjiskip)'
346 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
347 s = s .. ' (xkanjiskip)'
350 elseif pt == 'kern' then
351 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
353 s = s .. ' (for accent)'
354 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
355 s = s .. ' (italic correction)'
356 -- elseif get_attr_icflag(p)==ITALIC then
357 -- s = s .. ' (italic correction)'
358 elseif get_attr_icflag(p)>icflag_table.KINSOKU
359 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
360 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
363 elseif pt == 'penalty' then
364 s = base .. ' ' .. tostring(p.penalty)
365 if get_attr_icflag(p)==icflag_table.KINSOKU then
366 s = s .. ' (for kinsoku)'
369 elseif pt == 'whatsit' then
370 s = base .. ' subtype: ' .. tostring(p.subtype)
371 if p.subtype==sid_user then
372 if p.type ~= 110 then
373 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
376 s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
379 debug_depth=debug_depth.. '.'
381 debug_show_node_X(q, print_fn); q = node_next(q)
386 s = s .. node.subtype(p.subtype); print_fn(s)
388 -------- math node --------
389 elseif pt=='noad' then
390 s = base ; print_fn(s)
392 debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
395 debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
398 debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
401 elseif pt=='math_char' then
402 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
404 elseif pt=='sub_box' then
407 debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn);
414 function luatexja.ext_show_node_list(head,depth,print_fn)
418 debug_show_node_X(head, print_fn); head = node_next(head)
421 print_fn(debug_depth .. ' (null list)')
424 function luatexja.ext_show_node(head,depth,print_fn)
427 debug_show_node_X(head, print_fn)
429 print_fn(debug_depth .. ' (null list)')