Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/fontloader-data-con.lua

raw
1 if not modules then modules = { } end modules ['data-con'] = {
2 version = 1.100,
3 comment = "companion to luat-lib.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
9 local setmetatable = setmetatable
10 local format, lower, gsub = string.format, string.lower, string.gsub
11
12 local trace_cache = false trackers.register("resolvers.cache", function(v) trace_cache = v end)
13 local trace_containers = false trackers.register("resolvers.containers", function(v) trace_containers = v end)
14 local trace_storage = false trackers.register("resolvers.storage", function(v) trace_storage = v end)
15
16 -- Once we found ourselves defining similar cache constructs several times,
17 -- containers were introduced. Containers are used to collect tables in memory and
18 -- reuse them when possible based on (unique) hashes (to be provided by the calling
19 -- function).
20 --
21 -- Caching to disk is disabled by default. Version numbers are stored in the saved
22 -- table which makes it possible to change the table structures without bothering
23 -- about the disk cache.
24 --
25 -- Examples of usage can be found in the font related code. This code is not ideal
26 -- but we need it in generic too so we compromise.
27
28 containers = containers or { }
29 local containers = containers
30 containers.usecache = true
31
32 local getwritablepath = caches.getwritablepath
33 local getreadablepaths = caches.getreadablepaths
34 local cacheiswritable = caches.is_writable
35 local loaddatafromcache = caches.loaddata
36 local savedataincache = caches.savedata
37
38 local report_containers = logs.reporter("resolvers","containers")
39
40 local allocated = { }
41
42 local mt = {
43 __index = function(t,k)
44 if k == "writable" then
45 local writable = getwritablepath(t.category,t.subcategory) or { "." }
46 t.writable = writable
47 return writable
48 elseif k == "readables" then
49 local readables = getreadablepaths(t.category,t.subcategory) or { "." }
50 t.readables = readables
51 return readables
52 end
53 end,
54 __storage__ = true
55 }
56
57 function containers.define(category, subcategory, version, enabled, reload)
58 if category and subcategory then
59 local c = allocated[category]
60 if not c then
61 c = { }
62 allocated[category] = c
63 end
64 local s = c[subcategory]
65 if not s then
66 s = {
67 category = category,
68 subcategory = subcategory,
69 storage = { },
70 enabled = enabled,
71 reload = reload,
72 version = version or math.pi, -- after all, this is TeX
73 trace = false,
74 -- writable = getwritablepath and getwritablepath (category,subcategory) or { "." },
75 -- readables = getreadablepaths and getreadablepaths(category,subcategory) or { "." },
76 }
77 setmetatable(s,mt)
78 c[subcategory] = s
79 end
80 return s
81 end
82 end
83
84 function containers.is_usable(container,name)
85 return container.enabled and caches and cacheiswritable(container.writable, name)
86 end
87
88 function containers.is_valid(container,name)
89 if name and name ~= "" then
90 local storage = container.storage[name]
91 return storage and storage.cache_version == container.version
92 else
93 return false
94 end
95 end
96
97 function containers.read(container,name)
98 local storage = container.storage
99 local reload = container.reload
100 local stored = not reload and storage[name]
101 if not stored and container.enabled and caches and containers.usecache then
102 stored = loaddatafromcache(container.readables,name,container.writable)
103 if stored and stored.cache_version == container.version then
104 if trace_cache or trace_containers then
105 report_containers("action %a, category %a, name %a","load",container.subcategory,name)
106 end
107 else
108 stored = nil
109 end
110 storage[name] = stored
111 elseif stored then
112 if trace_cache or trace_containers then
113 report_containers("action %a, category %a, name %a","reuse",container.subcategory,name)
114 end
115 end
116 return stored
117 end
118
119 function containers.write(container, name, data, fast)
120 if data then
121 data.cache_version = container.version
122 if container.enabled and caches then
123 local unique = data.unique
124 local shared = data.shared
125 data.unique = nil
126 data.shared = nil
127 savedataincache(container.writable, name, data, fast)
128 if trace_cache or trace_containers then
129 report_containers("action %a, category %a, name %a","save",container.subcategory,name)
130 end
131 data.unique = unique
132 data.shared = shared
133 end
134 if trace_cache or trace_containers then
135 report_containers("action %a, category %a, name %a","store",container.subcategory,name)
136 end
137 container.storage[name] = data
138 end
139 return data
140 end
141
142 function containers.content(container,name)
143 return container.storage[name]
144 end
145
146 function containers.cleanname(name)
147 -- return (gsub(lower(name),"[^%w]+","-"))
148 return (gsub(lower(name),"[^%w\128-\255]+","-")) -- more utf friendly
149 end