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.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
70 for j,w in pairs(v.chars) do
71 if type(w) == 'number' and w~=-1 then
72 elseif type(w) == 'string' and utf.len(w)==1 then
74 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
75 w = utf.byte(utf.sub(w,1,1))
77 if not t.chars[w] then
80 defjfm_res= nil; return
85 if type(v.align)~='string' then
86 v.align = 'left' -- left
88 if type(v.width)~='number' then
89 v.width = (jfm_dir=='tate') and 1.0
91 if type(v.height)~='number' then
92 v.height = (jfm_dir=='tate') and 0.0
94 if type(v.depth)~='number' then
95 v.depth = (jfm_dir=='tate') and 0.0
97 if type(v.italic)~='number' then
100 if type(v.left)~='number' then
103 if type(v.down)~='number' then
106 v.kern = v.kern or {}; v.glue = v.glue or {}
107 for j,x in pairs(v.glue) do
108 if v.kern[j] then defjfm_res= nil; return end
109 x.ratio, x[5] = (x.ratio or (x[5] and 0.5*(1+x[5]) or 0.5)), nil
110 x.priority, x[4] = (x.priority or x[4] or 0), nil
111 x.kanjiskip_natural = norm_val(x.kanjiskip_natural)
112 x.kanjiskip_stretch = norm_val(x.kanjiskip_stretch)
113 x.kanjiskip_shrink = norm_val(x.kanjiskip_shrink)
115 for j,x in pairs(v.kern) do
116 if type(x)=='number' then
118 elseif type(x)=='table' then
119 v.kern[j] = { x[1], ratio=x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5) }
126 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
131 local update_jfm_cache
133 local function mult_table(old,scale) -- modified from table.fastcopy
136 for k,v in next, old do
137 if type(v) == "table" then
138 new[k] = mult_table(v,scale)
139 elseif type(v) == "number" then
140 new[k] = round(v*scale)
148 update_jfm_cache = function (j,sz)
149 if metrics[j].size_cache[sz] then return end
150 --local TEMP = node_new(id_kern)
152 metrics[j].size_cache[sz] = t
153 t.chars = metrics[j].chars
154 t.char_type = mult_table(metrics[j].char_type, sz)
155 for i,v in pairs(t.char_type) do
156 v.align = (v.align=='left') and 0 or
157 ((v.align=='right') and 1 or 0.5)
158 if type(i) == 'number' then -- char_type
159 for k,w in pairs(v.glue) do
163 priority=FROM_JFM + w.priority/sz,
164 width = w[1], stretch = w[2], shrink = w[3],
165 kanjiskip_natural = w.kanjiskip_natural and w.kanjiskip_natural/sz,
166 kanjiskip_stretch = w.kanjiskip_stretch and w.kanjiskip_stretch/sz,
167 kanjiskip_shrink = w.kanjiskip_shrink and w.kanjiskip_shrink/sz,
170 for k,w in pairs(v.kern) do
171 local g = node_new(id_kern)
172 setfield(g, 'kern', w[1])
173 setfield(g, 'subtype', 1)
174 set_attr(g, attr_icflag, FROM_JFM)
175 v[k] = {g, ratio=w[2]/sz}
178 v.glue, v.kern = nil, nil
180 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
181 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
182 t.zw = round(metrics[j].zw*sz)
183 t.zh = round(metrics[j].zh*sz)
189 luatexbase.create_callback("luatexja.find_char_class", "data",
190 function (arg, fmtable, char)
194 local start_time_measure = ltjb.start_time_measure
195 local stop_time_measure = ltjb.stop_time_measure
196 local fcc_temp = { chars_cbcache = {} }
198 fcc_temp.chars_cbcache,
200 __index = function () return 0 end,
202 function find_char_class(c,m)
203 -- c: character code, m:
204 local r = (m or fcc_temp).chars_cbcache[c]
207 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
208 m.chars_cbcache[c or 0] = r
215 ------------------------------------------------------------------------
216 -- LOADING JAPANESE FONTS
217 ------------------------------------------------------------------------
221 local global_flag -- true if \globaljfont, false if \jfont
222 local function load_jfont_metric()
223 if jfm_file_name=='' then
224 ltjb.package_error('luatexja',
226 'To load and define a Japanese font, a JFM must be specified.'..
227 "The JFM 'ujis' will be used for now.")
230 for j,v in ipairs(metrics) do
231 if v.name==jfm_file_name then return j end
233 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
235 defjfm_res.name = jfm_file_name
236 table.insert(metrics, defjfm_res)
244 local utf8 = unicode.utf8
245 function jfontdefX(g, dir, csname)
246 jfm_dir, is_def_jfont = dir, true
247 cstemp = csname:sub( (utf8.byte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
248 cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
249 global_flag = g and '\\global' or ''
250 tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
251 (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
254 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
257 local identifiers = fonts.hashes.identifiers
259 local j = load_jfont_metric(jfm_dir)
260 local fn = font.id(cstemp)
261 local f = font_getfont(fn)
263 ltjb.package_error('luatexja',
264 "bad JFM `" .. jfm_file_name .. "'",
265 'The JFM file you specified is not valid JFM file.\n'..
266 'So defining Japanese font is cancelled.')
267 tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
268 (cstemp==' ') and '\\space' or cstemp,
269 '\\endcsname=\\relax')
272 if not f then return end
273 update_jfm_cache(j, f.size)
274 local ad = identifiers[fn].parameters
275 local sz = metrics[j].size_cache[f.size]
276 local fmtable = { jfm = j, size = f.size, var = jfm_var,
277 with_kanjiskip = jfm_ksp,
278 zw = sz.zw, zh = sz.zh,
279 ascent = ad.ascender,
280 descent = ad.descender,
281 chars = sz.chars, char_type = sz.char_type,
282 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
284 vert_activated = is_vert_enabled,
287 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
288 font_metric_table[fn]=fmtable
289 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
290 (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
291 (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
296 local get_dir_count = ltjd.get_dir_count
297 local dir_tate = luatexja.dir_table.dir_tate
298 local tex_get_attr = tex.getattribute
301 local a = font_metric_table[
302 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
303 return a and a.zw or 0
306 local a = font_metric_table[
307 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
308 return a and a.zw or 0
313 -- extract jfm_file_name and jfm_var
314 -- normalize position of 'jfm=' and 'jfmvar=' keys
315 local function extract_metric(name)
316 local is_braced = name:match('^{(.*)}$')
317 name= is_braced or name
318 jfm_file_name = ''; jfm_var = ''; jfm_ksp = true
319 local tmp, index = name:sub(1, 5), 1
320 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
323 local p = name:find(":", index); index = p and (p+1) or index
325 local l = name:len()+1
326 local q = name:find(";", index+1) or l
327 if name:sub(index, index+3)=='jfm=' and q>index+4 then
328 jfm_file_name = name:sub(index+4, q-1)
330 name = name:sub(1,index-1) .. name:sub(q+1)
332 name = name:sub(1,index-1)
335 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
336 jfm_var = name:sub(index+7, q-1)
338 name = name:sub(1,index-1) .. name:sub(q+1)
340 name = name:sub(1,index-1)
344 index = (l~=q) and (q+1) or nil
347 if jfm_file_name~='' then
348 local l = name:sub(-1)
350 .. ((l==':' or l==';') and '' or ';')
351 .. 'jfm=' .. jfm_file_name
353 name = name .. 'jfmvar=' .. jfm_var
356 for x in string.gmatch (name, "[:;]([+%%-]?)ltjks") do
357 jfm_ksp = not (x=='-')
359 if jfm_dir == 'tate' then
360 is_vert_enabled = (not name:match('[:;]%-vert')) and (not name:match('[:;]%-vrt2'))
361 if not name:match('vert') and not name:match('vrt2') then
362 name = name .. ';+vert;+vrt2'
365 is_vert_enabled = nil
367 return is_braced and ('{' .. name .. '}') or name
370 -- define_font callback
372 local ltjr_font_callback = ltjr.font_callback
373 function luatexja.font_callback(name, size, id)
374 local new_name = is_def_jfont and extract_metric(name) or name
376 local res = ltjr_font_callback(new_name, size, id, otfl_fdr)
377 luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
378 -- this callback processes variation selector, so we execute it always
381 luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
382 otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
383 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
386 ------------------------------------------------------------------------
388 ------------------------------------------------------------------------
390 -- these function are called from ltj-latex.sty
391 local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
392 function add_fenc_list(enc) fenc_list[enc] = 'true ' end
393 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
394 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
395 function is_kyenc(enc)
396 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
398 function is_ktenc(enc)
399 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
401 function is_kenc(enc)
402 tex.sprint(cat_lp, '\\let\\ifin@\\if'
403 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
406 local kfam_list, Nkfam_list = {}, {}
407 function add_kfam(fam)
410 function search_kfam(fam, use_fd)
411 if kfam_list[fam] then
412 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
413 elseif Nkfam_list[fam] then
414 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
416 for i,_ in pairs(kyenc_list) do
417 if kpse.find_file(string.lower(i)..fam..'.fd') then
418 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
421 for i,_ in pairs(ktenc_list) do
422 if kpse.find_file(string.lower(i)..fam..'.fd') then
423 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
426 Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
428 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
431 local ffam_list, Nffam_list = {}, {}
432 function is_ffam(fam)
433 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[fam] or 'false '))
435 function add_ffam(fam)
436 ffam_list[fam]='true '
438 function search_ffam_declared()
440 for i,_ in pairs(fenc_list) do
441 s = s .. '\\cdp@elt{' .. i .. '}'
443 tex.sprint(cat_lp, s)
445 function search_ffam_fd(fam)
446 if Nffam_list[fam] then
447 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
449 for i,_ in pairs(fenc_list) do
450 if kpse.find_file(string.lower(i)..fam..'.fd') then
451 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
454 Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
459 ------------------------------------------------------------------------
461 ------------------------------------------------------------------------
463 local alt_font_table = alt_font_table
464 local attr_curaltfnt = {}
465 local ucs_out = 0x110000
467 ------ for TeX interface
469 function set_alt_font(b,e,ind,bfnt)
470 -- ind: 新フォント, bfnt: 基底フォント
471 if b>e then b, e = e, b end
473 ltjb.package_error('luatexja',
474 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
475 'I take the intersection with [0x80, 0x10ffff].')
476 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
477 elseif e<0 then -- b<e<0
479 elseif b<0x80 or e>=ucs_out then
480 ltjb.package_warning('luatexja',
481 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
482 'I take the intersection with [0x80, 0x10ffff].')
483 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
485 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
486 local t = alt_font_table[bfnt]
487 local ac = font_getfont(ind).characters
488 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
489 if e>=0 then -- character range
491 if ac[i]then t[i]=ind end
495 local tx = font_metric_table[bfnt].chars
496 for i,v in pairs(tx) do
497 if b<=v and v<=e and ac[i] then t[i]=ind end
503 function clear_alt_font(bfnt)
504 if alt_font_table[bfnt] then
505 local t = alt_font_table[bfnt]
506 for i,_ in pairs(t) do t[i]=nil; end
510 ------ used in ltjp.suppress_hyphenate_ja callback
511 function replace_altfont(pf, pc)
512 local a = alt_font_table[pf]
513 return a and a[pc] or pf
516 ------ for LaTeX interface
518 local alt_font_table_latex = {}
521 function clear_alt_font_latex(bbase)
522 local t = alt_font_table_latex[bbase]
524 for j,v in pairs(t) do t[j] = nil end
529 function set_alt_font_latex(b,e,ind,bbase)
530 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
531 if b>e then b, e = e, b end
533 ltjb.package_error('luatexja',
534 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
535 'I take the intersection with [0x80, 0x10ffff].')
536 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
537 elseif e<0 then -- b<e<0
539 elseif b<0x80 or e>=ucs_out then
540 ltjb.package_warning('luatexja',
541 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
542 'I take the intersection with [0x80, 0x10ffff].')
543 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
546 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
547 local t = alt_font_table_latex[bbase]
548 if not t[ind] then t[ind] = {} end
550 for j,v in pairs(t) do
551 if v[i] then -- remove old entry
552 if j~=ind then v[i]=nil end; break
557 -- remove the empty tables
558 for j,v in pairs(t) do
559 local flag_clear = true
560 for k,_ in pairs(v) do flag_clear = false; break end
561 if flag_clear then t[j]=nil end
563 if ind==bbase then t[bbase] = nil end
566 -- ここから先は 新 \selectfont の内部でしか実行されない
568 local alt_font_base, alt_font_base_num
571 function does_alt_set(bbase)
572 aftl_base = alt_font_table_latex[bbase]
573 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
576 function print_aftl_address()
577 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
581 function output_alt_font_cmd(dir, bbase)
582 alt_font_base = bbase
584 alt_font_base_num = tex.getattribute(attr_curtfnt)
586 alt_font_base_num = tex.getattribute(attr_curjfnt)
588 local t = alt_font_table[alt_font_base_num]
590 for i,_ in pairs(t) do t[i]=nil end
592 t = alt_font_table_latex[bbase]
594 for i,_ in pairs(t) do
595 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
601 function pickup_alt_font_a(size_str)
602 local t = alt_font_table_latex[alt_font_base]
604 for i,v in pairs(t) do
605 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
606 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
611 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
612 local t = alt_font_table[alt_font_base_num]
613 local tx = font_metric_table[alt_font_base_num].chars
614 for i,v in pairs(tx) do
615 if v==class and afnt_chars[i] then t[i]=afnt_num end
620 function pickup_alt_font_b(afnt_num, afnt_base)
621 local t = alt_font_table[alt_font_base_num]
622 local ac = font_getfont(afnt_num).characters
623 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
624 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
625 if i == afnt_base then
626 for j,_ in pairs(v) do
628 if ac[j] then t[j]=afnt_num end
629 else -- -n (n>=1) means that the character class n,
630 -- which is defined in the JFM
631 pickup_alt_font_class(-j, afnt_num, ac)
640 ------------------------------------------------------------------------
642 ------------------------------------------------------------------------
644 function cleanup_size_cache()
645 --local gs, ke = 0, 0
646 for _,n in pairs(metrics) do
647 for i,t in pairs(n.size_cache) do
648 for _,v in pairs(t.char_type) do
649 for k,w in pairs(v) do
650 if type(k)=='number' then
651 --if w[1] then gs = gs + 1 else ke = ke + 1 end
652 if w[1] then node_free(w[1]) end
662 ------------------------------------------------------------------------
663 -- VERT VARIANT TABLE
664 ------------------------------------------------------------------------
665 local vert_form_table = {
666 [0x2013]=0xFE32, [0x2014]=0xFE31, [0x2025]=0xFE30,
667 [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
668 [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
669 [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
670 [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
671 [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, [0xFF3F]=0xFE33,
674 ------------------------------------------------------------------------
676 ------------------------------------------------------------------------
678 local font_extra_info = font_extra_info -- key: fontnumber
679 local font_extra_basename = {} -- key: basename
681 -- IVS and vertical metrics
682 local prepare_fl_data
683 local supply_vkern_table
685 local fields = fontloader.fields
686 local function glyph_vmetric(glyph)
687 local flds = fields(glyph)
688 local vw, tsb, vk = nil, nil, nil
689 for _,i in ipairs(flds) do
690 if i=='vwidth' then vw = glyph.vwidth end
691 if i=='tsidebearing' then tsb = glyph.tsidebearing end
692 if i=='vkerns' then vk = glyph.vkerns end
697 local sort = table.sort
698 local function add_fl_table(dest, glyphs, unitable, asc_des, units, id)
699 local glyphmin, glyphmax = glyphs.glyphmin, glyphs.glyphmax
700 if glyphmax < 0 then return dest end
701 local tg = glyphs.glyphs
702 for i = glyphmin, glyphmax do
706 for _,at in pairs(gv.altuni) do
707 local bu, vsel = at.unicode, at.variant
709 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
710 local uniq_flag = true
711 if dest and dest[bu] then
712 for i,_ in pairs(dest[bu]) do
713 if i==vsel then uniq_flag = false; break end
717 dest = dest or {}; dest[bu] = dest[bu] or {}
718 dest[bu][vsel] = unitable[gv.name]
724 local gi = unitable[gv.name]
726 if unitable[gv.name .. '.vert'] then
727 dest = dest or {}; dest[gi] = dest[gi] or {};
728 dest[gi].vform = unitable[gv.name .. '.vert']
729 elseif id.characters[gi] and vert_form_table[gi] then
730 dest = dest or {}; dest[gi] = dest[gi] or {};
731 dest[gi].vform = vert_form_table[gi]
735 local vw, tsb, vk = glyph_vmetric(gv)
736 local gi = unitable[gv.name]
737 if gi and vw and vw~=asc_des then
738 -- We do not use tsidebearing, since (1) fontloader does not read VORG table
739 -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
740 -- Hence, we assume that vertical origin == ascender
741 -- (see capsule_glyph_tate in ltj-setwidth.lua)
742 dest = dest or {}; dest[gi] = dest[gi] or {}
743 dest[gi].vwidth = vw/units
748 local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
749 for _,v in pairs(vk) do
750 if unitable[v.char] then
752 if type(vl)=='table' then
753 for _,vlt in pairs(vl) do
754 dest_vk[vlt] = dest_vk[vlt] or {}
755 dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
756 dest_vk[vlt][gi][unitable[v.char]] = v.off
759 dest_vk[vl] = dest_vk[vl] or {}
760 dest_vk[vl][gi] = dest_vk[vl][gi] or {}
761 dest_vk[vl][gi][unitable[v.char]] = v.off
770 prepare_fl_data = function (dest, id)
771 local fl = fontloader.open(id.filename)
772 local ind_to_uni, unicodes = {}, {}
773 for i,v in pairs(id.characters) do
774 ind_to_uni[v.index] = i
777 local tg, glyphmin, glyphmax = fl.glyphs, fl.glyphmin, fl.glyphmax
778 if 0 <= glyphmax then
779 for i = glyphmin, glyphmax do
780 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
783 dest = add_fl_table(dest, fl, unicodes,
784 fl.ascent + fl.descent, fl.units_per_em, id)
787 for _,v in pairs(fl.subfonts) do
788 local tg, glyphmin, glyphmax = v.glyphs, v.glyphmin, v.glyphmax
789 if 0 <= glyphmax then
790 for i = glyphmin, glyphmax do
791 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
795 for _,v in pairs(fl.subfonts) do
796 dest = add_fl_table(dest, v, unicodes,
797 fl.ascent + fl.descent, fl.units_per_em, id)
800 if dest then dest.unicodes = unicodes end
801 fontloader.close(fl); collectgarbage("collect")
804 -- supply vkern table
805 supply_vkern_table = function(id, bname)
806 local bx = font_extra_basename[bname].vkerns
807 local lookuphash = id.resources.lookuphash
808 local desc = id.shared.rawdata.descriptions
809 if bx and lookuphash then
810 for i,v in pairs(bx) do
811 lookuphash[i] = lookuphash[i] or v
812 for j,w in pairs(v) do
813 desc[j].kerns = desc[j].kerns or {}
824 local checksum = file.checksum
826 local function prepare_extra_data_base(id)
827 if (not id) or (not id.filename) then return end
828 local bname = file.nameonly(id.filename)
829 if not font_extra_basename[bname] then
830 -- if the cache is present, read it
831 local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
832 local v = "extra_" .. string.lower(file.nameonly(id.filename))
833 local dat = ltjb.load_cache(
835 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
837 -- if the cache is not found or outdated, save the cache
839 font_extra_basename[bname] = dat[1] or {}
842 dat = prepare_fl_data(dat, id)
843 font_extra_basename[bname] = dat or {}
846 chksum = checksum(id.filename),
854 local function prepare_extra_data_font(id, res)
855 if type(res)=='table' and res.shared and res.filename then
856 font_extra_info[id] = font_extra_basename[file.nameonly(res.filename)]
859 luatexbase.add_to_callback(
860 'luaotfload.patch_font',
862 -- these function is executed one time per one fontfile
863 local bname = prepare_extra_data_base(tfmdata)
864 if bname then supply_vkern_table(tfmdata, bname) end
867 'ltj.prepare_extra_data', 1)
868 luatexbase.add_to_callback(
869 'luatexja.define_font',
870 function (res, name, size, id)
871 prepare_extra_data_font(id, res)
873 'ltj.prepare_extra_data', 1)
875 local nulltable = {} -- dummy
876 ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
878 local identifiers = fonts.hashes.identifiers
879 for i=1,font.nextid()-1 do
880 if identifiers[i] then
881 prepare_extra_data_base(identifiers[i])
882 prepare_extra_data_font(i,identifiers[i])
888 ------------------------------------------------------------------------
889 -- calculate vadvance
890 ------------------------------------------------------------------------
892 local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
893 for char_num,v in pairs(ft.shared.rawdata.descriptions) do
895 for sn, sv in pairs(v.slookups) do
896 if subtables[sn] and type(sv)=='table' then
899 = (table_vadv[char_num] or 0) + sv[4]
901 if sv[2]~=0 and not already_vert then
903 = (table_vorg[char_num] or 0) + sv[2]
911 luatexbase.add_to_callback(
912 "luatexja.define_jfont",
913 function (fmtable, fnum)
914 local vadv = {}; fmtable.v_advance = vadv
915 local vorg = {}; fmtable.v_origin = vorg
916 local ft = font_getfont(fnum)
918 if ft.specification then
919 for feat_name,v in pairs(ft.specification.features.normal) do
921 for _,i in pairs(ft.resources.sequences) do
922 if i.order[1]== feat_name and i.type == 'gpos_single' then
923 for _,st in pairs(i.subtables) do
930 acc_feature(vadv, vorg, subtables, ft,
931 ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
932 for i,v in pairs(vadv) do
933 vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
935 for i,v in pairs(vorg) do
936 vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
940 end, 1, 'ltj.v_advance'
944 ------------------------------------------------------------------------
945 -- supply tounicode entries
946 ------------------------------------------------------------------------
948 local ltjr_prepare_cid_font = ltjr.prepare_cid_font
949 luatexbase.add_to_callback(
950 'luaotfload.patch_font',
952 if tfmdata.cidinfo then
953 local rd = ltjr_prepare_cid_font(tfmdata.cidinfo.registry, tfmdata.cidinfo.ordering)
955 local ru, rc = rd.resources.unicodes, rd.characters
956 for i,v in pairs(tfmdata.characters) do
957 local w = ru["Japan1." .. tostring(v.index)]
959 v.tounicode = v.tounicode or rc[w]. tounicode
967 'ltj.supply_tounicode', 1)
971 ------------------------------------------------------------------------
973 ------------------------------------------------------------------------
975 local getfont = node.direct.getfont
976 local getchar = node.direct.getchar
977 local get_dir_count = ltjd.get_dir_count
978 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
979 local ensure_tex_attr = ltjb.ensure_tex_attr
980 local node_write = node.direct.write
983 if status.luatex_version>=89 then
984 new_ic_kern = function(g) return node_new(id_kern,3) end
986 local ITALIC = luatexja.icflag_table.ITALIC
987 new_ic_kern = function()
988 local g = node_new(id_kern)
989 setfield(g, 'subtype', 1); set_attr(g, attr_icflag, ITALIC)
993 -- EXT: italic correction
994 function append_italic()
995 local p = to_direct(tex.nest[tex.nest.ptr].tail)
996 local TEMP = node_new(id_kern)
997 if p and getid(p)==id_glyph then
998 if is_ucs_in_japanese_char(p) then
999 local j = font_metric_table[
1000 has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1002 local g = new_ic_kern()
1003 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
1004 node_write(g); ensure_tex_attr(attr_icflag, 0)
1006 local f = getfont(p)
1007 local h = font_getfont(f) or font.fonts[f]
1009 local g = new_ic_kern()
1010 if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1011 setfield(g, 'kern', h.characters[getchar(p)].italic)
1012 node_write(g); ensure_tex_attr(attr_icflag, 0)