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
13 luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
14 luatexja.load_module('direction'); local ltjd = luatexja.direction
16 local setfield = node.direct.setfield
17 local getid = node.direct.getid
18 local to_direct = node.direct.todirect
20 local node_new = node.direct.new
21 local node_free = node.direct.free
22 local has_attr = node.direct.has_attribute
23 local set_attr = node.direct.set_attribute
24 local round = tex.round
25 local font_getfont = font.getfont
27 local attr_icflag = luatexbase.attributes['ltj@icflag']
28 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
29 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
30 local id_glyph = node.id('glyph')
31 local id_kern = node.id('kern')
32 local cat_lp = luatexbase.catcodetables['latex-package']
33 local FROM_JFM = luatexja.icflag_table.FROM_JFM
34 ------------------------------------------------------------------------
36 ------------------------------------------------------------------------
38 metrics={} -- this table stores all metric informations
39 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
41 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
43 local jfm_file_name, jfm_var, jfm_ksp
45 local jfm_dir, is_def_jfont, is_vert_enabled, auto_enable_vrt2
47 local function norm_val(a)
48 if (not a) or (a==0.) then
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.version = (type(t.version)=='number') and t.version or 1
65 t.char_type = {}; t.chars = {}
66 for i,v in pairs(t) do
67 if type(i) == 'number' then -- char_type
69 if i ~= 0 then defjfm_res= nil; return end
71 for j,w in pairs(v.chars) do
72 if type(w) == 'number' and w~=-1 then
73 elseif type(w) == 'string' and utf.len(w)==1 then
75 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
76 w = utf.byte(utf.sub(w,1,1))
78 if not t.chars[w] then
81 defjfm_res= nil; return
86 if type(v.align)~='string' then
87 v.align = 'left' -- left
89 if type(v.width)~='number' then
92 if type(v.height)~='number' then
93 v.height = (jfm_dir=='tate') and 0.0
95 if type(v.depth)~='number' then
96 v.depth = (jfm_dir=='tate') and 0.0
98 if type(v.italic)~='number' then
101 if type(v.left)~='number' then
104 if type(v.down)~='number' then
108 if v.end_stretch then defjfm_res= nil; return end
109 if v.end_shrink then defjfm_res= nil; return end
111 if type(v.end_adjust)~='table' then
113 elseif #(v.end_adjust)==0 then
116 table.sort(v.end_adjust)
121 if v.end_stretch and v.end_stretch~=0.0 then
122 v.end_adjust = (v.end_adjust or {})
123 v.end_adjust[#(v.end_adjust)+1] = v.end_stretch
125 if v.end_shrink and v.end_ahrink~=0.0 then
126 v.end_adjust = (v.end_adjust or {})
127 v.end_adjust[#(v.end_adjust)+1] = -v.end_shrink
129 if v.end_adjust then v.end_adjust[#(v.end_adjust)+1] = 0.0 end
131 v.kern = v.kern or {}; v.glue = v.glue or {}
132 for j,x in pairs(v.glue) do
133 if v.kern[j] then defjfm_res= nil; return end
134 x.ratio, x[5] = (x.ratio or (x[5] and 0.5*(1+x[5]) or 0.5)), nil
137 xp, x[4] = (x.priority or x[4]), nil
138 if type(xp)=='table' and t.version>=2 then
139 if type(xp[1])~='number' or xp[1]<-4 or xp[1]>3 then defjfm_res=nil end -- stretch
140 if type(xp[2])~='number' or xp[2]<-4 or xp[2]>3 then defjfm_res=nil end -- shrink
141 xp = (xp[1]+4)*8+(xp[2]+4)
142 elseif xp and type(xp)~='number' then
146 if xp<0 or xp>=64 then defjfm_res=nil end
150 x.kanjiskip_natural = norm_val(x.kanjiskip_natural)
151 x.kanjiskip_stretch = norm_val(x.kanjiskip_stretch)
152 x.kanjiskip_shrink = norm_val(x.kanjiskip_shrink)
154 for j,x in pairs(v.kern) do
155 if type(x)=='number' then
157 elseif type(x)=='table' then
158 v.kern[j] = { x[1], (x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5)) }
166 -- In version 3, 'jcharbdd' is divided into
167 -- 'alchar': ALchar (or math boundary)
168 -- 'nox_alchar': ALchar (or math boundary), where xkanjiskip won't inserted
169 -- 'glue': glue/kern, 'jcharbdd': other cases (和文B, rule, ...)
170 t.chars.alchar = t.chars.jcharbdd
171 t.chars.nox_alchar = t.chars.jcharbdd
172 t.chars.glue = t.chars.jcharbdd
174 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
179 local update_jfm_cache
181 local function mult_table(old,scale) -- modified from table.fastcopy
184 for k,v in next, old do
185 if type(v) == "table" then
186 new[k] = mult_table(v,scale)
187 elseif type(v) == "number" then
188 new[k] = round(v*scale)
196 update_jfm_cache = function (j,sz)
197 if metrics[j].size_cache[sz] then return end
199 metrics[j].size_cache[sz] = t
200 t.chars = metrics[j].chars
201 t.char_type = mult_table(metrics[j].char_type, sz)
202 for i,v in pairs(t.char_type) do
203 v.align = (v.align=='left') and 0 or
204 ((v.align=='right') and 1 or 0.5)
205 if type(i) == 'number' then -- char_type
206 for k,w in pairs(v.glue) do
210 priority=FROM_JFM + w.priority/sz,
211 width = w[1], stretch = w[2], shrink = w[3],
212 kanjiskip_natural = w.kanjiskip_natural and w.kanjiskip_natural/sz,
213 kanjiskip_stretch = w.kanjiskip_stretch and w.kanjiskip_stretch/sz,
214 kanjiskip_shrink = w.kanjiskip_shrink and w.kanjiskip_shrink/sz,
217 for k,w in pairs(v.kern) do
218 local g = node_new(id_kern, 1)
219 setfield(g, 'kern', w[1])
220 set_attr(g, attr_icflag, FROM_JFM)
221 v[k] = {g, ratio=w[2]/sz}
224 v.glue, v.kern = nil, nil
226 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
227 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
228 t.zw = round(metrics[j].zw*sz)
229 t.zh = round(metrics[j].zh*sz)
234 luatexbase.create_callback("luatexja.find_char_class", "data",
235 function (arg, fmtable, char)
239 local start_time_measure = ltjb.start_time_measure
240 local stop_time_measure = ltjb.stop_time_measure
241 local fcc_temp = { chars_cbcache = {} }
243 fcc_temp.chars_cbcache,
245 __index = function () return 0 end,
247 function find_char_class(c,m)
248 -- c: character code, m:
249 local r = (m or fcc_temp).chars_cbcache[c]
252 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
253 m.chars_cbcache[c or 0] = r
260 ------------------------------------------------------------------------
261 -- LOADING JAPANESE FONTS
262 ------------------------------------------------------------------------
266 local global_flag -- true if \globaljfont, false if \jfont
267 local function load_jfont_metric()
268 if jfm_file_name=='' then
269 ltjb.package_error('luatexja',
271 'To load and define a Japanese font, a JFM must be specified.'..
272 "The JFM 'ujis' will be used for now.")
275 for j,v in ipairs(metrics) do
276 if v.name==jfm_file_name then return j end
278 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
280 defjfm_res.name = jfm_file_name
281 table.insert(metrics, defjfm_res)
289 local utf8 = unicode.utf8
290 function jfontdefX(g, dir, csname)
291 jfm_dir, is_def_jfont = dir, true
292 cstemp = csname:sub( (utf8.byte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
293 cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
294 global_flag = g and '\\global' or ''
295 tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
296 (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
299 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
302 local identifiers = fonts.hashes.identifiers
303 local provides_feature = luaotfload.aux.provides_feature
305 local j = load_jfont_metric(jfm_dir)
306 local fn = font.id(cstemp)
307 local f = font_getfont(fn)
309 ltjb.package_error('luatexja',
310 "bad JFM `" .. jfm_file_name .. "'",
311 'The JFM file you specified is not valid JFM file.\n'..
312 'So defining Japanese font is cancelled.')
313 tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
314 (cstemp==' ') and '\\space' or cstemp,
315 '\\endcsname=\\relax')
318 if not f then return end
319 update_jfm_cache(j, f.size)
320 local ad = identifiers[fn].parameters
321 local sz = metrics[j].size_cache[f.size]
322 local fmtable = { jfm = j, size = f.size, var = jfm_var,
323 with_kanjiskip = jfm_ksp,
324 zw = sz.zw, zh = sz.zh,
325 ascent = ad.ascender,
326 descent = ad.descender,
327 chars = sz.chars, char_type = sz.char_type,
328 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
330 vert_activated = is_vert_enabled,
332 if auto_enable_vrt2 then
333 local t = identifiers[fn]
334 local lang, scr = t.properties.language, t.properties.script
335 local vrt2_exist = provides_feature(
336 fn, t.properties.script, t.properties.language, 'vrt2'
338 t.shared.features[vrt2_exist and 'vrt2' or 'vert'] = true
341 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
342 font_metric_table[fn]=fmtable
343 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
344 (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
345 (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
350 local get_dir_count = ltjd.get_dir_count
351 local dir_tate = luatexja.dir_table.dir_tate
352 local tex_get_attr = tex.getattribute
355 local a = font_metric_table[
356 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
357 return a and a.zw or 0
360 local a = font_metric_table[
361 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
362 return a and a.zw or 0
367 -- extract jfm_file_name and jfm_var
368 -- normalize position of 'jfm=' and 'jfmvar=' keys
369 local function extract_metric(name)
370 local is_braced = name:match('^{(.*)}$')
371 name= is_braced or name
372 jfm_file_name = ''; jfm_var = ''; jfm_ksp = true
373 local tmp, index = name:sub(1, 5), 1
374 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
377 local p = name:find(":", index); index = p and (p+1) or index
379 local l = name:len()+1
380 local q = name:find(";", index+1) or l
381 if name:sub(index, index+3)=='jfm=' and q>index+4 then
382 jfm_file_name = name:sub(index+4, q-1)
384 name = name:sub(1,index-1) .. name:sub(q+1)
386 name = name:sub(1,index-1)
389 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
390 jfm_var = name:sub(index+7, q-1)
392 name = name:sub(1,index-1) .. name:sub(q+1)
394 name = name:sub(1,index-1)
398 index = (l~=q) and (q+1) or nil
401 if jfm_file_name~='' then
402 local l = name:sub(-1)
404 .. ((l==':' or l==';') and '' or ';')
405 .. 'jfm=' .. jfm_file_name
407 name = name .. 'jfmvar=' .. jfm_var
410 for x in string.gmatch (name, "[:;]([+%%-]?)ltjks") do
411 jfm_ksp = not (x=='-')
413 if jfm_dir == 'tate' then
414 is_vert_enabled = (not name:match('[:;]%-vert')) and (not name:match('[:;]%-vrt2'))
415 auto_enable_vrt2 = (not name:match('vert') and not name:match('vrt2'))
417 is_vert_enabled, auto_enable_vrt2 = nil, nil
419 return is_braced and ('{' .. name .. '}') or name
422 -- define_font callback
424 local ltjr_font_callback = ltjr.font_callback
425 function luatexja.font_callback(name, size, id)
426 local new_name = is_def_jfont and extract_metric(name) or name
428 local res = ltjr_font_callback(new_name, size, id, otfl_fdr)
429 luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
430 -- this callback processes variation selector, so we execute it always
433 luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
434 otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
435 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
438 ------------------------------------------------------------------------
440 ------------------------------------------------------------------------
442 -- these function are called from ltj-latex.sty
443 local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
444 function add_fenc_list(enc) fenc_list[enc] = 'true ' end
445 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
446 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
447 function is_kyenc(enc)
448 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
450 function is_ktenc(enc)
451 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
453 function is_kenc(enc)
454 tex.sprint(cat_lp, '\\let\\ifin@\\if'
455 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
458 local kfam_list, Nkfam_list = {}, {}
459 function add_kfam(fam)
462 function search_kfam(fam, use_fd)
463 if kfam_list[fam] then
464 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
465 elseif Nkfam_list[fam] then
466 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
468 for i,_ in pairs(kyenc_list) do
469 if kpse.find_file(string.lower(i)..fam..'.fd') then
470 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
473 for i,_ in pairs(ktenc_list) do
474 if kpse.find_file(string.lower(i)..fam..'.fd') then
475 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
478 Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
480 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
483 local ffam_list, Nffam_list = {}, {}
484 function is_ffam(fam)
485 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[fam] or 'false '))
487 function add_ffam(fam)
488 ffam_list[fam]='true '
490 function search_ffam_declared()
492 for i,_ in pairs(fenc_list) do
493 s = s .. '\\cdp@elt{' .. i .. '}'
495 tex.sprint(cat_lp, s)
497 function search_ffam_fd(fam)
498 if Nffam_list[fam] then
499 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
501 for i,_ in pairs(fenc_list) do
502 if kpse.find_file(string.lower(i)..fam..'.fd') then
503 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
506 Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
511 ------------------------------------------------------------------------
513 ------------------------------------------------------------------------
515 local alt_font_table = alt_font_table
516 local attr_curaltfnt = {}
517 local ucs_out = 0x110000
519 ------ for TeX interface
521 function set_alt_font(b,e,ind,bfnt)
522 -- ind: 新フォント, bfnt: 基底フォント
523 if b>e then b, e = e, b end
525 ltjb.package_error('luatexja',
526 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
527 'I take the intersection with [0x80, 0x10ffff].')
528 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
529 elseif e<0 then -- b<e<0
531 elseif b<0x80 or e>=ucs_out then
532 ltjb.package_warning('luatexja',
533 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
534 'I take the intersection with [0x80, 0x10ffff].')
535 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
537 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
538 local t = alt_font_table[bfnt]
539 local ac = font_getfont(ind).characters
540 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
541 if e>=0 then -- character range
543 if ac[i]then t[i]=ind end
547 local tx = font_metric_table[bfnt].chars
548 for i,v in pairs(tx) do
549 if b<=v and v<=e and ac[i] then t[i]=ind end
555 function clear_alt_font(bfnt)
556 if alt_font_table[bfnt] then
557 local t = alt_font_table[bfnt]
558 for i,_ in pairs(t) do t[i]=nil; end
562 ------ used in ltjp.suppress_hyphenate_ja callback
563 function replace_altfont(pf, pc)
564 local a = alt_font_table[pf]
565 return a and a[pc] or pf
568 ------ for LaTeX interface
570 local alt_font_table_latex = {}
573 function clear_alt_font_latex(bbase)
574 local t = alt_font_table_latex[bbase]
576 for j,v in pairs(t) do t[j] = nil end
581 function set_alt_font_latex(b,e,ind,bbase)
582 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
583 if b>e then b, e = e, b end
585 ltjb.package_error('luatexja',
586 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
587 'I take the intersection with [0x80, 0x10ffff].')
588 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
589 elseif e<0 then -- b<e<0
591 elseif b<0x80 or e>=ucs_out then
592 ltjb.package_warning('luatexja',
593 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
594 'I take the intersection with [0x80, 0x10ffff].')
595 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
598 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
599 local t = alt_font_table_latex[bbase]
600 if not t[ind] then t[ind] = {} end
602 for j,v in pairs(t) do
603 if v[i] then -- remove old entry
604 if j~=ind then v[i]=nil end; break
609 -- remove the empty tables
610 for j,v in pairs(t) do
611 local flag_clear = true
612 for k,_ in pairs(v) do flag_clear = false; break end
613 if flag_clear then t[j]=nil end
615 if ind==bbase then t[bbase] = nil end
618 -- ここから先は 新 \selectfont の内部でしか実行されない
620 local alt_font_base, alt_font_base_num
623 function does_alt_set(bbase)
624 aftl_base = alt_font_table_latex[bbase]
625 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
628 function print_aftl_address()
629 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
633 function output_alt_font_cmd(dir, bbase)
634 alt_font_base = bbase
636 alt_font_base_num = tex.getattribute(attr_curtfnt)
638 alt_font_base_num = tex.getattribute(attr_curjfnt)
640 local t = alt_font_table[alt_font_base_num]
642 for i,_ in pairs(t) do t[i]=nil end
644 t = alt_font_table_latex[bbase]
646 for i,_ in pairs(t) do
647 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
653 function pickup_alt_font_a(size_str)
654 local t = alt_font_table_latex[alt_font_base]
656 for i,v in pairs(t) do
657 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
658 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
663 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
664 local t = alt_font_table[alt_font_base_num]
665 local tx = font_metric_table[alt_font_base_num].chars
666 for i,v in pairs(tx) do
667 if v==class and afnt_chars[i] then t[i]=afnt_num end
672 function pickup_alt_font_b(afnt_num, afnt_base)
673 local t = alt_font_table[alt_font_base_num]
674 local ac = font_getfont(afnt_num).characters
675 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
676 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
677 if i == afnt_base then
678 for j,_ in pairs(v) do
680 if ac[j] then t[j]=afnt_num end
681 else -- -n (n>=1) means that the character class n,
682 -- which is defined in the JFM
683 pickup_alt_font_class(-j, afnt_num, ac)
692 ------------------------------------------------------------------------
694 ------------------------------------------------------------------------
696 function cleanup_size_cache()
697 --local gs, ke = 0, 0
698 for _,n in pairs(metrics) do
699 for i,t in pairs(n.size_cache) do
700 for _,v in pairs(t.char_type) do
701 for k,w in pairs(v) do
702 if type(k)=='number' then
703 --if w[1] then gs = gs + 1 else ke = ke + 1 end
704 if w[1] then node_free(w[1]) end
714 ------------------------------------------------------------------------
715 -- VERT VARIANT TABLE
716 ------------------------------------------------------------------------
717 local vert_form_table = {
718 [0x2013]=0xFE32, [0x2014]=0xFE31, [0x2025]=0xFE30,
719 [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
720 [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
721 [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
722 [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
723 [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, [0xFF3F]=0xFE33,
726 ------------------------------------------------------------------------
728 ------------------------------------------------------------------------
730 local font_extra_info = font_extra_info -- key: fontnumber
731 local font_extra_basename = {} -- key: basename
733 -- IVS and vertical metrics
734 local prepare_fl_data
735 local supply_vkern_table
737 local fields = fontloader.fields
738 local function glyph_vmetric(glyph)
739 local flds = fields(glyph)
740 local vw, tsb, vk = nil, nil, nil
741 for _,i in ipairs(flds) do
742 if i=='vwidth' then vw = glyph.vwidth end
743 if i=='tsidebearing' then tsb = glyph.tsidebearing end
744 if i=='vkerns' then vk = glyph.vkerns end
749 local sort = table.sort
750 local function add_fl_table(dest, glyphs, unitable, asc_des, units, id)
751 local glyphmin, glyphmax = glyphs.glyphmin, glyphs.glyphmax
752 if glyphmax < 0 then return dest end
753 local tg = glyphs.glyphs
754 for i = glyphmin, glyphmax do
758 for _,at in pairs(gv.altuni) do
759 local bu, vsel = at.unicode, at.variant
761 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
762 local uniq_flag = true
763 if dest and dest[bu] then
764 for i,_ in pairs(dest[bu]) do
765 if i==vsel then uniq_flag = false; break end
769 dest = dest or {}; dest[bu] = dest[bu] or {}
770 dest[bu][vsel] = unitable[gv.name]
776 local gi = unitable[gv.name]
778 if unitable[gv.name .. '.vert'] then
779 dest = dest or {}; dest[gi] = dest[gi] or {};
780 dest[gi].vform = unitable[gv.name .. '.vert']
781 elseif id.characters[gi] and vert_form_table[gi] then
782 dest = dest or {}; dest[gi] = dest[gi] or {};
783 dest[gi].vform = vert_form_table[gi]
787 local vw, tsb, vk = glyph_vmetric(gv)
788 local gi = unitable[gv.name]
789 if gi and vw and vw~=asc_des then
790 -- We do not use tsidebearing, since (1) fontloader does not read VORG table
791 -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
792 -- Hence, we assume that vertical origin == ascender
793 -- (see capsule_glyph_tate in ltj-setwidth.lua)
794 dest = dest or {}; dest[gi] = dest[gi] or {}
795 dest[gi].vwidth = vw/units
800 local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
801 for _,v in pairs(vk) do
802 if unitable[v.char] then
804 if type(vl)=='table' then
805 for _,vlt in pairs(vl) do
806 dest_vk[vlt] = dest_vk[vlt] or {}
807 dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
808 dest_vk[vlt][gi][unitable[v.char]] = v.off
811 dest_vk[vl] = dest_vk[vl] or {}
812 dest_vk[vl][gi] = dest_vk[vl][gi] or {}
813 dest_vk[vl][gi][unitable[v.char]] = v.off
822 prepare_fl_data = function (dest, id)
823 local t = fontloader.info(id.filename)
826 fl = fontloader.open(id.filename)
828 fl = fontloader.open(id.filename, id.fullname)
830 local ind_to_uni, unicodes = {}, {}
831 for i,v in pairs(id.characters) do
832 ind_to_uni[v.index] = i
835 local tg, glyphmin, glyphmax = fl.glyphs, fl.glyphmin, fl.glyphmax
836 if 0 <= glyphmax then
837 for i = glyphmin, glyphmax do
838 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
841 dest = add_fl_table(dest, fl, unicodes,
842 fl.ascent + fl.descent, fl.units_per_em, id)
845 for _,v in pairs(fl.subfonts) do
846 local tg, glyphmin, glyphmax = v.glyphs, v.glyphmin, v.glyphmax
847 if 0 <= glyphmax then
848 for i = glyphmin, glyphmax do
849 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
853 for _,v in pairs(fl.subfonts) do
854 dest = add_fl_table(dest, v, unicodes,
855 fl.ascent + fl.descent, fl.units_per_em, id)
858 if dest then dest.unicodes = unicodes end
859 fontloader.close(fl); collectgarbage("collect")
862 -- supply vkern table
863 supply_vkern_table = function(id, bname)
864 local bx = font_extra_basename[bname].vkerns
865 local lookuphash = id.resources.lookuphash
866 local desc = id.shared.rawdata.descriptions
867 if bx and lookuphash then
868 for i,v in pairs(bx) do
869 lookuphash[i] = lookuphash[i] or v
870 for j,w in pairs(v) do
871 desc[j].kerns = desc[j].kerns or {}
882 local checksum = file.checksum
884 local function prepare_extra_data_base(id)
885 if (not id) or (not id.filename) then return end
886 local bname = id.psname or file.nameonly(id.filename)
887 if not font_extra_basename[bname] then
888 -- if the cache is present, read it
889 local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
890 local v = "extra_" .. string.lower(bname)
891 local dat = ltjb.load_cache(
893 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
895 -- if the cache is not found or outdated, save the cache
897 font_extra_basename[bname] = dat[1] or {}
900 dat = prepare_fl_data(dat, id)
901 font_extra_basename[bname] = dat or {}
904 chksum = checksum(id.filename),
912 local function prepare_extra_data_font(id, res)
913 if type(res)=='table' and res.shared and (res.psname or res.filename) then
914 font_extra_info[id] = font_extra_basename[res.psname or file.nameonly(res.filename)]
917 luatexbase.add_to_callback(
918 'luaotfload.patch_font',
920 -- these function is executed one time per one fontfile
921 local bname = prepare_extra_data_base(tfmdata)
922 if bname then supply_vkern_table(tfmdata, bname) end
925 'ltj.prepare_extra_data', 1)
926 luatexbase.add_to_callback(
927 'luatexja.define_font',
928 function (res, name, size, id)
929 prepare_extra_data_font(id, res)
931 'ltj.prepare_extra_data', 1)
933 local nulltable = {} -- dummy
934 ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
936 local identifiers = fonts.hashes.identifiers
937 for i=1,font.nextid()-1 do
938 if identifiers[i] then
939 prepare_extra_data_base(identifiers[i])
940 prepare_extra_data_font(i,identifiers[i])
946 ------------------------------------------------------------------------
947 -- calculate vadvance
948 ------------------------------------------------------------------------
950 local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
951 for char_num,v in pairs(ft.shared.rawdata.descriptions) do
953 for sn, sv in pairs(v.slookups) do
954 if subtables[sn] and type(sv)=='table' then
957 = (table_vadv[char_num] or 0) + sv[4]
959 if sv[2]~=0 and not already_vert then
961 = (table_vorg[char_num] or 0) + sv[2]
969 luatexbase.add_to_callback(
970 "luatexja.define_jfont",
971 function (fmtable, fnum)
972 local vadv = {}; fmtable.v_advance = vadv
973 local vorg = {}; fmtable.v_origin = vorg
974 local ft = font_getfont(fnum)
976 if ft.specification then
977 for feat_name,v in pairs(ft.specification.features.normal) do
978 if v==true and ft.resources then
979 for _,i in pairs(ft.resources.sequences) do
980 if i.order[1]== feat_name and i.type == 'gpos_single' and type(i.subtables)=='table' then
981 for _,st in pairs(i.subtables) do
988 acc_feature(vadv, vorg, subtables, ft,
989 ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
990 for i,v in pairs(vadv) do
991 vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
993 for i,v in pairs(vorg) do
994 vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
998 end, 1, 'ltj.v_advance'
1002 ------------------------------------------------------------------------
1003 -- supply tounicode entries
1004 ------------------------------------------------------------------------
1006 local ltjr_prepare_cid_font = ltjr.prepare_cid_font
1007 luatexbase.add_to_callback(
1008 'luaotfload.patch_font',
1010 local cidinfo = tfmdata.cidinfo or tfmdata.resources.cidinfo
1011 if cidinfo and cidinfo.registry and cidinfo.ordering then
1012 local rd = ltjr_prepare_cid_font(cidinfo.registry, cidinfo.ordering)
1014 local ru, rc = rd.resources.unicodes, rd.characters
1015 for i,v in pairs(tfmdata.characters) do
1016 local w = ru["Japan1." .. tostring(v.index)]
1018 v.tounicode = v.tounicode or rc[w]. tounicode
1026 'ltj.supply_tounicode', 1)
1030 ------------------------------------------------------------------------
1032 ------------------------------------------------------------------------
1034 local getfont = node.direct.getfont
1035 local getchar = node.direct.getchar
1036 local get_dir_count = ltjd.get_dir_count
1037 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
1038 local ensure_tex_attr = ltjb.ensure_tex_attr
1039 local node_write = node.direct.write
1042 if status.luatex_version>=89 then
1043 new_ic_kern = function(g) return node_new(id_kern,3) end
1045 local ITALIC = luatexja.icflag_table.ITALIC
1046 new_ic_kern = function()
1047 local g = node_new(id_kern, 1)
1048 set_attr(g, attr_icflag, ITALIC)
1052 -- EXT: italic correction
1053 function append_italic()
1054 local p = to_direct(tex.nest[tex.nest.ptr].tail)
1055 local TEMP = node_new(id_kern)
1056 if p and getid(p)==id_glyph then
1057 if is_ucs_in_japanese_char(p) then
1058 local j = font_metric_table[
1059 has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1061 local g = new_ic_kern()
1062 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
1063 node_write(g); ensure_tex_attr(attr_icflag, 0)
1065 local f = getfont(p)
1066 local h = font_getfont(f) or font.fonts[f]
1068 local g = new_ic_kern()
1069 if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1070 setfield(g, 'kern', h.characters[getchar(p)].italic)
1071 node_write(g); ensure_tex_attr(attr_icflag, 0)