summaryrefslogtreecommitdiff
path: root/otfl-font-dum.lua
blob: 2bcc8eff157f26a43843d15cf519e92cf6569347 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
if not modules then modules = { } end modules ['font-dum'] = {
    version   = 1.001,
    comment   = "companion to luatex-*.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

fonts = fonts or { }

-- general

fonts.otf.pack       = false
fonts.tfm.resolve_vf = false -- no sure about this

-- readers

fonts.tfm.readers          = fonts.tfm.readers or { }
fonts.tfm.readers.sequence = { 'otf', 'ttf', 'tfm' }
fonts.tfm.readers.afm      = nil

-- define

fonts.define = fonts.define or { }

--~ fonts.define.method = "tfm"

fonts.define.specify.colonized_default_lookup = "name"

function fonts.define.get_specification(str)
    return "", str, "", ":", str
end

-- logger

fonts.logger = fonts.logger or { }

function fonts.logger.save()
end

-- names
--
-- Watch out, the version number is the same as the one used in
-- the mtx-fonts.lua function scripts.fonts.names as we use a
-- simplified font database in the plain solution and by using
-- a different number we're less dependent on context.

fonts.names = fonts.names or { }

fonts.names.version    = 2.002 -- not the same as in context
fonts.names.basename   = "otfl-names.lua"
fonts.names.new_to_old = { }
fonts.names.old_to_new = { }

local data, loaded = nil, false

local synonyms = {
    regular     = {"normal", "roman", "plain", "book", "medium"},
    italic      = {"regularitalic", "normalitalic", "oblique", "slant"},
    bolditalic  = {"boldoblique", "boldslant"},
}

local function sanitize(str)
    return string.gsub(string.lower(str), "[^%a%d]", "")
end

function fonts.names.resolve(specification)
    local name, style = specification.name, specification.style or "regular"
    if not loaded then
        local basename = fonts.names.basename
        if basename and basename ~= "" then
            for _, format in ipairs { "lua", "tex", "other text files" } do
                local foundname = resolvers.find_file(basename,format) or ""
                if foundname ~= "" then
                    data = dofile(foundname)
                    break
                end
            end
        end
        loaded = true
    end
    if type(data) == "table" and data.version == fonts.names.version then
        if data.mappings then
            local family = data.families[name]
            if family and type(family) == "table" then
                for _,v in ipairs(family) do
                   local face      = data.mappings[v]
                   local subfamily = face.names.subfamily
                   local dsize     = face.size[1] and face.size[1] / 10
                   local ssize     = specification.size and specification.size / 65536
                   local osize     = tonumber(specification.optsize)
                   local maxsize   = face.design_range_bottom
                   local minsize   = face.design_range_top
                   local filename  = face.filename
                   if subfamily then
                       if sanitize(subfamily) == style then
                           if not dsize or dsize == osize or dsize == ssize then
                               found = filename
                               break
                           end
                       else
                           if synonyms[style] then
                               for _,v in ipairs(synonyms[style]) do
                                   if sanitize(subfamily) == v then
                                       if not dsize or dsize == osize or dsize == ssize then
                                            found = filename
                                            break
                                       end
                                   end
                               end
                           end
                       end
                   end
                end
                if found then
                   return found, false
                else
                   return name, false -- fallback to filename
                end
            end
        end
    end
    logs.report("define font", "Font names database version mismatch")
end

fonts.names.resolvespec = fonts.names.resolve -- only supported in mkiv

-- For the moment we put this (adapted) pseudo feature here.

table.insert(fonts.triggers,"itlc")

local function itlc(tfmdata,value)
    if value then
        -- the magic 40 and it formula come from Dohyun Kim
        local metadata = tfmdata.shared.otfdata.metadata
        if metadata then
            local italicangle = metadata.italicangle
            if italicangle and italicangle ~= 0 then
                local uwidth = (metadata.uwidth or 40)/2
                for unicode, d in next, tfmdata.descriptions do
                    local it = d.boundingbox[3] - d.width + uwidth
                    if it ~= 0 then
                        d.italic = it
                    end
                end
                tfmdata.has_italic = true
            end
        end
    end
end

fonts.initializers.base.otf.itlc = itlc
fonts.initializers.node.otf.itlc = itlc

function fonts.register_message()
end