OSDN Git Service

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