OSDN Git Service

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