OSDN Git Service

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