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
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], ratio=x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5) }
165 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
170 local update_jfm_cache
172 local function mult_table(old,scale) -- modified from table.fastcopy
175 for k,v in next, old do
176 if type(v) == "table" then
177 new[k] = mult_table(v,scale)
178 elseif type(v) == "number" then
179 new[k] = round(v*scale)
187 update_jfm_cache = function (j,sz)
188 if metrics[j].size_cache[sz] then return end
190 metrics[j].size_cache[sz] = t
191 t.chars = metrics[j].chars
192 t.char_type = mult_table(metrics[j].char_type, sz)
193 for i,v in pairs(t.char_type) do
194 v.align = (v.align=='left') and 0 or
195 ((v.align=='right') and 1 or 0.5)
196 if type(i) == 'number' then -- char_type
197 for k,w in pairs(v.glue) do
201 priority=FROM_JFM + w.priority/sz,
202 width = w[1], stretch = w[2], shrink = w[3],
203 kanjiskip_natural = w.kanjiskip_natural and w.kanjiskip_natural/sz,
204 kanjiskip_stretch = w.kanjiskip_stretch and w.kanjiskip_stretch/sz,
205 kanjiskip_shrink = w.kanjiskip_shrink and w.kanjiskip_shrink/sz,
208 for k,w in pairs(v.kern) do
209 local g = node_new(id_kern, 1)
210 setfield(g, 'kern', w[1])
211 set_attr(g, attr_icflag, FROM_JFM)
212 v[k] = {g, ratio=w[2]/sz}
215 v.glue, v.kern = nil, nil
217 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
218 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
219 t.zw = round(metrics[j].zw*sz)
220 t.zh = round(metrics[j].zh*sz)
225 luatexbase.create_callback("luatexja.find_char_class", "data",
226 function (arg, fmtable, char)
230 local start_time_measure = ltjb.start_time_measure
231 local stop_time_measure = ltjb.stop_time_measure
232 local fcc_temp = { chars_cbcache = {} }
234 fcc_temp.chars_cbcache,
236 __index = function () return 0 end,
238 function find_char_class(c,m)
239 -- c: character code, m:
240 local r = (m or fcc_temp).chars_cbcache[c]
243 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
244 m.chars_cbcache[c or 0] = r
251 ------------------------------------------------------------------------
252 -- LOADING JAPANESE FONTS
253 ------------------------------------------------------------------------
257 local global_flag -- true if \globaljfont, false if \jfont
258 local function load_jfont_metric()
259 if jfm_file_name=='' then
260 ltjb.package_error('luatexja',
262 'To load and define a Japanese font, a JFM must be specified.'..
263 "The JFM 'ujis' will be used for now.")
266 for j,v in ipairs(metrics) do
267 if v.name==jfm_file_name then return j end
269 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
271 defjfm_res.name = jfm_file_name
272 table.insert(metrics, defjfm_res)
280 local utf8 = unicode.utf8
281 function jfontdefX(g, dir, csname)
282 jfm_dir, is_def_jfont = dir, true
283 cstemp = csname:sub( (utf8.byte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
284 cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
285 global_flag = g and '\\global' or ''
286 tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
287 (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
290 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
293 local identifiers = fonts.hashes.identifiers
295 local j = load_jfont_metric(jfm_dir)
296 local fn = font.id(cstemp)
297 local f = font_getfont(fn)
299 ltjb.package_error('luatexja',
300 "bad JFM `" .. jfm_file_name .. "'",
301 'The JFM file you specified is not valid JFM file.\n'..
302 'So defining Japanese font is cancelled.')
303 tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
304 (cstemp==' ') and '\\space' or cstemp,
305 '\\endcsname=\\relax')
308 if not f then return end
309 update_jfm_cache(j, f.size)
310 local ad = identifiers[fn].parameters
311 local sz = metrics[j].size_cache[f.size]
312 local fmtable = { jfm = j, size = f.size, var = jfm_var,
313 with_kanjiskip = jfm_ksp,
314 zw = sz.zw, zh = sz.zh,
315 ascent = ad.ascender,
316 descent = ad.descender,
317 chars = sz.chars, char_type = sz.char_type,
318 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
320 vert_activated = is_vert_enabled,
323 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
324 font_metric_table[fn]=fmtable
325 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
326 (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
327 (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
332 local get_dir_count = ltjd.get_dir_count
333 local dir_tate = luatexja.dir_table.dir_tate
334 local tex_get_attr = tex.getattribute
337 local a = font_metric_table[
338 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
339 return a and a.zw or 0
342 local a = font_metric_table[
343 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
344 return a and a.zw or 0
349 -- extract jfm_file_name and jfm_var
350 -- normalize position of 'jfm=' and 'jfmvar=' keys
351 local function extract_metric(name)
352 local is_braced = name:match('^{(.*)}$')
353 name= is_braced or name
354 jfm_file_name = ''; jfm_var = ''; jfm_ksp = true
355 local tmp, index = name:sub(1, 5), 1
356 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
359 local p = name:find(":", index); index = p and (p+1) or index
361 local l = name:len()+1
362 local q = name:find(";", index+1) or l
363 if name:sub(index, index+3)=='jfm=' and q>index+4 then
364 jfm_file_name = name:sub(index+4, q-1)
366 name = name:sub(1,index-1) .. name:sub(q+1)
368 name = name:sub(1,index-1)
371 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
372 jfm_var = name:sub(index+7, q-1)
374 name = name:sub(1,index-1) .. name:sub(q+1)
376 name = name:sub(1,index-1)
380 index = (l~=q) and (q+1) or nil
383 if jfm_file_name~='' then
384 local l = name:sub(-1)
386 .. ((l==':' or l==';') and '' or ';')
387 .. 'jfm=' .. jfm_file_name
389 name = name .. 'jfmvar=' .. jfm_var
392 for x in string.gmatch (name, "[:;]([+%%-]?)ltjks") do
393 jfm_ksp = not (x=='-')
395 if jfm_dir == 'tate' then
396 is_vert_enabled = (not name:match('[:;]%-vert')) and (not name:match('[:;]%-vrt2'))
397 if not name:match('vert') and not name:match('vrt2') then
398 name = name .. ';+vert;+vrt2'
401 is_vert_enabled = nil
403 return is_braced and ('{' .. name .. '}') or name
406 -- define_font callback
408 local ltjr_font_callback = ltjr.font_callback
409 function luatexja.font_callback(name, size, id)
410 local new_name = is_def_jfont and extract_metric(name) or name
412 local res = ltjr_font_callback(new_name, size, id, otfl_fdr)
413 luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
414 -- this callback processes variation selector, so we execute it always
417 luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
418 otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
419 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
422 ------------------------------------------------------------------------
424 ------------------------------------------------------------------------
426 -- these function are called from ltj-latex.sty
427 local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
428 function add_fenc_list(enc) fenc_list[enc] = 'true ' end
429 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
430 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
431 function is_kyenc(enc)
432 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
434 function is_ktenc(enc)
435 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
437 function is_kenc(enc)
438 tex.sprint(cat_lp, '\\let\\ifin@\\if'
439 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
442 local kfam_list, Nkfam_list = {}, {}
443 function add_kfam(fam)
446 function search_kfam(fam, use_fd)
447 if kfam_list[fam] then
448 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
449 elseif Nkfam_list[fam] then
450 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
452 for i,_ in pairs(kyenc_list) do
453 if kpse.find_file(string.lower(i)..fam..'.fd') then
454 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
457 for i,_ in pairs(ktenc_list) do
458 if kpse.find_file(string.lower(i)..fam..'.fd') then
459 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
462 Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
464 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
467 local ffam_list, Nffam_list = {}, {}
468 function is_ffam(fam)
469 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[fam] or 'false '))
471 function add_ffam(fam)
472 ffam_list[fam]='true '
474 function search_ffam_declared()
476 for i,_ in pairs(fenc_list) do
477 s = s .. '\\cdp@elt{' .. i .. '}'
479 tex.sprint(cat_lp, s)
481 function search_ffam_fd(fam)
482 if Nffam_list[fam] then
483 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
485 for i,_ in pairs(fenc_list) do
486 if kpse.find_file(string.lower(i)..fam..'.fd') then
487 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
490 Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
495 ------------------------------------------------------------------------
497 ------------------------------------------------------------------------
499 local alt_font_table = alt_font_table
500 local attr_curaltfnt = {}
501 local ucs_out = 0x110000
503 ------ for TeX interface
505 function set_alt_font(b,e,ind,bfnt)
506 -- ind: 新フォント, bfnt: 基底フォント
507 if b>e then b, e = e, b end
509 ltjb.package_error('luatexja',
510 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
511 'I take the intersection with [0x80, 0x10ffff].')
512 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
513 elseif e<0 then -- b<e<0
515 elseif b<0x80 or e>=ucs_out then
516 ltjb.package_warning('luatexja',
517 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
518 'I take the intersection with [0x80, 0x10ffff].')
519 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
521 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
522 local t = alt_font_table[bfnt]
523 local ac = font_getfont(ind).characters
524 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
525 if e>=0 then -- character range
527 if ac[i]then t[i]=ind end
531 local tx = font_metric_table[bfnt].chars
532 for i,v in pairs(tx) do
533 if b<=v and v<=e and ac[i] then t[i]=ind end
539 function clear_alt_font(bfnt)
540 if alt_font_table[bfnt] then
541 local t = alt_font_table[bfnt]
542 for i,_ in pairs(t) do t[i]=nil; end
546 ------ used in ltjp.suppress_hyphenate_ja callback
547 function replace_altfont(pf, pc)
548 local a = alt_font_table[pf]
549 return a and a[pc] or pf
552 ------ for LaTeX interface
554 local alt_font_table_latex = {}
557 function clear_alt_font_latex(bbase)
558 local t = alt_font_table_latex[bbase]
560 for j,v in pairs(t) do t[j] = nil end
565 function set_alt_font_latex(b,e,ind,bbase)
566 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
567 if b>e then b, e = e, b end
569 ltjb.package_error('luatexja',
570 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
571 'I take the intersection with [0x80, 0x10ffff].')
572 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
573 elseif e<0 then -- b<e<0
575 elseif b<0x80 or e>=ucs_out then
576 ltjb.package_warning('luatexja',
577 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
578 'I take the intersection with [0x80, 0x10ffff].')
579 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
582 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
583 local t = alt_font_table_latex[bbase]
584 if not t[ind] then t[ind] = {} end
586 for j,v in pairs(t) do
587 if v[i] then -- remove old entry
588 if j~=ind then v[i]=nil end; break
593 -- remove the empty tables
594 for j,v in pairs(t) do
595 local flag_clear = true
596 for k,_ in pairs(v) do flag_clear = false; break end
597 if flag_clear then t[j]=nil end
599 if ind==bbase then t[bbase] = nil end
602 -- ここから先は 新 \selectfont の内部でしか実行されない
604 local alt_font_base, alt_font_base_num
607 function does_alt_set(bbase)
608 aftl_base = alt_font_table_latex[bbase]
609 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
612 function print_aftl_address()
613 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
617 function output_alt_font_cmd(dir, bbase)
618 alt_font_base = bbase
620 alt_font_base_num = tex.getattribute(attr_curtfnt)
622 alt_font_base_num = tex.getattribute(attr_curjfnt)
624 local t = alt_font_table[alt_font_base_num]
626 for i,_ in pairs(t) do t[i]=nil end
628 t = alt_font_table_latex[bbase]
630 for i,_ in pairs(t) do
631 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
637 function pickup_alt_font_a(size_str)
638 local t = alt_font_table_latex[alt_font_base]
640 for i,v in pairs(t) do
641 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
642 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
647 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
648 local t = alt_font_table[alt_font_base_num]
649 local tx = font_metric_table[alt_font_base_num].chars
650 for i,v in pairs(tx) do
651 if v==class and afnt_chars[i] then t[i]=afnt_num end
656 function pickup_alt_font_b(afnt_num, afnt_base)
657 local t = alt_font_table[alt_font_base_num]
658 local ac = font_getfont(afnt_num).characters
659 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
660 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
661 if i == afnt_base then
662 for j,_ in pairs(v) do
664 if ac[j] then t[j]=afnt_num end
665 else -- -n (n>=1) means that the character class n,
666 -- which is defined in the JFM
667 pickup_alt_font_class(-j, afnt_num, ac)
676 ------------------------------------------------------------------------
678 ------------------------------------------------------------------------
680 function cleanup_size_cache()
681 --local gs, ke = 0, 0
682 for _,n in pairs(metrics) do
683 for i,t in pairs(n.size_cache) do
684 for _,v in pairs(t.char_type) do
685 for k,w in pairs(v) do
686 if type(k)=='number' then
687 --if w[1] then gs = gs + 1 else ke = ke + 1 end
688 if w[1] then node_free(w[1]) end
698 ------------------------------------------------------------------------
699 -- VERT VARIANT TABLE
700 ------------------------------------------------------------------------
701 local vert_form_table = {
702 [0x2013]=0xFE32, [0x2014]=0xFE31, [0x2025]=0xFE30,
703 [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
704 [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
705 [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
706 [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
707 [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, [0xFF3F]=0xFE33,
710 ------------------------------------------------------------------------
712 ------------------------------------------------------------------------
714 local font_extra_info = font_extra_info -- key: fontnumber
715 local font_extra_basename = {} -- key: basename
717 -- IVS and vertical metrics
718 local prepare_fl_data
719 local supply_vkern_table
721 local fields = fontloader.fields
722 local function glyph_vmetric(glyph)
723 local flds = fields(glyph)
724 local vw, tsb, vk = nil, nil, nil
725 for _,i in ipairs(flds) do
726 if i=='vwidth' then vw = glyph.vwidth end
727 if i=='tsidebearing' then tsb = glyph.tsidebearing end
728 if i=='vkerns' then vk = glyph.vkerns end
733 local sort = table.sort
734 local function add_fl_table(dest, glyphs, unitable, asc_des, units, id)
735 local glyphmin, glyphmax = glyphs.glyphmin, glyphs.glyphmax
736 if glyphmax < 0 then return dest end
737 local tg = glyphs.glyphs
738 for i = glyphmin, glyphmax do
742 for _,at in pairs(gv.altuni) do
743 local bu, vsel = at.unicode, at.variant
745 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
746 local uniq_flag = true
747 if dest and dest[bu] then
748 for i,_ in pairs(dest[bu]) do
749 if i==vsel then uniq_flag = false; break end
753 dest = dest or {}; dest[bu] = dest[bu] or {}
754 dest[bu][vsel] = unitable[gv.name]
760 local gi = unitable[gv.name]
762 if unitable[gv.name .. '.vert'] then
763 dest = dest or {}; dest[gi] = dest[gi] or {};
764 dest[gi].vform = unitable[gv.name .. '.vert']
765 elseif id.characters[gi] and vert_form_table[gi] then
766 dest = dest or {}; dest[gi] = dest[gi] or {};
767 dest[gi].vform = vert_form_table[gi]
771 local vw, tsb, vk = glyph_vmetric(gv)
772 local gi = unitable[gv.name]
773 if gi and vw and vw~=asc_des then
774 -- We do not use tsidebearing, since (1) fontloader does not read VORG table
775 -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
776 -- Hence, we assume that vertical origin == ascender
777 -- (see capsule_glyph_tate in ltj-setwidth.lua)
778 dest = dest or {}; dest[gi] = dest[gi] or {}
779 dest[gi].vwidth = vw/units
784 local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
785 for _,v in pairs(vk) do
786 if unitable[v.char] then
788 if type(vl)=='table' then
789 for _,vlt in pairs(vl) do
790 dest_vk[vlt] = dest_vk[vlt] or {}
791 dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
792 dest_vk[vlt][gi][unitable[v.char]] = v.off
795 dest_vk[vl] = dest_vk[vl] or {}
796 dest_vk[vl][gi] = dest_vk[vl][gi] or {}
797 dest_vk[vl][gi][unitable[v.char]] = v.off
806 prepare_fl_data = function (dest, id)
807 local fl = fontloader.open(id.filename)
808 local ind_to_uni, unicodes = {}, {}
809 for i,v in pairs(id.characters) do
810 ind_to_uni[v.index] = i
813 local tg, glyphmin, glyphmax = fl.glyphs, fl.glyphmin, fl.glyphmax
814 if 0 <= glyphmax then
815 for i = glyphmin, glyphmax do
816 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
819 dest = add_fl_table(dest, fl, unicodes,
820 fl.ascent + fl.descent, fl.units_per_em, id)
823 for _,v in pairs(fl.subfonts) do
824 local tg, glyphmin, glyphmax = v.glyphs, v.glyphmin, v.glyphmax
825 if 0 <= glyphmax then
826 for i = glyphmin, glyphmax do
827 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
831 for _,v in pairs(fl.subfonts) do
832 dest = add_fl_table(dest, v, unicodes,
833 fl.ascent + fl.descent, fl.units_per_em, id)
836 if dest then dest.unicodes = unicodes end
837 fontloader.close(fl); collectgarbage("collect")
840 -- supply vkern table
841 supply_vkern_table = function(id, bname)
842 local bx = font_extra_basename[bname].vkerns
843 local lookuphash = id.resources.lookuphash
844 local desc = id.shared.rawdata.descriptions
845 if bx and lookuphash then
846 for i,v in pairs(bx) do
847 lookuphash[i] = lookuphash[i] or v
848 for j,w in pairs(v) do
849 desc[j].kerns = desc[j].kerns or {}
860 local checksum = file.checksum
862 local function prepare_extra_data_base(id)
863 if (not id) or (not id.filename) then return end
864 local bname = file.nameonly(id.filename)
865 if not font_extra_basename[bname] then
866 -- if the cache is present, read it
867 local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
868 local v = "extra_" .. string.lower(file.nameonly(id.filename))
869 local dat = ltjb.load_cache(
871 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
873 -- if the cache is not found or outdated, save the cache
875 font_extra_basename[bname] = dat[1] or {}
878 dat = prepare_fl_data(dat, id)
879 font_extra_basename[bname] = dat or {}
882 chksum = checksum(id.filename),
890 local function prepare_extra_data_font(id, res)
891 if type(res)=='table' and res.shared and res.filename then
892 font_extra_info[id] = font_extra_basename[file.nameonly(res.filename)]
895 luatexbase.add_to_callback(
896 'luaotfload.patch_font',
898 -- these function is executed one time per one fontfile
899 local bname = prepare_extra_data_base(tfmdata)
900 if bname then supply_vkern_table(tfmdata, bname) end
903 'ltj.prepare_extra_data', 1)
904 luatexbase.add_to_callback(
905 'luatexja.define_font',
906 function (res, name, size, id)
907 prepare_extra_data_font(id, res)
909 'ltj.prepare_extra_data', 1)
911 local nulltable = {} -- dummy
912 ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
914 local identifiers = fonts.hashes.identifiers
915 for i=1,font.nextid()-1 do
916 if identifiers[i] then
917 prepare_extra_data_base(identifiers[i])
918 prepare_extra_data_font(i,identifiers[i])
924 ------------------------------------------------------------------------
925 -- calculate vadvance
926 ------------------------------------------------------------------------
928 local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
929 for char_num,v in pairs(ft.shared.rawdata.descriptions) do
931 for sn, sv in pairs(v.slookups) do
932 if subtables[sn] and type(sv)=='table' then
935 = (table_vadv[char_num] or 0) + sv[4]
937 if sv[2]~=0 and not already_vert then
939 = (table_vorg[char_num] or 0) + sv[2]
947 luatexbase.add_to_callback(
948 "luatexja.define_jfont",
949 function (fmtable, fnum)
950 local vadv = {}; fmtable.v_advance = vadv
951 local vorg = {}; fmtable.v_origin = vorg
952 local ft = font_getfont(fnum)
954 if ft.specification then
955 for feat_name,v in pairs(ft.specification.features.normal) do
956 if v==true and ft.resources then
957 for _,i in pairs(ft.resources.sequences) do
958 if i.order[1]== feat_name and i.type == 'gpos_single' and type(i.subtables)=='table' then
959 for _,st in pairs(i.subtables) do
966 acc_feature(vadv, vorg, subtables, ft,
967 ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
968 for i,v in pairs(vadv) do
969 vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
971 for i,v in pairs(vorg) do
972 vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
976 end, 1, 'ltj.v_advance'
980 ------------------------------------------------------------------------
981 -- supply tounicode entries
982 ------------------------------------------------------------------------
984 local ltjr_prepare_cid_font = ltjr.prepare_cid_font
985 luatexbase.add_to_callback(
986 'luaotfload.patch_font',
988 if tfmdata.cidinfo then
989 local rd = ltjr_prepare_cid_font(tfmdata.cidinfo.registry, tfmdata.cidinfo.ordering)
991 local ru, rc = rd.resources.unicodes, rd.characters
992 for i,v in pairs(tfmdata.characters) do
993 local w = ru["Japan1." .. tostring(v.index)]
995 v.tounicode = v.tounicode or rc[w]. tounicode
1003 'ltj.supply_tounicode', 1)
1007 ------------------------------------------------------------------------
1009 ------------------------------------------------------------------------
1011 local getfont = node.direct.getfont
1012 local getchar = node.direct.getchar
1013 local get_dir_count = ltjd.get_dir_count
1014 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
1015 local ensure_tex_attr = ltjb.ensure_tex_attr
1016 local node_write = node.direct.write
1019 if status.luatex_version>=89 then
1020 new_ic_kern = function(g) return node_new(id_kern,3) end
1022 local ITALIC = luatexja.icflag_table.ITALIC
1023 new_ic_kern = function()
1024 local g = node_new(id_kern, 1)
1025 set_attr(g, attr_icflag, ITALIC)
1029 -- EXT: italic correction
1030 function append_italic()
1031 local p = to_direct(tex.nest[tex.nest.ptr].tail)
1032 local TEMP = node_new(id_kern)
1033 if p and getid(p)==id_glyph then
1034 if is_ucs_in_japanese_char(p) then
1035 local j = font_metric_table[
1036 has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1038 local g = new_ic_kern()
1039 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
1040 node_write(g); ensure_tex_attr(attr_icflag, 0)
1042 local f = getfont(p)
1043 local h = font_getfont(f) or font.fonts[f]
1045 local g = new_ic_kern()
1046 if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1047 setfield(g, 'kern', h.characters[getchar(p)].italic)
1048 node_write(g); ensure_tex_attr(attr_icflag, 0)