summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/font-off.lua
blob: 7e509c2c3d75d54683548650ffe14127eee449bb (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
if not modules then modules = { } end modules ['font-off'] = {
    version   = 1.001,
    comment   = "companion to font-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tonumber          = tonumber
local lower             = string.lower
local round             = math.round
local setmetatableindex = table.setmetatableindex

local fontloader        = fontloader
----- font_to_table     = fontloader.to_table
local open_font         = fontloader.open
----- get_font_info     = fontloader.info
local close_font        = fontloader.close
----- font_fields       = fontloader.fields

-- table={
--  ["familyname"]="TeXGyrePagella",
--  ["fontname"]="TeXGyrePagella-Regular",
--  ["fullname"]="TeXGyrePagella-Regular",
--  ["italicangle"]=0,
--  ["names"]={
--   {
--    ["lang"]="English (US)",
--    ["names"]={
--     ["copyright"]="Copyright 2006, 2009 for TeX Gyre extensions by B. Jackowski and J.M. Nowacki (on behalf of TeX users groups). This work is released under the GUST Font License --  see http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt for details.",
--     ["family"]="TeXGyrePagella",
--     ["fullname"]="TeXGyrePagella-Regular",
--     ["postscriptname"]="TeXGyrePagella-Regular",
--     ["preffamilyname"]="TeX Gyre Pagella",
--     ["subfamily"]="Regular",
--     ["trademark"]="Please refer to the Copyright section for the font trademark attribution notices.",
--     ["uniqueid"]="2.004;UKWN;TeXGyrePagella-Regular",
--     ["version"]="Version 2.004;PS 2.004;hotconv 1.0.49;makeotf.lib2.0.14853",
--    },
--   },
--  },
--  ["pfminfo"]={
--   ["avgwidth"]=528,
--   ["codepages"]={ 536871315, 0 },
--   ["firstchar"]=32,
--   ["fstype"]=12,
--   ["hhead_ascent"]=1098,
--   ["hhead_descent"]=-283,
--   ["hheadascent_add"]=0,
--   ["hheaddescent_add"]=0,
--   ["hheadset"]=1,
--   ["lastchar"]=64260,
--   ["linegap"]=0,
--   ["os2_breakchar"]=32,
--   ["os2_capheight"]=692,
--   ["os2_defaultchar"]=0,
--   ["os2_family_class"]=0,
--   ["os2_strikeypos"]=269,
--   ["os2_strikeysize"]=50,
--   ["os2_subxoff"]=0,
--   ["os2_subxsize"]=650,
--   ["os2_subyoff"]=75,
--   ["os2_subysize"]=600,
--   ["os2_supxoff"]=0,
--   ["os2_supxsize"]=650,
--   ["os2_supyoff"]=350,
--   ["os2_supysize"]=600,
--   ["os2_typoascent"]=726,
--   ["os2_typodescent"]=-274,
--   ["os2_typolinegap"]=200,
--   ["os2_vendor"]="UKWN",
--   ["os2_winascent"]=1098,
--   ["os2_windescent"]=283,
--   ["os2_xheight"]=449,
--   ["panose"]={
--    ["armstyle"]="Any",
--    ["contrast"]="Any",
--    ["familytype"]="Any",
--    ["letterform"]="Any",
--    ["midline"]="Any",
--    ["proportion"]="Any",
--    ["serifstyle"]="Any",
--    ["strokevariation"]="Any",
--    ["weight"]="Book",
--    ["xheight"]="Any",
--   },
--   ["panose_set"]=1,
--   ["pfmfamily"]=81,
--   ["pfmset"]=1,
--   ["subsuper_set"]=1,
--   ["typoascent_add"]=0,
--   ["typodescent_add"]=0,
--   ["unicoderanges"]={ 536871047, 0, 0, 0 },
--   ["vheadset"]=0,
--   ["vlinegap"]=0,
--   ["weight"]=400,
--   ["width"]=5,
--   ["winascent_add"]=0,
--   ["windescent_add"]=0,
--  },
--  ["units_per_em"]=1000,
--  ["version"]="2.004;PS 2.004;hotconv 1.0.49;makeotf.lib2.0.14853",
--  ["weight"]="Book",
-- }

-- We had this as temporary solution because we needed a bit more info but in the
-- meantime it got an interesting side effect: currently luatex delays loading of e.g.
-- glyphs so here we first load and then discard which is a waste. In the past it did
-- free memory because a full load was done. One of these things that goes unnoticed.
--
-- local function get_full_info(...) -- check with taco what we get / could get
--     local ff = open_font(...)
--     if ff then
--         local d = ff -- and font_to_table(ff)
--         d.glyphs, d.subfonts, d.gpos, d.gsub, d.lookups = nil, nil, nil, nil, nil
--         close_font(ff)
--         return d
--     else
--         return nil, "error in loading font"
--     end
-- end

-- Phillip suggested this faster variant but it's still a hack as fontloader.info should
-- return these keys/values (and maybe some more) but at least we close the loader which
-- might save some memory in the end.

-- local function get_full_info(name)
--     local ff = open_font(name)
--     if ff then
--         local fields = table.tohash(font_fields(ff),true) -- isn't that one stable
--         local d   = {
--             names       = fields.names               and ff.names,
--             familyname  = fields.familyname          and ff.familyname,
--             fullname    = fields.fullname            and ff.fullname,
--             fontname    = fields.fontname            and ff.fontname,
--             weight      = fields.weight              and ff.weight,
--             italicangle = fields.italicangle         and ff.italicangle,
--             units       = fields.units_per_em        and ff.units_per_em,
--             designsize  = fields.design_size         and ff.design_size,
--             minsize     = fields.design_range_bottom and ff.design_range_bottom,
--             maxsize     = fields.design_range_top    and ff.design_range_top,
--             italicangle = fields.italicangle         and ff.italicangle,
--             pfmweight   = pfminfo and pfminfo.weight or 400,
--             pfmwidth    = pfminfo and pfminfo.width  or 5,
--         }
--      -- setmetatableindex(d,function(t,k)
--      --     report_names("warning, trying to access field %a in font table of %a",k,name)
--      -- end)
--         close_font(ff)
--         return d
--     else
--         return nil, "error in loading font"
--     end
-- end

-- more efficient:

local fields = nil

local function check_names(names)
    if names then
        for i=1,#names do
            local name = names[i]
            if lower(name.lang) == "english (us)" then -- lower added
                return name.names
            end
        end
    end
end

local function get_full_info(name)
    local ff = open_font(name)
    if ff then
        -- unfortunately luatex aborts when a field is not available but we just make
        -- sure that we only access a few known ones
        local pfminfo = ff.pfminfo or { }
        local names   = check_names(ff.names) or { }
        local weight  = names.weight or ff.weight
        local width   = names.width -- no: ff.width
        local d   = {
            fontname      = ff.fontname,
            fullname      = names.fullname or ff.fullname,
            family        = names.family,
            subfamily     = names.subfamily,
            familyname    = names.preffamilyname or names.family or ff.familyname,
            subfamilyname = names.prefmodifiers or names.subfamily,
            weight        = weight and lower(weight),
            width         = width and lower(width),
            italicangle   = round(1000*(tonumber(ff.italicangle) or 0))/1000 or 0,
            units         = ff.units_per_em,
            designsize    = ff.design_size,
            minsize       = ff.design_range_bottom,
            maxsize       = ff.design_range_top,
            pfmweight     = pfminfo.weight or 400,
            pfmwidth      = pfminfo.width  or 5,
            monospaced    = pfminfo.panose and pfminfo.panose.proportion == "Monospaced",
        }
        close_font(ff)
        return d
    else
        return nil, "error in loading font"
    end
end

-- As we have lazy loading anyway, this one still is full and with less code than
-- the previous one. But this depends on the garbage collector to kick in and in the
-- current version that somehow happens not that often (on my machine I end up with
-- some 3 GB extra before that happens).

-- local function get_full_info(...)
--     local ff = open_font(...)
--     if ff then
--         local d = { } -- ff is userdata so [1] or # fails on it
--         setmetatableindex(d,ff)
--         return d -- garbage collection will do the close_font(ff)
--     else
--         return nil, "error in loading font"
--     end
-- end

fonts          = fonts or { }
local handlers = fonts.handlers or { }
fonts.handlers = handlers
local otf      = handlers.otf or { }
handlers.otf   = otf
local readers  = otf.readers or { }
otf.readers    = readers

fontloader.fullinfo = get_full_info
readers.getinfo     = readers.getinfo or get_full_info