OSDN Git Service

\ltj@charcode: a glyph_node is produced from that character code.
[luatex-ja/luatexja.git] / src / luatexja.lua
1
2 require('lualibs')
3
4 ------------------------------------------------------------------------
5 -- naming:
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')
11 end
12 function luatexja.load_lua(fn)
13    local found = kpse.find_file(fn, 'tex')
14    if not found then
15       tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
16    else
17       texio.write_nl('(' .. found .. ')')
18       dofile(found)
19    end
20 end
21
22 --- 以下は全ファイルで共有される定数
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 →優先度高(伸びやすく,縮みやすい)
30 -- 6 が標準
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
39
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.RIPRE  = 0xA00000 -- characterごと,ruby pre
47 stack_table_index.RIPOST = 0xC00000 -- characterごと,ruby post
48 stack_table_index.JWP  = 0 -- これだけ
49 stack_table_index.KSK  = 1 -- これだけ
50 stack_table_index.XSK  = 2 -- これだけ
51 stack_table_index.MJT  = 0x100 -- 0--255
52 stack_table_index.MJS  = 0x200 -- 0--255
53 stack_table_index.MJSS = 0x300 -- 0--255
54 stack_table_index.KSJ  = 0x400 -- 0--9
55
56 local userid_table = {}
57 luatexja.userid_table = userid_table
58 userid_table.IHB  = luatexbase.newuserwhatsitid('inhibitglue',  'luatexja') -- \inhibitglue
59 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
60 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par',    'luatexja') -- 「段落始め」
61 userid_table.DIR  = luatexbase.newuserwhatsitid('direction',    'luatexja') -- 組方向
62
63 local dir_table = {}
64 luatexja.dir_table = dir_table
65 dir_table.dir_dtou = 1
66 dir_table.dir_tate = 3
67 dir_table.dir_yoko = 4
68 dir_table.dir_math_mod    = 8
69 dir_table.dir_node_auto   = 128 -- 組方向を合わせるために自動で作られたもの
70 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
71 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
72    -- 「縦数式ディレクション」 in pTeX
73
74
75 ------------------------------------------------------------------------
76 -- FIX node.remove
77 ------------------------------------------------------------------------
78 do
79    local node_remove, node_next, node_prev = node.remove, node.next, node.prev
80    function luatexja.node_remove (head, current)
81       if head==current then
82          local q, r = node_next(current), node_prev(current)
83          if q then q.prev = r end
84          if r and node_next(r)==current then
85             r.next = q
86          end
87          return q, q
88       else
89          return node_remove(head, current)
90       end
91    end
92    local Dnode = node.direct or node
93    if Dnode~=node then
94       local Dnode_remove, setfield = Dnode.remove, Dnode.setfield
95       local Dnode_next, Dnode_prev = Dnode.getnext, Dnode.getprev
96       function luatexja.Dnode_remove (head, current)
97          if head==current then
98             local q, r = Dnode_next(current), Dnode_prev(current)
99             if q then setfield(q, 'prev', r) end
100             if r and Dnode_next(r) == current then
101                setfield(r, 'next', q)
102             end
103             return q, q
104          else
105             return Dnode_remove(head, current)
106          end
107       end
108    else
109       luatexja.Dnode_remove = luatexja.node_remove
110    end
111 end
112
113 --- 定義終わり
114
115 local load_module = luatexja.load_module
116 load_module('base');      local ltjb = luatexja.base
117 load_module('rmlgbm');    local ltjr = luatexja.rmlgbm -- must be 1st
118
119 if luatexja_debug then load_module('debug') end
120
121 load_module('charrange'); local ltjc = luatexja.charrange
122 load_module('stack');     local ltjs = luatexja.stack
123 load_module('direction'); local ltjd = luatexja.direction
124 load_module('jfont');     local ltjf = luatexja.jfont
125 load_module('inputbuf');  local ltji = luatexja.inputbuf
126 load_module('pretreat');  local ltjp = luatexja.pretreat
127 load_module('jfmglue');   local ltjj = luatexja.jfmglue
128 load_module('setwidth');  local ltjw = luatexja.setwidth
129 load_module('math');      local ltjm = luatexja.math
130 load_module('tangle');    local ltjb = luatexja.base
131
132 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
133 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
134 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
135 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
136 local attr_icflag = luatexbase.attributes['ltj@icflag']
137 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
138 local attr_dir = luatexbase.attributes['ltj@dir']
139 local cat_lp = luatexbase.catcodetables['latex-package']
140
141 -- Three aux. functions, bollowed from tex.web
142
143 local unity=65536
144 local floor = math.floor
145
146 local function print_scaled(s)
147    local out=''
148    local delta=10
149    if s<0 then
150       out=out..'-'; s=-s
151    end
152    out=out..tostring(floor(s/unity)) .. '.'
153    s=10*(s%unity)+5
154    repeat
155       if delta>unity then s=s+32768-50000 end
156       out=out .. tostring(floor(s/unity))
157       s=10*(s%unity)
158       delta=delta*10
159    until s<=delta
160    return out
161 end
162
163 local function print_glue(d,order)
164    local out=print_scaled(d)
165    if order>0 then
166       out=out..'fi'
167       while order>1 do
168          out=out..'l'; order=order-1
169       end
170    else
171       out=out..'pt'
172    end
173    return out
174 end
175
176 local function print_spec(p)
177    local out=print_scaled(p.width)..'pt'
178    if p.stretch~=0 then
179       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
180    end
181    if p.shrink~=0 then
182       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
183    end
184 return out
185 end
186
187
188 ------------------------------------------------------------------------
189 -- CODE FOR GETTING/SETTING PARAMETERS
190 ------------------------------------------------------------------------
191
192 -- EXT: print parameters that don't need arguments
193 do
194    luatexja.unary_pars = {
195       yalbaselineshift = function(t)
196          return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
197       end,
198       yjabaselineshift = function(t)
199          return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
200       end,
201       talbaselineshift = function(t)
202          return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
203       end,
204       tjabaselineshift = function(t)
205          return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
206       end,
207       kanjiskip = function(t)
208          return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
209       end,
210       xkanjiskip = function(t)
211          return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
212       end,
213       jcharwidowpenalty = function(t)
214          return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
215       end,
216       autospacing = function(t)
217          return tex.getattribute('ltj@autospc')
218       end,
219       autoxspacing = function(t)
220          return tex.getattribute('ltj@autoxspc')
221       end,
222       differentjfm = function(t)
223          local f, r = luatexja.jfmglue.diffmet_rule, '???'
224          if f == math.max then r = 'large'
225          elseif f == math.min then r = 'small'
226          elseif f == math.two_average then r = 'average'
227          elseif f == math.two_paverage then r = 'paverage'
228          elseif f == math.two_pleft then r = 'pleft'
229          elseif f == math.two_pright then r = 'pright'
230          elseif f == math.two_add then r = 'both'
231          end
232          return r
233       end,
234       direction = function()
235          local v = ltjd.get_dir_count()
236          if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
237             v = dir_table.dir_utod
238          end
239          return v
240       end,
241       adjustdir = ltjd.get_adjust_dir_count,
242    }
243
244    local unary_pars = luatexja.unary_pars
245    function luatexja.ext_get_parameter_unary(k)
246       if unary_pars[k] then
247          tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
248       end
249       ltjb.stop_time_measure('get_par')
250    end
251 end
252
253
254 -- EXT: print parameters that need arguments
255 do
256    luatexja.binary_pars = {
257       jacharrange = function(c, t)
258          if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
259             -- 0 はエラーにしない(隠し)
260             ltjb.package_error('luatexja',
261                                'invalid character range number (' .. tostring(c) .. ')',
262                                'A character range number should be in the range 1..'
263                                   .. 31*ltjc.ATTR_RANGE .. ",\n"..
264                                   'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
265             c=0 -- external range 217 == internal range 0
266          elseif c==31*ltjc.ATTR_RANGE then c=0
267          end
268       -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
269          return (c<0) and 1 or ltjc.get_range_setting(c)
270       end,
271       prebreakpenalty = function(c, t)
272          return ltjs.get_stack_table(stack_table_index.PRE
273                                           + ltjb.in_unicode(c, true), 0, t)
274       end,
275       postbreakpenalty = function(c, t)
276          return ltjs.get_stack_table(stack_table_index.POST
277                                           + ltjb.in_unicode(c, true), 0, t)
278       end,
279       kcatcode = function(c, t)
280          return ltjs.get_stack_table(stack_table_index.KCAT
281                                           + ltjb.in_unicode(c, false), 0, t)
282       end,
283       chartorange = function(c, t)
284          return ltjc.char_to_range(ltjb.in_unicode(c, false))
285       end,
286       jaxspmode = function(c, t)
287          return ltjs.get_stack_table(stack_table_index.XSP
288                                           + ltjb.in_unicode(c, true), 3, t)
289       end,
290       boxdir = function(c, t)
291          if type(c)~='number' or c<0 or c>65535 then
292             ltjb.package_error('luatexja',
293                                'Bad register code (' .. tostring(c) .. ')',
294                                'A register must be between 0 and 65535.\n'..
295                                   'I changed this one to zero.')
296             c=0
297          end
298          return ltjd.get_register_dir(c)
299       end,
300    }
301    local binary_pars = luatexja.binary_pars
302
303    binary_pars.alxspmode = binary_pars.jaxspmode
304    function luatexja.ext_get_parameter_binary(k,c)
305       if binary_pars[k] then
306          tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
307       end
308       ltjb.stop_time_measure('get_par')
309    end
310 end
311
312 -- EXT: print \global if necessary
313 function luatexja.ext_print_global()
314    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
315 end
316
317
318 -- main process
319 do
320    local start_time_measure, stop_time_measure
321       = ltjb.start_time_measure, ltjb.stop_time_measure
322    local Dnode = node.direct or node
323    local nullfunc = function (n) return n end
324    local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
325    local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
326    local tex_set_attr = tex.setattribute
327    -- mode = true iff main_process is called from pre_linebreak_filter
328    local function main_process(head, mode, dir, gc)
329       tex_set_attr('global', attr_icflag, 0)
330       if gc == 'fin_row' then return head
331       else
332             --luatexja.ext_show_node_list(head, 'T> ', print)
333             start_time_measure('jfmglue')
334             local p = ltjj.main(to_direct(head),mode, dir)
335             stop_time_measure('jfmglue')
336             return to_node(p)
337       end
338    end
339
340    local function adjust_icflag(h)
341       -- kern from luaotfload will have icflag = 1
342       -- (same as italic correction)
343       tex_set_attr('global', attr_icflag, 1)
344       return h
345    end
346
347    -- callbacks
348    luatexbase.add_to_callback(
349       'pre_linebreak_filter',
350       function (head,groupcode)
351          return main_process(head, true, tex.textdir, groupcode)
352       end,'ltj.pre_linebreak_filter',
353       luatexbase.priority_in_callback('pre_linebreak_filter',
354                                       'luaotfload.node_processor') + 1)
355    luatexbase.add_to_callback(
356       'hpack_filter',
357       function (head,groupcode,size,packtype, dir)
358          return main_process(head, false, dir, groupcode)
359       end,'ltj.hpack_filter',
360       luatexbase.priority_in_callback('hpack_filter',
361                                       'luaotfload.node_processor') + 1)
362    luatexbase.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
363    luatexbase.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
364
365 end
366
367 -- debug
368
369 do
370
371 local node_type = node.type
372 local node_next = node.next
373 local has_attr = node.has_attribute
374
375 local id_penalty = node.id('penalty')
376 local id_glyph = node.id('glyph')
377 local id_glue_spec = node.id('glue_spec')
378 local id_glue = node.id('glue')
379 local id_kern = node.id('kern')
380 local id_hlist = node.id('hlist')
381 local id_vlist = node.id('vlist')
382 local id_rule = node.id('rule')
383 local id_math = node.id('math')
384 local id_whatsit = node.id('whatsit')
385 local sid_user = node.subtype('user_defined')
386
387 local function get_attr_icflag(p)
388    return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
389 end
390
391 local prefix, inner_depth
392
393 local function debug_show_node_X(p,print_fn, limit)
394    local k = prefix
395    local s
396    local pt=node_type(p.id)
397    local base = prefix .. string.format('%X', get_attr_icflag(p))
398    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
399    if pt == 'glyph' then
400       s = base .. ' ' .. utf.char(p.char) .. ' '
401          .. tostring(p.font)
402          .. ' (' .. print_scaled(p.height) .. '+'
403          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
404       print_fn(s)
405    elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
406       if pt=='ins' then
407          s = base .. '(' .. print_scaled(p.height) .. '+'
408             .. print_scaled(p.depth) .. ')'
409             .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
410       else
411          s = base .. '(' .. print_scaled(p.height) .. '+'
412             .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
413             .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
414       end
415       if (p.shift or 0)~=0 then
416          s = s .. ', shifted ' .. print_scaled(p.shift)
417       end
418       if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
419          s = s .. ' glue set '
420          if p.glue_sign == 2 then s = s .. '-' end
421          s = s .. tostring(floor(p.glue_set*10000)/10000)
422          if p.glue_order == 0 then
423             s = s .. 'pt'
424          else
425             s = s .. 'fi'
426             for i = 2,  p.glue_order do s = s .. 'l' end
427          end
428       end
429       if get_attr_icflag(p) == icflag_table.PACKED then
430          s = s .. ' (packed)'
431       end
432       print_fn(s);
433       local bid = inner_depth
434       prefix, inner_depth = prefix.. '.', inner_depth + 1
435       if inner_depth < limit then
436          for q in node.traverse(p.head) do
437             debug_show_node_X(q, print_fn, limit)
438          end
439       end
440       prefix=k
441    elseif pt=='rule' then
442       s = base .. '(' .. print_scaled(p.height) .. '+'
443          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
444          .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
445       print_fn(s)
446    elseif pt == 'glue' then
447       s = base .. ' ' ..  print_spec(p.spec)
448       if get_attr_icflag(p)>icflag_table.KINSOKU
449          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
450          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
451       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
452          s = s .. ' (kanjiskip)'
453       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
454          s = s .. ' (kanjiskip, JFM specified)'
455       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
456          s = s .. ' (xkanjiskip)'
457       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
458          s = s .. ' (xkanjiskip, JFM specified)'
459       end
460       print_fn(s)
461    elseif pt == 'kern' then
462       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
463       if p.subtype==2 then
464          s = s .. ' (for accent)'
465       elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
466          s = s .. ' (italic correction)'
467          -- elseif get_attr_icflag(p)==ITALIC then
468          --    s = s .. ' (italic correction)'
469       elseif get_attr_icflag(p)>icflag_table.KINSOKU
470          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
471          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
472       end
473       print_fn(s)
474    elseif pt == 'penalty' then
475       s = base .. ' ' .. tostring(p.penalty)
476       if get_attr_icflag(p)==icflag_table.KINSOKU then
477          s = s .. ' (for kinsoku)'
478       end
479       print_fn(s)
480    elseif pt == 'whatsit' then
481       s = base
482       if p.subtype==sid_user then
483          local t = tostring(p.user_id) .. ' (' ..
484             luatexbase.get_user_whatsit_name(p.user_id) .. ') '
485          if p.type ~= 110 then
486             s = s .. ' userid:' .. t .. p.value
487             print_fn(s)
488          else
489             s = s .. ' userid:' .. t .. '(node list)'
490             if p.user_id==userid_table.DIR then
491                s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
492             end
493             print_fn(s)
494             local bid = inner_depth
495             prefix, inner_depth =prefix.. '.', inner_depth + 1
496             if inner_depth < limit then
497                for q in node.traverse(p.value) do
498                   debug_show_node_X(q, print_fn, limit)
499                end
500             end
501             prefix, inner_depth = k, bid
502          end
503       else
504          s = s .. node.subtype(p.subtype)
505          if p.subtype==1 then
506             s = s .. ' stream=' .. p.stream
507             print_fn(s)
508             for i=1,#p.data do
509                print_fn(s .. '  [' .. i .. '] = ' .. tostring(token.command_name(p.data[i])))
510             end
511          else
512             print_fn(s)
513          end
514       end
515    -------- math node --------
516    elseif pt=='noad' then
517       s = base ; print_fn(s)
518       if p.nucleus then
519          prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
520       end
521       if p.sup then
522          prefix = k .. '^'; debug_show_node_X(p.sup, print_fn);
523       end
524       if p.sub then
525          prefix = k .. '_'; debug_show_node_X(p.sub, print_fn);
526       end
527       prefix = k;
528    elseif pt=='math_char' then
529       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
530       print_fn(s)
531    elseif pt=='sub_box' or pt=='sub_mlist' then
532       print_fn(base)
533       if p.head then
534          prefix = k .. '.';
535          for q in node.traverse(p.head) do
536             debug_show_node_X(q, print_fn)
537          end
538       end
539    else
540       print_fn(base)
541    end
542    p=node_next(p)
543 end
544 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
545    prefix = depth
546    inner_depth = 0
547    if head then
548       while head do
549          debug_show_node_X(head, print_fn, lim or 1/0); head = node_next(head)
550       end
551    else
552       print_fn(prefix .. ' (null list)')
553    end
554 end
555 function luatexja.ext_show_node(head,depth,print_fn, lim)
556    prefix = depth
557    inner_depth = 0
558    if head then
559       debug_show_node_X(head, print_fn, lim or 1/0)
560    else
561       print_fn(prefix .. ' (null list)')
562    end
563 end
564
565 end