OSDN Git Service

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