OSDN Git Service

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