OSDN Git Service

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