OSDN Git Service

Improved impl. of \utod.
[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('jfont');     local ltjf = luatexja.jfont
123
124 -- define_font
125 do
126    local otfl_fdr = fonts.definers.read
127    local ltjr_font_callback = ltjr.font_callback
128    local ltjf_extract_metric = ltjf.extract_metric
129    function luatexja.font_callback(name, size, id)
130       local new_name = ltjf_extract_metric(name)
131       local res =  ltjr_font_callback(new_name, size, id, otfl_fdr)
132       luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
133       return res
134    end
135    luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
136    luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
137 end
138
139
140 load_module('inputbuf');  local ltji = luatexja.inputbuf
141 load_module('stack');     local ltjs = luatexja.stack
142 load_module('pretreat');  local ltjp = luatexja.pretreat
143 load_module('jfmglue');   local ltjj = luatexja.jfmglue
144 load_module('setwidth');  local ltjw = luatexja.setwidth
145 load_module('math');      local ltjm = luatexja.math
146 load_module('tangle');    local ltjb = luatexja.base
147 load_module('direction'); local ltjd = luatexja.direction
148
149 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
150 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
151 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
152 local attr_icflag = luatexbase.attributes['ltj@icflag']
153 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
154 local attr_dir = luatexbase.attributes['ltj@dir']
155 local cat_lp = luatexbase.catcodetables['latex-package']
156
157 -- Three aux. functions, bollowed from tex.web
158
159 local unity=65536
160 local floor = math.floor
161
162 local function print_scaled(s)
163    local out=''
164    local delta=10
165    if s<0 then
166       out=out..'-'; s=-s
167    end
168    out=out..tostring(floor(s/unity)) .. '.'
169    s=10*(s%unity)+5
170    repeat
171       if delta>unity then s=s+32768-50000 end
172       out=out .. tostring(floor(s/unity))
173       s=10*(s%unity)
174       delta=delta*10
175    until s<=delta
176    return out
177 end
178
179 local function print_glue(d,order)
180    local out=print_scaled(d)
181    if order>0 then
182       out=out..'fi'
183       while order>1 do
184          out=out..'l'; order=order-1
185       end
186    else
187       out=out..'pt'
188    end
189    return out
190 end
191
192 local function print_spec(p)
193    local out=print_scaled(p.width)..'pt'
194    if p.stretch~=0 then
195       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
196    end
197    if p.shrink~=0 then
198       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
199    end
200 return out
201 end
202
203
204 ------------------------------------------------------------------------
205 -- CODE FOR GETTING/SETTING PARAMETERS
206 ------------------------------------------------------------------------
207
208 -- EXT: print parameters that don't need arguments
209 do
210    luatexja.unary_pars = {
211       yalbaselineshift = function(t)
212          return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
213       end,
214       yjabaselineshift = function(t)
215          return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
216       end,
217       talbaselineshift = function(t)
218          return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
219       end,
220       tjabaselineshift = function(t)
221          return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
222       end,
223       kanjiskip = function(t)
224          return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
225       end,
226       xkanjiskip = function(t)
227          return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
228       end,
229       jcharwidowpenalty = function(t)
230          return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
231       end,
232       autospacing = function(t)
233          return tex.getattribute('ltj@autospc')
234       end,
235       autoxspacing = function(t)
236          return tex.getattribute('ltj@autoxspc')
237       end,
238       differentjfm = function(t)
239          local f, r = luatexja.jfmglue.diffmet_rule, '???'
240          if f == math.max then r = 'large'
241          elseif f == math.min then r = 'small'
242          elseif f == math.two_average then r = 'average'
243          elseif f == math.two_paverage then r = 'paverage'
244          elseif f == math.two_pleft then r = 'pleft'
245          elseif f == math.two_pright then r = 'pright'
246          elseif f == math.two_add then r = 'both'
247          end
248          return r
249       end,
250       direction = function() 
251          local v = ltjd.get_dir_count()
252          if math.abs(tex.nest[tex.nest.ptr].mode) == ltjs.mmode and v == dir_table.dir_tate then
253             v = dir_table.dir_utod
254          end
255          return v
256       end,
257       adjustdir = ltjd.get_adjust_dir_count,
258    }
259
260    local unary_pars = luatexja.unary_pars
261    function luatexja.ext_get_parameter_unary(k)
262       if unary_pars[k] then
263          tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
264       end
265    end
266 end
267
268
269 -- EXT: print parameters that need arguments
270 do
271    luatexja.binary_pars = {
272       jacharrange = function(c, t)
273          if type(c)~='number' or c<0 or c>31*ltjc.ATTR_RANGE then
274             -- 0 はエラーにしない(隠し)
275             ltjb.package_error('luatexja',
276                                'invalid character range number (' .. tostring(c) .. ')',
277                                'A character range number should be in the range 1..'
278                                   .. 31*ltjc.ATTR_RANGE .. ",\n"..
279                                   'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
280             c=0 -- external range 217 == internal range 0
281          elseif c==31*ltjc.ATTR_RANGE then c=0
282          end
283       -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
284          return (c<0) and 1 or ltjc.get_range_setting(c)
285       end,
286       prebreakpenalty = function(c, t)
287          return ltjs.get_stack_table(stack_table_index.PRE
288                                           + ltjb.in_unicode(c, true), 0, t)
289       end,
290       postbreakpenalty = function(c, t)
291          return ltjs.get_stack_table(stack_table_index.POST
292                                           + ltjb.in_unicode(c, true), 0, t)
293       end,
294       kcatcode = function(c, t)
295          return ltjs.get_stack_table(stack_table_index.KCAT
296                                           + ltjb.in_unicode(c, false), 0, t)
297       end,
298       chartorange = function(c, t)
299          return ltjc.char_to_range(ltjb.in_unicode(c, false))
300       end,
301       jaxspmode = function(c, t)
302          return ltjs.get_stack_table(stack_table_index.XSP
303                                           + ltjb.in_unicode(c, true), 3, t)
304       end,
305       boxdir = function(c, t)
306          if type(c)~='number' or c<0 or c>65535 then
307             ltjb.package_error('luatexja',
308                                'Bad register code (' .. tostring(c) .. ')',
309                                'A register must be between 0 and 65535.\n'..
310                                   'I changed this one to zero.')
311             c=0
312          end
313          return ltjd.get_register_dir(c)
314       end,
315    }
316    local binary_pars = luatexja.binary_pars
317
318    binary_pars.alxspmode = binary_pars.jaxspmode
319    function luatexja.ext_get_parameter_binary(k,c)
320       if binary_pars[k] then
321          tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
322       end
323    end
324 end
325
326 -- EXT: print \global if necessary
327 function luatexja.ext_print_global()
328    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
329 end
330
331
332 -- main process
333 do
334    local start_time_measure, stop_time_measure 
335       = ltjb.start_time_measure, ltjb.stop_time_measure
336    local Dnode = node.direct or node
337    local nullfunc = function (n) return n end
338    local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
339    local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
340    local tex_set_attr = tex.setattribute
341    -- mode = true iff main_process is called from pre_linebreak_filter
342    local function main_process(head, mode, dir, gc)
343       tex_set_attr('global', attr_icflag, 0)
344       if gc == 'fin_row' then return head
345       else
346             local p = to_direct(head)
347             start_time_measure('jfmglue')
348             p = ltjj.main(p,mode)
349             stop_time_measure('jfmglue'); start_time_measure('setwidth')
350             if p then p = ltjw.set_ja_width(p, dir) end
351             stop_time_measure('setwidth')
352             return to_node(p)
353       end
354    end
355
356    local function adjust_icflag(h)
357       -- kern from luaotfload will have icflag = 1
358       -- (same as italic correction)
359       tex_set_attr('global', attr_icflag, 1)
360       return h
361    end
362
363    -- callbacks
364    luatexbase.add_to_callback(
365       'pre_linebreak_filter',
366       function (head,groupcode)
367          return main_process(head, true, tex.textdir, groupcode)
368       end,'ltj.pre_linebreak_filter',
369       luatexbase.priority_in_callback('pre_linebreak_filter',
370                                       'luaotfload.node_processor') + 1)
371    luatexbase.add_to_callback(
372       'hpack_filter',
373       function (head,groupcode,size,packtype, dir)
374          return main_process(head, false, dir, groupcode)
375       end,'ltj.hpack_filter',
376       luatexbase.priority_in_callback('hpack_filter',
377                                       'luaotfload.node_processor') + 1)
378    luatexbase.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
379    luatexbase.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
380
381 end
382
383 -- debug
384
385 do
386
387 local node_type = node.type
388 local node_next = node.next
389 local has_attr = node.has_attribute
390
391 local id_penalty = node.id('penalty')
392 local id_glyph = node.id('glyph')
393 local id_glue_spec = node.id('glue_spec')
394 local id_glue = node.id('glue')
395 local id_kern = node.id('kern')
396 local id_hlist = node.id('hlist')
397 local id_vlist = node.id('vlist')
398 local id_rule = node.id('rule')
399 local id_math = node.id('math')
400 local id_whatsit = node.id('whatsit')
401 local sid_user = node.subtype('user_defined')
402
403 local function get_attr_icflag(p)
404    return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
405 end
406
407 local debug_depth
408
409 local function debug_show_node_X(p,print_fn)
410    local k = debug_depth
411    local s
412    local pt=node_type(p.id)
413    local base = debug_depth .. string.format('%X', get_attr_icflag(p))
414    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
415    if pt == 'glyph' then
416       s = base .. ' ' .. utf.char(p.char) .. ' '
417          .. tostring(p.font)
418          .. ' (' .. print_scaled(p.height) .. '+'
419          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
420       print_fn(s)
421    elseif pt=='hlist' or pt=='vlist' or pt=='unset' then
422       s = base .. '(' .. print_scaled(p.height) .. '+'
423          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) 
424          .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
425       if (p.shift or 0)~=0 then
426          s = s .. ', shifted ' .. print_scaled(p.shift)
427       end
428       if p.glue_sign >= 1 then
429          s = s .. ' glue set '
430          if p.glue_sign == 2 then s = s .. '-' end
431          s = s .. tostring(floor(p.glue_set*10000)/10000)
432          if p.glue_order == 0 then
433             s = s .. 'pt'
434          else
435             s = s .. 'fi'
436             for i = 2,  p.glue_order do s = s .. 'l' end
437          end
438       end
439       if get_attr_icflag(p) == icflag_table.PACKED then
440          s = s .. ' (packed)'
441       end
442       print_fn(s); debug_depth=debug_depth.. '.'
443       for q in node.traverse(p.head) do
444          debug_show_node_X(q, print_fn)
445       end
446       debug_depth=k
447    elseif pt == 'glue' then
448       s = base .. ' ' ..  print_spec(p.spec)
449       if get_attr_icflag(p)>icflag_table.KINSOKU
450          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
451          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
452       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
453          s = s .. ' (kanjiskip)'
454       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
455          s = s .. ' (kanjiskip, JFM specified)'
456       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
457          s = s .. ' (xkanjiskip)'
458       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
459          s = s .. ' (xkanjiskip, JFM specified)'
460       end
461       print_fn(s)
462    elseif pt == 'kern' then
463       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
464       if p.subtype==2 then
465          s = s .. ' (for accent)'
466       elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
467          s = s .. ' (italic correction)'
468          -- elseif get_attr_icflag(p)==ITALIC then
469          --    s = s .. ' (italic correction)'
470       elseif get_attr_icflag(p)>icflag_table.KINSOKU
471          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
472          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
473       end
474       print_fn(s)
475    elseif pt == 'penalty' then
476       s = base .. ' ' .. tostring(p.penalty)
477       if get_attr_icflag(p)==icflag_table.KINSOKU then
478          s = s .. ' (for kinsoku)'
479       end
480       print_fn(s)
481    elseif pt == 'whatsit' then
482       s = base
483       if p.subtype==sid_user then
484          local t = tostring(p.user_id) .. ' (' .. 
485             luatexbase.get_user_whatsit_name(p.user_id) .. ') '
486          if p.type ~= 110 then
487             s = s .. ' userid:' .. t .. p.value
488             print_fn(s)
489          else
490             s = s .. ' userid:' .. t .. '(node list)'
491             if p.user_id==userid_table.DIR then
492                s = s .. ' dir: ' .. tostring(node.has_attribute(p, attr_dir))
493             end
494             print_fn(s)
495             debug_depth=debug_depth.. '.'
496             for q in node.traverse(p.value) do
497                debug_show_node_X(q, print_fn)
498             end
499             debug_depth=k
500          end
501       else
502          s = s .. node.subtype(p.subtype)
503          if p.subtype==1 then
504             s = s .. ' stream=' .. p.stream
505             print_fn(s)
506             for i=1,#p.data do
507                print_fn(base .. ' [' .. i .. '] = ' .. tostring(p.data[i]))
508             end
509          else
510             print_fn(s)
511          end
512       end
513    -------- math node --------
514    elseif pt=='noad' then
515       s = base ; print_fn(s)
516       if p.nucleus then
517          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
518       end
519       if p.sup then
520          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
521       end
522       if p.sub then
523          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
524       end
525       debug_depth = k;
526    elseif pt=='math_char' then
527       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
528       print_fn(s)
529    elseif pt=='sub_box' or pt=='sub_mlist' then
530       print_fn(base)
531       if p.head then
532          debug_depth = k .. '.';
533          for q in node.traverse(p.head) do
534             debug_show_node_X(q, print_fn)
535          end
536       end
537    else
538       print_fn(base)
539    end
540    p=node_next(p)
541 end
542 function luatexja.ext_show_node_list(head,depth,print_fn)
543    debug_depth = depth
544    if head then
545       while head do
546          debug_show_node_X(head, print_fn); head = node_next(head)
547       end
548    else
549       print_fn(debug_depth .. ' (null list)')
550    end
551 end
552 function luatexja.ext_show_node(head,depth,print_fn)
553    debug_depth = depth
554    if head then
555       debug_show_node_X(head, print_fn)
556    else
557       print_fn(debug_depth .. ' (null list)')
558    end
559 end
560
561 end
562