OSDN Git Service

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