2013-03-24 15:59:08 +00:00
|
|
|
require "dbg" -- for DEBUG()
|
2012-03-10 08:41:23 +00:00
|
|
|
|
|
|
|
Font = {
|
2012-04-09 17:04:26 +00:00
|
|
|
fontmap = {
|
|
|
|
-- default font for menu contents
|
2012-12-12 17:40:35 +00:00
|
|
|
cfont = "droid/DroidSansFallback.ttf",
|
2012-04-09 17:04:26 +00:00
|
|
|
-- default font for title
|
2013-04-07 08:33:58 +00:00
|
|
|
--tfont = "NimbusSanL-BoldItal.cff",
|
|
|
|
tfont = "droid/DroidSansFallback.ttf",
|
2012-04-09 17:04:26 +00:00
|
|
|
-- default font for footer
|
2013-04-07 08:33:58 +00:00
|
|
|
ffont = "droid/DroidSansFallback.ttf",
|
2012-04-09 17:04:26 +00:00
|
|
|
|
|
|
|
-- default font for reading position info
|
|
|
|
rifont = "droid/DroidSans.ttf",
|
|
|
|
|
|
|
|
-- default font for pagination display
|
|
|
|
pgfont = "droid/DroidSans.ttf",
|
|
|
|
|
|
|
|
-- selectmenu: font for item shortcut
|
|
|
|
scfont = "droid/DroidSansMono.ttf",
|
|
|
|
|
|
|
|
-- help page: font for displaying keys
|
|
|
|
hpkfont = "droid/DroidSansMono.ttf",
|
|
|
|
-- font for displaying help messages
|
|
|
|
hfont = "droid/DroidSans.ttf",
|
|
|
|
|
|
|
|
-- font for displaying input content
|
|
|
|
-- we have to use mono here for better distance controlling
|
|
|
|
infont = "droid/DroidSansMono.ttf",
|
2012-04-15 00:28:48 +00:00
|
|
|
|
|
|
|
-- font for info messages
|
|
|
|
infofont = "droid/DroidSans.ttf",
|
2012-04-09 17:04:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fontdir = os.getenv("FONTDIR") or "./fonts",
|
2012-03-10 08:41:23 +00:00
|
|
|
|
|
|
|
-- face table
|
|
|
|
faces = {},
|
|
|
|
}
|
|
|
|
|
2012-04-09 17:04:26 +00:00
|
|
|
|
|
|
|
function Font:getFace(font, size)
|
2012-03-10 08:41:23 +00:00
|
|
|
if not font then
|
|
|
|
-- default to content font
|
|
|
|
font = self.cfont
|
|
|
|
end
|
2013-03-16 00:03:50 +00:00
|
|
|
|
|
|
|
local size = math.floor(scaleByDPI(size))
|
2012-03-10 08:41:23 +00:00
|
|
|
|
|
|
|
local face = self.faces[font..size]
|
|
|
|
-- build face if not found
|
|
|
|
if not face then
|
2012-04-09 17:04:26 +00:00
|
|
|
local realname = self.fontmap[font]
|
|
|
|
if not realname then
|
|
|
|
realname = font
|
2012-03-10 08:41:23 +00:00
|
|
|
end
|
2012-04-09 17:04:26 +00:00
|
|
|
realname = self.fontdir.."/"..realname
|
|
|
|
ok, face = pcall(freetype.newFace, realname, size)
|
|
|
|
if not ok then
|
2012-05-28 16:59:16 +00:00
|
|
|
DEBUG("#! Font "..font.." ("..realname..") not supported: "..face)
|
2012-03-10 08:41:23 +00:00
|
|
|
return nil
|
|
|
|
end
|
2012-04-09 17:04:26 +00:00
|
|
|
self.faces[font..size] = face
|
2012-05-28 16:59:16 +00:00
|
|
|
--DEBUG("getFace, found: "..realname.." size:"..size)
|
2012-03-10 08:41:23 +00:00
|
|
|
end
|
Added widget abstraction framework
An example for using it:
--snip
require "widget"
require "font"
fb = einkfb.open("/dev/fb0")
G_width, G_height = fb:getSize()
dialog = CenterContainer:new({
dimen = { w = G_width, h = G_height },
VerticalGroup:new({
align = "center",
FrameContainer:new({
CenterContainer:new({
dimen = { w = 400, h = 200 },
TextWidget:new({
text = "Hi there! jgVJV",
face = Font:getFace("cfont", 30)
})
})
}),
ImageWidget:new({
file = "test.png"
}),
FrameContainer:new({
CenterContainer:new({
dimen = { w = 300, h = 200 },
TextWidget:new({
text = "another box",
face = Font:getFace("cfont", 30)
})
})
})
})
})
dialog:paintTo(fb.bb, 0, 0)
fb:refresh(0)
input.waitForEvent()
--snip
2012-04-14 23:59:49 +00:00
|
|
|
return { size = size, ftface = face, hash = font..size }
|
2012-04-09 17:04:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Font:_readList(target, dir, effective_dir)
|
|
|
|
for f in lfs.dir(dir) do
|
|
|
|
if lfs.attributes(dir.."/"..f, "mode") == "directory" and f ~= "." and f ~= ".." then
|
|
|
|
self:_readList(target, dir.."/"..f, effective_dir..f.."/")
|
|
|
|
else
|
|
|
|
local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "")
|
|
|
|
if file_type == "ttf" or file_type == "cff" or file_type == "otf" then
|
|
|
|
table.insert(target, effective_dir..f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Font:getFontList()
|
|
|
|
fontlist = {}
|
|
|
|
self:_readList(fontlist, self.fontdir, "")
|
|
|
|
table.sort(fontlist)
|
|
|
|
return fontlist
|
2012-03-10 08:41:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Font:update()
|
2012-04-09 17:04:26 +00:00
|
|
|
for _k, _v in ipairs(self.faces) do
|
|
|
|
_v:done()
|
|
|
|
end
|
2012-03-10 08:41:23 +00:00
|
|
|
self.faces = {}
|
2012-03-20 19:15:24 +00:00
|
|
|
clearGlyphCache()
|
2012-03-10 08:41:23 +00:00
|
|
|
end
|