OSDN Git Service

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