OSDN Git Service

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