OSDN Git Service

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