OSDN Git Service

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