-
Notifications
You must be signed in to change notification settings - Fork 3
/
luametalatex-pdf-font-deduplicate.lua
48 lines (43 loc) · 1.56 KB
/
luametalatex-pdf-font-deduplicate.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require'luametalatex-font-resolve' -- Ensure that font.fonts exists
local keymap = {}
-- There are multiple criteria for sharing backend fonts:
-- * Obviously encodingbytes have to be the same.
-- * The filename better is the same too.
-- * Similarly the index must be the same.
-- * Specifically in the PK font case *without* fixed DPI,
-- The size must be the same too.
-- * For fontmap based fonts, compare the name field instead,
-- of the normal filename, especially to capture encoding differences.
-- An alternative might be to only take the encoding into account.
-- This is also required for other fonts which might not be backed by
-- traditional files
local function build_sharekey(fontdir)
local encodingbytes = assert(fontdir.encodingbytes)
local key = string.format("%i:%s:", fontdir.encodingbytes, fontdir.format)
if encodingbytes == 1 then
if fontdir.format:sub(1,5) == "type3" then
return string.format("%s%i:%s", key, fontdir.size, fontdir.name)
end
key = string.format("%s%s:", key, fontdir.encoding or '')
end
key = string.format("%s%i:%s", key, fontdir.subfont or 1, fontdir.filename)
return key
end
local fonts = font.fonts
local fontmap = setmetatable({}, {
__index = function(t, fid)
local key = build_sharekey(assert(fonts[fid]))
local mapped = keymap[key]
local share_parent
if mapped then
share_parent = t[mapped]
else
share_parent, keymap[key], t[fid] = fid, fid, fid
end
return share_parent
end,
})
function pdf.getfontname(fid)
return fontmap[fid]
end
return fontmap