OSDN Git Service

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