summaryrefslogtreecommitdiff
path: root/luaotfload-fonts.lua
blob: 0502a9ce05623c00696a13faacad5e3fd308d780 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
luaotfload              = luaotfload or { }
luaotfload.fonts        = { }

luaotfload.fonts.module = {
    name          = "luaotfload.fonts",
    version       = 1.001,
    date          = "2010/01/12",
    description   = "luaotfload font database.",
    author        = "Khaled Hosny",
    copyright     = "Khaled Hosny",
    license       = "CC0"
}

kpse.set_program_name("luatex")

dofile(kpse.find_file("luaextra.lua"))

local upper, splitpath, expandpath, glob, basename = string.upper, file.split_path, kpse.expand_path, dir.glob, file.basename

luaotfload.fonts.basename   = "otfl-names.lua"
luaotfload.fonts.version    = 2.000
luaotfload.fonts.log_level  = 1

local function info(fmt,...)
    texio.write_nl(string.format("luaotfload | %s", string.format(fmt,...)))
end

local function log(lvl, ...)
    if lvl <= luaotfload.fonts.log_level then
        info(...)
    end
end

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

function fontloader.fullinfo(...)
    local t, n = { }, { }
    local f = fontloader.open(...)
    local m = f and fontloader.to_table(f)
    fontloader.close(f)
    if m.names then
        for _,v in pairs(m.names) do
            if v.lang == "English (US)" then
                n.name   = v.names.compatfull     or v.names.fullname
                n.family = v.names.preffamilyname or v.names.family
                n.style  = v.names.prefmodifiers  or v.names.subfamily
            end
        end
    end
    if m.fontstyle_name then
        for _,v in pairs(m.fontstyle_name) do
            if v.lang == 1033 then
                m.style = v.name
            end
        end
    end
    t.psname   = m.fontname
    t.fullname = n.name   or m.fullname
    t.family   = n.family or m.familyname
    t.style    = n.style  or m.style
    for k,v in pairs(t) do
        t[k] = sanitize(v)
    end
    m, n = nil, nil
    return t
end

-- table containing hard to guess styles
local styles = {
    calibrii = "reg-no-no-norm-0-text",
    bookosi = "reg-it-no-norm-0-text",
    bookosb = "bd-no-no-norm-0-text",
    lsansi = "reg-it-no-norm-0-text",
    antquab = "bd-no-no-norm-0-text",
    antquai = "reg-it-no-norm-0-text",
    }

-- euristics to normalize the style
local function normalize_style(style, family)
    local s = {}
    if style:find("semibold") or style:find("demibold")
            or style:find("lightbold") or style:match("lb$") then
        s.weight = "smbd"
    elseif style:find("bold") or style:match("xb$")
            or style:match("bd$") or style:match("bb$") then
        s.weight = "bd"
    elseif style:find("medium") or style:find("med") then
        s.weight = "med"
    elseif style:find("narrow") then
        s.weight = "nar" -- ?
    end
    if style:find("italic") or style:find("oblique")  or style:match("it$") then
        s.italic = "it"
    elseif style:find("oblique") then
        s.italic = "obl"
    end
    if style:find("heavy") then
        s.type = "heavy"
    elseif style:find("light") then
        s.type = "light"
    end
    if style:find("regular") or style:match("rg$") then
        s.regular = true
    end
    if style:find("condensed") then
        s.condensed = true
    end
    if style:find("caption") or style:find("capt") then
        s.size_type = 'capt'
    elseif style:find("display") or style:find("disp") then
        s.size_type = 'disp'
    elseif style:find("subhead") or style:find("subh") then
        s.size_type = 'subh'
    end
    local size = tonumber(string.match(style, "%d+"))
    if size and size > 4 and size < 25 then 
        s.size = size
    end
    if not next(s) then -- more aggressive guessing
        local truncated = style:sub(1,-2)
        local endletter = style:sub(-1, -1)
        if family:find(truncated) and family:sub(-1,-1) ~= endletter then 
            if endletter =='b' then
                s.weight = "bd"
            elseif endletter == 'i' then
                s.italic = "it"
            end
        end
    end
    if not next(s) and styles[style] then
        return styles[style]
    end
    local result = nil
    if s.weight then
        result = s.weight
    else
        result = 'reg'
    end
    if s.italic then
        result = result.."-"..s.italic
    else
        result = result.."-no"
    end
    if s.condensed then
        result = result.."-cond"
    else
        result = result.."-no"
    end
    if s.type then
        result = result.."-"..s.type
    else
        result = result.."-norm"
    end
    if s.size then
        result = result.."-"..s.size
    else
        result = result.."-0"
    end
    if s.size_type then
        result = result.."-"..s.size_type
    else
        result = result.."-text"
    end
    return result
end

local function load_font(filename, names, texmf)
    log(3, "Loading font %s", filename)
    local psnames, families = names.mappings.psnames, names.mappings.families
    if filename then
        local info = fontloader.info(filename)
        if info then
            if type(info) == "table" and #info > 1 then
                for index,_ in ipairs(info) do
                    local fullinfo = fontloader.fullinfo(filename, index-1)
                    if not families[fullinfo.family] then
                        families[fullinfo.family] = { }
                    end
                    families[fullinfo.family][fullinfo.style] = {texmf and basename(filename) or filename, index-1}
                    psnames[fullinfo.psname] = {texmf and basename(filename) or filename, index-1}
                end
            else
                local fullinfo = fontloader.fullinfo(filename)
                if texmf == true then
                    filename = basename(filename)
                end
                if not families[fullinfo.family] then
                    families[fullinfo.family] = { }
                end
                if not fullinfo.style then
                    fullinfo.style = sanitize(file.nameonly(filename))
                end
                local guessed = normalize_style(fullinfo.style, fullinfo.family)
                families[fullinfo.family][texmf and basename(filename) or filename] = 
                    {
                      guessed_style = guessed,
                      raw_style = fullinfo.style,
                    }
                psnames[fullinfo.psname] = {texmf and basename(filename) or filename}
            end
        else
            log(1, "Failed to load %s", filename)
        end
    end
end

local function scan_dir(dirname, names, recursive, texmf)
    log(1, "Scanning directory %s", dirname)
    local list, found = { }, { }
    for _,ext in ipairs { "otf", "ttf", "ttc", "dfont" } do
        if recursive then pat = "/**." else pat = "/*." end
        log(2, "Scanning '%s' for '%s' fonts", dirname, ext)
        found = glob(dirname .. pat .. ext)
        log(2, "%s fonts found", #found)
        table.append(list, found)
        log(2, "Scanning '%s' for '%s' fonts", dirname, upper(ext))
        found = glob(dirname .. pat .. upper(ext))
        log(2, "%s fonts found", #found)
        table.append(list, found)
    end
    for _,fnt in ipairs(list) do
        load_font(fnt, names, texmf)
    end
end

local system = LUAROCKS_UNAME_S or io.popen("uname -s"):read("*l")
if system then
    if system:match("^CYGWIN") then
        system = 'cygwin'
    elseif system:match("^Windows") then
        system = 'windows'
    else
        system = 'unix'
    end
else
    system = 'unix' -- ?
end
log(1, "detecting system: %s", system)

local texmfdist = kpse.expand_var("$TEXMFDIST")
local texmfmain = kpse.expand_var("$TEXMFMAIN")
local texmflocal = kpse.expand_var("$TEXMFLOCAL")
-- We lowercase everything under Windows, in order to get a bit of consistency
if system == 'windows' or system == 'cygwin' then
    texmfdist = string.lower(texmfdist)
    texmfmain = string.lower(texmfmain)
    texmflocal = string.lower(texmflocal)
end


local function is_texmf(dir)
    if dir:find(texmfdist) or dir:find(texmfmain) or dir:find(texmflocal) then
        return true
    end
    return false
end

local function read_fcdata(fontdirs, data, translate)
    local to_add = nil
    local done = nil
    for line in data:lines() do
        if not done then done = true end
        local match = line:match("^Directory: (.+)")
        if match then
            if match:find("ype1") then
                to_add = nil
            else
                to_add = translate(match)
            end
        elseif to_add then
            match = line:match('^"[^"]+%.[^"]+"')
            if match then
                if to_add then
                    fontdirs[to_add] = true
                    to_add = nil
                end
            end
        end
    end
    if not done then
        return nil
    else
        return fontdirs
    end
end

local function cygwin_translate(name)
    local res = string.lower(io.popen(string.format("cygpath.exe --mixed %s", name)):read("*all"))
    -- a very strange thing: spaces are replaced by \n and there is a trailing \n at the end
    res = res:gsub("\n$", '')
    res = res:gsub("\n", ' ')
    return res
end

local function windows_translate(name)
    return string.lower(name)
end

local function no_translate(name)
    return name
end

local function append_fccatdirs(fontdirs)
    -- under cygwin we have the choice between the
    -- fc-cat of cygwin and the fc-cat of TeXLive.
    -- we try the fc-cat from TeXLive.
    local translate = no_translate
    if system == 'cygwin' then
        local path = kpse.expand_var("$TEXMFMAIN")..'/../bin/win32/fc-cat.exe'
        if lfs.isfile(path) then
            log(1, "executing `%s' -v\n", path)
            -- dirty hack...
            path = io.popen(string.format('cygpath.exe -C ANSI -w -s "%s"', path)):read("*all")
            local data = io.popen('"'..path..' -v"', 'r')
            local result = read_fcdata(fontdirs, data, windows_translate)
            data:close()
            if result then
                return result
            else
                log(1, "fail")
            end
        else
            log(1, "unable to find TeXLive's fc-cat.exe")
        end
        translate = cygwin_translate
    elseif system == 'windows' then
        translate = windows_translate
    end
    log(1, "executing `fc-cat -v'\n")
    local data = io.popen("fc-cat -v", 'r')
    local result = read_fcdata(fontdirs, data, translate)
    data:close()
    -- this part may be removed (needs further tests though, under non-cygwin Windows systems)
    if not result then
        log(1, "fail, now trying `fc-cat.exe -v'\n")
        data = io.popen("fc-cat.exe -v", 'r')
        result = read_fcdata(fontdirs, data, translate)
        data:close()
        if not result then
            info("Unable to execute fc-cat nor fc-cat.exe, system fonts will not be available")
            return fontdirs
        end
    end
    return result
end

local function scan_all(names)
    local fontdirs = string.gsub(expandpath("$OPENTYPEFONTS"), "^\.[;:]", "")
    fontdirs = fontdirs .. string.gsub(expandpath("$TTFONTS"), "^\.", "")
    if system == 'windows' or system == 'cygwin' then
        fontdirs = string.lower(fontdirs)
    end
    if not fontdirs:is_empty() then
        fontdirs = splitpath(fontdirs)
        fontdirs = table.tohash(fontdirs)
        fontdirs = append_fccatdirs(fontdirs)
        for d,_ in pairs(fontdirs) do
            scan_dir(d, names, false, is_texmf(d))
        end
    end
end

local function generate()
    local fnames = {
        mappings = {
            families = { },
            psnames  = { },
        },
        version  = luaotfload.fonts.version
    }
    local savepath
    scan_all(fnames)
    info("%s fonts saved in the database", #table.keys(fnames.mappings.psnames))
    savepath = kpse.expand_var("$TEXMFVAR") .. "/tex/"
    if not file.isreadable(savepath) then
        log(1, "Creating directory %s", savepath)
        lfs.mkdir(savepath)
    end
    if not file.iswritable(savepath) then
        info("Error: cannot write in directory %s\n", savepath)
    else
        savepath = savepath .. luaotfload.fonts.basename
        io.savedata(savepath, table.serialize(fnames, true))
        info("Saved font names database in %s\n", savepath)
    end
end

luaotfload.fonts.scan     = scan_dir
luaotfload.fonts.generate = generate

if arg[0] == "luaotfload-fonts.lua" then
    generate()
end