luatex-fonts-syn.lua /size: 3674 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['luatex-fonts-syn'] = {
2    version   = 1.001,
3    comment   = "companion to luatex-*.tex",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9if context then
10    os.exit()
11end
12
13-- Generic font names support.
14--
15-- Watch out, the version number is the same as the one used in
16-- the mtx-fonts.lua function scripts.fonts.names as we use a
17-- simplified font database in the plain solution and by using
18-- a different number we're less dependent on context.
19--
20-- mtxrun --script font --reload --simple
21--
22-- The format of the file is as follows:
23--
24-- return {
25--     ["version"]       = 1.001,
26--     ["cache_version"] = 1.001,
27--     ["mappings"]      = {
28--         ["somettcfontone"] = { "Some TTC Font One", "SomeFontA.ttc", 1 },
29--         ["somettcfonttwo"] = { "Some TTC Font Two", "SomeFontA.ttc", 2 },
30--         ["somettffont"]    = { "Some TTF Font",     "SomeFontB.ttf"    },
31--         ["someotffont"]    = { "Some OTF Font",     "SomeFontC.otf"    },
32--     },
33-- }
34
35local fonts = fonts
36fonts.names = fonts.names or { }
37
38fonts.names.version  = 1.001 -- not the same as in context but matches mtx-fonts --simple
39fonts.names.basename = "luatex-fonts-names"
40fonts.names.cache    = containers.define("fonts","data",fonts.names.version,true)
41
42local data           = nil
43local loaded         = false
44
45local fileformats    = { "lua", "tex", "other text files" }
46
47function fonts.names.reportmissingbase()
48    logs.report("fonts","missing font database, run: mtxrun --script fonts --reload --simple")
49    fonts.names.reportmissingbase = nil
50end
51
52function fonts.names.reportmissingname()
53    logs.report("fonts","unknown font in font database, run: mtxrun --script fonts --reload --simple")
54    fonts.names.reportmissingname = nil
55end
56
57function fonts.names.resolve(name,sub)
58    if not loaded then
59        local basename = fonts.names.basename
60        if basename and basename ~= "" then
61            data = containers.read(fonts.names.cache,basename)
62            if not data then
63                basename = file.addsuffix(basename,"lua")
64                for i=1,#fileformats do
65                    local format = fileformats[i]
66                    local foundname = resolvers.findfile(basename,format) or ""
67                    if foundname ~= "" then
68                        data = dofile(foundname)
69                        logs.report("fonts","font database '%s' loaded",foundname)
70                        break
71                    end
72                end
73            end
74        end
75        loaded = true
76    end
77    if type(data) == "table" and data.version == fonts.names.version then
78        local condensed = string.gsub(string.lower(name),"[^%a%d]","")
79        local found = data.mappings and data.mappings[condensed]
80        if found then
81            local fontname, filename, subfont = found[1], found[2], found[3]
82            if subfont then
83                return filename, fontname
84            else
85                return filename, false
86            end
87        elseif fonts.names.reportmissingname then
88            fonts.names.reportmissingname()
89            return name, false -- fallback to filename
90        end
91    elseif fonts.names.reportmissingbase then
92        fonts.names.reportmissingbase()
93    end
94end
95
96fonts.names.resolvespec = fonts.names.resolve -- only supported in mkiv
97
98function fonts.names.getfilename(askedname,suffix)  -- only supported in mkiv
99    return ""
100end
101
102function fonts.names.ignoredfile(filename) -- only supported in mkiv
103    return false -- will be overloaded
104end
105