OSDN Git Service

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