OSDN Git Service

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