OSDN Git Service

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