OSDN Git Service

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