OSDN Git Service

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