OSDN Git Service

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