Add support for virtual fonts in addcharacters

This commit is contained in:
Marcel Krüger 2020-07-07 04:54:27 +02:00
parent fe4f3355f3
commit 7c392c7575
5 changed files with 62 additions and 2 deletions

View File

@ -5,6 +5,7 @@ local writer = require'luametalatex-nodewriter'
local newpdf = require'luametalatex-pdf'
local nametree = require'luametalatex-pdf-nametree'
local build_fontdir = require'luametalatex-pdf-font'
local fontmap = require'luametalatex-pdf-font-map'
local utils = require'luametalatex-pdf-utils'
local strip_floats = utils.strip_floats
@ -744,6 +745,8 @@ token.luacmd("pdfextension", function(_, imm)
error[[Unsupported dest type]]
end
node.write(whatsit)
elseif token.scan_keyword'mapline' then
fontmap.mapline(token.scan_string())
else
-- The following error message gobbles the next word as a side effect.
-- This is intentional to make error-recovery easier.

View File

@ -16,6 +16,6 @@ return {
return (i ^ j) & mask32
end,
extract = function(v, shift, count)
return ((bit32 & v) >> shift) & ((1<<count)-1)
return ((mask32 & v) >> shift) & ((1<<count)-1)
end,
}

View File

@ -5,6 +5,7 @@ local fontmap = require'luametalatex-pdf-font-map'.fontmap
local callback_find = callback.find
local old_font_define = font.define
local old_addcharacters = font.addcharacters
require'luametalatex-pdf-font-map'.mapfile(kpse.find_file('pdftex.map', 'map', true))
@ -110,3 +111,57 @@ function font.define(f)
end
return id
end
function font.addcharacters(fid, newdir)
old_addcharacters(fid, newdir) -- The easy part, the remaining stuff gets crazy
local fontdir = assert(all_fonts[fid], 'addcharacters expects an existing font')
local fonts_map
if newdir.fonts then -- FIXME: Handle default fonts table
if fontdir.fonts then
fonts_map = {}
for i,f in next, newdir.fonts do
if not f.id then
f.id = assert(callback_find'define_font'(f.name, f.size or -1000))
elseif f.id == 0 then
f.id = fid
end
for j,ff in next, fontdir.fonts do
if ff.id == f.id then
fonts_map[i] = j
goto FONT_MAPPING_SET -- A typical for ... do ... else ... end implemented using goto.
end
end
-- NOT FOUND, so add it
local new_f = #fontdir.fonts + 1
fontdir.fonts[new_f] = f
fonts_map[i] = f
::FONT_MAPPING_SET::
end
else
fontdir.fonts = newdir.fonts
end
end
for cp, glyph in next, newdir do
local existing = fontdir[cp]
if existing ~= glyph then
if existing then
texio.write_nl'Overwriting existing character. Here be dragons'
end
end
if glyph.commands then
local font_seen
for _, cmd in ipairs(glyph.commands) do
if cmd[1] == 'font' then
font_seen = true
cmd[2] = fonts_map[cmd[2]]
elseif cmd[1] == 'slot' then
font_seen = true
cmd[2] = fonts_map[cmd[2]]
elseif not font_seen and cmd[1] == 'char' then
font_seen = true
cmd[1], cmd[2], cmd[3] = 'slot', fonts_map[1], cmd[2]
end
end
end
end
end

View File

@ -23,6 +23,7 @@ do
end
end
end
os.arg0 = arg0
kpse.set_program_name(arg0, progname)
end
package.searchers[2] = function(modname)

View File

@ -394,9 +394,10 @@ local function do_commands(p, c, f, fid, x, y, outer, ...)
x = x + getwidth(n)
direct.free(n)
elseif cmd[1] == "slot" then
current_font = assert(fonts[cmd[2]], "invalid font requested")
local n = direct.new'glyph'
setsubtype(n, 256)
setfont(n, cmd[2], cmd[3])
setfont(n, current_font.id, cmd[2])
nodehandler.glyph(p, n, x, y, outer, ...)
direct.free(n)
x = x + getwidth(n)