OSDN Git Service

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