+-- IVS and vertical metrics
+local prepare_fl_data
+local supply_vkern_table
+do
+ local fields = fontloader.fields
+ local function glyph_vmetric(glyph)
+ local flds = fields(glyph)
+ local vw, tsb, vk = nil, nil, nil
+ for _,i in ipairs(flds) do
+ if i=='vwidth' then vw = glyph.vwidth end
+ if i=='tsidebearing' then tsb = glyph.tsidebearing end
+ if i=='vkerns' then vk = glyph.vkerns end
+ end
+ return vw, tsb, vk
+ end
+
+ local sort = table.sort
+ local function add_fl_table(dest, glyphs, unitable, asc_des, units, id)
+ local glyphmin, glyphmax = glyphs.glyphmin, glyphs.glyphmax
+ if glyphmax < 0 then return dest end
+ local tg = glyphs.glyphs
+ for i = glyphmin, glyphmax do
+ local gv = tg[i]
+ if gv then
+ if gv.altuni then
+ for _,at in pairs(gv.altuni) do
+ local bu, vsel = at.unicode, at.variant
+ if vsel then
+ if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
+ local uniq_flag = true
+ if dest and dest[bu] then
+ for i,_ in pairs(dest[bu]) do
+ if i==vsel then uniq_flag = false; break end
+ end
+ end
+ if uniq_flag then
+ dest = dest or {}; dest[bu] = dest[bu] or {}
+ dest[bu][vsel] = unitable[gv.name]
+ end
+ end
+ end
+ end
+ -- vertical form
+ local gi = unitable[gv.name]
+ if gi then
+ if unitable[gv.name .. '.vert'] then
+ dest = dest or {}; dest[gi] = dest[gi] or {};
+ dest[gi].vform = unitable[gv.name .. '.vert']
+ elseif id.characters[gi] and vert_form_table[gi] then
+ dest = dest or {}; dest[gi] = dest[gi] or {};
+ dest[gi].vform = vert_form_table[gi]
+ end
+ end
+ -- vertical metric
+ local vw, tsb, vk = glyph_vmetric(gv)
+ local gi = unitable[gv.name]
+ if gi and vw and vw~=asc_des then
+ -- We do not use tsidebearing, since (1) fontloader does not read VORG table
+ -- and (2) 'tsidebearing' doea not appear in the returned table by fontloader.fields.
+ -- Hence, we assume that vertical origin == ascender
+ -- (see capsule_glyph_tate in ltj-setwidth.lua)
+ dest = dest or {}; dest[gi] = dest[gi] or {}
+ dest[gi].vwidth = vw/units
+ end
+ -- vertical kern
+ if gi and vk then
+ dest = dest or {};
+ local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
+ for _,v in pairs(vk) do
+ if unitable[v.char] then
+ local vl = v.lookup
+ if type(vl)=='table' then
+ for _,vlt in pairs(vl) do
+ dest_vk[vlt] = dest_vk[vlt] or {}
+ dest_vk[vlt][gi] = dest_vk[vlt][gi] or {}
+ dest_vk[vlt][gi][unitable[v.char]] = v.off
+ end
+ else
+ dest_vk[vl] = dest_vk[vl] or {}
+ dest_vk[vl][gi] = dest_vk[vl][gi] or {}
+ dest_vk[vl][gi][unitable[v.char]] = v.off
+ end
+ end
+ end
+ end
+ end
+ end
+ return dest
+ end
+ prepare_fl_data = function (dest, id)
+ local fl = fontloader.open(id.filename)
+ local ind_to_uni, unicodes = {}, {}
+ for i,v in pairs(id.characters) do
+ ind_to_uni[v.index] = i
+ end
+ if fl.glyphs then
+ local tg, glyphmin, glyphmax = fl.glyphs, fl.glyphmin, fl.glyphmax
+ if 0 <= glyphmax then
+ for i = glyphmin, glyphmax do
+ if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
+ end
+ end
+ dest = add_fl_table(dest, fl, unicodes,
+ fl.ascent + fl.descent, fl.units_per_em, id)
+ end
+ if fl.subfonts then
+ for _,v in pairs(fl.subfonts) do
+ local tg, glyphmin, glyphmax = v.glyphs, v.glyphmin, v.glyphmax
+ if 0 <= glyphmax then
+ for i = glyphmin, glyphmax do
+ if tg[i] and tg[i].name then unicodes[tg[i].name] = ind_to_uni[i] end
+ end
+ end
+ end
+ for _,v in pairs(fl.subfonts) do
+ dest = add_fl_table(dest, v, unicodes,
+ fl.ascent + fl.descent, fl.units_per_em, id)
+ end
+ end
+ if dest then dest.unicodes = unicodes end
+ fontloader.close(fl); collectgarbage("collect")
+ return dest
+ end
+ -- supply vkern table
+ supply_vkern_table = function(id, bname)
+ local bx = font_extra_basename[bname].vkerns
+ local lookuphash = id.resources.lookuphash
+ local desc = id.shared.rawdata.descriptions
+ if bx and lookuphash then
+ for i,v in pairs(bx) do
+ lookuphash[i] = lookuphash[i] or v
+ for j,w in pairs(v) do
+ desc[j].kerns = desc[j].kerns or {}
+ desc[j].kerns[i] = w
+ end
+ end
+ end
+ end
+end
+
+--
+do
+ local cache_ver = 11
+ local checksum = file.checksum
+
+ local function prepare_extra_data_base(id)
+ if (not id) or (not id.filename) then return end
+ local bname = file.nameonly(id.filename)
+ if not font_extra_basename[bname] then
+ -- if the cache is present, read it
+ local newsum = checksum(id.filename) -- MD5 checksum of the fontfile
+ local v = "extra_" .. string.lower(file.nameonly(id.filename))
+ local dat = ltjb.load_cache(
+ v,
+ function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
+ )
+ -- if the cache is not found or outdated, save the cache
+ if dat then
+ font_extra_basename[bname] = dat[1] or {}
+ else
+ local dat = nil
+ dat = prepare_fl_data(dat, id)
+ font_extra_basename[bname] = dat or {}
+ ltjb.save_cache( v,
+ {
+ chksum = checksum(id.filename),
+ version = cache_ver,
+ dat,
+ })
+ end
+ return bname
+ end
+ end
+ local function prepare_extra_data_font(id, res)
+ if type(res)=='table' and res.shared and res.filename then
+ font_extra_info[id] = font_extra_basename[file.nameonly(res.filename)]
+ end
+ end
+ luatexbase.add_to_callback(
+ 'luaotfload.patch_font',
+ function (tfmdata)
+ -- these function is executed one time per one fontfile
+ local bname = prepare_extra_data_base(tfmdata)
+ if bname then supply_vkern_table(tfmdata, bname) end
+ return tfmdata
+ end,
+ 'ltj.prepare_extra_data', 1)
+ luatexbase.add_to_callback(
+ 'luatexja.define_font',
+ function (res, name, size, id)
+ prepare_extra_data_font(id, res)
+ end,
+ 'ltj.prepare_extra_data', 1)
+
+ local nulltable = {} -- dummy
+ ltjr.vert_addfunc = function (n) font_extra_info[n] = nulltable end
+
+ local identifiers = fonts.hashes.identifiers
+ for i=1,font.nextid()-1 do
+ if identifiers[i] then
+ prepare_extra_data_base(identifiers[i])
+ prepare_extra_data_font(i,identifiers[i])
+ end
+ end
+end
+
+
+------------------------------------------------------------------------
+-- calculate vadvance
+------------------------------------------------------------------------
+do
+ local function acc_feature(table_vadv, table_vorg, subtables, ft, already_vert)
+ for char_num,v in pairs(ft.shared.rawdata.descriptions) do
+ if v.slookups then
+ for sn, sv in pairs(v.slookups) do
+ if subtables[sn] and type(sv)=='table' then
+ if sv[4]~=0 then
+ table_vadv[char_num]
+ = (table_vadv[char_num] or 0) + sv[4]
+ end
+ if sv[2]~=0 and not already_vert then
+ table_vorg[char_num]
+ = (table_vorg[char_num] or 0) + sv[2]
+ end
+ end
+ end
+ end
+ end
+ end
+
+luatexbase.add_to_callback(
+ "luatexja.define_jfont",
+ function (fmtable, fnum)
+ local vadv = {}; fmtable.v_advance = vadv
+ local vorg = {}; fmtable.v_origin = vorg
+ local ft = font_getfont(fnum)
+ local subtables = {}
+ if ft.specification then
+ for feat_name,v in pairs(ft.specification.features.normal) do
+ if v==true then
+ for _,i in pairs(ft.resources.sequences) do
+ if i.order[1]== feat_name and i.type == 'gpos_single' then
+ for _,st in pairs(i.subtables) do
+ subtables[st] = true
+ end
+ end
+ end
+ end
+ end
+ acc_feature(vadv, vorg, subtables, ft,
+ ft.specification.features.normal.vrt2 or ft.specification.features.normal.vert)
+ for i,v in pairs(vadv) do
+ vadv[i]=vadv[i]/ft.units_per_em*fmtable.size
+ end
+ for i,v in pairs(vorg) do
+ vorg[i]=vorg[i]/ft.units_per_em*fmtable.size
+ end
+ end
+ return fmtable
+ end, 1, 'ltj.v_advance'
+)
+end
+
+------------------------------------------------------------------------
+-- supply tounicode entries
+------------------------------------------------------------------------
+do
+ local ltjr_prepare_cid_font = ltjr.prepare_cid_font
+ luatexbase.add_to_callback(
+ 'luaotfload.patch_font',
+ function (tfmdata)
+ if tfmdata.cidinfo then
+ local rd = ltjr_prepare_cid_font(tfmdata.cidinfo.registry, tfmdata.cidinfo.ordering)
+ if rd then
+ local ru, rc = rd.resources.unicodes, rd.characters
+ for i,v in pairs(tfmdata.characters) do
+ local w = ru["Japan1." .. tostring(v.index)]
+ if w then
+ v.tounicode = v.tounicode or rc[w]. tounicode
+ end
+ end
+ end
+ end
+
+ return tfmdata
+ end,
+ 'ltj.supply_tounicode', 1)
+end
+
+
+------------------------------------------------------------------------
+-- MISC
+------------------------------------------------------------------------
+do
+ local getfont = node.direct.getfont
+ local getchar = node.direct.getchar
+ local get_dir_count = ltjd.get_dir_count
+ local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
+ local ensure_tex_attr = ltjb.ensure_tex_attr
+ local node_write = node.direct.write
+ local font = font
+ local new_ic_kern
+ if status.luatex_version>=89 then
+ new_ic_kern = function(g) return node_new(id_kern,3) end
+ else
+ local ITALIC = luatexja.icflag_table.ITALIC
+ new_ic_kern = function()
+ local g = node_new(id_kern)
+ setfield(g, 'subtype', 1); set_attr(g, attr_icflag, ITALIC)
+ return g
+ end
+ end
+ -- EXT: italic correction
+ function append_italic()
+ local p = to_direct(tex.nest[tex.nest.ptr].tail)
+ local TEMP = node_new(id_kern)
+ if p and getid(p)==id_glyph then
+ if is_ucs_in_japanese_char(p) then
+ local j = font_metric_table[
+ has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
+ ]
+ local g = new_ic_kern()
+ setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
+ node_write(g); ensure_tex_attr(attr_icflag, 0)