OSDN Git Service

better handling of dir_node
[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 userid_table.DNODE= luatexbase.newuserwhatsitid('dir_node',    'luatexja')  -- dir_node
63
64 local dir_table = {}
65 luatexja.dir_table = dir_table
66 dir_table.dir_dtou = 1
67 dir_table.dir_tate = 3
68 dir_table.dir_yoko = 4
69 dir_table.dir_node_auto   = 16 -- 組方向を合わせるために自動で作られたもの
70 dir_table.dir_node_manual = 32 -- 寸法代入によって作られたもの
71
72
73 ------------------------------------------------------------------------
74 -- FIX node.remove
75 ------------------------------------------------------------------------
76 do
77    local node_remove, node_next, node_prev = node.remove, node.next, node.prev
78    function luatexja.node_remove (head, current)
79       if head==current then
80          local q, r = node_next(current), node_prev(current)
81          if q then q.prev = r end
82          if r and node_next(r)==current then
83             r.next = q
84          end
85          return q, q
86       else
87          return node_remove(head, current)
88       end
89    end
90    local Dnode = node.direct or node
91    if Dnode~=node then
92       local Dnode_remove, setfield = Dnode.remove, Dnode.setfield
93       local Dnode_next, Dnode_prev = Dnode.getnext, Dnode.getprev
94       function luatexja.Dnode_remove (head, current)
95          if head==current then
96             local q, r = Dnode_next(current), Dnode_prev(current)
97             if q then setfield(q, 'prev', r) end
98             if r and Dnode_next(r) == current then
99                setfield(r, 'next', q)
100             end
101             return q, q
102          else
103             return Dnode_remove(head, current)
104          end
105       end
106    else
107       luatexja.Dnode_remove = luatexja.node_remove
108    end
109 end
110
111 --- 定義終わり
112
113 local load_module = luatexja.load_module
114 load_module('base');      local ltjb = luatexja.base
115 load_module('rmlgbm');    local ltjr = luatexja.rmlgbm -- must be 1st
116
117 -- define_font
118 do
119    local otfl_fdr = fonts.definers.read
120    local ltjr_font_callback = ltjr.font_callback
121    function luatexja.font_callback(name, size, id)
122       local res =  ltjr_font_callback(name, size, id, otfl_fdr)
123       luatexbase.call_callback('luatexja.define_font', res, name, size, id)
124       return res
125    end
126    luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
127    luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
128 end
129
130 if luatexja_debug then load_module('debug') end
131 load_module('charrange'); local ltjc = luatexja.charrange
132 load_module('jfont');     local ltjf = luatexja.jfont
133 load_module('inputbuf');  local ltji = luatexja.inputbuf
134 load_module('stack');     local ltjs = luatexja.stack
135 load_module('pretreat');  local ltjp = luatexja.pretreat
136 load_module('jfmglue');   local ltjj = luatexja.jfmglue
137 load_module('setwidth');  local ltjw = luatexja.setwidth
138 load_module('math');      local ltjm = luatexja.math
139 load_module('tangle');    local ltjb = luatexja.base
140 load_module('direction'); local ltjd = luatexja.direction
141
142 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
143 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
144 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
145 local attr_icflag = luatexbase.attributes['ltj@icflag']
146 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
147 local attr_dir = luatexbase.attributes['ltj@dir']
148 local cat_lp = luatexbase.catcodetables['latex-package']
149
150 -- Three aux. functions, bollowed from tex.web
151
152 local unity=65536
153 local floor = math.floor
154
155 local function print_scaled(s)
156    local out=''
157    local delta=10
158    if s<0 then
159       out=out..'-'; s=-s
160    end
161    out=out..tostring(floor(s/unity)) .. '.'
162    s=10*(s%unity)+5
163    repeat
164       if delta>unity then s=s+32768-50000 end
165       out=out .. tostring(floor(s/unity))
166       s=10*(s%unity)
167       delta=delta*10
168    until s<=delta
169    return out
170 end
171
172 local function print_glue(d,order)
173    local out=print_scaled(d)
174    if order>0 then
175       out=out..'fi'
176       while order>1 do
177          out=out..'l'; order=order-1
178       end
179    else
180       out=out..'pt'
181    end
182    return out
183 end
184
185 local function print_spec(p)
186    local out=print_scaled(p.width)..'pt'
187    if p.stretch~=0 then
188       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
189    end
190    if p.shrink~=0 then
191       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
192    end
193 return out
194 end
195
196
197 ------------------------------------------------------------------------
198 -- CODE FOR GETTING/SETTING PARAMETERS
199 ------------------------------------------------------------------------
200
201 -- EXT: print parameters that don't need arguments
202 do
203    luatexja.unary_pars = {
204       yalbaselineshift = function(t)
205          return print_scaled(tex.getattribute('ltj@yablshift'))..'pt'
206       end,
207       yjabaselineshift = function(t)
208          return print_scaled(tex.getattribute('ltj@ykblshift'))..'pt'
209       end,
210       talbaselineshift = function(t)
211          return print_scaled(tex.getattribute('ltj@tablshift'))..'pt'
212       end,
213       tjabaselineshift = function(t)
214          return print_scaled(tex.getattribute('ltj@tkblshift'))..'pt'
215       end,
216       kanjiskip = function(t)
217          return print_spec(ltjs.get_stack_skip(stack_table_index.KSK, t))
218       end,
219       xkanjiskip = function(t)
220          return print_spec(ltjs.get_stack_skip(stack_table_index.XSK, t))
221       end,
222       jcharwidowpenalty = function(t)
223          return ltjs.get_stack_table(stack_table_index.JWP, 0, t)
224       end,
225       autospacing = function(t)
226          return tex.getattribute('ltj@autospc')
227       end,
228       autoxspacing = function(t)
229          return tex.getattribute('ltj@autoxspc')
230       end,
231       differentjfm = function(t)
232          local f, r = luatexja.jfmglue.diffmet_rule, '???'
233          if f == math.max then r = 'large'
234          elseif f == math.min then r = 'small'
235          elseif f == math.two_average then r = 'average'
236          elseif f == math.two_paverage then r = 'paverage'
237          elseif f == math.two_pleft then r = 'pleft'
238          elseif f == math.two_pright then r = 'pright'
239          elseif f == math.two_add then r = 'both'
240          end
241          return r
242       end
243    }
244
245    local unary_pars = luatexja.unary_pars
246    function luatexja.ext_get_parameter_unary(k)
247       if unary_pars[k] then
248          tex.write(tostring(unary_pars[k](tex.getcount('ltj@@stack'))))
249       end
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    }
291    local binary_pars = luatexja.binary_pars
292
293    binary_pars.alxspmode = binary_pars.jaxspmode
294    function luatexja.ext_get_parameter_binary(k,c)
295       if binary_pars[k] then
296          tex.write(tostring(binary_pars[k](c,tex.getcount('ltj@@stack'))))
297       end
298    end
299 end
300
301 -- EXT: print \global if necessary
302 function luatexja.ext_print_global()
303    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
304 end
305
306 -- main process
307 do
308    local start_time_measure, stop_time_measure 
309       = ltjb.start_time_measure, ltjb.stop_time_measure
310    local Dnode = node.direct or node
311    local nullfunc = function (n) return n end
312    local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
313    local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
314    local time_jfm, time_width = 0,0 
315    -- mode = true iff main_process is called from pre_linebreak_filter
316    local function main_process(head, mode, dir, gc)
317       tex.setattribute('global', attr_icflag, 0)
318       if gc == 'fin_row' then return head
319       else
320             local p = to_direct(head)
321             start_time_measure('jfmglue')
322             p = ltjj.main(p,mode)
323             stop_time_measure('jfmglue'); start_time_measure('setwidth')
324             if p then p = ltjw.set_ja_width(p, dir) end
325             stop_time_measure('setwidth')
326             return to_node(p)
327       end
328    end
329
330    local function adjust_icflag(h)
331       -- kern from luaotfload will have icflag = 1
332       -- (same as italic correction)
333       tex.setattribute('global', attr_icflag, 1)
334       return h
335    end
336
337    -- callbacks
338
339    luatexbase.add_to_callback(
340       'pre_linebreak_filter',
341       function (head,groupcode)
342          return main_process(head, true, tex.textdir, groupcode)
343       end,'ltj.pre_linebreak_filter',
344       luatexbase.priority_in_callback('pre_linebreak_filter',
345                                       'luaotfload.node_processor') + 1)
346    luatexbase.add_to_callback(
347       'hpack_filter',
348       function (head,groupcode,size,packtype, dir)
349          return main_process(head, false, dir, groupcode)
350       end,'ltj.hpack_filter',
351       luatexbase.priority_in_callback('hpack_filter',
352                                       'luaotfload.node_processor') + 1)
353    luatexbase.add_to_callback('pre_linebreak_filter', adjust_icflag, 'adjust_icflag', 1)
354    luatexbase.add_to_callback('hpack_filter', adjust_icflag, 'adjust_icflag', 1)
355
356 end
357
358 -- debug
359
360 do
361
362 local node_type = node.type
363 local node_next = node.next
364 local has_attr = node.has_attribute
365
366 local id_penalty = node.id('penalty')
367 local id_glyph = node.id('glyph')
368 local id_glue_spec = node.id('glue_spec')
369 local id_glue = node.id('glue')
370 local id_kern = node.id('kern')
371 local id_hlist = node.id('hlist')
372 local id_vlist = node.id('vlist')
373 local id_rule = node.id('rule')
374 local id_math = node.id('math')
375 local id_whatsit = node.id('whatsit')
376 local sid_user = node.subtype('user_defined')
377
378 local function get_attr_icflag(p)
379    return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
380 end
381
382 local debug_depth
383
384 local function debug_show_node_X(p,print_fn)
385    local k = debug_depth
386    local s
387    local pt=node_type(p.id)
388    local base = debug_depth .. string.format('%X', get_attr_icflag(p))
389    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
390    if pt == 'glyph' then
391       s = base .. ' ' .. utf.char(p.char) .. ' '  .. tostring(p.font)
392          .. ' (' .. print_scaled(p.height) .. '+'
393          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
394       print_fn(s)
395    elseif pt=='hlist' or pt=='vlist' or pt=='unset' then
396       s = base .. '(' .. print_scaled(p.height) .. '+'
397          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) 
398          .. ', dir=' .. tostring(node.has_attribute(p, attr_dir))
399       if (p.shift or 0)~=0 then
400          s = s .. ', shifted ' .. print_scaled(p.shift)
401       end
402       if p.glue_sign >= 1 then
403          s = s .. ' glue set '
404          if p.glue_sign == 2 then s = s .. '-' end
405          s = s .. tostring(floor(p.glue_set*10000)/10000)
406          if p.glue_order == 0 then
407             s = s .. 'pt'
408          else
409             s = s .. 'fi'
410             for i = 2,  p.glue_order do s = s .. 'l' end
411          end
412       end
413       if get_attr_icflag(p) == icflag_table.PACKED then
414          s = s .. ' (packed)'
415       end
416       print_fn(s); debug_depth=debug_depth.. '.'
417       for q in node.traverse(p.head) do
418          debug_show_node_X(q, print_fn)
419       end
420       debug_depth=k
421    elseif pt == 'glue' then
422       s = base .. ' ' ..  print_spec(p.spec)
423       if get_attr_icflag(p)>icflag_table.KINSOKU
424          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
425          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
426       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
427          s = s .. ' (kanjiskip)'
428       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP_JFM then
429          s = s .. ' (kanjiskip, JFM specified)'
430       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
431          s = s .. ' (xkanjiskip)'
432       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP_JFM then
433          s = s .. ' (xkanjiskip, JFM specified)'
434       end
435       print_fn(s)
436    elseif pt == 'kern' then
437       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
438       if p.subtype==2 then
439          s = s .. ' (for accent)'
440       elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
441          s = s .. ' (italic correction)'
442          -- elseif get_attr_icflag(p)==ITALIC then
443          --    s = s .. ' (italic correction)'
444       elseif get_attr_icflag(p)>icflag_table.KINSOKU
445          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
446          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
447       end
448       print_fn(s)
449    elseif pt == 'penalty' then
450       s = base .. ' ' .. tostring(p.penalty)
451       if get_attr_icflag(p)==icflag_table.KINSOKU then
452          s = s .. ' (for kinsoku)'
453       end
454       print_fn(s)
455    elseif pt == 'whatsit' then
456       s = base
457       if p.subtype==sid_user then
458          local t = tostring(p.user_id) .. ' (' .. 
459             luatexbase.get_user_whatsit_name(p.user_id) .. ') '
460          if p.type ~= 110 then
461             s = s .. ' userid:' .. t .. p.value
462             print_fn(s)
463          else
464             s = s .. ' userid:' .. t .. '(node list)'
465             print_fn(s)
466             debug_depth=debug_depth.. '.'
467             for q in node.traverse(p.value) do
468                debug_show_node_X(q, print_fn)
469             end
470             debug_depth=k
471          end
472       else
473          s = s .. node.subtype(p.subtype); print_fn(s)
474       end
475    -------- math node --------
476    elseif pt=='noad' then
477       s = base ; print_fn(s)
478       if p.nucleus then
479          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn);
480       end
481       if p.sup then
482          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn);
483       end
484       if p.sub then
485          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn);
486       end
487       debug_depth = k;
488    elseif pt=='math_char' then
489       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
490       print_fn(s)
491    elseif pt=='sub_box' or pt=='sub_mlist' then
492       print_fn(base)
493       if p.head then
494          debug_depth = k .. '.';
495          for q in node.traverse(p.head) do
496             debug_show_node_X(q, print_fn)
497          end
498       end
499    else
500       print_fn(base)
501    end
502    p=node_next(p)
503 end
504 function luatexja.ext_show_node_list(head,depth,print_fn)
505    debug_depth = depth
506    if head then
507       while head do
508          debug_show_node_X(head, print_fn); head = node_next(head)
509       end
510    else
511       print_fn(debug_depth .. ' (null list)')
512    end
513 end
514 function luatexja.ext_show_node(head,depth,print_fn)
515    debug_depth = depth
516    if head then
517       debug_show_node_X(head, print_fn)
518    else
519       print_fn(debug_depth .. ' (null list)')
520    end
521 end
522
523 end