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.KANJI_SKIP_JFM = 10
33 icflag_table.XKANJI_SKIP = 11
34 icflag_table.XKANJI_SKIP_JFM = 12
35 icflag_table.PROCESSED = 13
36 icflag_table.IC_PROCESSED = 14
37 icflag_table.BOXBDD = 15
38 icflag_table.PROCESSED_BEGIN_FLAG = 128
40 local stack_table_index = {}
41 luatexja.stack_table_index = stack_table_index
42 stack_table_index.PRE = 0x200000 -- characterごと
43 stack_table_index.POST = 0x400000 -- characterごと
44 stack_table_index.KCAT = 0x600000 -- characterごと
45 stack_table_index.XSP = 0x800000 -- characterごと
46 stack_table_index.JWP = 0 -- これだけ
47 stack_table_index.KSK = 1 -- これだけ
48 stack_table_index.XSK = 2 -- これだけ
49 stack_table_index.MJT = 0x100 -- 0--255
50 stack_table_index.MJS = 0x200 -- 0--255
51 stack_table_index.MJSS = 0x300 -- 0--255
52 stack_table_index.KSJ = 0x400 -- 0--9
54 local userid_table = {}
55 luatexja.userid_table = userid_table
56 userid_table.IHB = luatexbase.newuserwhatsitid('inhibitglue', 'luatexja') -- \inhibitglue
57 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
58 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par', 'luatexja') -- 「段落始め」
61 local node_remove, node_next, node_prev = node.remove, node.next, node.prev
62 function luatexja.node_remove (head, current)
64 local q, r = node_next(current), node_prev(current)
66 if q then q.prev = r end
67 if r and node_next(r)==current then -- r is "real prev"
72 return node_remove(head, current)
76 local Dnode = node.direct or node
77 local Dnode_remove, Dnode_next, Dnode_prev = Dnode.remove, Dnode.getnext, Dnode.getprev
78 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
79 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
80 function luatexja.Dnode_remove (head, current)
82 local q, r = Dnode_next(current), Dnode_prev(current)
83 setfield(current, 'next', nil)
84 if q then setfield(q, 'prev', r) end
85 if r and Dnode_next(r) == current then -- r is "real prev"
86 setfield(r, 'next', q)
90 return Dnode_remove(head, current)
98 local load_module = luatexja.load_module
99 load_module('base'); local ltjb = luatexja.base
100 load_module('rmlgbm'); local ltjr = luatexja.rmlgbm -- must be 1st
101 load_module('charrange'); local ltjc = luatexja.charrange
102 load_module('jfont'); local ltjf = luatexja.jfont
103 load_module('inputbuf'); local ltji = luatexja.inputbuf
104 load_module('stack'); local ltjs = luatexja.stack
105 load_module('pretreat'); local ltjp = luatexja.pretreat
106 load_module('jfmglue'); local ltjj = luatexja.jfmglue
107 load_module('setwidth'); local ltjw = luatexja.setwidth
108 load_module('math'); local ltjm = luatexja.math
109 load_module('tangle'); local ltjb = luatexja.base
111 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
112 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
113 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
114 local attr_icflag = luatexbase.attributes['ltj@icflag']
115 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
116 local cat_lp = luatexbase.catcodetables['latex-package']
119 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
122 -- Three aux. functions, bollowed from tex.web
125 local floor = math.floor
127 local function print_scaled(s)
133 out=out..tostring(floor(s/unity)) .. '.'
136 if delta>unity then s=s+32768-50000 end
137 out=out .. tostring(floor(s/unity))
144 local function print_glue(d,order)
145 local out=print_scaled(d)
149 out=out..'l'; order=order-1
157 local function print_spec(p)
158 local out=print_scaled(p.width)..'pt'
160 out=out..' plus '..print_glue(p.stretch,p.stretch_order)
163 out=out..' minus '..print_glue(p.shrink,p.shrink_order)
169 ------------------------------------------------------------------------
170 -- CODE FOR GETTING/SETTING PARAMETERS
171 ------------------------------------------------------------------------
173 -- EXT: print parameters that don't need arguments
175 luatexja.unary_pars = {
176 yalbaselineshift = function(t)
177 return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
179 yjabaselineshift = function(t)
180 return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
182 kanjiskip = function(t)
183 return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
185 xkanjiskip = function(t)
186 return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
188 jcharwidowpenalty = function(t)
189 return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
191 autospacing = function(t)
192 return tex.getattribute('ltj@autospc')
194 autoxspacing = function(t)
195 return tex.getattribute('ltj@autoxspc')
197 differentjfm = function(t)
198 local f, r = luatexja.jfmglue.diffmet_rule, '???'
199 if f == math.max then r = 'large'
200 elseif f == math.min then r = 'small'
201 elseif f == math.two_average then r = 'average'
202 elseif f == math.two_paverage then r = 'paverage'
203 elseif f == math.two_pleft then r = 'pleft'
204 elseif f == math.two_pright then r = 'pright'
205 elseif f == math.two_add then r = 'both'
211 local unary_pars = luatexja.unary_pars
212 function luatexja.ext_get_parameter_unary(k)
213 if unary_pars[k] then
214 tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
220 -- EXT: print parameters that need arguments
222 luatexja.binary_pars = {
223 jacharrange = function(c, t)
224 if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
226 ltjb.package_error('luatexja',
227 'invalid character range number (' .. tostring(c) .. ')',
228 'A character range number should be in the range 1..'
229 .. 31*ltjc.ATTR_RANGE .. ",\n"..
230 'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
231 c=0 -- external range 217 == internal range 0
232 elseif c==31*ltjc.ATTR_RANGE then c=0
234 -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
235 return (c<0) and 1 or ltjc.get_range_setting(c)
237 prebreakpenalty = function(c, t)
238 return ltjs.get_stack_table(stack_table_index.PRE
239 + ltjb.in_unicode(c, true), 0, t)
241 postbreakpenalty = function(c, t)
242 return ltjs.get_stack_table(stack_table_index.POST
243 + ltjb.in_unicode(c, true), 0, t)
245 kcatcode = function(c, t)
246 return ltjs.get_stack_table(stack_table_index.KCAT
247 + ltjb.in_unicode(c, false), 0, t)
249 chartorange = function(c, t)
250 return ltjc.char_to_range(ltjb.in_unicode(c, false))
252 jaxspmode = function(c, t)
253 return ltjs.get_stack_table(stack_table_index.XSP
254 + ltjb.in_unicode(c, true), 3, t)
257 local binary_pars = luatexja.binary_pars
259 binary_pars.alxspmode = binary_pars.jaxspmode
260 function luatexja.ext_get_parameter_binary(k,c)
261 if binary_pars[k] then
262 tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
267 -- EXT: print \global if necessary
268 function luatexja.ext_print_global()
269 if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
274 local Dnode = node.direct or node
275 local nullfunc = function (n) return n end
276 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
277 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
278 -- mode = true iff main_process is called from pre_linebreak_filter
279 local function main_process(head, mode, dir)
280 local p = to_direct(head)
281 p = ltjj.main(p,mode)
282 if p then p = ltjw.set_ja_width(p, dir) end
288 luatexbase.add_to_callback(
289 'pre_linebreak_filter',
290 function (head,groupcode)
291 return main_process(head, true, tex.textdir)
292 end,'ltj.pre_linebreak_filter',
293 luatexbase.priority_in_callback('pre_linebreak_filter',
294 'luaotfload.node_processor') + 1)
295 luatexbase.add_to_callback(
297 function (head,groupcode,size,packtype, dir)
298 return main_process(head, false, dir)
299 end,'ltj.hpack_filter',
300 luatexbase.priority_in_callback('hpack_filter',
301 'luaotfload.node_processor') + 1)
306 local otfl_fdr = fonts.definers.read
307 local ltjr_font_callback = ltjr.font_callback
308 function luatexja.font_callback(name, size, id)
309 return ltjf.font_callback(
311 function (name, size, id) return ltjr_font_callback(name, size, id, otfl_fdr) end
314 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
324 local node_type = node.type
325 local node_next = node.next
326 local has_attr = node.has_attribute
328 local id_penalty = node.id('penalty')
329 local id_glyph = node.id('glyph')
330 local id_glue_spec = node.id('glue_spec')
331 local id_glue = node.id('glue')
332 local id_kern = node.id('kern')
333 local id_hlist = node.id('hlist')
334 local id_vlist = node.id('vlist')
335 local id_rule = node.id('rule')
336 local id_math = node.id('math')
337 local id_whatsit = node.id('whatsit')
338 local sid_user = node.subtype('user_defined')
340 local function get_attr_icflag(p)
341 return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
346 local function debug_show_node_X(p,print_fn)
347 local k = debug_depth
349 local pt=node_type(p.id)
350 local base = debug_depth .. string.format('%X', get_attr_icflag(p))
351 .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
352 if pt == 'glyph' then
353 s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
354 .. ' (' .. print_scaled(p.height) .. '+'
355 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
357 elseif pt=='hlist' or pt=='vlist' then
358 s = base .. '(' .. print_scaled(p.height) .. '+'
359 .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
361 s = s .. ', shifted ' .. print_scaled(p.shift)
363 if p.glue_sign >= 1 then
364 s = s .. ' glue set '
365 if p.glue_sign == 2 then s = s .. '-' end
366 s = s .. tostring(floor(p.glue_set*10000)/10000)
367 if p.glue_order == 0 then
371 for i = 2, p.glue_order do s = s .. 'l' end
374 if get_attr_icflag(p) == icflag_table.PACKED then
379 debug_depth=debug_depth.. '.'
381 debug_show_node_X(q, print_fn); q = node_next(q)
384 elseif pt == 'glue' then
385 s = base .. ' ' .. print_spec(p.spec)
386 if get_attr_icflag(p)>icflag_table.KINSOKU
387 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
388 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
389 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
390 s = s .. ' (kanjiskip)'
391 elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
392 s = s .. ' (kanjiskip, JFM specified)'
393 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
394 s = s .. ' (xkanjiskip)'
395 elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
396 s = s .. ' (xkanjiskip, JFM specified)'
399 elseif pt == 'kern' then
400 s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
402 s = s .. ' (for accent)'
403 elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
404 s = s .. ' (italic correction)'
405 -- elseif get_attr_icflag(p)==ITALIC then
406 -- s = s .. ' (italic correction)'
407 elseif get_attr_icflag(p)>icflag_table.KINSOKU
408 and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
409 s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
412 elseif pt == 'penalty' then
413 s = base .. ' ' .. tostring(p.penalty)
414 if get_attr_icflag(p)==icflag_table.KINSOKU then
415 s = s .. ' (for kinsoku)'
418 elseif pt == 'whatsit' then
419 s = base .. ' subtype: ' .. tostring(p.subtype)
420 if p.subtype==sid_user then
421 if p.type ~= 110 then
422 s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
425 s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
428 debug_depth=debug_depth.. '.'
430 debug_show_node_X(q, print_fn); q = node_next(q)
435 s = s .. node.subtype(p.subtype); print_fn(s)
437 -------- math node --------
438 elseif pt=='noad' then
439 s = base ; print_fn(s)
441 debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
444 debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
447 debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
450 elseif pt=='math_char' then
451 s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
453 elseif pt=='sub_box' then
456 debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn);
463 function luatexja.ext_show_node_list(head,depth,print_fn)
467 debug_show_node_X(head, print_fn); head = node_next(head)
470 print_fn(debug_depth .. ' (null list)')
473 function luatexja.ext_show_node(head,depth,print_fn)
476 debug_show_node_X(head, print_fn)
478 print_fn(debug_depth .. ' (null list)')