OSDN Git Service

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