OSDN Git Service

a3147857883fb20be196f1e439a109cf54c6b8d9
[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 = 6
83 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高
84 -- 6 が標準
85 local KANJI_SKIP = 9
86 local XKANJI_SKIP = 10
87 local PROCESSED = 11
88 local IC_PROCESSED = 12
89 local BOXBDD = 15
90 local PROCESSED_BEGIN_FLAG = 32
91
92
93 -- Three aux. functions, bollowed from tex.web
94 local unity=65536
95 local function print_scaled(s)
96    local out=''
97    local delta=10
98    if s<0 then 
99       out=out..'-'; s=-s
100    end
101    out=out..tostring(floor(s/unity)) .. '.'
102    s=10*(s%unity)+5
103    repeat
104       if delta>unity then s=s+32768-50000 end
105       out=out .. tostring(floor(s/unity)) 
106       s=10*(s%unity)
107       delta=delta*10
108    until s<=delta
109    return out
110 end
111
112 local function print_glue(d,order)
113    local out=print_scaled(d)
114    if order>0 then
115       out=out..'fi'
116       while order>1 do
117          out=out..'l'; order=order-1
118       end
119    else 
120       out=out..'pt'
121    end
122    return out
123 end
124
125 function print_spec(p)
126    local out=print_scaled(p.width)..'pt'
127    if p.stretch~=0 then
128       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
129    end
130    if p.shrink~=0 then
131       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
132    end
133 return out
134 end
135
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>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       -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
192       tex.write( (c<0) and -1 or ltjc.get_range_setting(c))
193    else
194       if c<0 or c>0x10FFFF then
195          ltjb.package_error('luatexja',
196                             'bad character code (' .. c .. ')',
197                             'A character number must be between -1 and 0x10ffff.\n'..
198                                "(-1 is used for denoting `math boundary')\n"..
199                                'So I changed this one to zero.')
200          c=0
201       end
202       if k == 'prebreakpenalty' then
203          tex.write(ltjs.get_penalty_table('pre', c, 0, tex.getcount('ltj@@stack')))
204       elseif k == 'postbreakpenalty' then
205          tex.write(ltjs.get_penalty_table('post', c, 0, tex.getcount('ltj@@stack')))
206       elseif k == 'kcatcode' then
207          tex.write(ltjs.get_penalty_table('kcat', c, 0, tex.getcount('ltj@@stack')))
208       elseif k == 'chartorange' then 
209          tex.write(ltjc.char_to_range(c))
210       elseif k == 'jaxspmode' or k == 'alxspmode' then
211          tex.write(ltjs.get_penalty_table('xsp', c, 3, tex.getcount('ltj@@stack')))
212       end
213    end
214 end
215
216 -- EXT: print \global if necessary
217 function luatexja.ext_print_global()
218    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
219 end
220
221 -- main process
222 -- mode = true iff main_process is called from pre_linebreak_filter
223 local function main_process(head, mode, dir)
224    local p = head
225    p = ltjj.main(p,mode)
226    if p then p = ltjw.set_ja_width(p, dir) end
227    return p
228 end
229
230 -- callbacks
231
232 luatexbase.add_to_callback('pre_linebreak_filter', 
233    function (head,groupcode)
234       return main_process(head, true, tex.textdir)
235    end,'ltj.pre_linebreak_filter',
236    luatexbase.priority_in_callback('pre_linebreak_filter',
237                                    'luaotfload.pre_linebreak_filter') + 1)
238 luatexbase.add_to_callback('hpack_filter', 
239   function (head,groupcode,size,packtype, dir)
240      return main_process(head, false, dir)
241   end,'ltj.hpack_filter',
242    luatexbase.priority_in_callback('hpack_filter',
243                                    'luaotfload.hpack_filter') + 1)
244
245 -- debug
246 local function get_attr_icflag(p)
247    return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
248 end
249
250 local debug_depth
251
252 local function debug_show_node_X(p,print_fn)
253    local k = debug_depth
254    local s
255    local pt=node_type(p.id)
256    local base = debug_depth .. string.format('%X', get_attr_icflag(p))
257    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
258    if pt == 'glyph' then
259       s = base .. ' ' .. utf.char(p.char) .. ' '  .. tostring(p.font)
260          .. ' (' .. print_scaled(p.height) .. '+' 
261          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
262       print_fn(s)
263    elseif pt=='hlist' or pt=='vlist' then
264       s = base .. '(' .. print_scaled(p.height) .. '+' 
265          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
266       if p.shift~=0 then
267          s = s .. ', shifted ' .. print_scaled(p.shift)
268       end
269       if p.glue_sign >= 1 then 
270          s = s .. ' glue set '
271          if p.glue_sign == 2 then s = s .. '-' end
272          s = s .. tostring(floor(p.glue_set*10000)/10000)
273          if p.glue_order == 0 then 
274             s = s .. 'pt' 
275          else 
276             s = s .. 'fi'
277             for i = 2,  p.glue_order do s = s .. 'l' end
278          end
279       end
280       if get_attr_icflag(p) == PACKED then
281          s = s .. ' (packed)'
282       end
283       print_fn(s)
284       local q = p.head
285       debug_depth=debug_depth.. '.'
286       while q do 
287          debug_show_node_X(q, print_fn); q = node_next(q)
288       end
289       debug_depth=k
290    elseif pt == 'glue' then
291       s = base .. ' ' ..  print_spec(p.spec)
292       if get_attr_icflag(p)>KINSOKU and get_attr_icflag(p)<KANJI_SKIP then
293          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-FROM_JFM .. ')'
294       elseif get_attr_icflag(p)==KANJI_SKIP then
295          s = s .. ' (kanjiskip)'
296       elseif get_attr_icflag(p)==XKANJI_SKIP then
297          s = s .. ' (xkanjiskip)'
298       end
299       print_fn(s)
300    elseif pt == 'kern' then
301       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
302       if p.subtype==2 then
303          s = s .. ' (for accent)'
304       elseif get_attr_icflag(p)==IC_PROCESSED then
305          s = s .. ' (italic correction)'
306          -- elseif get_attr_icflag(p)==ITALIC then
307          --    s = s .. ' (italic correction)'
308       elseif get_attr_icflag(p)>KINSOKU and get_attr_icflag(p)<KANJI_SKIP then
309          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-FROM_JFM .. ')'
310       end
311       print_fn(s)
312    elseif pt == 'penalty' then
313       s = base .. ' ' .. tostring(p.penalty)
314       if get_attr_icflag(p)==KINSOKU then
315          s = s .. ' (for kinsoku)'
316       end
317       print_fn(s)
318    elseif pt == 'whatsit' then
319       s = base .. ' subtype: ' ..  tostring(p.subtype)
320       if p.subtype==sid_user then
321          if p.type ~= 110 then 
322             s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
323             print_fn(s)
324          else
325             s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
326             print_fn(s)
327             local q = p.value
328             debug_depth=debug_depth.. '.'
329             while q do 
330                debug_show_node_X(q, print_fn); q = node_next(q)
331             end
332             debug_depth=k
333          end
334       else
335          s = s .. node.subtype(p.subtype); print_fn(s)
336       end
337    -------- math node --------
338    elseif pt=='noad' then
339       s = base ; print_fn(s)
340       if p.nucleus then
341          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn); 
342       end
343       if p.sup then
344          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn); 
345       end
346       if p.sub then
347          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn); 
348       end
349       debug_depth = k;
350    elseif pt=='math_char' then
351       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
352       print_fn(s)
353    elseif pt=='sub_box' then
354       print_fn(base)
355       if p.head then
356          debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn); 
357       end
358    else
359       print_fn(base)
360    end
361    p=node_next(p)
362 end
363 function luatexja.ext_show_node_list(head,depth,print_fn)
364    debug_depth = depth
365    if head then
366       while head do
367          debug_show_node_X(head, print_fn); head = node_next(head)
368       end
369    else
370       print_fn(debug_depth .. ' (null list)')
371    end
372 end
373 function luatexja.ext_show_node(head,depth,print_fn)
374    debug_depth = depth
375    if head then
376       debug_show_node_X(head, print_fn)
377    else
378       print_fn(debug_depth .. ' (null list)')
379    end
380 end