OSDN Git Service

Bug fix of \ltjgetparameter{jacharrange}.
[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 local PROCESSED_BEGIN_FLAG = 16
90
91
92 -- Three aux. functions, bollowed from tex.web
93 local unity=65536
94 local function print_scaled(s)
95    local out=''
96    local delta=10
97    if s<0 then 
98       out=out..'-'; s=-s
99    end
100    out=out..tostring(floor(s/unity)) .. '.'
101    s=10*(s%unity)+5
102    repeat
103       if delta>unity then s=s+32768-50000 end
104       out=out .. tostring(floor(s/unity)) 
105       s=10*(s%unity)
106       delta=delta*10
107    until s<=delta
108    return out
109 end
110
111 local function print_glue(d,order)
112    local out=print_scaled(d)
113    if order>0 then
114       out=out..'fi'
115       while order>1 do
116          out=out..'l'; order=order-1
117       end
118    else 
119       out=out..'pt'
120    end
121    return out
122 end
123
124 function print_spec(p)
125    local out=print_scaled(p.width)..'pt'
126    if p.stretch~=0 then
127       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
128    end
129    if p.shrink~=0 then
130       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
131    end
132 return out
133 end
134
135
136 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
137
138 ------------------------------------------------------------------------
139 -- CODE FOR GETTING/SETTING PARAMETERS 
140 ------------------------------------------------------------------------
141
142 -- EXT: print parameters that don't need arguments
143 function luatexja.ext_get_parameter_unary(k)
144    if k == 'yalbaselineshift' then
145       tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
146    elseif k == 'yjabaselineshift' then
147       tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
148    elseif k == 'kanjiskip' then
149       tex.write(print_spec(ltjs.get_skip_table('kanjiskip', tex.getcount('ltj@@stack'))))
150    elseif k == 'xkanjiskip' then
151       tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', tex.getcount('ltj@@stack'))))
152    elseif k == 'jcharwidowpenalty' then
153       tex.write(ltjs.get_penalty_table('jwp', 0, 0, tex.getcount('ltj@@stack')))
154    elseif k == 'autospacing' then
155       tex.write(tex.getattribute('ltj@autospc'))
156    elseif k == 'autoxspacing' then
157       tex.write(tex.getattribute('ltj@autoxspc'))
158    elseif k == 'differentjfm' then
159       if luatexja.jfmglue.diffmet_rule == math.max then
160          tex.write('large')
161       elseif luatexja.jfmglue.diffmet_rule == math.min then
162          tex.write('small')
163       elseif luatexja.jfmglue.diffmet_rule == math.two_average then
164          tex.write('average')
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    if type(c)~='number' then
177       ltjb.package_error('luatexja',
178                          'invalid the second argument (' .. tostring(c) .. ')',
179                          'I changed this one to zero.')
180       c=0
181    end
182    if k == 'jacharrange' then
183       if c>216 then 
184          ltjb.package_error('luatexja',
185                             'invalid character range number (' .. c .. ')',
186                             'A character range number should be in the range 0..216,\n'..
187                              'So I changed this one to zero.')
188          c=0
189       end
190       -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
191       tex.write( (c<0) and -1 or 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 luatexja.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 function get_attr_icflag(p)
246    return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
247 end
248
249 local debug_depth
250
251 local function debug_show_node_X(p,print_fn)
252    local k = debug_depth
253    local s
254    local pt=node_type(p.id)
255    local base = debug_depth .. string.format('%X', get_attr_icflag(p))
256    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
257    if pt == 'glyph' then
258       s = base .. ' ' .. utf.char(p.char) .. ' '  .. tostring(p.font)
259          .. ' (' .. print_scaled(p.height) .. '+' 
260          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
261       print_fn(s)
262    elseif pt=='hlist' or pt=='vlist' then
263       s = base .. '(' .. print_scaled(p.height) .. '+' 
264          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
265       if p.shift~=0 then
266          s = s .. ', shifted ' .. print_scaled(p.shift)
267       end
268       if p.glue_sign >= 1 then 
269          s = s .. ' glue set '
270          if p.glue_sign == 2 then s = s .. '-' end
271          s = s .. tostring(floor(p.glue_set*10000)/10000)
272          if p.glue_order == 0 then 
273             s = s .. 'pt' 
274          else 
275             s = s .. 'fi'
276             for i = 2,  p.glue_order do s = s .. 'l' end
277          end
278       end
279       if get_attr_icflag(p) == PACKED then
280          s = s .. ' (packed)'
281       end
282       print_fn(s)
283       local q = p.head
284       debug_depth=debug_depth.. '.'
285       while q do 
286          debug_show_node_X(q, print_fn); q = node_next(q)
287       end
288       debug_depth=k
289    elseif pt == 'glue' then
290       s = base .. ' ' ..  print_spec(p.spec)
291       if get_attr_icflag(p)==FROM_JFM then
292          s = s .. ' (from JFM)'
293       elseif get_attr_icflag(p)==KANJI_SKIP then
294          s = s .. ' (kanjiskip)'
295       elseif get_attr_icflag(p)==XKANJI_SKIP then
296          s = s .. ' (xkanjiskip)'
297       end
298       print_fn(s)
299    elseif pt == 'kern' then
300       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
301       if p.subtype==2 then
302          s = s .. ' (for accent)'
303       elseif get_attr_icflag(p)==IC_PROCESSED then
304          s = s .. ' (italic correction)'
305          -- elseif get_attr_icflag(p)==ITALIC then
306          --    s = s .. ' (italic correction)'
307       elseif get_attr_icflag(p)==FROM_JFM then
308          s = s .. ' (from JFM)'
309       elseif get_attr_icflag(p)==LINE_END then
310          s = s .. " (from 'lineend' in JFM)"
311       end
312       print_fn(s)
313    elseif pt == 'penalty' then
314       s = base .. ' ' .. tostring(p.penalty)
315       if get_attr_icflag(p)==KINSOKU then
316          s = s .. ' (for kinsoku)'
317       end
318       print_fn(s)
319    elseif pt == 'whatsit' then
320       s = base .. ' subtype: ' ..  tostring(p.subtype)
321       if p.subtype==sid_user then
322          if p.type ~= 110 then 
323             s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
324             print_fn(s)
325          else
326             s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
327             print_fn(s)
328             local q = p.value
329             debug_depth=debug_depth.. '.'
330             while q do 
331                debug_show_node_X(q, print_fn); q = node_next(q)
332             end
333             debug_depth=k
334          end
335       else
336          s = s .. node.subtype(p.subtype); print_fn(s)
337       end
338    -------- math node --------
339    elseif pt=='noad' then
340       s = base ; print_fn(s)
341       if p.nucleus then
342          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn); 
343       end
344       if p.sup then
345          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn); 
346       end
347       if p.sub then
348          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn); 
349       end
350       debug_depth = k;
351    elseif pt=='math_char' then
352       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
353       print_fn(s)
354    elseif pt=='sub_box' then
355       print_fn(base)
356       if p.head then
357          debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn); 
358       end
359    else
360       print_fn(base)
361    end
362    p=node_next(p)
363 end
364 function luatexja.ext_show_node_list(head,depth,print_fn)
365    debug_depth = depth
366    if head then
367       while head do
368          debug_show_node_X(head, print_fn); head = node_next(head)
369       end
370    else
371       print_fn(debug_depth .. ' (null list)')
372    end
373 end
374 function luatexja.ext_show_node(head,depth,print_fn)
375    debug_depth = depth
376    if head then
377       debug_show_node_X(head, print_fn)
378    else
379       print_fn(debug_depth .. ' (null list)')
380    end
381 end