OSDN Git Service

Fix ticket #27998.
[luatex-ja/luatexja.git] / src / luatexja-core.lua
1 require('lualibs')
2 require('luatexja.rmlgbm');    local ltjr = luatexja.rmlgbm -- must be 1st
3 require('luatexja.base');      local ltjb = luatexja.base
4 require('luatexja.charrange'); local ltjc = luatexja.charrange
5 require('luatexja.jfont');     local ltjf = luatexja.jfont
6 require('luatexja.inputbuf');  local ltji = luatexja.inputbuf
7 require('luatexja.jfmglue');   local ltjj = luatexja.jfmglue
8 require('luatexja.math');      local ltjm = luatexja.math
9 require('luatexja.pretreat');  local ltjp = luatexja.pretreat
10 require('luatexja.stack');     local ltjs = luatexja.stack
11 require('luatexja.setwidth');  local ltjw = luatexja.setwidth
12
13 local node_type = node.type
14 local node_new = node.new
15 local node_prev = node.prev
16 local node_next = node.next
17 local has_attr = node.has_attribute
18 local node_insert_before = node.insert_before
19 local node_insert_after = node.insert_after
20 local node_hpack = node.hpack
21
22 local id_penalty = node.id('penalty')
23 local id_glyph = node.id('glyph')
24 local id_glue_spec = node.id('glue_spec')
25 local id_glue = node.id('glue')
26 local id_kern = node.id('kern')
27 local id_hlist = node.id('hlist')
28 local id_vlist = node.id('vlist')
29 local id_rule = node.id('rule')
30 local id_math = node.id('math')
31 local id_whatsit = node.id('whatsit')
32 local sid_user = node.subtype('user_defined')
33
34 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
35 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
36 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
37 local attr_icflag = luatexbase.attributes['ltj@icflag']
38 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
39 local cat_lp = luatexbase.catcodetables['latex-package']
40
41 local ITALIC = 1
42 local PACKED = 2
43 local KINSOKU = 3
44 local FROM_JFM = 4
45 local LINE_END = 5
46 local KANJI_SKIP = 6
47 local XKANJI_SKIP = 7
48 local PROCESSED = 8
49 local IC_PROCESSED = 9
50 local BOXBDD = 15
51
52 ------------------------------------------------------------------------
53 -- naming:
54 --    ltj.ext_... : called from \directlua{}
55 --    ltj.int_... : called from other Lua codes, but not from \directlua{}
56 --    (other)     : only called from this file
57
58 -- error messages
59 function ltj.error(s,t)
60   tex.error('LuaTeX-ja error: ' .. s ,t) 
61 end
62
63 -- Three aux. functions, bollowed from tex.web
64 local unity=65536
65 function print_scaled(s)
66    local out=''
67    local delta=10
68    if s<0 then 
69       out=out..'-'; s=-s
70    end
71    out=out..tostring(math.floor(s/unity)) .. '.'
72    s=10*(s%unity)+5
73    repeat
74       if delta>unity then s=s+32768-50000 end
75       out=out .. tostring(math.floor(s/unity)) 
76       s=10*(s%unity)
77       delta=delta*10
78    until s<=delta
79    return out
80 end
81
82 local function print_glue(d,order)
83    local out=print_scaled(d)
84    if order>0 then
85       out=out..'fi'
86       while order>1 do
87          out=out..'l'; order=order-1
88       end
89    else 
90       out=out..'pt'
91    end
92    return out
93 end
94
95 local function print_spec(p)
96    local out=print_scaled(p.width)..'pt'
97    if p.stretch~=0 then
98       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
99    end
100    if p.shrink~=0 then
101       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
102    end
103 return out
104 end
105
106 function math.two_add(a,b) return a+b end
107 function math.two_average(a,b) return (a+b)/2 end
108
109 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
110
111 ------------------------------------------------------------------------
112 -- CODE FOR GETTING/SETTING PARAMETERS 
113 ------------------------------------------------------------------------
114
115 -- EXT: print parameters that don't need arguments
116 function ltj.ext_get_parameter_unary(k)
117    if k == 'yalbaselineshift' then
118       tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
119    elseif k == 'yjabaselineshift' then
120       tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
121    elseif k == 'kanjiskip' then
122       tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
123    elseif k == 'xkanjiskip' then
124       tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
125    elseif k == 'jcharwidowpenalty' then
126       tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
127    elseif k == 'autospacing' then
128       tex.write(tex.getattribute('ltj@autospc'))
129    elseif k == 'autoxspacing' then
130       tex.write(tex.getattribute('ltj@autoxspc'))
131    elseif k == 'differentjfm' then
132       if luatexja.jfmglue.diffmet_rule == math.max then
133          tex.write('large')
134       elseif luatexja.jfmglue.diffmet_rule == math.min then
135          tex.write('small')
136       elseif luatexja.jfmglue.diffmet_rule == math.two_average then
137          tex.write('average')
138       elseif luatexja.jfmglue.diffmet_rule == math.two_add then
139          tex.write('both')
140       else -- This can't happen.
141          tex.write('???')
142       end
143    end
144 end
145
146 -- EXT: print parameters that need arguments
147 function ltj.ext_get_parameter_binary(k,c)
148    if k == 'jacharrange' then
149       if c<0 or c>216 then 
150          ltjb.package_error('luatexja',
151                             'invalid character range number (' .. c .. ')',
152                             'A character range number should be in the range 0..216,\n'..
153                              'So I changed this one to zero.')
154          c=0
155       end
156       tex.write(ltjc.get_range_setting(c))
157    else
158       if c<0 or c>0x10FFFF then
159          ltjb.package_error('luatexja',
160                             'bad character code (' .. c .. ')',
161                             'A character number must be between -1 and 0x10ffff.\n'..
162                                "(-1 is used for denoting `math boundary')\n"..
163                                'So I changed this one to zero.')
164          c=0
165       end
166       if k == 'prebreakpenalty' then
167          tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
168       elseif k == 'postbreakpenalty' then
169          tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
170       elseif k == 'kcatcode' then
171          tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
172       elseif k == 'chartorange' then 
173          tex.write(ltjc.char_to_range(c))
174       elseif k == 'jaxspmode' or k == 'alxspmode' then
175          tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
176       end
177    end
178 end
179
180 -- EXT: print \global if necessary
181 function ltj.ext_print_global()
182   if ltj.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
183 end
184
185 -- main process
186 -- mode = true iff main_process is called from pre_linebreak_filter
187 local function main_process(head, mode, dir)
188    local p = head
189    p = ltjj.main(p,mode)
190    p = ltjw.set_ja_width(p, dir)
191    return p
192 end
193
194
195 -- debug
196 local debug_depth
197 function ltj.ext_show_node_list(head,depth,print_fn)
198    debug_depth = depth
199    if head then
200       while head do
201          debug_show_node_X(head, print_fn); head = node_next(head)
202       end
203    else
204       print_fn(debug_depth .. ' (null list)')
205    end
206 end
207 function ltj.ext_show_node(head,depth,print_fn)
208    debug_depth = depth
209    if head then
210       debug_show_node_X(head, print_fn)
211    else
212       print_fn(debug_depth .. ' (null list)')
213    end
214 end
215 function debug_show_node_X(p,print_fn)
216    local k = debug_depth
217    local s
218    local pt=node_type(p.id)
219    local base = debug_depth .. string.format('%X', has_attr(p,attr_icflag) or 0)
220    .. ' ' .. string.format('%X', has_attr(p, attr_uniqid) or 0)
221    .. ' ' .. node.type(p.id) .. ' ' ..  tostring(p.subtype )
222    if pt == 'glyph' then
223       s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
224          .. ' (' .. print_scaled(p.height) .. '+' 
225          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
226       print_fn(s)
227    elseif pt=='hlist' or pt=='vlist' then
228       s = base .. '(' .. print_scaled(p.height) .. '+' 
229          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
230       if p.shift~=0 then
231          s = s .. ', shifted ' .. print_scaled(p.shift)
232       end
233       if p.glue_sign >= 1 then 
234          s = s .. ' glue set '
235          if p.glue_sign == 2 then s = s .. '-' end
236          s = s .. tostring(math.floor(p.glue_set*10000)/10000)
237          if p.glue_order == 0 then 
238             s = s .. 'pt' 
239          else 
240             s = s .. 'fi'
241             for i = 2,  p.glue_order do s = s .. 'l' end
242          end
243       end
244       if has_attr(p, attr_icflag, PACKED) then
245          s = s .. ' (packed)'
246       end
247       print_fn(s)
248       local q = p.head
249       debug_depth=debug_depth.. '.'
250       while q do 
251          debug_show_node_X(q, print_fn); q = node_next(q)
252       end
253       debug_depth=k
254    elseif pt == 'glue' then
255       s = base .. ' ' ..  print_spec(p.spec)
256       if has_attr(p, attr_icflag)==FROM_JFM then
257             s = s .. ' (from JFM)'
258       elseif has_attr(p, attr_icflag)==KANJI_SKIP then
259          s = s .. ' (kanjiskip)'
260       elseif has_attr(p, attr_icflag)==XKANJI_SKIP then
261          s = s .. ' (xkanjiskip)'
262       end
263       print_fn(s)
264    elseif pt == 'kern' then
265       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
266       if p.subtype==2 then
267          s = s .. ' (for accent)'
268       elseif has_attr(p, attr_icflag)==IC_PROCESSED then
269          s = s .. ' (italic correction)'
270       -- elseif has_attr(p, attr_icflag)==ITALIC then
271       --    s = s .. ' (italic correction)'
272       elseif has_attr(p, attr_icflag)==FROM_JFM then
273          s = s .. ' (from JFM)'
274       elseif has_attr(p, attr_icflag)==LINE_END then
275          s = s .. " (from 'lineend' in JFM)"
276       end
277       print_fn(s)
278    elseif pt == 'penalty' then
279       s = base .. ' ' .. tostring(p.penalty)
280       if has_attr(p, attr_icflag)==KINSOKU then
281          s = s .. ' (for kinsoku)'
282       end
283       print_fn(s)
284    elseif pt == 'whatsit' then
285       s = base .. ' subtype: ' ..  tostring(p.subtype)
286       if p.subtype==sid_user then
287          if p.type ~= 110 then 
288             s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
289          else
290             s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
291          end
292       else
293          s = s .. node.subtype(p.subtype)
294       end
295       print_fn(s)
296    -------- math node --------
297    elseif pt=='noad' then
298       s = base ; print_fn(s)
299       if p.nucleus then
300          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn); 
301       end
302       if p.sup then
303          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn); 
304       end
305       if p.sub then
306          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn); 
307       end
308       debug_depth = k;
309    elseif pt=='math_char' then
310       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
311       print_fn(s)
312    elseif pt=='sub_box' then
313       print_fn(base)
314       if p.head then
315          debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn); 
316       end
317    else
318       print_fn(base)
319    end
320    p=node_next(p)
321 end
322
323
324 -- callbacks
325
326 luatexbase.add_to_callback('pre_linebreak_filter', 
327    function (head,groupcode)
328       return main_process(head, true, tex.textdir)
329    end,'ltj.pre_linebreak_filter',
330    luatexbase.priority_in_callback('pre_linebreak_filter',
331                                    'luaotfload.pre_linebreak_filter') + 1)
332 luatexbase.add_to_callback('hpack_filter', 
333   function (head,groupcode,size,packtype, dir)
334      return main_process(head, false, dir)
335   end,'ltj.hpack_filter',
336    luatexbase.priority_in_callback('hpack_filter',
337                                    'luaotfload.hpack_filter') + 1)