OSDN Git Service

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