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
90 v.width = (jfm_dir=='tate') and 1.0
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
114 table.sort(v.end_adjust)
119 if v.end_stretch and v.end_stretch~=0.0 then
120 v.end_adjust = (v.end_adjust or {})
121 v.end_adjust[#(v.end_adjust)+1] = v.end_stretch
123 if v.end_shrink and v.end_ahrink~=0.0 then
124 v.end_adjust = (v.end_adjust or {})
125 v.end_adjust[#(v.end_adjust)+1] = -v.end_shrink
127 if v.end_adjust then v.end_adjust[#(v.end_adjust)+1] = 0.0 end
129 v.kern = v.kern or {}; v.glue = v.glue or {}
130 for j,x in pairs(v.glue) do
131 if v.kern[j] then defjfm_res= nil; return end
132 x.ratio, x[5] = (x.ratio or (x[5] and 0.5*(1+x[5]) or 0.5)), nil
133 x.priority, x[4] = (x.priority or x[4] or 0), nil
134 x.kanjiskip_natural = norm_val(x.kanjiskip_natural)
135 x.kanjiskip_stretch = norm_val(x.kanjiskip_stretch)
136 x.kanjiskip_shrink = norm_val(x.kanjiskip_shrink)
138 for j,x in pairs(v.kern) do
139 if type(x)=='number' then
141 elseif type(x)=='table' then
142 v.kern[j] = { x[1], ratio=x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5) }
149 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
154 local update_jfm_cache
156 local function mult_table(old,scale) -- modified from table.fastcopy
159 for k,v in next, old do
160 if type(v) == "table" then
161 new[k] = mult_table(v,scale)
162 elseif type(v) == "number" then
163 new[k] = round(v*scale)
171 update_jfm_cache = function (j,sz)
172 if metrics[j].size_cache[sz] then return end
173 --local TEMP = node_new(id_kern)
175 metrics[j].size_cache[sz] = t
176 t.chars = metrics[j].chars
177 t.char_type = mult_table(metrics[j].char_type, sz)
178 for i,v in pairs(t.char_type) do
179 v.align = (v.align=='left') and 0 or
180 ((v.align=='right') and 1 or 0.5)
181 if type(i) == 'number' then -- char_type
182 for k,w in pairs(v.glue) do
186 priority=FROM_JFM + w.priority/sz,
187 width = w[1], stretch = w[2], shrink = w[3],
188 kanjiskip_natural = w.kanjiskip_natural and w.kanjiskip_natural/sz,
189 kanjiskip_stretch = w.kanjiskip_stretch and w.kanjiskip_stretch/sz,
190 kanjiskip_shrink = w.kanjiskip_shrink and w.kanjiskip_shrink/sz,
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] = {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 if not f then return end
296 update_jfm_cache(j, f.size)
297 local ad = identifiers[fn].parameters
298 local sz = metrics[j].size_cache[f.size]
299 local fmtable = { jfm = j, size = f.size, var = jfm_var,
300 with_kanjiskip = jfm_ksp,
301 zw = sz.zw, zh = sz.zh,
302 ascent = ad.ascender,
303 descent = ad.descender,
304 chars = sz.chars, char_type = sz.char_type,
305 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
307 vert_activated = is_vert_enabled,
310 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
311 font_metric_table[fn]=fmtable
312 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
313 (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
314 (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
319 local get_dir_count = ltjd.get_dir_count
320 local dir_tate = luatexja.dir_table.dir_tate
321 local tex_get_attr = tex.getattribute
324 local a = font_metric_table[
325 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
326 return a and a.zw or 0
329 local a = font_metric_table[
330 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
331 return a and a.zw or 0
336 -- extract jfm_file_name and jfm_var
337 -- normalize position of 'jfm=' and 'jfmvar=' keys
338 local function extract_metric(name)
339 local is_braced = name:match('^{(.*)}$')
340 name= is_braced or name
341 jfm_file_name = ''; jfm_var = ''; jfm_ksp = true
342 local tmp, index = name:sub(1, 5), 1
343 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
346 local p = name:find(":", index); index = p and (p+1) or index
348 local l = name:len()+1
349 local q = name:find(";", index+1) or l
350 if name:sub(index, index+3)=='jfm=' and q>index+4 then
351 jfm_file_name = name:sub(index+4, q-1)
353 name = name:sub(1,index-1) .. name:sub(q+1)
355 name = name:sub(1,index-1)
358 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
359 jfm_var = name:sub(index+7, q-1)
361 name = name:sub(1,index-1) .. name:sub(q+1)
363 name = name:sub(1,index-1)
367 index = (l~=q) and (q+1) or nil
370 if jfm_file_name~='' then
371 local l = name:sub(-1)
373 .. ((l==':' or l==';') and '' or ';')
374 .. 'jfm=' .. jfm_file_name
376 name = name .. 'jfmvar=' .. jfm_var
379 for x in string.gmatch (name, "[:;]([+%%-]?)ltjks") do
380 jfm_ksp = not (x=='-')
382 if jfm_dir == 'tate' then
383 is_vert_enabled = (not name:match('[:;]%-vert')) and (not name:match('[:;]%-vrt2'))
384 if not name:match('vert') and not name:match('vrt2') then
385 name = name .. ';+vert;+vrt2'
388 is_vert_enabled = nil
390 return is_braced and ('{' .. name .. '}') or name
393 -- define_font callback
395 local ltjr_font_callback = ltjr.font_callback
396 function luatexja.font_callback(name, size, id)
397 local new_name = is_def_jfont and extract_metric(name) or name
399 local res = ltjr_font_callback(new_name, size, id, otfl_fdr)
400 luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
401 -- this callback processes variation selector, so we execute it always
404 luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
405 otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
406 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
409 ------------------------------------------------------------------------
411 ------------------------------------------------------------------------
413 -- these function are called from ltj-latex.sty
414 local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
415 function add_fenc_list(enc) fenc_list[enc] = 'true ' end
416 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
417 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
418 function is_kyenc(enc)
419 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
421 function is_ktenc(enc)
422 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
424 function is_kenc(enc)
425 tex.sprint(cat_lp, '\\let\\ifin@\\if'
426 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
429 local kfam_list, Nkfam_list = {}, {}
430 function add_kfam(fam)
433 function search_kfam(fam, use_fd)
434 if kfam_list[fam] then
435 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
436 elseif Nkfam_list[fam] then
437 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
439 for i,_ in pairs(kyenc_list) do
440 if kpse.find_file(string.lower(i)..fam..'.fd') then
441 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
444 for i,_ in pairs(ktenc_list) do
445 if kpse.find_file(string.lower(i)..fam..'.fd') then
446 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
449 Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
451 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
454 local ffam_list, Nffam_list = {}, {}
455 function is_ffam(fam)
456 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[fam] or 'false '))
458 function add_ffam(fam)
459 ffam_list[fam]='true '
461 function search_ffam_declared()
463 for i,_ in pairs(fenc_list) do
464 s = s .. '\\cdp@elt{' .. i .. '}'
466 tex.sprint(cat_lp, s)
468 function search_ffam_fd(fam)
469 if Nffam_list[fam] then
470 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
472 for i,_ in pairs(fenc_list) do
473 if kpse.find_file(string.lower(i)..fam..'.fd') then
474 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
477 Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
482 ------------------------------------------------------------------------
484 ------------------------------------------------------------------------
486 local alt_font_table = alt_font_table
487 local attr_curaltfnt = {}
488 local ucs_out = 0x110000
490 ------ for TeX interface
492 function set_alt_font(b,e,ind,bfnt)
493 -- ind: 新フォント, bfnt: 基底フォント
494 if b>e then b, e = e, b end
496 ltjb.package_error('luatexja',
497 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
498 'I take the intersection with [0x80, 0x10ffff].')
499 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
500 elseif e<0 then -- b<e<0
502 elseif b<0x80 or e>=ucs_out then
503 ltjb.package_warning('luatexja',
504 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
505 'I take the intersection with [0x80, 0x10ffff].')
506 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
508 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
509 local t = alt_font_table[bfnt]
510 local ac = font_getfont(ind).characters
511 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
512 if e>=0 then -- character range
514 if ac[i]then t[i]=ind end
518 local tx = font_metric_table[bfnt].chars
519 for i,v in pairs(tx) do
520 if b<=v and v<=e and ac[i] then t[i]=ind end
526 function clear_alt_font(bfnt)
527 if alt_font_table[bfnt] then
528 local t = alt_font_table[bfnt]
529 for i,_ in pairs(t) do t[i]=nil; end
533 ------ used in ltjp.suppress_hyphenate_ja callback
534 function replace_altfont(pf, pc)
535 local a = alt_font_table[pf]
536 return a and a[pc] or pf
539 ------ for LaTeX interface
541 local alt_font_table_latex = {}
544 function clear_alt_font_latex(bbase)
545 local t = alt_font_table_latex[bbase]
547 for j,v in pairs(t) do t[j] = nil end
552 function set_alt_font_latex(b,e,ind,bbase)
553 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
554 if b>e then b, e = e, b end
556 ltjb.package_error('luatexja',
557 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
558 'I take the intersection with [0x80, 0x10ffff].')
559 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
560 elseif e<0 then -- b<e<0
562 elseif b<0x80 or e>=ucs_out then
563 ltjb.package_warning('luatexja',
564 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
565 'I take the intersection with [0x80, 0x10ffff].')
566 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
569 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
570 local t = alt_font_table_latex[bbase]
571 if not t[ind] then t[ind] = {} end
573 for j,v in pairs(t) do
574 if v[i] then -- remove old entry
575 if j~=ind then v[i]=nil end; break
580 -- remove the empty tables
581 for j,v in pairs(t) do
582 local flag_clear = true
583 for k,_ in pairs(v) do flag_clear = false; break end
584 if flag_clear then t[j]=nil end
586 if ind==bbase then t[bbase] = nil end
589 -- ここから先は 新 \selectfont の内部でしか実行されない
591 local alt_font_base, alt_font_base_num
594 function does_alt_set(bbase)
595 aftl_base = alt_font_table_latex[bbase]
596 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
599 function print_aftl_address()
600 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
604 function output_alt_font_cmd(dir, bbase)
605 alt_font_base = bbase
607 alt_font_base_num = tex.getattribute(attr_curtfnt)
609 alt_font_base_num = tex.getattribute(attr_curjfnt)
611 local t = alt_font_table[alt_font_base_num]
613 for i,_ in pairs(t) do t[i]=nil end
615 t = alt_font_table_latex[bbase]
617 for i,_ in pairs(t) do
618 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
624 function pickup_alt_font_a(size_str)
625 local t = alt_font_table_latex[alt_font_base]
627 for i,v in pairs(t) do
628 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
629 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
634 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
635 local t = alt_font_table[alt_font_base_num]
636 local tx = font_metric_table[alt_font_base_num].chars
637 for i,v in pairs(tx) do
638 if v==class and afnt_chars[i] then t[i]=afnt_num end
643 function pickup_alt_font_b(afnt_num, afnt_base)
644 local t = alt_font_table[alt_font_base_num]
645 local ac = font_getfont(afnt_num).characters
646 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
647 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
648 if i == afnt_base then
649 for j,_ in pairs(v) do
651 if ac[j] then t[j]=afnt_num end
652 else -- -n (n>=1) means that the character class n,
653 -- which is defined in the JFM
654 pickup_alt_font_class(-j, afnt_num, ac)
663 ------------------------------------------------------------------------
665 ------------------------------------------------------------------------
667 function cleanup_size_cache()
668 --local gs, ke = 0, 0
669 for _,n in pairs(metrics) do
670 for i,t in pairs(n.size_cache) do
671 for _,v in pairs(t.char_type) do
672 for k,w in pairs(v) do
673 if type(k)=='number' then
674 --if w[1] then gs = gs + 1 else ke = ke + 1 end
675 if w[1] then node_free(w[1]) end
685 ------------------------------------------------------------------------
686 -- VERT VARIANT TABLE
687 ------------------------------------------------------------------------
688 local vert_form_table = {
689 [0x2013]=0xFE32, [0x2014]=0xFE31, [0x2025]=0xFE30,
690 [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
691 [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
692 [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
693 [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
694 [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, [0xFF3F]=0xFE33,
697 ------------------------------------------------------------------------
699 ------------------------------------------------------------------------
701 local font_extra_info = font_extra_info -- key: fontnumber
702 local font_extra_basename = {} -- key: basename
704 -- IVS and vertical metrics
705 local prepare_fl_data
706 local supply_vkern_table
708 local fields = fontloader.fields
709 local function glyph_vmetric(glyph)
710 local flds = fields(glyph)
711 local vw, tsb, vk = nil, nil, nil
712 for _,i in ipairs(flds) do
713 if i=='vwidth' then vw = glyph.vwidth end
714 if i=='tsidebearing' then tsb = glyph.tsidebearing end
715 if i=='vkerns' then vk = glyph.vkerns end
720 local sort = table.sort
721 local function add_fl_table(dest, glyphs, unitable, asc_des, units, id)
722 local glyphmin, glyphmax = glyphs.glyphmin, glyphs.glyphmax
723 if glyphmax < 0 then return dest end
724 local tg = glyphs.glyphs
725 for i = glyphmin, glyphmax do
729 for _,at in pairs(gv.altuni) do
730 local bu, vsel = at.unicode, at.variant
732 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
733 local uniq_flag = true
734 if dest and dest[bu] then
735 for i,_ in pairs(dest[bu]) do
736 if i==vsel then uniq_flag = false; break end
740 dest = dest or {}; dest[bu] = dest[bu] or {}
741 dest[bu][vsel] = unitable[gv.name]
747 local gi = unitable[gv.name]
749 if unitable[gv.name .. '.vert'] then
750 dest = dest or {}; dest[gi] = dest[gi] or {};
751 dest[gi].vform = unitable[gv.name .. '.vert']
752 elseif id.characters[gi] and vert_form_table[gi] then
753 dest = dest or {}; dest[gi] = dest[gi] or {};
754 dest[gi].vform = vert_form_table[gi]
758 local vw, tsb, vk = glyph_vmetric(gv)
759 local gi = unitable[gv.name]
760 if gi and vw and vw~=asc_des then
761 -- We do not use tsidebearing, since (1) fontloader does not read VORG table
762 -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
763 -- Hence, we assume that vertical origin == ascender
764 -- (see capsule_glyph_tate in ltj-setwidth.lua)
765 dest = dest or {}; dest[gi] = dest[gi] or {}
766 dest[gi].vwidth = vw/units
771 local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
772 for _,v in pairs(vk) do
773 if unitable[v.char] then
775 if type(vl)=='table' then
776 for _,vlt in pairs(vl) do
777 dest_vk[vlt] = dest_vk[vlt] or {}
778 dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
779 dest_vk[vlt][gi][unitable[v.char]] = v.off
782 dest_vk[vl] = dest_vk[vl] or {}
783 dest_vk[vl][gi] = dest_vk[vl][gi] or {}
784 dest_vk[vl][gi][unitable[v.char]] = v.off
793 prepare_fl_data = function (dest, id)
794 local fl = fontloader.open(id.filename)
795 local ind_to_uni, unicodes = {}, {}
796 for i,v in pairs(id.characters) do
797 ind_to_uni[v.index] = i
800 local tg, glyphmin, glyphmax = fl.glyphs, fl.glyphmin, fl.glyphmax
801 if 0 <= glyphmax then
802 for i = glyphmin, glyphmax do
803 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
806 dest = add_fl_table(dest, fl, unicodes,
807 fl.ascent + fl.descent, fl.units_per_em, id)
810 for _,v in pairs(fl.subfonts) do
811 local tg, glyphmin, glyphmax = v.glyphs, v.glyphmin, v.glyphmax
812 if 0 <= glyphmax then
813 for i = glyphmin, glyphmax do
814 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
818 for _,v in pairs(fl.subfonts) do
819 dest = add_fl_table(dest, v, unicodes,
820 fl.ascent + fl.descent, fl.units_per_em, id)
823 if dest then dest.unicodes = unicodes end
824 fontloader.close(fl); collectgarbage("collect")
827 -- supply vkern table
828 supply_vkern_table = function(id, bname)
829 local bx = font_extra_basename[bname].vkerns
830 local lookuphash = id.resources.lookuphash
831 local desc = id.shared.rawdata.descriptions
832 if bx and lookuphash then
833 for i,v in pairs(bx) do
834 lookuphash[i] = lookuphash[i] or v
835 for j,w in pairs(v) do
836 desc[j].kerns = desc[j].kerns or {}
847 local checksum = file.checksum
849 local function prepare_extra_data_base(id)
850 if (not id) or (not id.filename) then return end
851 local bname = file.nameonly(id.filename)
852 if not font_extra_basename[bname] then
853 -- if the cache is present, read it
854 local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
855 local v = "extra_" .. string.lower(file.nameonly(id.filename))
856 local dat = ltjb.load_cache(
858 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
860 -- if the cache is not found or outdated, save the cache
862 font_extra_basename[bname] = dat[1] or {}
865 dat = prepare_fl_data(dat, id)
866 font_extra_basename[bname] = dat or {}
869 chksum = checksum(id.filename),
877 local function prepare_extra_data_font(id, res)
878 if type(res)=='table' and res.shared and res.filename then
879 font_extra_info[id] = font_extra_basename[file.nameonly(res.filename)]
882 luatexbase.add_to_callback(
883 'luaotfload.patch_font',
885 -- these function is executed one time per one fontfile
886 local bname = prepare_extra_data_base(tfmdata)
887 if bname then supply_vkern_table(tfmdata, bname) end
890 'ltj.prepare_extra_data', 1)
891 luatexbase.add_to_callback(
892 'luatexja.define_font',
893 function (res, name, size, id)
894 prepare_extra_data_font(id, res)
896 'ltj.prepare_extra_data', 1)
898 local nulltable = {} -- dummy
899 ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
901 local identifiers = fonts.hashes.identifiers
902 for i=1,font.nextid()-1 do
903 if identifiers[i] then
904 prepare_extra_data_base(identifiers[i])
905 prepare_extra_data_font(i,identifiers[i])
911 ------------------------------------------------------------------------
912 -- calculate vadvance
913 ------------------------------------------------------------------------
915 local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
916 for char_num,v in pairs(ft.shared.rawdata.descriptions) do
918 for sn, sv in pairs(v.slookups) do
919 if subtables[sn] and type(sv)=='table' then
922 = (table_vadv[char_num] or 0) + sv[4]
924 if sv[2]~=0 and not already_vert then
926 = (table_vorg[char_num] or 0) + sv[2]
934 luatexbase.add_to_callback(
935 "luatexja.define_jfont",
936 function (fmtable, fnum)
937 local vadv = {}; fmtable.v_advance = vadv
938 local vorg = {}; fmtable.v_origin = vorg
939 local ft = font_getfont(fnum)
941 if ft.specification then
942 for feat_name,v in pairs(ft.specification.features.normal) do
944 for _,i in pairs(ft.resources.sequences) do
945 if i.order[1]== feat_name and i.type == 'gpos_single' then
946 for _,st in pairs(i.subtables) do
953 acc_feature(vadv, vorg, subtables, ft,
954 ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
955 for i,v in pairs(vadv) do
956 vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
958 for i,v in pairs(vorg) do
959 vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
963 end, 1, 'ltj.v_advance'
967 ------------------------------------------------------------------------
968 -- supply tounicode entries
969 ------------------------------------------------------------------------
971 local ltjr_prepare_cid_font = ltjr.prepare_cid_font
972 luatexbase.add_to_callback(
973 'luaotfload.patch_font',
975 if tfmdata.cidinfo then
976 local rd = ltjr_prepare_cid_font(tfmdata.cidinfo.registry, tfmdata.cidinfo.ordering)
978 local ru, rc = rd.resources.unicodes, rd.characters
979 for i,v in pairs(tfmdata.characters) do
980 local w = ru["Japan1." .. tostring(v.index)]
982 v.tounicode = v.tounicode or rc[w]. tounicode
990 'ltj.supply_tounicode', 1)
994 ------------------------------------------------------------------------
996 ------------------------------------------------------------------------
998 local getfont = node.direct.getfont
999 local getchar = node.direct.getchar
1000 local get_dir_count = ltjd.get_dir_count
1001 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
1002 local ensure_tex_attr = ltjb.ensure_tex_attr
1003 local node_write = node.direct.write
1006 if status.luatex_version>=89 then
1007 new_ic_kern = function(g) return node_new(id_kern,3) end
1009 local ITALIC = luatexja.icflag_table.ITALIC
1010 new_ic_kern = function()
1011 local g = node_new(id_kern)
1012 setfield(g, 'subtype', 1); set_attr(g, attr_icflag, ITALIC)
1016 -- EXT: italic correction
1017 function append_italic()
1018 local p = to_direct(tex.nest[tex.nest.ptr].tail)
1019 local TEMP = node_new(id_kern)
1020 if p and getid(p)==id_glyph then
1021 if is_ucs_in_japanese_char(p) then
1022 local j = font_metric_table[
1023 has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1025 local g = new_ic_kern()
1026 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
1027 node_write(g); ensure_tex_attr(attr_icflag, 0)
1029 local f = getfont(p)
1030 local h = font_getfont(f) or font.fonts[f]
1032 local g = new_ic_kern()
1033 if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1034 setfield(g, 'kern', h.characters[getchar(p)].italic)
1035 node_write(g); ensure_tex_attr(attr_icflag, 0)