OSDN Git Service

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