+------------------------------------------------------------------------
+-- 追加のフォント情報
+------------------------------------------------------------------------
+font_extra_info = {}
+local font_extra_info = font_extra_info -- key: fontnumber
+local font_extra_basename = {} -- key: basename
+
+-- 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, tg, unitable, glyphmax, asc_des, units)
+ for i = 0, glyphmax-1 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
+ dest = dest or {}; dest[bu] = dest[bu] or {}
+ local uniq_flag = true
+ for i,_ in pairs(dest[bu]) do
+ if i==vs then uniq_flag = false; break end
+ end
+ if uniq_flag then
+ dest[bu][vsel] = unitable[gv.name]
+ end
+ end
+ end
+ end
+ -- vertical metric
+ local vw, tsb, vk = glyph_vmetric(gv)
+ local gi = unitable[gv.name]
+ if 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 vk then
+ dest = dest or {};
+ local dest_vk = dest.vkerns or {}; dest.vkerns = dest_vk
+ for _,v in pairs(vk) do
+ dest_vk[v.lookup] = dest_vk[v.lookup] or {}
+ dest_vk[v.lookup][gi] = dest_vk[v.lookup][gi] or {}
+ dest_vk[v.lookup][gi][unitable[v.char]] = v.off
+ end
+ end
+ end
+ end
+ return dest
+ end
+ prepare_fl_data = function (dest, id)
+ local fl = fontloader.open(id.filename)
+ local unicodes = id.resources.unicodes
+ if fl.glyphs then
+ dest = add_fl_table(dest, fl.glyphs, id.resources.unicodes, fl.glyphmax,
+ fl.ascent + fl.descent, fl.units_per_em)
+ end
+ if fl.subfonts then
+ for _,v in pairs(fl.subfonts) do
+ dest = add_fl_table(dest, v.glyphs, id.resources.unicodes, v.glyphmax,
+ fl.ascent + fl.descent, fl.units_per_em)
+ end
+ end
+ fontloader.close(fl)
+ 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 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