/usr/share/doc/instead/manual/examples/fonts.lua is in instead-doc-base 1.9.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | -- example module
require "sprites"
require "theme"
function font(name, size, scale)
if scale == nil then
scale = true
end
local v = obj {
nam = 'styler';
fname = name;
size = size;
cache = { };
list = { };
scaled = scale;
cache_get = function(s, w, color, t)
local k = w..color..tostring(t)
if s.cache[k].time ~= -1 then
s.cache[k].time = stead.time()
end
return s.cache[k]
end;
cache_clear = function(self, age)
local k, v
local new_list = {}
if not age then
age = 0
end
for k,v in ipairs(self.list) do
local key = v.word..v.color..tostring(v.t)
if v.time ~= -1 and stead.time() - v.time >= age then
sprite.free(v.img);
self.cache[key] = nil
else
table.insert(new_list, v)
if v.time ~= -1 then
self:cache_add(v.word, v.color, v.t, v.img, nil) -- renew time
else
self:cache_add(v.word, v.color, v.t, v.img, -1)
end
end
end
self.list = new_list
end;
life = function(s)
if player_moved() then
s:cache_clear(2)
end
end;
txt = function(st, txt, color, t)
local s, e;
local ss = 1
local res = ''
if not color then
color = theme.get 'win.col.fg'
end
if not t then
t = 0
end
while true do
s, e = txt:find("[ \t]+", ss);
local w = txt:sub(ss, s);
if w and w ~= '' then
st:cache_add(w, color, t)
res = res .. img(st:cache_get(w, color, t).img);
end
if not e then break end
ss = e + 1
res = res .. ' ';
end
return res;
end;
cache_add = function(s, w, color, t, key, time)
local k = w..color..tostring(t)
if not s.cache[k] then
s.cache[k] = {}
s.cache[k].img = sprite.text(s.font, w, color, t);
if key and key ~= s.cache[k].img then
stead.sprite_dup(s.cache[k].img, key);
sprite.free(s.cache[k].img);
s.cache[k].img = key
end
s.cache[k].word = w;
s.cache[k].color = color;
s.cache[k].t = t;
s.cache[k].time = stead.time();
stead.table.insert(s.list, s.cache[k]);
end
if not stead.started and not time then
time = -1
end
if time then
s.cache[k].time = time
else
s.cache[k].time = stead.time(); -- renew time
end
end;
save = function(self, name, h, need)
h:write(stead.string.format("%s = font(%q, %d, %s);\n", name, self.fname, self.size, stead.tostring(self.scaled)))
local k, v
for k,v in ipairs(self.list) do
h:write(stead.string.format("%s:cache_add(%q, %q, %d, %q, %d);\n", name, v.word, v.color, v.t, v.img, v.time))
end
end;
}
if v.scaled then
size = sprite.font_scaled_size(size)
end
v.font = sprite.font(name, size);
lifeon(v);
return v;
end
|