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
15 local Dnode = node.direct or node
17 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
18 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
19 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
20 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
22 local nullfunc = function(n) return n end
23 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
25 local node_new = Dnode.new
26 local node_free = Dnode.free
27 local has_attr = Dnode.has_attribute
28 local set_attr = Dnode.set_attribute
29 local node_write = Dnode.write
30 local round = tex.round
31 local font_getfont = font.getfont
33 local attr_icflag = luatexbase.attributes['ltj@icflag']
34 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
35 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
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
57 function define_jfm(t)
58 local real_char -- Does current character class have the 'real' character?
59 if t.dir~=jfm_dir then
60 defjfm_res= nil; return
61 elseif type(t.zw)~='number' or type(t.zh)~='number' then
62 defjfm_res= nil; return
64 t.char_type = {}; t.chars = {}
65 for i,v in pairs(t) do
66 if type(i) == 'number' then -- char_type
68 if i ~= 0 then defjfm_res= nil; return end
72 for j,w in pairs(v.chars) do
73 if type(w) == 'number' and w~=-1 then
75 elseif type(w) == 'string' and utf.len(w)==1 then
76 real_char = true; w = utf.byte(w)
77 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
78 real_char = true; w = utf.byte(utf.sub(w,1,1))
79 if not t.chars[-w] then
82 defjfm_res= nil; return
85 if not t.chars[w] then
88 defjfm_res= nil; return
91 if type(v.align)~='string' then
92 v.align = 'left' -- left
95 if type(v.width)~='number' and v.width~='prop' then
96 defjfm_res= nil; return
98 if v.width=='prop' and jfm_dir=='tate' then
101 if type(v.height)~='number' then
104 if type(v.depth)~='number' then
107 if type(v.italic)~='number' then
110 if type(v.left)~='number' then
113 if type(v.down)~='number' then
120 v.kern = v.kern or {}; v.glue = v.glue or {}
121 for j in pairs(v.glue) do
122 if v.kern[j] then defjfm_res= nil; return end
124 for j,x in pairs(v.kern) do
125 if type(x)=='number' then
127 elseif type(x)=='table' then
128 v.kern[j] = {x[1], x[2] or 0}
135 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
140 local update_jfm_cache
142 local function mult_table(old,scale) -- modified from table.fastcopy
145 for k,v in next, old do
146 if type(v) == "table" then
147 new[k] = mult_table(v,scale)
148 elseif type(v) == "number" then
149 new[k] = round(v*scale)
158 update_jfm_cache = function (j,sz)
159 if metrics[j].size_cache[sz] then return end
161 metrics[j].size_cache[sz] = t
162 t.chars = metrics[j].chars
163 t.char_type = mult_table(metrics[j].char_type, sz)
164 for i,v in pairs(t.char_type) do
165 v.align = (v.align=='left') and 0 or
166 ((v.align=='right') and 1 or 0.5)
167 if type(i) == 'number' then -- char_type
168 for k,w in pairs(v.glue) do
169 local h = node_new(id_glue_spec)
170 v[k] = {true, h, (w[5] and w[5]/sz or 0), FROM_JFM + (w[4] and w[4]/sz or 0)}
171 setfield(h, 'width', w[1])
172 setfield(h, 'stretch', w[2])
173 setfield(h, 'shrink', w[3])
174 setfield(h, 'stretch_order', 0)
175 setfield(h, 'shrink_order', 0)
177 for k,w in pairs(v.kern) do
178 local g = node_new(id_kern)
179 setfield(g, 'kern', w[1])
180 setfield(g, 'subtype', 1)
181 set_attr(g, attr_icflag, FROM_JFM)
182 v[k] = {false, g, w[2]/sz}
185 v.glue, v.kern = nil, nil
187 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
188 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
189 t.zw = round(metrics[j].zw*sz)
190 t.zh = round(metrics[j].zh*sz)
194 luatexbase.create_callback("luatexja.find_char_class", "data",
195 function (arg, fmtable, char)
199 function find_char_class(c,m)
200 -- c: character code, m:
201 if not m then return 0 end
203 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
207 ------------------------------------------------------------------------
208 -- LOADING JAPANESE FONTS
209 ------------------------------------------------------------------------
213 local global_flag -- true if \globaljfont, false if \jfont
214 local function load_jfont_metric()
215 if jfm_file_name=='' then
216 ltjb.package_error('luatexja',
218 'To load and define a Japanese font, a JFM must be specified.'..
219 "The JFM 'ujis' will be used for now.")
222 for j,v in ipairs(metrics) do
223 if v.name==jfm_file_name then return j end
225 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
227 defjfm_res.name = jfm_file_name
228 table.insert(metrics, defjfm_res)
236 function jfontdefX(g)
237 local t = token.get_next()
238 cstemp=token.csname_name(t)
239 global_flag = g and '\\global' or ''
240 tex.sprint(cat_lp, '\\expandafter\\font\\csname ', cstemp, '\\endcsname')
243 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
246 local identifiers = fonts.hashes.identifiers
247 function jfontdefY(dir)
249 local j = load_jfont_metric(dir)
250 local fn = font.id(cstemp)
251 local f = font_getfont(fn)
253 ltjb.package_error('luatexja',
254 "bad JFM `" .. jfm_file_name .. "'",
255 'The JFM file you specified is not valid JFM file.\n'..
256 'So defining Japanese font is cancelled.')
257 tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ', cstemp,
258 '\\endcsname=\\relax')
261 update_jfm_cache(j, f.size)
262 local ad = identifiers[fn].parameters
263 local sz = metrics[j].size_cache[f.size]
264 local fmtable = { jfm = j, size = f.size, var = jfm_var,
265 zw = sz.zw, zh = sz.zh,
266 ascent = ad.ascender,
267 descent = ad.descender,
268 chars = sz.chars, char_type = sz.char_type,
269 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
272 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
273 font_metric_table[fn]=fmtable
274 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
275 cstemp , '\\endcsname{\\ltj@cur'..
276 (dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
283 local a = font_metric_table[tex.attribute[attr_curjfnt]]
284 return a and a.zw or 0
287 local a = font_metric_table[tex.attribute[attr_curjfnt]]
288 return a and a.zw or 0
293 -- extract jfm_file_name and jfm_var
294 -- normalize position of 'jfm=' and 'jfmvar=' keys
295 local function extract_metric(name)
296 jfm_file_name = ''; jfm_var = ''
297 local tmp, index = name:sub(1, 5), 1
298 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
301 local p = name:find(":", index); index = p and (p+1) or index
303 local l = name:len()+1
304 local q = name:find(";", index+1) or l
305 if name:sub(index, index+3)=='jfm=' and q>index+4 then
306 jfm_file_name = name:sub(index+4, q-1)
308 name = name:sub(1,index-1) .. name:sub(q+1)
310 name = name:sub(1,index-1)
313 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
314 jfm_var = name:sub(index+7, q-1)
316 name = name:sub(1,index-1) .. name:sub(q+1)
318 name = name:sub(1,index-1)
322 index = (l~=q) and (q+1) or nil
325 if jfm_file_name~='' then
326 local l = name:sub(-1)
328 .. ((l==':' or l==';') and '' or ';')
329 .. 'jfm=' .. jfm_file_name
331 name = name .. 'jfmvar=' .. jfm_var
336 luatexja.jfont.extract_metric = extract_metric
339 ------------------------------------------------------------------------
341 ------------------------------------------------------------------------
343 -- these function are called from ltj-latex.sty
344 local kyenc_list, ktenc_list = {}, {}
345 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
346 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
347 function is_kyenc(enc)
348 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
350 function is_ktenc(enc)
351 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
353 function is_kenc(enc)
354 tex.sprint(cat_lp, '\\let\\ifin@\\if'
355 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
358 local kfam_list, Nkfam_list = {}, {}
359 function add_kfam_list(enc, fam)
360 if not kfam_list[enc] then kfam_list[enc] = {} end
361 kfam_list[enc][fam] = 'true '
363 function add_Nkfam_list(enc, fam)
364 if not Nkfam_list[enc] then Nkfam_list[enc] = {} end
365 Nkfam_list[enc][fam] = 'true '
367 function is_kfam(enc, fam)
368 tex.sprint(cat_lp, '\\let\\ifin@\\if'
369 .. (kfam_list[enc] and kfam_list[enc][fam] or 'false ')) end
370 function is_Nkfam(enc, fam)
371 tex.sprint(cat_lp, '\\let\\ifin@\\if'
372 .. (Nkfam_list[enc] and Nkfam_list[enc][fam] or 'false ')) end
374 local ffam_list, Nffam_list = {}, {}
375 function add_ffam_list(enc, fam)
376 if not ffam_list[enc] then ffam_list[enc] = {} end
377 ffam_list[enc][fam] = 'true '
379 function add_Nffam_list(enc, fam)
380 if not Nffam_list[enc] then Nffam_list[enc] = {} end
381 Nffam_list[enc][fam] = 'true '
383 function is_ffam(enc, fam)
384 tex.sprint(cat_lp, '\\let\\ifin@\\if'
385 .. (ffam_list[enc] and ffam_list[enc][fam] or 'false ')) end
386 function is_Nffam(enc, fam)
387 tex.sprint(cat_lp, '\\let\\ifin@\\if'
388 .. (Nffam_list[enc] and Nffam_list[enc][fam] or 'false ')) end
390 ------------------------------------------------------------------------
392 ------------------------------------------------------------------------
394 local alt_font_table = alt_font_table
395 local attr_curaltfnt = {}
396 local ucs_out = 0x110000
398 ------ for TeX interface
400 function set_alt_font(b,e,ind,bfnt)
401 -- ind: 新フォント, bfnt: 基底フォント
402 if b>e then b, e = e, b end
404 ltjb.package_error('luatexja',
405 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
406 'I take the intersection with [0x80, 0x10ffff].')
407 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
408 elseif e<0 then -- b<e<0
410 elseif b<0x80 or e>=ucs_out then
411 ltjb.package_warning('luatexja',
412 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
413 'I take the intersection with [0x80, 0x10ffff].')
414 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
416 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
417 local t = alt_font_table[bfnt]
418 local ac = font_getfont(ind).characters
419 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
420 if e>=0 then -- character range
422 if ac[i]then t[i]=ind end
426 local tx = font_metric_table[bfnt].chars
427 for i,v in pairs(tx) do
428 if b<=v and v<=e and ac[i] then t[i]=ind end
434 function clear_alt_font(bfnt)
435 if alt_font_table[bfnt] then
436 local t = alt_font_table[bfnt]
437 for i,_ in pairs(t) do t[i]=nil; end
441 ------ used in ltjp.suppress_hyphenate_ja callback
442 function replace_altfont(pf, pc)
443 local a = alt_font_table[pf]
444 return a and a[pc] or pf
447 ------ for LaTeX interface
449 local alt_font_table_latex = {}
452 function clear_alt_font_latex(bbase)
453 local t = alt_font_table_latex[bbase]
455 for j,v in pairs(t) do t[j] = nil end
460 function set_alt_font_latex(b,e,ind,bbase)
461 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
462 if b>e then b, e = e, b end
464 ltjb.package_error('luatexja',
465 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
466 'I take the intersection with [0x80, 0x10ffff].')
467 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
468 elseif e<0 then -- b<e<0
470 elseif b<0x80 or e>=ucs_out then
471 ltjb.package_warning('luatexja',
472 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
473 'I take the intersection with [0x80, 0x10ffff].')
474 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
477 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
478 local t = alt_font_table_latex[bbase]
479 if not t[ind] then t[ind] = {} end
481 for j,v in pairs(t) do
482 if v[i] then -- remove old entry
483 if j~=ind then v[i]=nil end; break
488 -- remove the empty tables
489 for j,v in pairs(t) do
490 local flag_clear = true
491 for k,_ in pairs(v) do flag_clear = false; break end
492 if flag_clear then t[j]=nil end
494 if ind==bbase then t[bbase] = nil end
497 -- ここから先は 新 \selectfont の内部でしか実行されない
499 local alt_font_base, alt_font_base_num
502 function does_alt_set(bbase)
503 aftl_base = alt_font_table_latex[bbase]
504 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
507 function print_aftl_address()
508 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
512 function output_alt_font_cmd(dir, bbase)
513 alt_font_base = bbase
515 alt_font_base_num = tex.getattribute(attr_curtfnt)
517 alt_font_base_num = tex.getattribute(attr_curjfnt)
519 local t = alt_font_table[alt_font_base_num]
521 for i,_ in pairs(t) do t[i]=nil end
523 t = alt_font_table_latex[bbase]
525 for i,_ in pairs(t) do
526 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
532 function pickup_alt_font_a(size_str)
533 local t = alt_font_table_latex[alt_font_base]
535 for i,v in pairs(t) do
536 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
537 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
542 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
543 local t = alt_font_table[alt_font_base_num]
544 local tx = font_metric_table[alt_font_base_num].chars
545 for i,v in pairs(tx) do
546 if v==class and afnt_chars[i] then t[i]=afnt_num end
551 function pickup_alt_font_b(afnt_num, afnt_base)
552 local t = alt_font_table[alt_font_base_num]
553 local ac = font_getfont(afnt_num).characters
554 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
555 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
556 if i == afnt_base then
557 for j,_ in pairs(v) do
559 if ac[j] then t[j]=afnt_num end
560 else -- -n (n>=1) means that the character class n,
561 -- which is defined in the JFM
562 pickup_alt_font_class(-j, afnt_num, ac)
573 ------------------------------------------------------------------------
575 ------------------------------------------------------------------------
577 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
578 -- EXT: italic correction
579 function append_italic()
580 local p = to_direct(tex.nest[tex.nest.ptr].tail)
581 if p and getid(p)==id_glyph then
583 local g = node_new(id_kern)
584 setfield(g, 'subtype', 1)
585 set_attr(g, attr_icflag, ITALIC)
586 if is_ucs_in_japanese_char(p) then
587 f = has_attr(p, attr_curjfnt)
588 local j = font_metric_table[f]
589 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
591 local h = font_getfont(f)
593 setfield(g, 'kern', h.characters[getchar(p)].italic)
595 tex.attribute[attr_icflag] = 0
600 tex.attribute[attr_icflag] = 0