OSDN Git Service

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