mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
major overhaul of the code structure, more OO like
This commit is contained in:
@@ -357,6 +357,8 @@ int luaopen_blitbuffer(lua_State *L) {
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
luaL_register(L, NULL, blitbuffer_meth);
|
||||
luaL_register(L, "blitbuffer", blitbuffer_func);
|
||||
lua_setglobal(L, "blitbuffer");
|
||||
luaL_register(L, "Blitbuffer", blitbuffer_func);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
7
graphics.lua
Normal file
7
graphics.lua
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
blitbuffer.paintBorder = function (bb, x, y, w, h, bw, c)
|
||||
bb:paintRect(x, y, w, bw, c)
|
||||
bb:paintRect(x, y+h-bw, w, bw, c)
|
||||
bb:paintRect(x, y+bw, bw, h - 2*bw, c)
|
||||
bb:paintRect(x+w-bw, y+bw, bw, h - 2*bw, c)
|
||||
end
|
||||
359
pdfreader.lua
Normal file
359
pdfreader.lua
Normal file
@@ -0,0 +1,359 @@
|
||||
require "keys"
|
||||
require "settings"
|
||||
|
||||
PDFReader = {
|
||||
-- "constants":
|
||||
ZOOM_BY_VALUE = 0,
|
||||
ZOOM_FIT_TO_PAGE = -1,
|
||||
ZOOM_FIT_TO_PAGE_WIDTH = -2,
|
||||
ZOOM_FIT_TO_PAGE_HEIGHT = -3,
|
||||
ZOOM_FIT_TO_CONTENT = -4,
|
||||
ZOOM_FIT_TO_CONTENT_WIDTH = -5,
|
||||
ZOOM_FIT_TO_CONTENT_HEIGHT = -6,
|
||||
|
||||
GAMMA_NO_GAMMA = 1.0,
|
||||
|
||||
-- framebuffer update policy state:
|
||||
rcount = 5,
|
||||
rcountmax = 5,
|
||||
|
||||
-- zoom state:
|
||||
globalzoom = 1.0,
|
||||
globalzoommode = -1, -- ZOOM_FIT_TO_PAGE
|
||||
|
||||
-- gamma setting:
|
||||
globalgamma = 1.0, -- GAMMA_NO_GAMMA
|
||||
|
||||
-- size of current page for current zoom level in pixels
|
||||
fullwidth = 0,
|
||||
fullheight = 0,
|
||||
offset_x = 0,
|
||||
offset_y = 0,
|
||||
|
||||
-- set panning distance
|
||||
shift_x = 100,
|
||||
shift_y = 50,
|
||||
pan_by_page = false, -- using shift_[xy] or width/height
|
||||
|
||||
-- keep track of input state:
|
||||
shiftmode = false, -- shift pressed
|
||||
altmode = false, -- alt pressed
|
||||
|
||||
-- the pdf document:
|
||||
doc = nil,
|
||||
-- the document's setting store:
|
||||
settings = nil,
|
||||
|
||||
-- we will use this one often, so keep it "static":
|
||||
nulldc = pdf.newDC(),
|
||||
|
||||
-- tile cache configuration:
|
||||
cache_max_memsize = 1024*1024*5, -- 5MB tile cache
|
||||
cache_item_max_pixels = 1024*1024*2, -- max. size of rendered tiles
|
||||
cache_max_ttl = 20, -- time to live
|
||||
-- tile cache state:
|
||||
cache_current_memsize = 0,
|
||||
cache = {},
|
||||
}
|
||||
|
||||
-- guarantee that we have enough memory in cache
|
||||
function PDFReader:cacheclaim(size)
|
||||
if(size > self.cache_max_memsize) then
|
||||
-- we're not allowed to claim this much at all
|
||||
error("too much memory claimed")
|
||||
return false
|
||||
end
|
||||
while self.cache_current_memsize + size > self.cache_max_memsize do
|
||||
-- repeat this until we have enough free memory
|
||||
for k, _ in pairs(self.cache) do
|
||||
if self.cache[k].ttl > 0 then
|
||||
-- reduce ttl
|
||||
self.cache[k].ttl = self.cache[k].ttl - 1
|
||||
else
|
||||
-- cache slot is at end of life, so kick it out
|
||||
self.cache_current_memsize = self.cache_current_memsize - self.cache[k].size
|
||||
self.cache[k] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
self.cache_current_memsize = self.cache_current_memsize + size
|
||||
return true
|
||||
end
|
||||
|
||||
function PDFReader:draworcache(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||
-- hash draw state
|
||||
local hash = self:cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||
if self.cache[hash] == nil then
|
||||
-- not in cache, so prepare cache slot...
|
||||
self:cacheclaim(width * height / 2);
|
||||
self.cache[hash] = {
|
||||
ttl = self.cache_max_ttl,
|
||||
size = width * height / 2,
|
||||
bb = Blitbuffer.new(width, height)
|
||||
}
|
||||
-- and draw the page
|
||||
local page = self.doc:openPage(no)
|
||||
local dc = self:setzoom(page, hash)
|
||||
page:draw(dc, self.cache[hash].bb, 0, 0)
|
||||
page:close()
|
||||
else
|
||||
-- we have the page in our cache,
|
||||
-- so give it more ttl.
|
||||
self.cache[hash].ttl = self.cache_max_ttl
|
||||
end
|
||||
return hash
|
||||
end
|
||||
|
||||
-- calculate a hash for our current state
|
||||
function PDFReader:cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||
-- TODO (?): make this a "real" hash...
|
||||
return no..'_'..zoom..'_'..offset_x..','..offset_y..'-'..width..'x'..height..'_'..gamma;
|
||||
end
|
||||
|
||||
-- blank the cache
|
||||
function PDFReader:clearcache()
|
||||
self.cache = {}
|
||||
end
|
||||
|
||||
-- open a PDF file and its settings store
|
||||
function PDFReader:open(filename, password)
|
||||
if self.doc ~= nil then
|
||||
self.doc:close()
|
||||
end
|
||||
if self.settings ~= nil then
|
||||
self.settings:close()
|
||||
end
|
||||
|
||||
self.doc = pdf.openDocument(filename, password or "")
|
||||
if self.doc ~= nil then
|
||||
self.settings = DocSettings:open(filename)
|
||||
self:clearcache()
|
||||
end
|
||||
end
|
||||
|
||||
-- set viewer state according to zoom state
|
||||
function PDFReader:setzoom(page)
|
||||
local dc = pdf.newDC()
|
||||
local pwidth, pheight = page:getSize(self.nulldc)
|
||||
|
||||
if self.globalzoommode == self.ZOOM_FIT_TO_PAGE then
|
||||
self.globalzoom = width / pwidth ----------
|
||||
self.offset_x = 0
|
||||
self.offset_y = (height - (self.globalzoom * pheight)) / 2
|
||||
if height / pheight < self.globalzoom then
|
||||
self.globalzoom = height / pheight
|
||||
self.offset_x = (width - (self.globalzoom * pwidth)) / 2
|
||||
self.offset_y = 0
|
||||
end
|
||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_PAGE_WIDTH then
|
||||
self.globalzoom = width / pwidth
|
||||
self.offset_x = 0
|
||||
self.offset_y = (height - (globalzoom * pheight)) / 2
|
||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_PAGE_HEIGHT then
|
||||
self.globalzoom = height / pheight
|
||||
self.offset_x = (width - (self.globalzoom * pwidth)) / 2
|
||||
self.offset_y = 0
|
||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_CONTENT then
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
self.globalzoom = width / (x1 - x0)
|
||||
self.offset_x = -1 * x0 * globalzoom
|
||||
self.offset_y = -1 * y0 * self.globalzoom + (height - (self.globalzoom * (y1 - y0))) / 2
|
||||
if height / (y1 - y0) < self.globalzoom then
|
||||
self.globalzoom = height / (y1 - y0)
|
||||
self.offset_x = -1 * x0 * self.globalzoom + (width - (self.globalzoom * (x1 - x0))) / 2
|
||||
self.offset_y = -1 * y0 * self.globalzoom
|
||||
end
|
||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_CONTENT_WIDTH then
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
self.globalzoom = width / (x1 - x0)
|
||||
self.offset_x = -1 * x0 * self.globalzoom
|
||||
self.offset_y = -1 * y0 * self.globalzoom + (height - (self.globalzoom * (y1 - y0))) / 2
|
||||
elseif self.globalzoommode == self.ZOOM_FIT_TO_CONTENT_HEIGHT then
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
self.globalzoom = height / (y1 - y0)
|
||||
self.offset_x = -1 * x0 * self.globalzoom + (width - (self.globalzoom * (x1 - x0))) / 2
|
||||
self.offset_y = -1 * y0 * self.globalzoom
|
||||
end
|
||||
dc:setZoom(self.globalzoom)
|
||||
dc:setOffset(self.offset_x, self.offset_y)
|
||||
self.fullwidth, self.fullheight = page:getSize(dc)
|
||||
|
||||
-- set gamma here, we don't have any other good place for this right now:
|
||||
if self.globalgamma ~= self.GAMMA_NO_GAMMA then
|
||||
print("gamma correction: "..self.globalgamma)
|
||||
dc:setGamma(self.globalgamma)
|
||||
end
|
||||
return dc
|
||||
end
|
||||
|
||||
-- render and blit a page
|
||||
function PDFReader:show(no)
|
||||
local slot
|
||||
if self.globalzoommode ~= self.ZOOM_BY_VALUE then
|
||||
slot = self:draworcache(no,self.globalzoommode,self.offset_x,self.offset_y,width,height,self.globalgamma)
|
||||
else
|
||||
slot = self:draworcache(no,self.globalzoom,self.offset_x,self.offset_y,width,height,self.globalgamma)
|
||||
end
|
||||
fb.bb:blitFullFrom(self.cache[slot].bb)
|
||||
if self.rcount == self.rcountmax then
|
||||
print("full refresh")
|
||||
self.rcount = 1
|
||||
fb:refresh(0)
|
||||
else
|
||||
print("partial refresh")
|
||||
self.rcount = self.rcount + 1
|
||||
fb:refresh(1)
|
||||
end
|
||||
self.slot_visible = slot;
|
||||
end
|
||||
|
||||
-- change current page and cache next page after rendering
|
||||
function PDFReader:goto(no)
|
||||
if no < 1 or no > self.doc:getPages() then
|
||||
return
|
||||
end
|
||||
self.pageno = no
|
||||
self:show(no)
|
||||
if no < self.doc:getPages() then
|
||||
if self.globalzoommode ~= self.ZOOM_BY_VALUE then
|
||||
-- pre-cache next page
|
||||
self:draworcache(no+1,self.globalzoommode,self.offset_x,self.offset_y,width,height,self.globalgamma)
|
||||
else
|
||||
self:draworcache(no,self.globalzoom,self.offset_x,self.offset_y,width,height,self.globalgamma)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- adjust global gamma setting
|
||||
function PDFReader:modify_gamma(factor)
|
||||
print("modify_gamma, gamma="..self.globalgamma.." factor="..self.factor)
|
||||
self.globalgamma = self.globalgamma * self.factor;
|
||||
self:goto(self.pageno)
|
||||
end
|
||||
|
||||
-- adjust zoom state and trigger re-rendering
|
||||
function PDFReader:setglobalzoommode(newzoommode)
|
||||
if self.globalzoommode ~= newzoommode then
|
||||
self.globalzoommode = newzoommode
|
||||
self:goto(self.pageno)
|
||||
end
|
||||
end
|
||||
|
||||
-- adjust zoom state and trigger re-rendering
|
||||
function PDFReader:setglobalzoom(zoom)
|
||||
if self.globalzoom ~= zoom then
|
||||
self.globalzoommode = self.ZOOM_BY_VALUE
|
||||
self.globalzoom = zoom
|
||||
self:goto(self.pageno)
|
||||
end
|
||||
end
|
||||
|
||||
-- wait for input and handle it
|
||||
function PDFReader:inputloop()
|
||||
while 1 do
|
||||
local ev = input.waitForEvent()
|
||||
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
|
||||
local secs, usecs = util.gettime()
|
||||
if ev.code == KEY_SHIFT then
|
||||
self.shiftmode = true
|
||||
elseif ev.code == KEY_ALT then
|
||||
self.altmode = true
|
||||
elseif ev.code == KEY_PGFWD then
|
||||
if self.shiftmode then
|
||||
self:setglobalzoom(self.globalzoom*1.2)
|
||||
elseif altmode then
|
||||
self:setglobalzoom(self.globalzoom*1.1)
|
||||
else
|
||||
self:goto(self.pageno + 1)
|
||||
end
|
||||
elseif ev.code == KEY_PGBCK then
|
||||
if self.shiftmode then
|
||||
self:setglobalzoom(self.globalzoom*0.8)
|
||||
elseif altmode then
|
||||
self:setglobalzoom(self.globalzoom*0.9)
|
||||
else
|
||||
self:goto(self.pageno - 1)
|
||||
end
|
||||
elseif ev.code == KEY_BACK then
|
||||
settings.savesetting("last_page", self.pageno)
|
||||
settings:close()
|
||||
return
|
||||
elseif ev.code == KEY_VPLUS then
|
||||
self:modify_gamma( 1.25 )
|
||||
elseif ev.code == KEY_VMINUS then
|
||||
self:modify_gamma( 0.8 )
|
||||
elseif ev.code == KEY_A then
|
||||
if self.shiftmode then
|
||||
self:setglobalzoommode(self.ZOOM_FIT_TO_CONTENT)
|
||||
else
|
||||
self:setglobalzoommode(self.ZOOM_FIT_TO_PAGE)
|
||||
end
|
||||
elseif ev.code == KEY_S then
|
||||
if self.shiftmode then
|
||||
self:setglobalzoommode(self.ZOOM_FIT_TO_CONTENT_WIDTH)
|
||||
else
|
||||
self:setglobalzoommode(self.ZOOM_FIT_TO_PAGE_WIDTH)
|
||||
end
|
||||
elseif ev.code == KEY_D then
|
||||
if self.shiftmode then
|
||||
self:setglobalzoommode(self.ZOOM_FIT_TO_CONTENT_HEIGHT)
|
||||
else
|
||||
self:setglobalzoommode(self.ZOOM_FIT_TO_PAGE_HEIGHT)
|
||||
end
|
||||
end
|
||||
|
||||
if self.globalzoommode == self.ZOOM_BY_VALUE then
|
||||
local x
|
||||
local y
|
||||
|
||||
if self.shiftmode then -- shift always moves in small steps
|
||||
x = self.shift_x / 2
|
||||
y = self.shift_y / 2
|
||||
elseif self.altmode then
|
||||
x = self.shift_x / 5
|
||||
y = self.shift_y / 5
|
||||
elseif self.pan_by_page then
|
||||
x = self.width - 5; -- small overlap when moving by page
|
||||
y = self.height - 5;
|
||||
else
|
||||
x = self.shift_x
|
||||
y = self.shift_y
|
||||
end
|
||||
|
||||
print("offset "..self.offset_x.."*"..self.offset_x.." shift "..x.."*"..y.." globalzoom="..self.globalzoom)
|
||||
|
||||
if ev.code == KEY_FW_LEFT then
|
||||
self.offset_x = self.offset_x + x
|
||||
self:goto(self.pageno)
|
||||
elseif ev.code == KEY_FW_RIGHT then
|
||||
self.offset_x = self.offset_x - x
|
||||
self:goto(self.pageno)
|
||||
elseif ev.code == KEY_FW_UP then
|
||||
self.offset_y = self.offset_y + y
|
||||
self:goto(self.pageno)
|
||||
elseif ev.code == KEY_FW_DOWN then
|
||||
self.offset_y = self.offset_y - y
|
||||
self:goto(self.pageno)
|
||||
elseif ev.code == KEY_FW_PRESS then
|
||||
if self.shiftmode then
|
||||
self.offset_x = 0
|
||||
self.offset_y = 0
|
||||
self:goto(pageno)
|
||||
else
|
||||
self.pan_by_page = not self.pan_by_page
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local nsecs, nusecs = util.gettime()
|
||||
local dur = (nsecs - secs) * 1000000 + nusecs - usecs
|
||||
print("E: T="..ev.type.." V="..ev.value.." C="..ev.code.." DUR="..dur)
|
||||
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE and ev.code == KEY_SHIFT then
|
||||
self.shiftmode = false
|
||||
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE and ev.code == KEY_ALT then
|
||||
self.altmode = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
261
reader.lua
261
reader.lua
@@ -18,19 +18,7 @@
|
||||
]]--
|
||||
|
||||
require "alt_getopt"
|
||||
require "keys"
|
||||
require "tilecache"
|
||||
require "settings"
|
||||
|
||||
ZOOM_BY_VALUE = 0
|
||||
ZOOM_FIT_TO_PAGE = -1
|
||||
ZOOM_FIT_TO_PAGE_WIDTH = -2
|
||||
ZOOM_FIT_TO_PAGE_HEIGHT = -3
|
||||
ZOOM_FIT_TO_CONTENT = -4
|
||||
ZOOM_FIT_TO_CONTENT_WIDTH = -5
|
||||
ZOOM_FIT_TO_CONTENT_HEIGHT = -6
|
||||
|
||||
GAMMA_NO_GAMMA = 1.0
|
||||
require "pdfreader"
|
||||
|
||||
-- option parsing:
|
||||
longopts = {
|
||||
@@ -58,25 +46,6 @@ if optarg["h"] or ARGV[optind] == nil then
|
||||
return
|
||||
end
|
||||
|
||||
rcount = 5
|
||||
rcountmax = 5
|
||||
|
||||
globalzoom = 1.0
|
||||
globalzoommode = ZOOM_FIT_TO_PAGE
|
||||
globalgamma = GAMMA_NO_GAMMA
|
||||
|
||||
fullwidth = 0
|
||||
fullheight = 0
|
||||
offset_x = 0
|
||||
offset_y = 0
|
||||
|
||||
shift_x = 100
|
||||
shift_y = 50
|
||||
pan_by_page = false -- using shift_[xy] or width/height
|
||||
|
||||
shiftmode = false
|
||||
altmode = false
|
||||
|
||||
if optarg["d"] == "k3" then
|
||||
-- for now, the only difference is the additional input device
|
||||
input.open("/dev/input/event0")
|
||||
@@ -96,232 +65,10 @@ if optarg["G"] ~= nil then
|
||||
globalgamma = optarg["G"]
|
||||
end
|
||||
|
||||
doc = pdf.openDocument(ARGV[optind], optarg["p"] or "")
|
||||
settings = DocSettings:open(ARGV[optind])
|
||||
|
||||
print("pdf has "..doc:getPages().." pages.")
|
||||
|
||||
fb = einkfb.open("/dev/fb0")
|
||||
width, height = fb:getSize()
|
||||
|
||||
nulldc = pdf.newDC()
|
||||
PDFReader:open(ARGV[optind], optarg["p"])
|
||||
PDFReader:goto(tonumber(optarg["g"]) or tonumber(PDFReader.settings:readsetting("last_page") or 1))
|
||||
PDFReader:inputloop()
|
||||
|
||||
function setzoom(page, cacheslot)
|
||||
local dc = pdf.newDC()
|
||||
local pwidth, pheight = page:getSize(nulldc)
|
||||
|
||||
if globalzoommode == ZOOM_FIT_TO_PAGE then
|
||||
globalzoom = width / pwidth
|
||||
offset_x = 0
|
||||
offset_y = (height - (globalzoom * pheight)) / 2
|
||||
if height / pheight < globalzoom then
|
||||
globalzoom = height / pheight
|
||||
offset_x = (width - (globalzoom * pwidth)) / 2
|
||||
offset_y = 0
|
||||
end
|
||||
elseif globalzoommode == ZOOM_FIT_TO_PAGE_WIDTH then
|
||||
globalzoom = width / pwidth
|
||||
offset_x = 0
|
||||
offset_y = (height - (globalzoom * pheight)) / 2
|
||||
elseif globalzoommode == ZOOM_FIT_TO_PAGE_HEIGHT then
|
||||
globalzoom = height / pheight
|
||||
offset_x = (width - (globalzoom * pwidth)) / 2
|
||||
offset_y = 0
|
||||
elseif globalzoommode == ZOOM_FIT_TO_CONTENT then
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
globalzoom = width / (x1 - x0)
|
||||
offset_x = -1 * x0 * globalzoom
|
||||
offset_y = -1 * y0 * globalzoom + (height - (globalzoom * (y1 - y0))) / 2
|
||||
if height / (y1 - y0) < globalzoom then
|
||||
globalzoom = height / (y1 - y0)
|
||||
offset_x = -1 * x0 * globalzoom + (width - (globalzoom * (x1 - x0))) / 2
|
||||
offset_y = -1 * y0 * globalzoom
|
||||
end
|
||||
elseif globalzoommode == ZOOM_FIT_TO_CONTENT_WIDTH then
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
globalzoom = width / (x1 - x0)
|
||||
offset_x = -1 * x0 * globalzoom
|
||||
offset_y = -1 * y0 * globalzoom + (height - (globalzoom * (y1 - y0))) / 2
|
||||
elseif globalzoommode == ZOOM_FIT_TO_CONTENT_HEIGHT then
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
globalzoom = height / (y1 - y0)
|
||||
offset_x = -1 * x0 * globalzoom + (width - (globalzoom * (x1 - x0))) / 2
|
||||
offset_y = -1 * y0 * globalzoom
|
||||
end
|
||||
dc:setZoom(globalzoom)
|
||||
dc:setOffset(offset_x, offset_y)
|
||||
fullwidth, fullheight = page:getSize(dc)
|
||||
|
||||
-- set gamma here, we don't have any other good place for this right now:
|
||||
if globalgamma ~= GAMMA_NO_GAMMA then
|
||||
print("gamma correction: "..globalgamma)
|
||||
dc:setGamma(globalgamma)
|
||||
end
|
||||
return dc
|
||||
end
|
||||
|
||||
function show(no)
|
||||
local slot
|
||||
if globalzoommode ~= ZOOM_BY_VALUE then
|
||||
slot = draworcache(no,globalzoommode,offset_x,offset_y,width,height,globalgamma)
|
||||
else
|
||||
slot = draworcache(no,globalzoom,offset_x,offset_y,width,height,globalgamma)
|
||||
end
|
||||
fb.bb:blitFullFrom(cache[slot].bb)
|
||||
if rcount == rcountmax then
|
||||
print("full refresh")
|
||||
rcount = 1
|
||||
fb:refresh(0)
|
||||
else
|
||||
print("partial refresh")
|
||||
rcount = rcount + 1
|
||||
fb:refresh(1)
|
||||
end
|
||||
slot_visible = slot;
|
||||
end
|
||||
|
||||
function goto(no)
|
||||
if no < 1 or no > doc:getPages() then
|
||||
return
|
||||
end
|
||||
pageno = no
|
||||
show(no)
|
||||
if no < doc:getPages() then
|
||||
-- always pre-cache next page
|
||||
if globalzoommode ~= ZOOM_BY_VALUE then
|
||||
draworcache(no,globalzoommode,offset_x,offset_y,width,height,globalgamma)
|
||||
else
|
||||
draworcache(no,globalzoom,offset_x,offset_y,width,height,globalgamma)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function modify_gamma(factor)
|
||||
print("modify_gamma, gamma="..globalgamma.." factor="..factor)
|
||||
globalgamma = globalgamma * factor;
|
||||
goto(pageno)
|
||||
end
|
||||
function setglobalzoommode(newzoommode)
|
||||
if globalzoommode ~= newzoommode then
|
||||
globalzoommode = newzoommode
|
||||
goto(pageno)
|
||||
end
|
||||
end
|
||||
function setglobalzoom(zoom)
|
||||
if globalzoom ~= zoom then
|
||||
globalzoommode = ZOOM_BY_VALUE
|
||||
globalzoom = zoom
|
||||
goto(pageno)
|
||||
end
|
||||
end
|
||||
|
||||
function mainloop()
|
||||
while 1 do
|
||||
local ev = input.waitForEvent()
|
||||
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
|
||||
local secs, usecs = util.gettime()
|
||||
if ev.code == KEY_SHIFT then
|
||||
shiftmode = true
|
||||
elseif ev.code == KEY_ALT then
|
||||
altmode = true
|
||||
elseif ev.code == KEY_PGFWD then
|
||||
if shiftmode then
|
||||
setglobalzoom(globalzoom*1.2)
|
||||
elseif altmode then
|
||||
setglobalzoom(globalzoom*1.1)
|
||||
else
|
||||
goto(pageno + 1)
|
||||
end
|
||||
elseif ev.code == KEY_PGBCK then
|
||||
if shiftmode then
|
||||
setglobalzoom(globalzoom*0.8)
|
||||
elseif altmode then
|
||||
setglobalzoom(globalzoom*0.9)
|
||||
else
|
||||
goto(pageno - 1)
|
||||
end
|
||||
elseif ev.code == KEY_BACK then
|
||||
settings.savesetting("last_page", pageno)
|
||||
settings:close()
|
||||
return
|
||||
elseif ev.code == KEY_VPLUS then
|
||||
modify_gamma( 1.25 )
|
||||
elseif ev.code == KEY_VMINUS then
|
||||
modify_gamma( 0.8 )
|
||||
elseif ev.code == KEY_A then
|
||||
if shiftmode then
|
||||
setglobalzoommode(ZOOM_FIT_TO_CONTENT)
|
||||
else
|
||||
setglobalzoommode(ZOOM_FIT_TO_PAGE)
|
||||
end
|
||||
elseif ev.code == KEY_S then
|
||||
if shiftmode then
|
||||
setglobalzoommode(ZOOM_FIT_TO_CONTENT_WIDTH)
|
||||
else
|
||||
setglobalzoommode(ZOOM_FIT_TO_PAGE_WIDTH)
|
||||
end
|
||||
elseif ev.code == KEY_D then
|
||||
if shiftmode then
|
||||
setglobalzoommode(ZOOM_FIT_TO_CONTENT_HEIGHT)
|
||||
else
|
||||
setglobalzoommode(ZOOM_FIT_TO_PAGE_HEIGHT)
|
||||
end
|
||||
end
|
||||
|
||||
if globalzoommode == ZOOM_BY_VALUE then
|
||||
local x
|
||||
local y
|
||||
|
||||
if shiftmode then -- shift always moves in small steps
|
||||
x = shift_x / 2
|
||||
y = shift_y / 2
|
||||
elseif altmode then
|
||||
x = shift_x / 5
|
||||
y = shift_y / 5
|
||||
elseif pan_by_page then
|
||||
x = width - 5; -- small overlap when moving by page
|
||||
y = height - 5;
|
||||
else
|
||||
x = shift_x
|
||||
y = shift_y
|
||||
end
|
||||
|
||||
print("offset "..offset_x.."*"..offset_x.." shift "..x.."*"..y.." globalzoom="..globalzoom)
|
||||
|
||||
if ev.code == KEY_FW_LEFT then
|
||||
offset_x = offset_x + x
|
||||
goto(pageno)
|
||||
elseif ev.code == KEY_FW_RIGHT then
|
||||
offset_x = offset_x - x
|
||||
goto(pageno)
|
||||
elseif ev.code == KEY_FW_UP then
|
||||
offset_y = offset_y + y
|
||||
goto(pageno)
|
||||
elseif ev.code == KEY_FW_DOWN then
|
||||
offset_y = offset_y - y
|
||||
goto(pageno)
|
||||
elseif ev.code == KEY_FW_PRESS then
|
||||
if shiftmode then
|
||||
offset_x = 0
|
||||
offset_y = 0
|
||||
goto(pageno)
|
||||
else
|
||||
pan_by_page = not pan_by_page
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local nsecs, nusecs = util.gettime()
|
||||
local dur = (nsecs - secs) * 1000000 + nusecs - usecs
|
||||
print("E: T="..ev.type.." V="..ev.value.." C="..ev.code.." DUR="..dur)
|
||||
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE and ev.code == KEY_SHIFT then
|
||||
shiftmode = false
|
||||
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE and ev.code == KEY_ALT then
|
||||
altmode = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
goto(tonumber(optarg["g"]) or tonumber(settings:readsetting("last_page") or 1))
|
||||
|
||||
mainloop()
|
||||
|
||||
@@ -49,17 +49,19 @@ function renderUtf8Text(buffer, x, y, face, facehash, text, kerning)
|
||||
local pen_x = 0
|
||||
local prevcharcode = 0
|
||||
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
|
||||
local charcode = util.utf8charcode(uchar)
|
||||
local glyph = getglyph(face, facehash, charcode)
|
||||
if kerning and prevcharcode then
|
||||
local kern = face:getKerning(prevcharcode, charcode)
|
||||
pen_x = pen_x + kern
|
||||
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
|
||||
else
|
||||
buffer:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
|
||||
if pen_x < buffer:getWidth() then
|
||||
local charcode = util.utf8charcode(uchar)
|
||||
local glyph = getglyph(face, facehash, charcode)
|
||||
if kerning and prevcharcode then
|
||||
local kern = face:getKerning(prevcharcode, charcode)
|
||||
pen_x = pen_x + kern
|
||||
buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
|
||||
else
|
||||
buffer:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight())
|
||||
end
|
||||
pen_x = pen_x + glyph.ax
|
||||
prevcharcode = charcode
|
||||
end
|
||||
pen_x = pen_x + glyph.ax
|
||||
prevcharcode = charcode
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require "rendertext"
|
||||
require "graphics"
|
||||
|
||||
fb = einkfb.open("/dev/fb0")
|
||||
width, height = fb:getSize()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
DocSettings = {}
|
||||
DocSettings_mt = { __index = DocSettings }
|
||||
|
||||
function DocSettings:open(docfile)
|
||||
local new = {}
|
||||
@@ -9,7 +8,7 @@ function DocSettings:open(docfile)
|
||||
new.stmt_readsetting = new.docdb:prepare("SELECT value FROM settings WHERE key = ?;")
|
||||
new.stmt_savesetting = new.docdb:prepare("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?);")
|
||||
end
|
||||
return setmetatable(new, DocSettings_mt)
|
||||
return setmetatable(new, { __index = DocSettings})
|
||||
end
|
||||
|
||||
function DocSettings:readsetting(key)
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
--[[
|
||||
a cache for rendered tiles
|
||||
]]--
|
||||
cache_max_memsize = 1024*1024*5 -- 5MB tile cache
|
||||
cache_current_memsize = 0
|
||||
cache = {}
|
||||
cache_max_age = 20
|
||||
function cacheclaim(size)
|
||||
if(size > cache_max_memsize) then
|
||||
error("too much memory claimed")
|
||||
return false
|
||||
end
|
||||
repeat
|
||||
for k, _ in pairs(cache) do
|
||||
if cache[k].age > 0 then
|
||||
print("aging slot="..k)
|
||||
cache[k].age = cache[k].age - 1
|
||||
else
|
||||
cache_current_memsize = cache_current_memsize - cache[k].size
|
||||
cache[k] = nil
|
||||
end
|
||||
end
|
||||
until cache_current_memsize + size <= cache_max_memsize
|
||||
cache_current_memsize = cache_current_memsize + size
|
||||
print("cleaned cache to fit new tile (size="..size..")")
|
||||
return true
|
||||
end
|
||||
function draworcache(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||
local hash = cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||
if cache[hash] == nil then
|
||||
cacheclaim(width * height / 2);
|
||||
cache[hash] = {
|
||||
age = cache_max_age,
|
||||
size = width * height / 2,
|
||||
bb = blitbuffer.new(width, height)
|
||||
}
|
||||
print("drawing page="..no.." to slot="..hash)
|
||||
local page = doc:openPage(no)
|
||||
local dc = setzoom(page, hash)
|
||||
page:draw(dc, cache[hash].bb, 0, 0)
|
||||
page:close()
|
||||
end
|
||||
return hash
|
||||
end
|
||||
function cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||
return no..'_'..zoom..'_'..offset_x..','..offset_y..'-'..width..'x'..height..'_'..gamma;
|
||||
end
|
||||
function clearcache()
|
||||
cache = {}
|
||||
end
|
||||
Reference in New Issue
Block a user