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
17 local Dnode = node.direct or node
19 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
20 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id 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 round = tex.round
30 local font_getfont = font.getfont
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
33 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
34 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
35 local id_glyph = node.id('glyph')
36 local id_kern = node.id('kern')
37 local id_glue_spec = node.id('glue_spec')
38 local id_glue = node.id('glue')
39 local cat_lp = luatexbase.catcodetables['latex-package']
40 local FROM_JFM = luatexja.icflag_table.FROM_JFM
41 local tokenlib = luatexja.token
42 ------------------------------------------------------------------------
44 ------------------------------------------------------------------------
46 metrics={} -- this table stores all metric informations
47 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
49 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
51 local jfm_file_name, jfm_var
53 local jfm_dir, is_def_jfont, is_vert_enabled
55 local function norm_val(a)
56 if (not a) or (a==0.) then
66 function define_jfm(t)
67 local real_char -- Does current character class have the 'real' character?
68 if t.dir~=jfm_dir then
69 defjfm_res= nil; return
70 elseif type(t.zw)~='number' or type(t.zh)~='number' then
71 defjfm_res= nil; return
73 t.char_type = {}; t.chars = {}
74 for i,v in pairs(t) do
75 if type(i) == 'number' then -- char_type
77 if i ~= 0 then defjfm_res= nil; return end
81 for j,w in pairs(v.chars) do
82 if type(w) == 'number' and w~=-1 then
84 elseif type(w) == 'string' and utf.len(w)==1 then
85 real_char = true; w = utf.byte(w)
86 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
87 real_char = true; w = utf.byte(utf.sub(w,1,1))
89 if not t.chars[w] then
92 defjfm_res= nil; return
95 if type(v.align)~='string' then
96 v.align = 'left' -- left
99 if type(v.width)~='number' and v.width~='prop' then
100 defjfm_res= nil; return
102 if v.width=='prop' and jfm_dir=='tate' then
105 if type(v.height)~='number' then
108 if type(v.depth)~='number' then
111 if type(v.italic)~='number' then
114 if type(v.left)~='number' then
117 if type(v.down)~='number' then
124 v.kern = v.kern or {}; v.glue = v.glue or {}
125 for j,x in pairs(v.glue) do
126 if v.kern[j] then defjfm_res= nil; return end
127 x.ratio, x[5] = (x.ratio or (x[5] and 0.5*(1+x[5]) or 0.5)), nil
128 x.priority, x[4] = (x.priority or x[4] or 0), nil
129 x.ksp_natural = norm_val(x.ksp_natural)
130 x.ksp_stretch = norm_val(x.ksp_stretch)
131 x.ksp_shrink = norm_val(x.ksp_shrink)
133 for j,x in pairs(v.kern) do
134 if type(x)=='number' then
136 elseif type(x)=='table' then
137 v.kern[j] = { x[1], ratio=x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5) }
144 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
149 local update_jfm_cache
151 local function mult_table(old,scale) -- modified from table.fastcopy
154 for k,v in next, old do
155 if type(v) == "table" then
156 new[k] = mult_table(v,scale)
157 elseif type(v) == "number" then
158 new[k] = round(v*scale)
166 update_jfm_cache = function (j,sz)
167 if metrics[j].size_cache[sz] then return end
168 --local TEMP = node_new(id_kern)
170 metrics[j].size_cache[sz] = t
171 t.chars = metrics[j].chars
172 t.char_type = mult_table(metrics[j].char_type, sz)
173 for i,v in pairs(t.char_type) do
174 v.align = (v.align=='left') and 0 or
175 ((v.align=='right') and 1 or 0.5)
176 if type(i) == 'number' then -- char_type
177 for k,w in pairs(v.glue) do
178 local h = node_new(id_glue_spec)
182 priority=FROM_JFM + w.priority/sz,
183 ksp_natural = w.ksp_natural and w.ksp_natural/sz,
184 ksp_stretch = w.ksp_stretch and w.ksp_stretch/sz,
185 ksp_shrink = w.ksp_shrink and w.ksp_shrink/sz,
187 setfield(h, 'width', w[1])
188 setfield(h, 'stretch', w[2])
189 setfield(h, 'shrink', w[3])
190 setfield(h, 'stretch_order', 0)
191 setfield(h, 'shrink_order', 0)
193 for k,w in pairs(v.kern) do
194 local g = node_new(id_kern)
195 setfield(g, 'kern', w[1])
196 setfield(g, 'subtype', 1)
197 set_attr(g, attr_icflag, FROM_JFM)
198 v[k] = {false, g, ratio=w[2]/sz}
201 v.glue, v.kern = nil, nil
203 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
204 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
205 t.zw = round(metrics[j].zw*sz)
206 t.zh = round(metrics[j].zh*sz)
212 luatexbase.create_callback("luatexja.find_char_class", "data",
213 function (arg, fmtable, char)
217 local start_time_measure = ltjb.start_time_measure
218 local stop_time_measure = ltjb.stop_time_measure
219 local fcc_temp = { chars_cbcache = {} }
221 fcc_temp.chars_cbcache,
223 __index = function () return 0 end,
225 function find_char_class(c,m)
226 -- c: character code, m:
227 local r = (m or fcc_temp).chars_cbcache[c]
230 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
231 m.chars_cbcache[c or 0] = r
238 ------------------------------------------------------------------------
239 -- LOADING JAPANESE FONTS
240 ------------------------------------------------------------------------
244 local global_flag -- true if \globaljfont, false if \jfont
245 local function load_jfont_metric()
246 if jfm_file_name=='' then
247 ltjb.package_error('luatexja',
249 'To load and define a Japanese font, a JFM must be specified.'..
250 "The JFM 'ujis' will be used for now.")
253 for j,v in ipairs(metrics) do
254 if v.name==jfm_file_name then return j end
256 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
258 defjfm_res.name = jfm_file_name
259 table.insert(metrics, defjfm_res)
267 local utf8 = unicode.utf8
268 function jfontdefX(g, dir, csname)
269 jfm_dir, is_def_jfont = dir, true
270 cstemp = csname:sub( (utf8.byte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
271 cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
272 global_flag = g and '\\global' or ''
273 tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
274 (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
277 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
280 local identifiers = fonts.hashes.identifiers
282 local j = load_jfont_metric(jfm_dir)
283 local fn = font.id(cstemp)
284 local f = font_getfont(fn)
286 ltjb.package_error('luatexja',
287 "bad JFM `" .. jfm_file_name .. "'",
288 'The JFM file you specified is not valid JFM file.\n'..
289 'So defining Japanese font is cancelled.')
290 tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
291 (cstemp==' ') and '\\space' or cstemp,
292 '\\endcsname=\\relax')
295 update_jfm_cache(j, f.size)
296 local ad = identifiers[fn].parameters
297 local sz = metrics[j].size_cache[f.size]
298 local fmtable = { jfm = j, size = f.size, var = jfm_var,
299 zw = sz.zw, zh = sz.zh,
300 ascent = ad.ascender,
301 descent = ad.descender,
302 chars = sz.chars, char_type = sz.char_type,
303 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
305 vert_activated = is_vert_enabled,
308 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
309 font_metric_table[fn]=fmtable
310 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
311 (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
312 (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
317 local get_dir_count = ltjd.get_dir_count
318 local dir_tate = luatexja.dir_table.dir_tate
319 local tex_get_attr = tex.getattribute
322 local a = font_metric_table[
323 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
324 return a and a.zw or 0
327 local a = font_metric_table[
328 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
329 return a and a.zw or 0
334 -- extract jfm_file_name and jfm_var
335 -- normalize position of 'jfm=' and 'jfmvar=' keys
336 local function extract_metric(name)
337 jfm_file_name = ''; jfm_var = ''
338 local tmp, index = name:sub(1, 5), 1
339 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
342 local p = name:find(":", index); index = p and (p+1) or index
344 local l = name:len()+1
345 local q = name:find(";", index+1) or l
346 if name:sub(index, index+3)=='jfm=' and q>index+4 then
347 jfm_file_name = name:sub(index+4, q-1)
349 name = name:sub(1,index-1) .. name:sub(q+1)
351 name = name:sub(1,index-1)
354 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
355 jfm_var = name:sub(index+7, q-1)
357 name = name:sub(1,index-1) .. name:sub(q+1)
359 name = name:sub(1,index-1)
363 index = (l~=q) and (q+1) or nil
366 if jfm_file_name~='' then
367 local l = name:sub(-1)
369 .. ((l==':' or l==';') and '' or ';')
370 .. 'jfm=' .. jfm_file_name
372 name = name .. 'jfmvar=' .. jfm_var
375 if jfm_dir == 'tate' then
376 is_vert_enabled = (not name:match('-vert')) and (not name:match('-vrt2'))
377 if not name:match('vert') and not name:match('vrt2') then
378 name = name .. ';vert;vrt2'
381 is_vert_enabled = nil
386 -- define_font callback
387 local otfl_fdr = fonts.definers.read
388 local ltjr_font_callback = ltjr.font_callback
389 function luatexja.font_callback(name, size, id)
390 local new_name = is_def_jfont and extract_metric(name) or name
392 local res = ltjr_font_callback(new_name, size, id, otfl_fdr)
393 luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
394 -- this callback processes variation selector, so we execute it always
397 luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
398 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
401 ------------------------------------------------------------------------
403 ------------------------------------------------------------------------
405 -- these function are called from ltj-latex.sty
406 local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
407 function add_fenc_list(enc) fenc_list[enc] = 'true ' end
408 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
409 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
410 function is_kyenc(enc)
411 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
413 function is_ktenc(enc)
414 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
416 function is_kenc(enc)
417 tex.sprint(cat_lp, '\\let\\ifin@\\if'
418 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
421 local kfam_list, Nkfam_list = {}, {}
422 function add_kfam(fam)
425 function search_kfam(fam, use_fd)
426 if kfam_list[fam] then
427 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
428 elseif Nkfam_list[fam] then
429 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
431 for i,_ in pairs(kyenc_list) do
432 if kpse.find_file(string.lower(i)..fam..'.fd') then
433 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
436 for i,_ in pairs(ktenc_list) do
437 if kpse.find_file(string.lower(i)..fam..'.fd') then
438 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
441 Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
443 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
446 local ffam_list, Nffam_list = {}, {}
447 function is_ffam(fam)
448 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[fam] or 'false '))
450 function add_ffam(fam)
451 ffam_list[fam]='true '
453 function search_ffam_declared()
455 for i,_ in pairs(fenc_list) do
456 s = s .. '\\cdp@elt{' .. i .. '}'
458 tex.sprint(cat_lp, s)
460 function search_ffam_fd(fam)
461 if Nffam_list[fam] then
462 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
464 for i,_ in pairs(fenc_list) do
465 if kpse.find_file(string.lower(i)..fam..'.fd') then
466 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
469 Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
474 ------------------------------------------------------------------------
476 ------------------------------------------------------------------------
478 local alt_font_table = alt_font_table
479 local attr_curaltfnt = {}
480 local ucs_out = 0x110000
482 ------ for TeX interface
484 function set_alt_font(b,e,ind,bfnt)
485 -- ind: 新フォント, bfnt: 基底フォント
486 if b>e then b, e = e, b end
488 ltjb.package_error('luatexja',
489 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
490 'I take the intersection with [0x80, 0x10ffff].')
491 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
492 elseif e<0 then -- b<e<0
494 elseif b<0x80 or e>=ucs_out then
495 ltjb.package_warning('luatexja',
496 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
497 'I take the intersection with [0x80, 0x10ffff].')
498 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
500 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
501 local t = alt_font_table[bfnt]
502 local ac = font_getfont(ind).characters
503 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
504 if e>=0 then -- character range
506 if ac[i]then t[i]=ind end
510 local tx = font_metric_table[bfnt].chars
511 for i,v in pairs(tx) do
512 if b<=v and v<=e and ac[i] then t[i]=ind end
518 function clear_alt_font(bfnt)
519 if alt_font_table[bfnt] then
520 local t = alt_font_table[bfnt]
521 for i,_ in pairs(t) do t[i]=nil; end
525 ------ used in ltjp.suppress_hyphenate_ja callback
526 function replace_altfont(pf, pc)
527 local a = alt_font_table[pf]
528 return a and a[pc] or pf
531 ------ for LaTeX interface
533 local alt_font_table_latex = {}
536 function clear_alt_font_latex(bbase)
537 local t = alt_font_table_latex[bbase]
539 for j,v in pairs(t) do t[j] = nil end
544 function set_alt_font_latex(b,e,ind,bbase)
545 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
546 if b>e then b, e = e, b end
548 ltjb.package_error('luatexja',
549 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
550 'I take the intersection with [0x80, 0x10ffff].')
551 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
552 elseif e<0 then -- b<e<0
554 elseif b<0x80 or e>=ucs_out then
555 ltjb.package_warning('luatexja',
556 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
557 'I take the intersection with [0x80, 0x10ffff].')
558 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
561 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
562 local t = alt_font_table_latex[bbase]
563 if not t[ind] then t[ind] = {} end
565 for j,v in pairs(t) do
566 if v[i] then -- remove old entry
567 if j~=ind then v[i]=nil end; break
572 -- remove the empty tables
573 for j,v in pairs(t) do
574 local flag_clear = true
575 for k,_ in pairs(v) do flag_clear = false; break end
576 if flag_clear then t[j]=nil end
578 if ind==bbase then t[bbase] = nil end
581 -- ここから先は 新 \selectfont の内部でしか実行されない
583 local alt_font_base, alt_font_base_num
586 function does_alt_set(bbase)
587 aftl_base = alt_font_table_latex[bbase]
588 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
591 function print_aftl_address()
592 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
596 function output_alt_font_cmd(dir, bbase)
597 alt_font_base = bbase
599 alt_font_base_num = tex.getattribute(attr_curtfnt)
601 alt_font_base_num = tex.getattribute(attr_curjfnt)
603 local t = alt_font_table[alt_font_base_num]
605 for i,_ in pairs(t) do t[i]=nil end
607 t = alt_font_table_latex[bbase]
609 for i,_ in pairs(t) do
610 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
616 function pickup_alt_font_a(size_str)
617 local t = alt_font_table_latex[alt_font_base]
619 for i,v in pairs(t) do
620 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
621 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
626 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
627 local t = alt_font_table[alt_font_base_num]
628 local tx = font_metric_table[alt_font_base_num].chars
629 for i,v in pairs(tx) do
630 if v==class and afnt_chars[i] then t[i]=afnt_num end
635 function pickup_alt_font_b(afnt_num, afnt_base)
636 local t = alt_font_table[alt_font_base_num]
637 local ac = font_getfont(afnt_num).characters
638 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
639 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
640 if i == afnt_base then
641 for j,_ in pairs(v) do
643 if ac[j] then t[j]=afnt_num end
644 else -- -n (n>=1) means that the character class n,
645 -- which is defined in the JFM
646 pickup_alt_font_class(-j, afnt_num, ac)
655 ------------------------------------------------------------------------
657 ------------------------------------------------------------------------
659 function cleanup_size_cache()
660 --local gs, ke = 0, 0
661 for _,n in pairs(metrics) do
662 for i,t in pairs(n.size_cache) do
663 for _,v in pairs(t.char_type) do
664 for k,w in pairs(v) do
665 if type(k)=='number' then
666 --if w[1] then gs = gs + 1 else ke = ke + 1 end
674 --print('glue spec: ', gs, 'kern: ', ke)
678 ------------------------------------------------------------------------
680 ------------------------------------------------------------------------
682 local font_extra_info = font_extra_info -- key: fontnumber
683 local font_extra_basename = {} -- key: basename
685 -- IVS and vertical metrics
686 local prepare_fl_data
687 local supply_vkern_table
689 local fields = fontloader.fields
690 local function glyph_vmetric(glyph)
691 local flds = fields(glyph)
692 local vw, tsb, vk = nil, nil, nil
693 for _,i in ipairs(flds) do
694 if i=='vwidth' then vw = glyph.vwidth end
695 if i=='tsidebearing' then tsb = glyph.tsidebearing end
696 if i=='vkerns' then vk = glyph.vkerns end
701 local sort = table.sort
702 local function add_fl_table(dest, glyphs, unitable, asc_des, units)
703 local tg, glyphmin, glyphmax = glyphs.glyphs, 0, glyphs.glyphmax
704 for _,v in pairs(fields(glyphs)) do
705 if v=='glyphmin' then glyphmin = glyphs.glyphmin; break end
707 for i = glyphmin, glyphmax-1 do
711 for _,at in pairs(gv.altuni) do
712 local bu, vsel = at.unicode, at.variant
714 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
715 dest = dest or {}; dest[bu] = dest[bu] or {}
716 local uniq_flag = true
717 for i,_ in pairs(dest[bu]) do
718 if i==vs then uniq_flag = false; break end
721 dest[bu][vsel] = unitable[gv.name]
727 local vw, tsb, vk = glyph_vmetric(gv)
728 local gi = unitable[gv.name]
729 if gi and vw and vw~=asc_des then
730 -- We do not use tsidebearing, since (1) fontloader does not read VORG table
731 -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
732 -- Hence, we assume that vertical origin == ascender
733 -- (see capsule_glyph_tate in ltj-setwidth.lua)
734 dest = dest or {}; dest[gi] = dest[gi] or {}
735 dest[gi].vwidth = vw/units
740 local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
741 for _,v in pairs(vk) do
742 if unitable[v.char] then
744 if type(vl)=='table' then
745 for _,vlt in pairs(vl) do
746 dest_vk[vlt] = dest_vk[vlt] or {}
747 dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
748 dest_vk[vlt][gi][unitable[v.char]] = v.off
751 dest_vk[vl] = dest_vk[vl] or {}
752 dest_vk[vl][gi] = dest_vk[vl][gi] or {}
753 dest_vk[vl][gi][unitable[v.char]] = v.off
762 prepare_fl_data = function (dest, id)
763 local fl = fontloader.open(id.filename)
764 local unicodes = id.resources.unicodes
766 dest = add_fl_table(dest, fl, unicodes,
767 fl.ascent + fl.descent, fl.units_per_em)
770 for _,v in pairs(fl.subfonts) do
771 dest = add_fl_table(dest, v, unicodes,
772 fl.ascent + fl.descent, fl.units_per_em)
775 fontloader.close(fl); collectgarbage("collect")
778 -- supply vkern table
779 supply_vkern_table = function(id, bname)
780 local bx = font_extra_basename[bname].vkerns
781 local lookuphash = id.resources.lookuphash
782 local desc = id.shared.rawdata.descriptions
783 if bx and lookuphash then
784 for i,v in pairs(bx) do
785 lookuphash[i] = lookuphash[i] or v
786 for j,w in pairs(v) do
787 desc[j].kerns = desc[j].kerns or {}
798 local checksum = file.checksum
800 local function prepare_extra_data_base(id)
801 if not id then return end
802 local bname = file.nameonly(id.filename or '')
803 if not font_extra_basename[bname] then
804 -- if the cache is present, read it
805 local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
806 local v = "extra_" .. string.lower(file.nameonly(id.filename))
807 local dat = ltjb.load_cache(
809 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
811 -- if the cache is not found or outdated, save the cache
813 font_extra_basename[bname] = dat[1] or {}
816 dat = prepare_fl_data(dat, id)
817 font_extra_basename[bname] = dat or {}
820 chksum = checksum(id.filename),
828 local function prepare_extra_data_font(id, res)
829 if type(res)=='table' and res.shared then
830 font_extra_info[id] = font_extra_basename[file.nameonly(res.filename)]
833 luatexbase.add_to_callback(
834 'luaotfload.patch_font',
836 -- these function is executed one time per one fontfile
837 local bname = prepare_extra_data_base(tfmdata)
838 if bname then supply_vkern_table(tfmdata, bname) end
841 'ltj.prepare_extra_data', 1)
842 luatexbase.add_to_callback(
843 'luatexja.define_font',
844 function (res, name, size, id)
845 prepare_extra_data_font(id, res)
847 'ltj.prepare_extra_data', 1)
849 local nulltable = {} -- dummy
850 ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
852 local identifiers = fonts.hashes.identifiers
853 for i=1,font.nextid()-1 do
854 if identifiers[i] then
855 prepare_extra_data_base(identifiers[i])
856 prepare_extra_data_font(i,identifiers[i])
862 ------------------------------------------------------------------------
863 -- calculate vadvance
864 ------------------------------------------------------------------------
866 local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
867 for char_num,v in pairs(ft.shared.rawdata.descriptions) do
869 for sn, sv in pairs(v.slookups) do
870 if subtables[sn] and type(sv)=='table' then
873 = (table_vadv[char_num] or 0) + sv[4]
875 if sv[2]~=0 and not already_vert then
877 = (table_vorg[char_num] or 0) + sv[2]
885 luatexbase.add_to_callback(
886 "luatexja.define_jfont",
887 function (fmtable, fnum)
888 local vadv = {}; fmtable.v_advance = vadv
889 local vorg = {}; fmtable.v_origin = vorg
890 local ft = font_getfont(fnum)
892 if ft.specification then
893 for feat_name,v in pairs(ft.specification.features.normal) do
895 for _,i in pairs(ft.resources.sequences) do
896 if i.order[1]== feat_name and i.type == 'gpos_single' then
897 for _,st in pairs(i.subtables) do
904 acc_feature(vadv, vorg, subtables, ft,
905 ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
906 for i,v in pairs(vadv) do
907 vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
909 for i,v in pairs(vorg) do
910 vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
914 end, 1, 'ltj.v_advance'
918 ------------------------------------------------------------------------
919 -- supply tounicode entries
920 ------------------------------------------------------------------------
922 local ltjr_prepare_cid_font = ltjr.prepare_cid_font
923 luatexbase.add_to_callback(
924 'luaotfload.patch_font',
926 if tfmdata.cidinfo then
927 local rd = ltjr_prepare_cid_font(tfmdata.cidinfo.registry, tfmdata.cidinfo.ordering)
929 local ru, rc = rd.resources.unicodes, rd.characters
930 for i,v in pairs(tfmdata.characters) do
931 local w = ru["Japan1." .. tostring(v.index)]
933 v.tounicode = v.tounicode or rc[w]. tounicode
941 'ltj.supply_tounicode', 1)
945 ------------------------------------------------------------------------
947 ------------------------------------------------------------------------
949 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
950 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
951 local get_dir_count = ltjd.get_dir_count
952 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
953 local ensure_tex_attr = ltjb.ensure_tex_attr
954 local node_write = Dnode.write
956 local ITALIC = luatexja.icflag_table.ITALIC
957 -- EXT: italic correction
958 function append_italic()
959 local p = to_direct(tex.nest[tex.nest.ptr].tail)
960 local TEMP = node_new(id_kern)
961 if p and getid(p)==id_glyph then
962 if is_ucs_in_japanese_char(p) then
963 local j = font_metric_table[
964 has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
966 local g = node_new(id_kern)
967 setfield(g, 'subtype', 1); set_attr(g, attr_icflag, ITALIC)
968 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
969 node_write(g); ensure_tex_attr(attr_icflag, 0)
972 local h = font_getfont(f) or font.fonts[f]
974 local g = node_new(id_kern)
975 setfield(g, 'subtype', 1); set_attr(g, attr_icflag, ITALIC)
976 setfield(g, 'kern', h.characters[getchar(p)].italic)
977 node_write(g); ensure_tex_attr(attr_icflag, 0)
985 ------------------------------------------------------------------------
986 -- VERT VARIANT TABLE
987 ------------------------------------------------------------------------
989 [0x2013]=0xFE32, [0x2014]=0xFE31, [0x2025]=0xFE30,
990 [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
991 [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
992 [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
993 [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
994 [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, [0xFF3F]=0xFE33,
996 setmetatable(vert_form_table, {__index=function(t,k) return k end});