OSDN Git Service

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