font-prv.lua /size: 3161 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['font-prv'] = {
2    version   = 1.001,
3    comment   = "companion to font-ini.mkiv and hand-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local type, rawget = type, rawget
10local formatters = string.formatters
11
12local fonts             = fonts
13local helpers           = fonts.helpers
14local fontdata          = fonts.hashes.identifiers
15
16local setmetatableindex = table.setmetatableindex
17
18local currentprivate    = fonts.privateoffsets.textextrabase
19local maximumprivate    = currentprivate + 0xFFF
20
21local extraprivates     = { }
22helpers.extraprivates   = extraprivates
23
24function fonts.helpers.addextraprivate(name,f)
25    extraprivates[#extraprivates+1] = { name, f }
26end
27
28-- if we run out of space we can think of another range but by sharing we can
29-- use these privates for mechanisms like alignments-on-character and such
30
31local sharedprivates = setmetatableindex(function(t,k)
32    local v = currentprivate
33    if currentprivate < maximumprivate then
34        currentprivate = currentprivate + 1
35    else
36        -- reuse last slot, todo: warning
37    end
38    t[k] = v
39    return v
40end)
41
42function helpers.addprivate(tfmdata,name,characterdata)
43    local properties = tfmdata.properties
44    local characters = tfmdata.characters
45    local privates   = properties.privates
46    if not privates then
47        privates = { }
48        properties.privates = privates
49    end
50    if not name then
51        name = formatters["anonymous_private_0x%05X"](currentprivate)
52    end
53    local usedprivate = sharedprivates[name]
54    privates[name] = usedprivate
55    characters[usedprivate] = characterdata
56    return usedprivate
57end
58
59function helpers.getprivates(tfmdata)
60    if type(tfmdata) == "number" then
61        tfmdata = fontdata[tfmdata]
62    end
63    local properties = tfmdata.properties
64    return properties and properties.privates
65end
66
67function helpers.hasprivate(tfmdata,name)
68    if type(tfmdata) == "number" then
69        tfmdata = fontdata[tfmdata]
70    end
71    local properties = tfmdata.properties
72    local privates = properties and properties.privates
73    return privates and privates[name] or false
74end
75
76function helpers.privateslot(name)
77    return rawget(sharedprivates,name)
78end
79
80function helpers.newprivateslot(name)
81    return sharedprivates[name]
82end
83
84do
85
86    local context = context
87    local utfchar = utf.char
88
89    interfaces.implement {
90        name      = "privatecharacter",
91        public    = true,
92     -- protected = true,
93        arguments = "string",
94        actions   = function(name)
95            local c = sharedprivates[name]
96            if c then
97                context(utfchar(c))
98            end
99        end
100    }
101
102    interfaces.implement {
103        name      = "privatecharactercode",
104        public    = true,
105     -- protected = true,
106        arguments = "string",
107        actions   = function(name)
108            local c = sharedprivates[name]
109            if c then
110                context(c) -- serialized, not a number
111            end
112        end
113    }
114
115end
116