OSDN Git Service

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