OSDN Git Service

lltjfont.sty, ltj-jfont.lua: different font number from different 4-tuple (enc, fam...
[luatex-ja/luatexja.git] / src / luatexja.lua
1
2 require('lualibs')
3
4 ------------------------------------------------------------------------
5 -- naming:
6 --    ext_... : called from \directlua{}
7 --    int_... : called from other Lua codes, but not from \directlua{}
8 --    (other)     : only called from this file
9 function luatexja.load_module(name)
10    require('ltj-' .. name.. '.lua')
11 end
12 function luatexja.load_lua(fn)
13    local found = kpse.find_file(fn, 'tex')
14    if not found then
15       tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
16    else 
17       texio.write_nl('(' .. found .. ')')
18       dofile(found)
19    end
20 end
21
22 --- 以下は全ファイルで共有される定数
23 local icflag_table = {}
24 luatexja.icflag_table = icflag_table
25 icflag_table.ITALIC       = 1
26 icflag_table.PACKED       = 2
27 icflag_table.KINSOKU      = 3
28 icflag_table.FROM_JFM     = 6
29 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高
30 -- 6 が標準
31 icflag_table.KANJI_SKIP   = 9
32 icflag_table.XKANJI_SKIP  = 10
33 icflag_table.PROCESSED    = 11
34 icflag_table.IC_PROCESSED = 12
35 icflag_table.BOXBDD       = 15
36 icflag_table.PROCESSED_BEGIN_FLAG = 32
37
38 local stack_table_index = {}
39 luatexja.stack_table_index = stack_table_index
40 stack_table_index.PRE  = 0x200000 -- characterごと
41 stack_table_index.POST = 0x400000 -- characterごと
42 stack_table_index.KCAT = 0x600000 -- characterごと
43 stack_table_index.XSP  = 0x800000 -- characterごと
44 stack_table_index.JWP  = 0 -- 0のみ
45 stack_table_index.MJT  = 0x100 -- 0--255
46 stack_table_index.MJS  = 0x200 -- 0--255
47 stack_table_index.MJSS = 0x300 -- 0--255
48 stack_table_index.KSJ  = 0x400 -- 0--9
49
50 local userid_table = {}
51 luatexja.userid_table = userid_table
52 userid_table.IHB  = luatexbase.newuserwhatsitid('inhibitglue',  'luatexja') -- \inhibitglue
53 userid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
54 userid_table.OTF  = luatexbase.newuserwhatsitid('char_by_cid',  'luatexja') -- luatexja-otf
55 userid_table.BPAR = luatexbase.newuserwhatsitid('begin_par',    'luatexja') -- 「段落始め」
56
57 --- 定義終わり
58
59 local load_module = luatexja.load_module
60 load_module('base');      local ltjb = luatexja.base
61 load_module('rmlgbm');    local ltjr = luatexja.rmlgbm -- must be 1st
62 load_module('charrange'); local ltjc = luatexja.charrange
63 load_module('jfont');     local ltjf = luatexja.jfont
64 load_module('inputbuf');  local ltji = luatexja.inputbuf
65 load_module('stack');     local ltjs = luatexja.stack
66 load_module('pretreat');  local ltjp = luatexja.pretreat
67 load_module('jfmglue');   local ltjj = luatexja.jfmglue
68 load_module('setwidth');  local ltjw = luatexja.setwidth
69 load_module('math');      local ltjm = luatexja.math
70 load_module('tangle');    local ltjb = luatexja.base
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
80 ---- table: charprop_stack_table [stack_level].{pre|post|xsp}[chr_code]
81
82
83 -- Three aux. functions, bollowed from tex.web
84
85 local unity=65536
86 local floor = math.floor
87
88 local function print_scaled(s)
89    local out=''
90    local delta=10
91    if s<0 then 
92       out=out..'-'; s=-s
93    end
94    out=out..tostring(floor(s/unity)) .. '.'
95    s=10*(s%unity)+5
96    repeat
97       if delta>unity then s=s+32768-50000 end
98       out=out .. tostring(floor(s/unity)) 
99       s=10*(s%unity)
100       delta=delta*10
101    until s<=delta
102    return out
103 end
104
105 local function print_glue(d,order)
106    local out=print_scaled(d)
107    if order>0 then
108       out=out..'fi'
109       while order>1 do
110          out=out..'l'; order=order-1
111       end
112    else 
113       out=out..'pt'
114    end
115    return out
116 end
117
118 local function print_spec(p)
119    local out=print_scaled(p.width)..'pt'
120    if p.stretch~=0 then
121       out=out..' plus '..print_glue(p.stretch,p.stretch_order)
122    end
123    if p.shrink~=0 then
124       out=out..' minus '..print_glue(p.shrink,p.shrink_order)
125    end
126 return out
127 end
128
129
130 ------------------------------------------------------------------------
131 -- CODE FOR GETTING/SETTING PARAMETERS 
132 ------------------------------------------------------------------------
133
134 -- EXT: print parameters that don't need arguments
135 function luatexja.ext_get_parameter_unary(k)
136    local t = tex.getcount('ltj@@stack')
137    if k == 'yalbaselineshift' then
138       tex.write(print_scaled(tex.getattribute('ltj@yablshift'))..'pt')
139    elseif k == 'yjabaselineshift' then
140       tex.write(print_scaled(tex.getattribute('ltj@ykblshift'))..'pt')
141    elseif k == 'kanjiskip' then
142       tex.write(print_spec(ltjs.get_skip_table('kanjiskip', t)))
143    elseif k == 'xkanjiskip' then
144       tex.write(print_spec(ltjs.get_skip_table('xkanjiskip', t)))
145    elseif k == 'jcharwidowpenalty' then
146       tex.write(ltjs.get_penalty_table(stack_table_index.JWP, 0, t))
147    elseif k == 'autospacing' then
148       tex.write(tex.getattribute('ltj@autospc'))
149    elseif k == 'autoxspacing' then
150       tex.write(tex.getattribute('ltj@autoxspc'))
151    elseif k == 'differentjfm' then
152       if luatexja.jfmglue.diffmet_rule == math.max then
153          tex.write('large')
154       elseif luatexja.jfmglue.diffmet_rule == math.min then
155          tex.write('small')
156       elseif luatexja.jfmglue.diffmet_rule == math.two_average then
157          tex.write('average')
158       elseif luatexja.jfmglue.diffmet_rule == math.two_paverage then
159          tex.write('paverage')
160       elseif luatexja.jfmglue.diffmet_rule == math.two_pleft then
161          tex.write('pleft')
162       elseif luatexja.jfmglue.diffmet_rule == math.two_pright then
163          tex.write('pright')
164       elseif luatexja.jfmglue.diffmet_rule == math.two_add then
165          tex.write('both')
166       else -- This can't happen.
167          tex.write('???')
168       end
169    end
170 end
171
172
173 -- EXT: print parameters that need arguments
174 function luatexja.ext_get_parameter_binary(k,c)
175    local t = tex.getcount('ltj@@stack')
176    if type(c)~='number' then
177       ltjb.package_error('luatexja',
178                          'invalid the second argument (' .. tostring(c) .. ')',
179                          'I changed this one to zero.')
180       c=0
181    end
182    if k == 'jacharrange' then
183       if c>=31*ltjc.ATTR_RANGE then 
184          ltjb.package_error('luatexja',
185                             'invalid character range number (' .. c .. ')',
186                             'A character range number should be in the range 0..'
187                                .. 31*ltjc.ATTR_RANGE-1 .. ",\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(stack_table_index.PRE + c, 0, t))
204       elseif k == 'postbreakpenalty' then
205          tex.write(ltjs.get_penalty_table(stack_table_index.POST+ c, 0, t))
206       elseif k == 'kcatcode' then
207          tex.write(ltjs.get_penalty_table(stack_table_index.KCAT+ c, 0, t))
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(stack_table_index.XSP + c, 3, t))
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 do
223    -- mode = true iff main_process is called from pre_linebreak_filter
224    local function main_process(head, mode, dir)
225       local p = head
226       p = ltjj.main(p,mode)
227       if p then p = ltjw.set_ja_width(p, dir) end
228       return p
229    end
230    
231    -- callbacks
232    
233    luatexbase.add_to_callback(
234       'pre_linebreak_filter', 
235       function (head,groupcode)
236          return main_process(head, true, tex.textdir)
237       end,'ltj.pre_linebreak_filter',
238       luatexbase.priority_in_callback('pre_linebreak_filter',
239                                       'luaotfload.node_processor') + 1)
240    luatexbase.add_to_callback(
241       'hpack_filter', 
242       function (head,groupcode,size,packtype, dir)
243          return main_process(head, false, dir)
244       end,'ltj.hpack_filter',
245       luatexbase.priority_in_callback('hpack_filter',
246                                       'luaotfload.node_processor') + 1)
247 end
248
249 -- hyphenate
250 do
251    local replace_altfont = ltjf.replace_altfont
252    luatexbase.add_to_callback(
253       'hyphenate', 
254       function (head,tail)
255          return replace_altfont(head)
256       end,'ltj.replace_altfont',
257       luatexbase.priority_in_callback('hyphenate', 'ltj.hyphenate')+1)
258 end
259
260 -- define_font
261 do
262    local otfl_fdr = fonts.definers.read
263    local ltjr_font_callback = ltjr.font_callback
264    function luatexja.font_callback(name, size, id)
265       return ltjf.font_callback(
266          name, size, id, 
267          function (name, size, id) return ltjr_font_callback(name, size, id, otfl_fdr) end
268       )
269    end
270    luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
271 end
272
273
274
275
276 -- debug
277
278 do
279
280 local node_type = node.type
281 local node_next = node.next
282 local has_attr = node.has_attribute
283
284 local id_penalty = node.id('penalty')
285 local id_glyph = node.id('glyph')
286 local id_glue_spec = node.id('glue_spec')
287 local id_glue = node.id('glue')
288 local id_kern = node.id('kern')
289 local id_hlist = node.id('hlist')
290 local id_vlist = node.id('vlist')
291 local id_rule = node.id('rule')
292 local id_math = node.id('math')
293 local id_whatsit = node.id('whatsit')
294 local sid_user = node.subtype('user_defined')
295
296 local function get_attr_icflag(p)
297    return (has_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
298 end
299
300 local debug_depth
301
302 local function debug_show_node_X(p,print_fn)
303    local k = debug_depth
304    local s
305    local pt=node_type(p.id)
306    local base = debug_depth .. string.format('%X', get_attr_icflag(p))
307    .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
308    if pt == 'glyph' then
309       s = base .. ' ' .. utf.char(p.char) .. ' '  .. tostring(p.font)
310          .. ' (' .. print_scaled(p.height) .. '+' 
311          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
312       print_fn(s)
313    elseif pt=='hlist' or pt=='vlist' then
314       s = base .. '(' .. print_scaled(p.height) .. '+' 
315          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width) .. p.dir
316       if p.shift~=0 then
317          s = s .. ', shifted ' .. print_scaled(p.shift)
318       end
319       if p.glue_sign >= 1 then 
320          s = s .. ' glue set '
321          if p.glue_sign == 2 then s = s .. '-' end
322          s = s .. tostring(floor(p.glue_set*10000)/10000)
323          if p.glue_order == 0 then 
324             s = s .. 'pt' 
325          else 
326             s = s .. 'fi'
327             for i = 2,  p.glue_order do s = s .. 'l' end
328          end
329       end
330       if get_attr_icflag(p) == icflag_table.PACKED then
331          s = s .. ' (packed)'
332       end
333       print_fn(s)
334       local q = p.head
335       debug_depth=debug_depth.. '.'
336       while q do 
337          debug_show_node_X(q, print_fn); q = node_next(q)
338       end
339       debug_depth=k
340    elseif pt == 'glue' then
341       s = base .. ' ' ..  print_spec(p.spec)
342       if get_attr_icflag(p)>icflag_table.KINSOKU 
343          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
344          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
345       elseif get_attr_icflag(p)==icflag_table.KANJI_SKIP then
346          s = s .. ' (kanjiskip)'
347       elseif get_attr_icflag(p)==icflag_table.XKANJI_SKIP then
348          s = s .. ' (xkanjiskip)'
349       end
350       print_fn(s)
351    elseif pt == 'kern' then
352       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
353       if p.subtype==2 then
354          s = s .. ' (for accent)'
355       elseif get_attr_icflag(p)==icflag_table.IC_PROCESSED then
356          s = s .. ' (italic correction)'
357          -- elseif get_attr_icflag(p)==ITALIC then
358          --    s = s .. ' (italic correction)'
359       elseif get_attr_icflag(p)>icflag_table.KINSOKU 
360          and get_attr_icflag(p)<icflag_table.KANJI_SKIP then
361          s = s .. ' (from JFM: priority ' .. get_attr_icflag(p)-icflag_table.FROM_JFM .. ')'
362       end
363       print_fn(s)
364    elseif pt == 'penalty' then
365       s = base .. ' ' .. tostring(p.penalty)
366       if get_attr_icflag(p)==icflag_table.KINSOKU then
367          s = s .. ' (for kinsoku)'
368       end
369       print_fn(s)
370    elseif pt == 'whatsit' then
371       s = base .. ' subtype: ' ..  tostring(p.subtype)
372       if p.subtype==sid_user then
373          if p.type ~= 110 then 
374             s = s .. ' user_id: ' .. p.user_id .. ' ' .. p.value
375             print_fn(s)
376          else
377             s = s .. ' user_id: ' .. p.user_id .. ' (node list)'
378             print_fn(s)
379             local q = p.value
380             debug_depth=debug_depth.. '.'
381             while q do 
382                debug_show_node_X(q, print_fn); q = node_next(q)
383             end
384             debug_depth=k
385          end
386       else
387          s = s .. node.subtype(p.subtype); print_fn(s)
388       end
389    -------- math node --------
390    elseif pt=='noad' then
391       s = base ; print_fn(s)
392       if p.nucleus then
393          debug_depth = k .. 'N'; debug_show_node_X(p.nucleus, print_fn); 
394       end
395       if p.sup then
396          debug_depth = k .. '^'; debug_show_node_X(p.sup, print_fn); 
397       end
398       if p.sub then
399          debug_depth = k .. '_'; debug_show_node_X(p.sub, print_fn); 
400       end
401       debug_depth = k;
402    elseif pt=='math_char' then
403       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utf.char(p.char)
404       print_fn(s)
405    elseif pt=='sub_box' then
406       print_fn(base)
407       if p.head then
408          debug_depth = k .. '.'; debug_show_node_X(p.head, print_fn); 
409       end
410    else
411       print_fn(base)
412    end
413    p=node_next(p)
414 end
415 function luatexja.ext_show_node_list(head,depth,print_fn)
416    debug_depth = depth
417    if head then
418       while head do
419          debug_show_node_X(head, print_fn); head = node_next(head)
420       end
421    else
422       print_fn(debug_depth .. ' (null list)')
423    end
424 end
425 function luatexja.ext_show_node(head,depth,print_fn)
426    debug_depth = depth
427    if head then
428       debug_show_node_X(head, print_fn)
429    else
430       print_fn(debug_depth .. ' (null list)')
431    end
432 end
433
434 end