+-- ここから先は 新 \selectfont の内部でしか実行されない
+do
+ local alt_font_base, alt_font_base_num
+ local aftl_base
+ -- EXT
+ function does_alt_set(bbase)
+ aftl_base = alt_font_table_latex[bbase]
+ tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
+ end
+ -- EXT
+ function print_aftl_address()
+ tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
+ end
+
+-- EXT
+ function output_alt_font_cmd(dir, bbase)
+ alt_font_base = bbase
+ if dir == 't' then
+ alt_font_base_num = tex.getattribute(attr_curtfnt)
+ else
+ alt_font_base_num = tex.getattribute(attr_curjfnt)
+ end
+ local t = alt_font_table[alt_font_base_num]
+ if t then
+ for i,_ in pairs(t) do t[i]=nil end
+ end
+ t = alt_font_table_latex[bbase]
+ if t then
+ for i,_ in pairs(t) do
+ tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
+ end
+ end
+ end
+
+-- EXT
+ function pickup_alt_font_a(size_str)
+ local t = alt_font_table_latex[alt_font_base]
+ if t then
+ for i,v in pairs(t) do
+ tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
+ .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
+ end
+ end
+ end
+
+ local function pickup_alt_font_class(class, afnt_num, afnt_chars)
+ local t = alt_font_table[alt_font_base_num]
+ local tx = font_metric_table[alt_font_base_num].chars
+ for i,v in pairs(tx) do
+ if v==class and afnt_chars[i] then t[i]=afnt_num end
+ end
+ end
+
+-- EXT
+ function pickup_alt_font_b(afnt_num, afnt_base)
+ local t = alt_font_table[alt_font_base_num]
+ local ac = font_getfont(afnt_num).characters
+ if not t then t = {}; alt_font_table[alt_font_base_num] = t end
+ for i,v in pairs(alt_font_table_latex[alt_font_base]) do
+ if i == afnt_base then
+ for j,_ in pairs(v) do
+ if j>=0 then
+ if ac[j] then t[j]=afnt_num end
+ else -- -n (n>=1) means that the character class n,
+ -- which is defined in the JFM
+ pickup_alt_font_class(-j, afnt_num, ac)
+ end
+ end
+ return
+ end
+ end
+ end
+
+end
+------------------------------------------------------------------------
+-- 終了時に各種ノードを破棄
+------------------------------------------------------------------------
+do
+ function cleanup_size_cache()
+ --local gs, ke = 0, 0
+ for _,n in pairs(metrics) do
+ for i,t in pairs(n.size_cache) do
+ for _,v in pairs(t.char_type) do
+ for k,w in pairs(v) do
+ if type(k)=='number' then
+ --if w[1] then gs = gs + 1 else ke = ke + 1 end
+ node_free(w[2])
+ end
+ end
+ end
+ n.size_cache[i]=nil
+ end
+ end
+ --print('glue spec: ', gs, 'kern: ', ke)
+ end
+end
+
+------------------------------------------------------------------------
+-- 追加のフォント情報
+------------------------------------------------------------------------
+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, glyphs, unitable, asc_des, units)
+ local tg, glyphmin, glyphmax = glyphs.glyphs, glyphs.glyphmin or 0, glyphs.glyphmax
+ for i = glyphmin, 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 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 unicodes = id.resources.unicodes
+ if fl.glyphs then
+ dest = add_fl_table(dest, fl, unicodes,
+ 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, unicodes,
+ fl.ascent + fl.descent, fl.units_per_em)
+ end
+ 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 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