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
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
135 x.priority, x[4] = (x.priority or x[4] or 0), nil
136 x.kanjiskip_natural = norm_val(x.kanjiskip_natural)
137 x.kanjiskip_stretch = norm_val(x.kanjiskip_stretch)
138 x.kanjiskip_shrink = norm_val(x.kanjiskip_shrink)
140 for j,x in pairs(v.kern) do
141 if type(x)=='number' then
143 elseif type(x)=='table' then
144 v.kern[j] = { x[1], ratio=x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5) }
151 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
156 local update_jfm_cache
158 local function mult_table(old,scale) -- modified from table.fastcopy
161 for k,v in next, old do
162 if type(v) == "table" then
163 new[k] = mult_table(v,scale)
164 elseif type(v) == "number" then
165 new[k] = round(v*scale)
173 update_jfm_cache = function (j,sz)
174 if metrics[j].size_cache[sz] then return end
176 metrics[j].size_cache[sz] = t
177 t.chars = metrics[j].chars
178 t.char_type = mult_table(metrics[j].char_type, sz)
179 for i,v in pairs(t.char_type) do
180 v.align = (v.align=='left') and 0 or
181 ((v.align=='right') and 1 or 0.5)
182 if type(i) == 'number' then -- char_type
183 for k,w in pairs(v.glue) do
187 priority=FROM_JFM + w.priority/sz,
188 width = w[1], stretch = w[2], shrink = w[3],
189 kanjiskip_natural = w.kanjiskip_natural and w.kanjiskip_natural/sz,
190 kanjiskip_stretch = w.kanjiskip_stretch and w.kanjiskip_stretch/sz,
191 kanjiskip_shrink = w.kanjiskip_shrink and w.kanjiskip_shrink/sz,
194 for k,w in pairs(v.kern) do
195 local g = node_new(id_kern, 1)
196 setfield(g, 'kern', w[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)
211 luatexbase.create_callback("luatexja.find_char_class", "data",
212 function (arg, fmtable, char)
216 local start_time_measure = ltjb.start_time_measure
217 local stop_time_measure = ltjb.stop_time_measure
218 local fcc_temp = { chars_cbcache = {} }
220 fcc_temp.chars_cbcache,
222 __index = function () return 0 end,
224 function find_char_class(c,m)
225 -- c: character code, m:
226 local r = (m or fcc_temp).chars_cbcache[c]
229 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
230 m.chars_cbcache[c or 0] = r
237 ------------------------------------------------------------------------
238 -- LOADING JAPANESE FONTS
239 ------------------------------------------------------------------------
243 local global_flag -- true if \globaljfont, false if \jfont
244 local function load_jfont_metric()
245 if jfm_file_name=='' then
246 ltjb.package_error('luatexja',
248 'To load and define a Japanese font, a JFM must be specified.'..
249 "The JFM 'ujis' will be used for now.")
252 for j,v in ipairs(metrics) do
253 if v.name==jfm_file_name then return j end
255 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
257 defjfm_res.name = jfm_file_name
258 table.insert(metrics, defjfm_res)
266 local utf8 = unicode.utf8
267 function jfontdefX(g, dir, csname)
268 jfm_dir, is_def_jfont = dir, true
269 cstemp = csname:sub( (utf8.byte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
270 cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
271 global_flag = g and '\\global' or ''
272 tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
273 (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
276 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
279 local identifiers = fonts.hashes.identifiers
281 local j = load_jfont_metric(jfm_dir)
282 local fn = font.id(cstemp)
283 local f = font_getfont(fn)
285 ltjb.package_error('luatexja',
286 "bad JFM `" .. jfm_file_name .. "'",
287 'The JFM file you specified is not valid JFM file.\n'..
288 'So defining Japanese font is cancelled.')
289 tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
290 (cstemp==' ') and '\\space' or cstemp,
291 '\\endcsname=\\relax')
294 if not f then return end
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 with_kanjiskip = jfm_ksp,
300 zw = sz.zw, zh = sz.zh,
301 ascent = ad.ascender,
302 descent = ad.descender,
303 chars = sz.chars, char_type = sz.char_type,
304 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
306 vert_activated = is_vert_enabled,
309 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
310 font_metric_table[fn]=fmtable
311 tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
312 (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
313 (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
318 local get_dir_count = ltjd.get_dir_count
319 local dir_tate = luatexja.dir_table.dir_tate
320 local tex_get_attr = tex.getattribute
323 local a = font_metric_table[
324 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
325 return a and a.zw or 0
328 local a = font_metric_table[
329 tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
330 return a and a.zw or 0
335 -- extract jfm_file_name and jfm_var
336 -- normalize position of 'jfm=' and 'jfmvar=' keys
337 local function extract_metric(name)
338 local is_braced = name:match('^{(.*)}$')
339 name= is_braced or name
340 jfm_file_name = ''; jfm_var = ''; jfm_ksp = true
341 local tmp, index = name:sub(1, 5), 1
342 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
345 local p = name:find(":", index); index = p and (p+1) or index
347 local l = name:len()+1
348 local q = name:find(";", index+1) or l
349 if name:sub(index, index+3)=='jfm=' and q>index+4 then
350 jfm_file_name = name:sub(index+4, q-1)
352 name = name:sub(1,index-1) .. name:sub(q+1)
354 name = name:sub(1,index-1)
357 elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then
358 jfm_var = name:sub(index+7, q-1)
360 name = name:sub(1,index-1) .. name:sub(q+1)
362 name = name:sub(1,index-1)
366 index = (l~=q) and (q+1) or nil
369 if jfm_file_name~='' then
370 local l = name:sub(-1)
372 .. ((l==':' or l==';') and '' or ';')
373 .. 'jfm=' .. jfm_file_name
375 name = name .. 'jfmvar=' .. jfm_var
378 for x in string.gmatch (name, "[:;]([+%%-]?)ltjks") do
379 jfm_ksp = not (x=='-')
381 if jfm_dir == 'tate' then
382 is_vert_enabled = (not name:match('[:;]%-vert')) and (not name:match('[:;]%-vrt2'))
383 if not name:match('vert') and not name:match('vrt2') then
384 name = name .. ';+vert;+vrt2'
387 is_vert_enabled = nil
389 return is_braced and ('{' .. name .. '}') or name
392 -- define_font callback
394 local ltjr_font_callback = ltjr.font_callback
395 function luatexja.font_callback(name, size, id)
396 local new_name = is_def_jfont and extract_metric(name) or name
398 local res = ltjr_font_callback(new_name, size, id, otfl_fdr)
399 luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
400 -- this callback processes variation selector, so we execute it always
403 luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
404 otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
405 luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
408 ------------------------------------------------------------------------
410 ------------------------------------------------------------------------
412 -- these function are called from ltj-latex.sty
413 local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
414 function add_fenc_list(enc) fenc_list[enc] = 'true ' end
415 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
416 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
417 function is_kyenc(enc)
418 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
420 function is_ktenc(enc)
421 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[enc] or 'false '))
423 function is_kenc(enc)
424 tex.sprint(cat_lp, '\\let\\ifin@\\if'
425 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
428 local kfam_list, Nkfam_list = {}, {}
429 function add_kfam(fam)
432 function search_kfam(fam, use_fd)
433 if kfam_list[fam] then
434 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
435 elseif Nkfam_list[fam] then
436 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
438 for i,_ in pairs(kyenc_list) do
439 if kpse.find_file(string.lower(i)..fam..'.fd') then
440 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
443 for i,_ in pairs(ktenc_list) do
444 if kpse.find_file(string.lower(i)..fam..'.fd') then
445 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
448 Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
450 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
453 local ffam_list, Nffam_list = {}, {}
454 function is_ffam(fam)
455 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[fam] or 'false '))
457 function add_ffam(fam)
458 ffam_list[fam]='true '
460 function search_ffam_declared()
462 for i,_ in pairs(fenc_list) do
463 s = s .. '\\cdp@elt{' .. i .. '}'
465 tex.sprint(cat_lp, s)
467 function search_ffam_fd(fam)
468 if Nffam_list[fam] then
469 tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
471 for i,_ in pairs(fenc_list) do
472 if kpse.find_file(string.lower(i)..fam..'.fd') then
473 tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
476 Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
481 ------------------------------------------------------------------------
483 ------------------------------------------------------------------------
485 local alt_font_table = alt_font_table
486 local attr_curaltfnt = {}
487 local ucs_out = 0x110000
489 ------ for TeX interface
491 function set_alt_font(b,e,ind,bfnt)
492 -- ind: 新フォント, bfnt: 基底フォント
493 if b>e then b, e = e, b end
495 ltjb.package_error('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)
499 elseif e<0 then -- b<e<0
501 elseif b<0x80 or e>=ucs_out then
502 ltjb.package_warning('luatexja',
503 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
504 'I take the intersection with [0x80, 0x10ffff].')
505 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
507 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
508 local t = alt_font_table[bfnt]
509 local ac = font_getfont(ind).characters
510 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
511 if e>=0 then -- character range
513 if ac[i]then t[i]=ind end
517 local tx = font_metric_table[bfnt].chars
518 for i,v in pairs(tx) do
519 if b<=v and v<=e and ac[i] then t[i]=ind end
525 function clear_alt_font(bfnt)
526 if alt_font_table[bfnt] then
527 local t = alt_font_table[bfnt]
528 for i,_ in pairs(t) do t[i]=nil; end
532 ------ used in ltjp.suppress_hyphenate_ja callback
533 function replace_altfont(pf, pc)
534 local a = alt_font_table[pf]
535 return a and a[pc] or pf
538 ------ for LaTeX interface
540 local alt_font_table_latex = {}
543 function clear_alt_font_latex(bbase)
544 local t = alt_font_table_latex[bbase]
546 for j,v in pairs(t) do t[j] = nil end
551 function set_alt_font_latex(b,e,ind,bbase)
552 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
553 if b>e then b, e = e, b end
555 ltjb.package_error('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)
559 elseif e<0 then -- b<e<0
561 elseif b<0x80 or e>=ucs_out then
562 ltjb.package_warning('luatexja',
563 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
564 'I take the intersection with [0x80, 0x10ffff].')
565 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
568 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
569 local t = alt_font_table_latex[bbase]
570 if not t[ind] then t[ind] = {} end
572 for j,v in pairs(t) do
573 if v[i] then -- remove old entry
574 if j~=ind then v[i]=nil end; break
579 -- remove the empty tables
580 for j,v in pairs(t) do
581 local flag_clear = true
582 for k,_ in pairs(v) do flag_clear = false; break end
583 if flag_clear then t[j]=nil end
585 if ind==bbase then t[bbase] = nil end
588 -- ここから先は 新 \selectfont の内部でしか実行されない
590 local alt_font_base, alt_font_base_num
593 function does_alt_set(bbase)
594 aftl_base = alt_font_table_latex[bbase]
595 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
598 function print_aftl_address()
599 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
603 function output_alt_font_cmd(dir, bbase)
604 alt_font_base = bbase
606 alt_font_base_num = tex.getattribute(attr_curtfnt)
608 alt_font_base_num = tex.getattribute(attr_curjfnt)
610 local t = alt_font_table[alt_font_base_num]
612 for i,_ in pairs(t) do t[i]=nil end
614 t = alt_font_table_latex[bbase]
616 for i,_ in pairs(t) do
617 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
623 function pickup_alt_font_a(size_str)
624 local t = alt_font_table_latex[alt_font_base]
626 for i,v in pairs(t) do
627 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
628 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
633 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
634 local t = alt_font_table[alt_font_base_num]
635 local tx = font_metric_table[alt_font_base_num].chars
636 for i,v in pairs(tx) do
637 if v==class and afnt_chars[i] then t[i]=afnt_num end
642 function pickup_alt_font_b(afnt_num, afnt_base)
643 local t = alt_font_table[alt_font_base_num]
644 local ac = font_getfont(afnt_num).characters
645 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
646 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
647 if i == afnt_base then
648 for j,_ in pairs(v) do
650 if ac[j] then t[j]=afnt_num end
651 else -- -n (n>=1) means that the character class n,
652 -- which is defined in the JFM
653 pickup_alt_font_class(-j, afnt_num, ac)
662 ------------------------------------------------------------------------
664 ------------------------------------------------------------------------
666 function cleanup_size_cache()
667 --local gs, ke = 0, 0
668 for _,n in pairs(metrics) do
669 for i,t in pairs(n.size_cache) do
670 for _,v in pairs(t.char_type) do
671 for k,w in pairs(v) do
672 if type(k)=='number' then
673 --if w[1] then gs = gs + 1 else ke = ke + 1 end
674 if w[1] then node_free(w[1]) end
684 ------------------------------------------------------------------------
685 -- VERT VARIANT TABLE
686 ------------------------------------------------------------------------
687 local vert_form_table = {
688 [0x2013]=0xFE32, [0x2014]=0xFE31, [0x2025]=0xFE30,
689 [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
690 [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
691 [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
692 [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
693 [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, [0xFF3F]=0xFE33,
696 ------------------------------------------------------------------------
698 ------------------------------------------------------------------------
700 local font_extra_info = font_extra_info -- key: fontnumber
701 local font_extra_basename = {} -- key: basename
703 -- IVS and vertical metrics
704 local prepare_fl_data
705 local supply_vkern_table
707 local fields = fontloader.fields
708 local function glyph_vmetric(glyph)
709 local flds = fields(glyph)
710 local vw, tsb, vk = nil, nil, nil
711 for _,i in ipairs(flds) do
712 if i=='vwidth' then vw = glyph.vwidth end
713 if i=='tsidebearing' then tsb = glyph.tsidebearing end
714 if i=='vkerns' then vk = glyph.vkerns end
719 local sort = table.sort
720 local function add_fl_table(dest, glyphs, unitable, asc_des, units, id)
721 local glyphmin, glyphmax = glyphs.glyphmin, glyphs.glyphmax
722 if glyphmax < 0 then return dest end
723 local tg = glyphs.glyphs
724 for i = glyphmin, glyphmax do
728 for _,at in pairs(gv.altuni) do
729 local bu, vsel = at.unicode, at.variant
731 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
732 local uniq_flag = true
733 if dest and dest[bu] then
734 for i,_ in pairs(dest[bu]) do
735 if i==vsel then uniq_flag = false; break end
739 dest = dest or {}; dest[bu] = dest[bu] or {}
740 dest[bu][vsel] = unitable[gv.name]
746 local gi = unitable[gv.name]
748 if unitable[gv.name .. '.vert'] then
749 dest = dest or {}; dest[gi] = dest[gi] or {};
750 dest[gi].vform = unitable[gv.name .. '.vert']
751 elseif id.characters[gi] and vert_form_table[gi] then
752 dest = dest or {}; dest[gi] = dest[gi] or {};
753 dest[gi].vform = vert_form_table[gi]
757 local vw, tsb, vk = glyph_vmetric(gv)
758 local gi = unitable[gv.name]
759 if gi and vw and vw~=asc_des then
760 -- We do not use tsidebearing, since (1) fontloader does not read VORG table
761 -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
762 -- Hence, we assume that vertical origin == ascender
763 -- (see capsule_glyph_tate in ltj-setwidth.lua)
764 dest = dest or {}; dest[gi] = dest[gi] or {}
765 dest[gi].vwidth = vw/units
770 local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
771 for _,v in pairs(vk) do
772 if unitable[v.char] then
774 if type(vl)=='table' then
775 for _,vlt in pairs(vl) do
776 dest_vk[vlt] = dest_vk[vlt] or {}
777 dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
778 dest_vk[vlt][gi][unitable[v.char]] = v.off
781 dest_vk[vl] = dest_vk[vl] or {}
782 dest_vk[vl][gi] = dest_vk[vl][gi] or {}
783 dest_vk[vl][gi][unitable[v.char]] = v.off
792 prepare_fl_data = function (dest, id)
793 local fl = fontloader.open(id.filename)
794 local ind_to_uni, unicodes = {}, {}
795 for i,v in pairs(id.characters) do
796 ind_to_uni[v.index] = i
799 local tg, glyphmin, glyphmax = fl.glyphs, fl.glyphmin, fl.glyphmax
800 if 0 <= glyphmax then
801 for i = glyphmin, glyphmax do
802 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
805 dest = add_fl_table(dest, fl, unicodes,
806 fl.ascent + fl.descent, fl.units_per_em, id)
809 for _,v in pairs(fl.subfonts) do
810 local tg, glyphmin, glyphmax = v.glyphs, v.glyphmin, v.glyphmax
811 if 0 <= glyphmax then
812 for i = glyphmin, glyphmax do
813 if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
817 for _,v in pairs(fl.subfonts) do
818 dest = add_fl_table(dest, v, unicodes,
819 fl.ascent + fl.descent, fl.units_per_em, id)
822 if dest then dest.unicodes = unicodes end
823 fontloader.close(fl); collectgarbage("collect")
826 -- supply vkern table
827 supply_vkern_table = function(id, bname)
828 local bx = font_extra_basename[bname].vkerns
829 local lookuphash = id.resources.lookuphash
830 local desc = id.shared.rawdata.descriptions
831 if bx and lookuphash then
832 for i,v in pairs(bx) do
833 lookuphash[i] = lookuphash[i] or v
834 for j,w in pairs(v) do
835 desc[j].kerns = desc[j].kerns or {}
846 local checksum = file.checksum
848 local function prepare_extra_data_base(id)
849 if (not id) or (not id.filename) then return end
850 local bname = file.nameonly(id.filename)
851 if not font_extra_basename[bname] then
852 -- if the cache is present, read it
853 local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
854 local v = "extra_" .. string.lower(file.nameonly(id.filename))
855 local dat = ltjb.load_cache(
857 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
859 -- if the cache is not found or outdated, save the cache
861 font_extra_basename[bname] = dat[1] or {}
864 dat = prepare_fl_data(dat, id)
865 font_extra_basename[bname] = dat or {}
868 chksum = checksum(id.filename),
876 local function prepare_extra_data_font(id, res)
877 if type(res)=='table' and res.shared and res.filename then
878 font_extra_info[id] = font_extra_basename[file.nameonly(res.filename)]
881 luatexbase.add_to_callback(
882 'luaotfload.patch_font',
884 -- these function is executed one time per one fontfile
885 local bname = prepare_extra_data_base(tfmdata)
886 if bname then supply_vkern_table(tfmdata, bname) end
889 'ltj.prepare_extra_data', 1)
890 luatexbase.add_to_callback(
891 'luatexja.define_font',
892 function (res, name, size, id)
893 prepare_extra_data_font(id, res)
895 'ltj.prepare_extra_data', 1)
897 local nulltable = {} -- dummy
898 ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
900 local identifiers = fonts.hashes.identifiers
901 for i=1,font.nextid()-1 do
902 if identifiers[i] then
903 prepare_extra_data_base(identifiers[i])
904 prepare_extra_data_font(i,identifiers[i])
910 ------------------------------------------------------------------------
911 -- calculate vadvance
912 ------------------------------------------------------------------------
914 local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
915 for char_num,v in pairs(ft.shared.rawdata.descriptions) do
917 for sn, sv in pairs(v.slookups) do
918 if subtables[sn] and type(sv)=='table' then
921 = (table_vadv[char_num] or 0) + sv[4]
923 if sv[2]~=0 and not already_vert then
925 = (table_vorg[char_num] or 0) + sv[2]
933 luatexbase.add_to_callback(
934 "luatexja.define_jfont",
935 function (fmtable, fnum)
936 local vadv = {}; fmtable.v_advance = vadv
937 local vorg = {}; fmtable.v_origin = vorg
938 local ft = font_getfont(fnum)
940 if ft.specification then
941 for feat_name,v in pairs(ft.specification.features.normal) do
943 for _,i in pairs(ft.resources.sequences) do
944 if i.order[1]== feat_name and i.type == 'gpos_single' then
945 for _,st in pairs(i.subtables) do
952 acc_feature(vadv, vorg, subtables, ft,
953 ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
954 for i,v in pairs(vadv) do
955 vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
957 for i,v in pairs(vorg) do
958 vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
962 end, 1, 'ltj.v_advance'
966 ------------------------------------------------------------------------
967 -- supply tounicode entries
968 ------------------------------------------------------------------------
970 local ltjr_prepare_cid_font = ltjr.prepare_cid_font
971 luatexbase.add_to_callback(
972 'luaotfload.patch_font',
974 if tfmdata.cidinfo then
975 local rd = ltjr_prepare_cid_font(tfmdata.cidinfo.registry, tfmdata.cidinfo.ordering)
977 local ru, rc = rd.resources.unicodes, rd.characters
978 for i,v in pairs(tfmdata.characters) do
979 local w = ru["Japan1." .. tostring(v.index)]
981 v.tounicode = v.tounicode or rc[w]. tounicode
989 'ltj.supply_tounicode', 1)
993 ------------------------------------------------------------------------
995 ------------------------------------------------------------------------
997 local getfont = node.direct.getfont
998 local getchar = node.direct.getchar
999 local get_dir_count = ltjd.get_dir_count
1000 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
1001 local ensure_tex_attr = ltjb.ensure_tex_attr
1002 local node_write = node.direct.write
1005 if status.luatex_version>=89 then
1006 new_ic_kern = function(g) return node_new(id_kern,3) end
1008 local ITALIC = luatexja.icflag_table.ITALIC
1009 new_ic_kern = function()
1010 local g = node_new(id_kern, 1)
1011 set_attr(g, attr_icflag, ITALIC)
1015 -- EXT: italic correction
1016 function append_italic()
1017 local p = to_direct(tex.nest[tex.nest.ptr].tail)
1018 local TEMP = node_new(id_kern)
1019 if p and getid(p)==id_glyph then
1020 if is_ucs_in_japanese_char(p) then
1021 local j = font_metric_table[
1022 has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1024 local g = new_ic_kern()
1025 setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
1026 node_write(g); ensure_tex_attr(attr_icflag, 0)
1028 local f = getfont(p)
1029 local h = font_getfont(f) or font.fonts[f]
1031 local g = new_ic_kern()
1032 if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1033 setfield(g, 'kern', h.characters[getchar(p)].italic)
1034 node_write(g); ensure_tex_attr(attr_icflag, 0)