OSDN Git Service

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