add standalone glyph cache

I found it will be especially helpful to cache glyph separately
so that glyph caches won't be washed out by a single page cache.
pull/9/head
chrox 11 years ago
parent 039eea17ae
commit 01f045e7f9

@ -1,3 +1,4 @@
require "dbg"
--[[
Inheritable abstraction for cache items
]]--
@ -29,8 +30,14 @@ Cache = {
cache_order = {}
}
function Cache:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Cache:insert(key, object)
--@TODO add cache for different types of item 09.01 2013 (houqp)
-- guarantee that we have enough memory in cache
if(object.size > self.max_memsize) then
-- we're not allowed to claim this much at all

@ -4,11 +4,19 @@ require "cache"
TODO: all these functions should probably be methods on Face objects
]]--
GlyphCache = Cache:new{
max_memsize = 512*1024,
current_memsize = 0,
cache = {},
-- this will hold the LRU order of the cache
cache_order = {}
}
function getGlyph(face, charcode, bgcolor, fgcolor)
if bgcolor == nil then bgcolor = 0.0 end
if fgcolor == nil then fgcolor = 1.0 end
local hash = "glyph|"..face.hash.."|"..charcode.."|"..bgcolor.."|"..fgcolor
local glyph = Cache:check(hash)
local glyph = GlyphCache:check(hash)
if glyph then
-- cache hit
return glyph[1]
@ -20,8 +28,8 @@ function getGlyph(face, charcode, bgcolor, fgcolor)
end
glyph = CacheItem:new{rendered_glyph}
glyph.size = glyph[1].bb:getWidth() * glyph[1].bb:getHeight() / 2 + 32
Cache:insert(hash, glyph)
return glyph[1]
GlyphCache:insert(hash, glyph)
return rendered_glyph
end
function getSubTextByWidth(text, face, width, kerning)

Loading…
Cancel
Save