4 luatexbase.provides_module({
5 name = 'luatexja.jfont',
7 description = 'Loader for Japanese fonts',
9 module('luatexja.jfont', package.seeall)
11 luatexja.load_module('base'); local ltjb = luatexja.base
12 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
14 local mem_leak_glue, mem_leak_gs, mem_leak_kern = 0, 0, 0
16 local Dnode = node.direct or node
18 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
19 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
20 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
21 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
23 local nullfunc = function(n) return n end
24 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
26 local node_new = Dnode.new
27 local node_free = Dnode.free
28 local has_attr = Dnode.has_attribute
29 local set_attr = Dnode.set_attribute
30 local node_write = Dnode.write
31 local round = tex.round
32 local font_getfont = font.getfont
34 local attr_icflag = luatexbase.attributes['ltj@icflag']
35 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
36 local id_glyph = node.id('glyph')
37 local id_kern = node.id('kern')
38 local id_glue_spec = node.id('glue_spec')
39 local id_glue = node.id('glue')
40 local cat_lp = luatexbase.catcodetables['latex-package']
41 local ITALIC = luatexja.icflag_table.ITALIC
42 local FROM_JFM = luatexja.icflag_table.FROM_JFM
44 ------------------------------------------------------------------------
46 ------------------------------------------------------------------------
48 metrics={} -- this table stores all metric informations
49 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
51 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
53 local jfm_file_name, jfm_var
56 function define_jfm(t)
57 local real_char -- Does current character class have the 'real' character?
59 defjfm_res= nil; return
60 elseif type(t.zw)~='number' or type(t.zh)~='number' then
61 defjfm_res= nil; return
63 t.char_type = {}; t.chars = {}
64 for i,v in pairs(t) do
65 if type(i) == 'number' then -- char_type
67 if i ~= 0 then defjfm_res= nil; return end
71 for j,w in pairs(v.chars) do
72 if type(w) == 'number' then
74 elseif type(w) == 'string' and utf.len(w)==1 then
75 real_char = true; w = utf.byte(w)
76 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
77 real_char = true; w = utf.byte(utf.sub(w,1,1))
78 if not t.chars[-w] then
81 defjfm_res= nil; return
84 if not t.chars[w] then
87 defjfm_res= nil; return
90 if type(v.align)~='string' then
91 v.align = 'left' -- left
94 if not (type(v.width)=='number' or v.width~='prop') then
95 defjfm_res= nil; return
97 if type(v.height)~='number' then
100 if type(v.depth)~='number' then
103 if type(v.italic)~='number' then
106 if type(v.left)~='number' then
109 if type(v.down)~='number' then
116 v.kern = v.kern or {}; v.glue = v.glue or {}
117 for j in pairs(v.glue) do
118 if v.kern[j] then defjfm_res= nil; return end
120 for j,x in pairs(v.kern) do
121 if type(x)=='number' then
123 elseif type(x)=='table' then
124 v.kern[j] = {x[1], x[2] or 0}
131 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
136 local update_jfm_cache
138 local function mult_table(old,scale) -- modified from table.fastcopy
141 for k,v in next, old do
142 if type(v) == "table" then
143 new[k] = mult_table(v,scale)
144 elseif type(v) == "number" then
145 new[k] = round(v*scale)
154 update_jfm_cache = function (j,sz)
155 if metrics[j].size_cache[sz] then return end
157 metrics[j].size_cache[sz] = t
158 t.chars = metrics[j].chars
159 t.char_type = mult_table(metrics[j].char_type, sz)
160 for i,v in pairs(t.char_type) do
161 if type(i) == 'number' then -- char_type
162 for k,w in pairs(v.glue) do
163 local h = node_new(id_glue_spec)
164 mem_leak_gs = mem_leak_gs+1
165 v[k] = {true, h, (w[5] and w[5]/sz or 0), FROM_JFM + (w[4] and w[4]/sz or 0)}
166 setfield(h, 'width', w[1])
167 setfield(h, 'stretch', w[2])
168 setfield(h, 'shrink', w[3])
169 setfield(h, 'stretch_order', 0)
170 setfield(h, 'shrink_order', 0)
172 for k,w in pairs(v.kern) do
173 local g = node_new(id_kern)
174 mem_leak_kern = mem_leak_kern +1
175 setfield(g, 'kern', w[1])
176 setfield(g, 'subtype', 1)
177 set_attr(g, attr_icflag, FROM_JFM)
178 v[k] = {false, g, w[2]/sz}
182 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
183 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
184 t.zw = round(metrics[j].zw*sz)
185 t.zh = round(metrics[j].zh*sz)
189 luatexbase.create_callback("luatexja.find_char_class", "data",
190 function (arg, fmtable, char)
194 function find_char_class(c,m)
195 -- c: character code, m:
196 if not m then return 0 end
198 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
202 ------------------------------------------------------------------------
203 -- LOADING JAPANESE FONTS
204 ------------------------------------------------------------------------
208 local global_flag -- true if \globaljfont, false if \jfont
209 local function load_jfont_metric()
210 if jfm_file_name=='' then
211 ltjb.package_error('luatexja',
213 'To load and define a Japanese font, a JFM must be specified.'..
214 "The JFM 'ujis' will be used for now.")
217 for j,v in ipairs(metrics) do
218 if v.name==jfm_file_name then return j end
220 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
222 defjfm_res.name = jfm_file_name
223 table.insert(metrics, defjfm_res)
231 function jfontdefX(g)
232 local t = token.get_next()
233 cstemp=token.csname_name(t)
234 global_flag = g and '\\global' or ''
235 tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
238 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
241 function jfontdefY() -- for horizontal font
242 local j = load_jfont_metric()
243 local fn = font.id(cstemp)
244 local f = font_getfont(fn)
246 ltjb.package_error('luatexja',
247 "bad JFM `" .. jfm_file_name .. "'",
248 'The JFM file you specified is not valid JFM file.\n'..
249 'So defining Japanese font is cancelled.')
250 tex.sprint(cat_lp, global_flag .. '\\expandafter\\let\\csname ' ..cstemp
251 .. '\\endcsname=\\relax')
254 update_jfm_cache(j, f.size)
255 --print('MEMORY LEAK (acc): ', mem_leak_glue, mem_leak_gs, mem_leak_kern)
256 local sz = metrics[j].size_cache[f.size]
257 local fmtable = { jfm = j, size = f.size, var = jfm_var,
258 zw = sz.zw, zh = sz.zh,
259 chars = sz.chars, char_type = sz.char_type,
260 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
263 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
264 font_metric_table[fn]=fmtable
265 tex.sprint(cat_lp, global_flag .. '\\protected\\expandafter\\def\\csname '
266 .. cstemp .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
273 local a = font_metric_table[tex.attribute[attr_curjfnt]]
274 tex.setdimen('ltj@zw', a and a.zw or 0)
278 local a = font_metric_table[tex.attribute[attr_curjfnt]]
279 tex.setdimen('ltj@zh', a and a.zh or 0)
284 -- extract jfm_file_name and jfm_var
285 local function extract_metric(name)
287 local tmp = utf.sub(basename, 1, 5)
288 jfm_file_name = ''; jfm_var = ''
289 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
290 basename = utf.sub(basename, 6)
292 local p = utf.find(basename, ":")
294 basename = utf.sub(basename, p+1)
297 -- now basename contains 'features' only.
300 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
301 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
302 jfm_file_name = utf.sub(basename, p+4, q-1)
303 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
304 jfm_var = utf.sub(basename, p+7, q-1)
306 if utf.len(basename)+1==q then p = nil else p = q + 1 end
311 -- replace fonts.define.read()
312 function font_callback(name, size, id, fallback)
314 -- In the present imple., we don't remove "jfm=..." from name.
315 return fallback(name, size, id)
319 ------------------------------------------------------------------------
321 ------------------------------------------------------------------------
323 local alt_font_table = alt_font_table
324 local attr_curaltfnt = {}
325 local ucs_out = 0x110000
327 ------ for TeX interface
329 function set_alt_font(b,e,ind,bfnt)
330 -- ind: 新フォント, bfnt: 基底フォント
331 if b>e then b, e = e, b end
333 ltjb.package_eror('luatexja',
334 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
335 'I take the intersection with [0x80, 0x10ffff].')
336 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
337 elseif e<0 then -- b<e<0
339 elseif b<0x80 or e>=ucs_out then
340 ltjb.package_warning('luatexja',
341 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
342 'I take the intersection with [0x80, 0x10ffff].')
343 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
345 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
346 local t = alt_font_table[bfnt]
347 local ac = font_getfont(ind).characters
348 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
349 if e>=0 then -- character range
351 if ac[i]then t[i]=ind end
355 local tx = font_metric_table[bfnt].chars
356 for i,v in pairs(tx) do
357 if b<=v and v<=e and ac[i] then t[i]=ind end
363 function clear_alt_font(bfnt)
364 if alt_font_table[bfnt] then
365 local t = alt_font_table[bfnt]
366 for i,_ in pairs(t) do t[i]=nil; end
370 ------ used in ltjp.suppress_hyphenate_ja callback
371 function replace_altfont(pf, pc)
372 local a = alt_font_table[pf]
373 return a and a[pc] or pf
376 ------ for LaTeX interface
378 local alt_font_table_latex = {}
381 function clear_alt_font_latex(bbase)
382 local t = alt_font_table_latex[bbase]
384 for j,v in pairs(t) do t[j] = nil end
389 function set_alt_font_latex(b,e,ind,bbase)
390 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
391 if b>e then b, e = e, b end
393 ltjb.package_eror('luatexja',
394 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
395 'I take the intersection with [0x80, 0x10ffff].')
396 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
397 elseif e<0 then -- b<e<0
399 elseif b<0x80 or e>=ucs_out then
400 ltjb.package_warning('luatexja',
401 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
402 'I take the intersection with [0x80, 0x10ffff].')
403 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
406 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
407 local t = alt_font_table_latex[bbase]
408 if not t[ind] then t[ind] = {} end
410 for j,v in pairs(t) do
411 if v[i] then -- remove old entry
412 if j~=ind then v[i]=nil end; break
417 -- remove the empty tables
418 for j,v in pairs(t) do
419 local flag_clear = true
420 for k,_ in pairs(v) do flag_clear = false; break end
421 if flag_clear then t[j]=nil end
423 if ind==bbase then t[bbase] = nil end
426 -- ここから先は 新 \selectfont の内部でしか実行されない
428 local alt_font_base, alt_font_base_num
431 function does_alt_set(bbase)
432 aftl_base = alt_font_table_latex[bbase]
433 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
436 function print_aftl_address()
437 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
441 function output_alt_font_cmd(bbase)
442 alt_font_base = bbase
443 alt_font_base_num = tex.getattribute(attr_curjfnt)
444 local t = alt_font_table[alt_font_base_num]
446 for i,_ in pairs(t) do t[i]=nil end
448 t = alt_font_table_latex[bbase]
450 for i,_ in pairs(t) do
451 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux{' .. i .. '}')
457 function pickup_alt_font_a(size_str)
458 local t = alt_font_table_latex[alt_font_base]
460 for i,v in pairs(t) do
461 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
462 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
467 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
468 local t = alt_font_table[alt_font_base_num]
469 local tx = font_metric_table[alt_font_base_num].chars
470 for i,v in pairs(tx) do
471 if v==class and afnt_chars[i] then t[i]=afnt_num end
476 function pickup_alt_font_b(afnt_num, afnt_base)
477 local t = alt_font_table[alt_font_base_num]
478 local ac = font_getfont(afnt_num).characters
479 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
480 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
481 if i == afnt_base then
482 for j,_ in pairs(v) do
484 if ac[j] then t[j]=afnt_num end
485 else -- -n (n>=1) means that the character class n,
486 -- which is defined in the JFM
487 pickup_alt_font_class(-j, afnt_num, ac)
498 ------------------------------------------------------------------------
500 ------------------------------------------------------------------------
502 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
503 -- EXT: italic correction
504 function append_italic()
505 local p = to_direct(tex.nest[tex.nest.ptr].tail)
506 if p and getid(p)==id_glyph then
508 local g = node_new(id_kern)
509 setfield(g, 'subtype', 1)
510 set_attr(g, attr_icflag, ITALIC)
511 if is_ucs_in_japanese_char(p) then
512 f = has_attr(p, attr_curjfnt)
513 local j = font_metric_table[f]
514 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
516 local h = font_getfont(f)
518 setfield(g, 'kern', h.characters[getchar(p)].italic)
520 tex.attribute[attr_icflag] = 0
525 tex.attribute[attr_icflag] = 0