OSDN Git Service

Moved several Lua programs.
[luatex-ja/luatexja.git] / src / luatexja-core.lua
1 local ltjb = luatexja.base
2 local ltjc = luatexja.charrange
3 local ltjs = luatexja.stack
4 local ltjj = luatexja.jfmglue
5 local ltjf = luatexja.jfont
6
7 local node_type = node.type
8 local node_new = node.new
9 local node_prev = node.prev
10 local node_next = node.next
11 local has_attr = node.has_attribute
12 local node_insert_before = node.insert_before
13 local node_insert_after = node.insert_after
14 local node_hpack = node.hpack
15 local round = tex.round
16
17 local id_penalty = node.id('penalty')
18 local id_glyph = node.id('glyph')
19 local id_glue_spec = node.id('glue_spec')
20 local id_glue = node.id('glue')
21 local id_kern = node.id('kern')
22 local id_hlist = node.id('hlist')
23 local id_vlist = node.id('vlist')
24 local id_rule = node.id('rule')
25 local id_math = node.id('math')
26 local id_whatsit = node.id('whatsit')
27 local sid_user = node.subtype('user_defined')
28
29 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
30 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
31 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
33
34 local ITALIC = 1
35 local PACKED = 2
36 local KINSOKU = 3
37 local FROM_JFM = 4
38 local LINE_END = 5
39 local KANJI_SKIP = 6
40 local XKANJI_SKIP = 7
41 local PROCESSED = 8
42 local IC_PROCESSED = 9
43 local BOXBDD = 15
44
45 ------------------------------------------------------------------------
46 -- naming:
47 --    ltj.ext_... : called from \directlua{}
48 --    ltj.int_... : called from other Lua codes, but not from \directlua{}
49 --    (other)     : only called from this file
50
51 -- error messages
52 function ltj.error(s,t)
53   tex.error('LuaTeX-ja error: ' .. s ,t) 
54 end
55
56 -- Three aux. functions, bollowed from tex.web
57 local unity=65536
58 local function print_scaled(s)
59    local out=''
60    local delta=10
61    if s<0 then 
62       out=out..'-'; s=-s
63    end
64    out=out..tostring(math.floor(s/unity)) .. '.'
65    s=10*(s%unity)+5
66    repeat
67       if delta>unity then s=s+32768-50000 end
68       out=out .. tostring(math.floor(s/unity)) 
69       s=10*(s%unity)
70       delta=delta*10
71    until s<=delta
72    return out
73 end
74
75 local function print_glue(d,order)
76    local out=print_scaled(d)
77    if order>0 then
78       out=out..'fi'
79       while order>1 do
80          out=out..'l'; order=order-1
81       end
82    else 
83       out=out..'pt'
84    end
85    return out
86 end
87
88 local function print_spec(p)
89    local out=print_scaled(p.width)..'pt'
90    if p.stretch~=0 then
91       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
92    end
93    if p.shrink~=0 then
94       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
95    end
96 return out
97 end
98
99 -- return true if and only if p is a Japanese character node
100 local function is_japanese_glyph_node(p)
101    return p and (p.id==id_glyph) 
102    and (p.font==has_attr(p,attr_curjfnt))
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('\\global') end
182 end
183
184
185 ------------------------------------------------------------------------
186 -- MAIN PROCESS STEP 4: width of japanese chars (prefix: main4)
187 ------------------------------------------------------------------------
188
189 -- TeX's \hss
190 local function main4_get_hss()
191    local hss = node_new(id_glue)
192    local fil_spec = node_new(id_glue_spec)
193    fil_spec.width = 0
194    fil_spec.stretch = 65536
195    fil_spec.stretch_order = 2
196    fil_spec.shrink = 65536
197    fil_spec.shrink_order = 2
198    hss.spec = fil_spec
199    return hss
200 end
201
202 local function main4_set_ja_width(head, dir)
203    local p = head
204    local met_tb, t, s, g, q, a, h
205    local m = false -- is in math mode?
206    while p do
207       local v=has_attr(p,attr_yablshift) or 0
208       if p.id==id_glyph then
209          p.yoffset = p.yoffset-v
210          if is_japanese_glyph_node(p) then
211             met_tb = ltjf.font_metric_table[p.font]
212             t = ltjf.metrics[met_tb.jfm]
213             s = t.char_type[has_attr(p,attr_jchar_class)]
214             if s.width ~= 'prop' and
215                not(s.left==0.0 and s.down==0.0 and s.align=='left' 
216                    and round(s.width*met_tb.size)==p.width) then
217                -- must be encapsuled by a \hbox
218                head, q = node.remove(head, p)
219                local full_width = round(s.width*met_tb.size)
220                p.yoffset=p.yoffset-round(met_tb.size*s.down)
221                p.xoffset=p.xoffset-round(met_tb.size*s.left)
222                
223                -- The next if block sets h:=(head of a new list)
224                if s.align=='left' then
225                   h = node_new(id_kern); h.subtype = 0
226                   h.kern = full_width - p.width
227                   p.next = h; h = p
228                elseif s.align=='right' then
229                   h = node_new(id_kern); h.subtype = 0
230                   h.kern = full_width - p.width
231                   p.next = nil; h.next = p
232                elseif s.align=='middle' then
233                   local total = full_width - p.width
234                   h = node_new(id_kern); h.subtype = 0
235                   h.kern = round(total/2); p.next = h
236                   h = node_new(id_kern); h.subtype = 0
237                   h.kern = total - round(total/2); h.next = p
238                end
239                g = node_new(id_hlist); g.width = full_width
240                g.height = round(met_tb.size*s.height)
241                g.depth = round(met_tb.size*s.depth)
242                g.glue_set = 0; g.glue_order = 0; g.head = h
243                g.shift = 0; g.dir = dir or 'TLT'
244                node.set_attribute(g, attr_icflag, PACKED)
245                if q then
246                   head = node_insert_before(head, q, g)
247                else
248                   head = node_insert_after(head, node.tail(head), g)
249                end
250                p = q
251             else p=node_next(p)
252             end
253          else p=node_next(p)
254          end
255       elseif p.id==id_math then
256          m=(p.subtype==0); p=node_next(p)
257       else
258          if m then
259             if p.id==id_hlist or p.id==id_vlist then
260                p.shift=p.shift+v
261             elseif p.id==id_rule then
262                p.height=p.height-v; p.depth=p.depth+v 
263             end
264          end
265          p=node_next(p)
266       end
267    end
268 return head
269 end
270
271 -- main process
272 -- mode = true iff main_process is called from pre_linebreak_filter
273 local function main_process(head, mode, dir)
274    local p = head
275    p = ltjj.main(p,mode)
276    p = main4_set_ja_width(p, dir)
277    return p
278 end
279
280
281 -- debug
282 local debug_depth
283 function ltj.ext_show_node_list(head,depth,print_fn)
284    debug_depth = depth
285    if head then
286       while head do
287          debug_show_node_X(head, print_fn); head = node_next(head)
288       end
289    else
290       print_fn(debug_depth .. ' (null list)')
291    end
292 end
293 function ltj.ext_show_node(head,depth,print_fn)
294    debug_depth = depth
295    if head then
296       debug_show_node_X(head, print_fn)
297    else
298       print_fn(debug_depth .. ' (null list)')
299    end
300 end
301 function debug_show_node_X(p,print_fn)
302    local k = debug_depth
303    local s
304    local pt=node_type(p.id)
305    local base = debug_depth .. string.format('%X', has_attr(p,attr_icflag) or 0) 
306      .. ' ' .. pt .. ' ' ..  p.subtype 
307    if pt == 'glyph' then
308       print_fn(base, utf.char(p.char), p.font)
309    elseif pt=='hlist' then
310       s = base .. '(' .. print_scaled(p.height) .. '+' 
311          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
312       if p.glue_sign >= 1 then 
313          s = s .. ' glue set '
314          if p.glue_sign == 2 then s = s .. '-' end
315          s = s .. tostring(math.floor(p.glue_set*10000)/10000)
316          if p.glue_order == 0 then 
317             s = s .. 'pt' 
318          else 
319             s = s .. 'fi'
320             for i = 2,  p.glue_order do s = s .. 'l' end
321          end
322       end
323       if has_attr(p, attr_icflag, PACKED) then
324          s = s .. ' (packed)'
325       end
326       print_fn(s)
327       local q = p.head
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    elseif pt == 'glue' then
334       s = base .. ' ' ..  print_spec(p.spec)
335       if has_attr(p, attr_icflag)==FROM_JFM then
336             s = s .. ' (from JFM)'
337       elseif has_attr(p, attr_icflag)==KANJI_SKIP then
338          s = s .. ' (kanjiskip)'
339       elseif has_attr(p, attr_icflag)==XKANJI_SKIP then
340          s = s .. ' (xkanjiskip)'
341       end
342       print_fn(s)
343    elseif pt == 'kern' then
344       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
345       if p.subtype==2 then
346          s = s .. ' (for accent)'
347       elseif has_attr(p, attr_icflag)==IC_PROCESSED then
348          s = s .. ' (italic correction)'
349       -- elseif has_attr(p, attr_icflag)==ITALIC then
350       --    s = s .. ' (italic correction)'
351       elseif has_attr(p, attr_icflag)==FROM_JFM then
352          s = s .. ' (from JFM)'
353       elseif has_attr(p, attr_icflag)==LINE_END then
354          s = s .. " (from 'lineend' in JFM)"
355       end
356       print_fn(s)
357    elseif pt == 'penalty' then
358       s = base .. ' ' .. tostring(p.penalty)
359       if has_attr(p, attr_icflag)==KINSOKU then
360          s = s .. ' (for kinsoku)'
361       end
362       print_fn(s)
363    elseif pt == 'whatsit' then
364       s = base .. ' subtype: ' ..  tostring(p.subtype)
365       if p.subtype==sid_user then
366          s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
367       else
368          s = s .. node.subtype(p.subtype)
369       end
370       print_fn(s)
371    else
372       print_fn(base)
373    end
374    p=node_next(p)
375 end
376
377
378 -- callbacks
379
380 luatexbase.add_to_callback('pre_linebreak_filter', 
381    function (head,groupcode)
382       return main_process(head, true, tex.textdir)
383    end,'ltj.pre_linebreak_filter',
384    luatexbase.priority_in_callback('pre_linebreak_filter',
385                                    'luaotfload.pre_linebreak_filter') + 1)
386 luatexbase.add_to_callback('hpack_filter', 
387   function (head,groupcode,size,packtype, dir)
388      return main_process(head, false, dir)
389   end,'ltj.hpack_filter',
390    luatexbase.priority_in_callback('hpack_filter',
391                                    'luaotfload.hpack_filter') + 1)