OSDN Git Service

4b134d6c34e94cb110adb97f09203d65c6733be5
[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 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
135 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
136
137 ------------------------------------------------------------------------
138 -- CODE FOR GETTING/SETTING PARAMETERS 
139 ------------------------------------------------------------------------
140
141 -- EXT: print parameters that don't need arguments
142 function luatexja.ext_get_parameter_unary(k)
143    if k == 'yalbaselineshift' then
144       tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
145    elseif k == 'yjabaselineshift' then
146       tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
147    elseif k == 'kanjiskip' then
148       tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
149    elseif k == 'xkanjiskip' then
150       tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
151    elseif k == 'jcharwidowpenalty' then
152       tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
153    elseif k == 'autospacing' then
154       tex.write(tex.getattribute('ltj@autospc'))
155    elseif k == 'autoxspacing' then
156       tex.write(tex.getattribute('ltj@autoxspc'))
157    elseif k == 'differentjfm' then
158       if luatexja.jfmglue.diffmet_rule == math.max then
159          tex.write('large')
160       elseif luatexja.jfmglue.diffmet_rule == math.min then
161          tex.write('small')
162       elseif luatexja.jfmglue.diffmet_rule == math.two_average then
163          tex.write('average')
164       elseif luatexja.jfmglue.diffmet_rule == math.two_add then
165          tex.write('both')
166       else -- This can't happen.
167          tex.write('???')
168       end
169    end
170 end
171
172
173 -- EXT: print parameters that need arguments
174 function luatexja.ext_get_parameter_binary(k,c)
175    if type(c)~='number' then
176       ltjb.package_error('luatexja',
177                          'invalid the second argument (' .. tostring(c) .. ')',
178                          'I changed this one to zero.')
179       c=0
180    end
181    if k == 'jacharrange' then
182       if c<0 or c>216 then 
183          ltjb.package_error('luatexja',
184                             'invalid character range number (' .. c .. ')',
185                             'A character range number should be in the range 0..216,\n'..
186                              'So I changed this one to zero.')
187          c=0
188       end
189       tex.write(ltjc.get_range_setting(c))
190    else
191       if c<0 or c>0x10FFFF then
192          ltjb.package_error('luatexja',
193                             'bad character code (' .. c .. ')',
194                             'A character number must be between -1 and 0x10ffff.\n'..
195                                "(-1 is used for denoting `math boundary')\n"..
196                                'So I changed this one to zero.')
197          c=0
198       end
199       if k == 'prebreakpenalty' then
200          tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
201       elseif k == 'postbreakpenalty' then
202          tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
203       elseif k == 'kcatcode' then
204          tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
205       elseif k == 'chartorange' then 
206          tex.write(ltjc.char_to_range(c))
207       elseif k == 'jaxspmode' or k == 'alxspmode' then
208          tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
209       end
210    end
211 end
212
213 -- EXT: print \global if necessary
214 function luatexja.ext_print_global()
215   if isglobal=='global' then tex.sprint(cat_lp, '\\global') end
216 end
217
218 -- main process
219 -- mode = true iff main_process is called from pre_linebreak_filter
220 local function main_process(head, mode, dir)
221    local p = head
222    p = ltjj.main(p,mode)
223    if p then p = ltjw.set_ja_width(p, dir) end
224    return p
225 end
226
227 -- callbacks
228
229 luatexbase.add_to_callback('pre_linebreak_filter', 
230    function (head,groupcode)
231       return main_process(head, true, tex.textdir)
232    end,'ltj.pre_linebreak_filter',
233    luatexbase.priority_in_callback('pre_linebreak_filter',
234                                    'luaotfload.pre_linebreak_filter') + 1)
235 luatexbase.add_to_callback('hpack_filter', 
236   function (head,groupcode,size,packtype, dir)
237      return main_process(head, false, dir)
238   end,'ltj.hpack_filter',
239    luatexbase.priority_in_callback('hpack_filter',
240                                    'luaotfload.hpack_filter') + 1)
241
242 -- debug
243 local debug_depth
244
245 local function debug_show_node_X(p,print_fn)
246    local k = debug_depth
247    local s
248    local pt=node_type(p.id)
249    local base = debug_depth .. string.format('%X', has_attr(p,attr_icflag) or 0)
250    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
251    if pt == 'glyph' then
252       s = base .. ' ' .. utf.char(p.char) .. ' ' .. tostring(p.font)
253          .. ' (' .. print_scaled(p.height) .. '+' 
254          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
255       print_fn(s)
256    elseif pt=='hlist' or pt=='vlist' then
257       s = base .. '(' .. print_scaled(p.height) .. '+' 
258          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
259       if p.shift~=0 then
260          s = s .. ', shifted ' .. print_scaled(p.shift)
261       end
262       if p.glue_sign >= 1 then 
263          s = s .. ' glue set '
264          if p.glue_sign == 2 then s = s .. '-' end
265          s = s .. tostring(floor(p.glue_set*10000)/10000)
266          if p.glue_order == 0 then 
267             s = s .. 'pt' 
268          else 
269             s = s .. 'fi'
270             for i = 2,  p.glue_order do s = s .. 'l' end
271          end
272       end
273       if has_attr(p, attr_icflag, PACKED) then
274          s = s .. ' (packed)'
275       end
276       print_fn(s)
277       local q = p.head
278       debug_depth=debug_depth.. '.'
279       while q do 
280          debug_show_node_X(q, print_fn); q = node_next(q)
281       end
282       debug_depth=k
283    elseif pt == 'glue' then
284       s = base .. ' ' ..  print_spec(p.spec)
285       if has_attr(p, attr_icflag)==FROM_JFM then
286          s = s .. ' (from JFM)'
287       elseif has_attr(p, attr_icflag)==KANJI_SKIP then
288          s = s .. ' (kanjiskip)'
289       elseif has_attr(p, attr_icflag)==XKANJI_SKIP then
290          s = s .. ' (xkanjiskip)'
291       end
292       print_fn(s)
293    elseif pt == 'kern' then
294       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
295       if p.subtype==2 then
296          s = s .. ' (for accent)'
297       elseif has_attr(p, attr_icflag)==IC_PROCESSED then
298          s = s .. ' (italic correction)'
299          -- elseif has_attr(p, attr_icflag)==ITALIC then
300          --    s = s .. ' (italic correction)'
301       elseif has_attr(p, attr_icflag)==FROM_JFM then
302          s = s .. ' (from JFM)'
303       elseif has_attr(p, attr_icflag)==LINE_END then
304          s = s .. " (from 'lineend' in JFM)"
305       end
306       print_fn(s)
307    elseif pt == 'penalty' then
308       s = base .. ' ' .. tostring(p.penalty)
309       if has_attr(p, attr_icflag)==KINSOKU then
310          s = s .. ' (for kinsoku)'
311       end
312       print_fn(s)
313    elseif pt == 'whatsit' then
314       s = base .. ' subtype: ' ..  tostring(p.subtype)
315       if p.subtype==sid_user then
316          if p.type ~= 110 then 
317             s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
318             print_fn(s)
319          else
320             s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
321             print_fn(s)
322             local q = p.value
323             debug_depth=debug_depth.. '.'
324             while q do 
325                debug_show_node_X(q, print_fn); q = node_next(q)
326             end
327             debug_depth=k
328          end
329       else
330          s = s .. node.subtype(p.subtype); print_fn(s)
331       end
332    -------- math node --------
333    elseif pt=='noad' then
334       s = base ; print_fn(s)
335       if p.nucleus then
336          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn); 
337       end
338       if p.sup then
339          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn); 
340       end
341       if p.sub then
342          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn); 
343       end
344       debug_depth = k;
345    elseif pt=='math_char' then
346       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
347       print_fn(s)
348    elseif pt=='sub_box' then
349       print_fn(base)
350       if p.head then
351          debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn); 
352       end
353    else
354       print_fn(base)
355    end
356    p=node_next(p)
357 end
358 function luatexja.ext_show_node_list(head,depth,print_fn)
359    debug_depth = depth
360    if head then
361       while head do
362          debug_show_node_X(head, print_fn); head = node_next(head)
363       end
364    else
365       print_fn(debug_depth .. ' (null list)')
366    end
367 end
368 function luatexja.ext_show_node(head,depth,print_fn)
369    debug_depth = depth
370    if head then
371       debug_show_node_X(head, print_fn)
372    else
373       print_fn(debug_depth .. ' (null list)')
374    end
375 end