OSDN Git Service

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