OSDN Git Service

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