summaryrefslogtreecommitdiff
path: root/tex/context/base/font-mis.lua
blob: 63cae37f39d2b64539f01ebce4b992aa13832ed6 (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
if not modules then modules = { } end modules ['font-mis'] = {
    version   = 1.001,
    comment   = "companion to mtx-fonts",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local next = next
local lower, strip = string.lower, string.strip

-- also used in other scripts so we need to check some tables:

fonts          = fonts or { }

fonts.helpers  = fonts.helpers or { }
local helpers  = fonts.helpers

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

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

otf.version    = otf.version or 2.751
otf.cache      = otf.cache   or containers.define("fonts", "otf", otf.version, true)

function otf.loadcached(filename,format,sub)
    -- no recache when version mismatch
    local name = file.basename(file.removesuffix(filename))
    if sub == "" then sub = false end
    local hash = name
    if sub then
        hash = hash .. "-" .. sub
    end
    hash = containers.cleanname(hash)
    local data = containers.read(otf.cache, hash)
    if data and not data.verbose then
        otf.enhancers.unpack(data)
        return data
    else
        return nil
    end
end

local featuregroups = { "gsub", "gpos" }

function fonts.helpers.getfeatures(name,t,script,language) -- maybe per font type
    local t = lower(t or (name and file.suffix(name)) or "")
    if t == "otf" or t == "ttf" or t == "ttc" or t == "dfont" then
        local filename = resolvers.findfile(name,t) or ""
        if filename ~= "" then
            local data = otf.loadcached(filename)
            if data and data.resources and data.resources.features then
                return  data.resources.features
            else
                local ff = fontloader.open(filename)
                if ff then
                    local data = fontloader.to_table(ff)
                    fontloader.close(ff)
                    local features = { }
                    for k=1,#featuregroups do
                        local what = featuregroups[k]
                        local dw = data[what]
                        if dw then
                            local f = { }
                            features[what] = f
                            for i=1,#dw do
                                local d = dw[i]
                                local dfeatures = d.features
                                if dfeatures then
                                    for i=1,#dfeatures do
                                        local df = dfeatures[i]
                                        local tag = strip(lower(df.tag))
                                        local ft = f[tag] if not ft then ft = {} f[tag] = ft end
                                        local dfscripts = df.scripts
                                        for i=1,#dfscripts do
                                            local ds = dfscripts[i]
                                            local scri = strip(lower(ds.script))
                                            local fts = ft[scri] if not fts then fts = {} ft[scri] = fts end
                                            local dslangs = ds.langs
                                            for i=1,#dslangs do
                                                local lang = dslangs[i]
                                                lang = strip(lower(lang))
                                                if scri == script then
                                                    if lang == language then
                                                        fts[lang] = 'sl'
                                                    else
                                                        fts[lang] = 's'
                                                    end
                                                else
                                                    if lang == language then
                                                        fts[lang] = 'l'
                                                    else
                                                        fts[lang] = true
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                    return features
                end
            end
        end
    end
    return nil, nil
end