OSDN Git Service

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