OSDN Git Service

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