Merge pull request #188 from houqp/new_ui_code

djvu and crengine support for readerui
pull/2/merge
{Qingping,Dave} Hou 12 years ago
commit 4182297a15

@ -385,10 +385,9 @@ static int cursorRight(lua_State *L) {
return 0;
}
static int drawCurrentPage(lua_State *L) {
static int drawCurrentView(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 2, "blitbuffer");
int w = bb->w,
h = bb->h;
@ -460,7 +459,7 @@ static const struct luaL_Reg credocument_meth[] = {
{"toggleFontBolder", toggleFontBolder},
//{"cursorLeft", cursorLeft},
//{"cursorRight", cursorRight},
{"drawCurrentPage", drawCurrentPage},
{"drawCurrentView", drawCurrentView},
{"close", closeDocument},
{"__gc", closeDocument},
{NULL, NULL}

@ -26,7 +26,6 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/*@TODO check all the close method, ensure memories are freed 03.03 2012*/
typedef struct DjvuDocument {
ddjvu_context_t *context;
@ -363,6 +362,16 @@ static int closePage(lua_State *L) {
return 0;
}
/* draw part of the page to bb.
*
* @page: DjvuPage user data
* @dc: DrawContext user data
* @bb: BlitBuffer user data
* @x: x offset within zoomed page
* @y: y offset within zoomed page
*
* width and height for the visible_area is obtained from bb.
*/
static int drawPage(lua_State *L) {
DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
@ -382,18 +391,12 @@ static int drawPage(lua_State *L) {
ddjvu_format_set_gamma(pixelformat, dc->gamma);
/*ddjvu_format_set_ditherbits(dc->pixelformat, 2);*/
/*printf("@page %d, @@zoom:%f, offset: (%d, %d)\n", page->num, dc->zoom, dc->offset_x, dc->offset_y);*/
/* render full page into rectangle specified by pagerect */
/*pagerect.x = luaL_checkint(L, 4);*/
/*pagerect.y = luaL_checkint(L, 5);*/
pagerect.x = 0;
pagerect.y = 0;
pagerect.w = page->info.width * dc->zoom;
pagerect.h = page->info.height * dc->zoom;
/*printf("--pagerect--- (x: %d, y: %d), w: %d, h: %d.\n", 0, 0, pagerect.w, pagerect.h);*/
/* copy pixels area from pagerect specified by renderrect.
@ -405,12 +408,13 @@ static int drawPage(lua_State *L) {
* and up. So we need to handle positive offset manually when copying
* imagebuffer to blitbuffer (framebuffer).
*/
renderrect.x = MAX(-dc->offset_x, 0);
renderrect.y = MAX(-dc->offset_y, 0);
renderrect.x = luaL_checkint(L, 4);
renderrect.y = luaL_checkint(L, 5);
/*renderrect.x = MAX(-dc->offset_x, 0);*/
/*renderrect.y = MAX(-dc->offset_y, 0);*/
renderrect.w = MIN(pagerect.w - renderrect.x, bb->w);
renderrect.h = MIN(pagerect.h - renderrect.y, bb->h);
/*printf("--renderrect--- (%d, %d), w:%d, h:%d\n", renderrect.x, renderrect.y, renderrect.w, renderrect.h);*/
/* ddjvulibre library only supports rotation of 0, 90, 180 and 270 degrees.
* This four kinds of rotations can already be achieved by native system.

@ -0,0 +1,79 @@
require "cache"
require "ui/geometry"
CreDocument = Document:new{
_document = false,
line_space_percent = 100,
--dc_null = DrawContext.new()
}
-- NuPogodi, 20.05.12: inspect the zipfile content
function CreDocument:zipContentExt(fname)
local outfile = "./data/zip_content"
local s = ""
os.execute("unzip ".."-l \""..fname.."\" > "..outfile)
local i = 1
if io.open(outfile,"r") then
for lines in io.lines(outfile) do
if i == 4 then s = lines break else i = i + 1 end
end
end
-- return the extention
return string.lower(string.match(s, ".+%.([^.]+)"))
end
function CreDocument:init()
-- we need to initialize the CRE font list
local fonts = Font:getFontList()
for _k, _v in ipairs(fonts) do
local ok, err = pcall(cre.registerFont, Font.fontdir..'/'.._v)
if not ok then
DEBUG(err)
end
end
--local default_font = G_reader_settings:readSetting("cre_font")
--if default_font then
--self.default_font = default_font
--end
local ok
local file_type = string.lower(string.match(self.file, ".+%.([^.]+)"))
if file_type == "zip" then
-- NuPogodi, 20.05.12: read the content of zip-file
-- and return extention of the 1st file
file_type = self:zipContentExt(filename)
end
-- these two format use the same css file
if file_type == "html" then
file_type = "htm"
end
-- if native css-file doesn't exist, one needs to use default cr3.css
if not io.open("./data/"..file_type..".css") then
file_type = "cr3"
end
local style_sheet = "./data/"..file_type..".css"
ok, self._document = pcall(cre.openDocument, self.file, style_sheet,
G_width, G_height)
if not ok then
self.error_message = self.doc -- will contain error message
return
end
self.is_open = true
self.info.has_pages = false
self:_readMetadata()
self._document:setDefaultInterlineSpace(self.line_space_percent)
end
function CreDocument:hintPage(pageno, zoom, rotation)
end
function CreDocument:drawPage(target, x, y, rect, pageno, zoom, rotation)
end
function CreDocument:renderPage(pageno, rect, zoom, rotation)
end
DocumentRegistry:addProvider("txt", "application/txt", CreDocument)

@ -0,0 +1,40 @@
require "cache"
require "ui/geometry"
DjvuDocument = Document:new{
_document = false,
-- libdjvulibre manages its own additional cache, default value is hard written in c module.
djvulibre_cache_size = nil,
dc_null = DrawContext.new()
}
function DjvuDocument:init()
local ok
ok, self._document = pcall(djvu.openDocument, self.file, self.djvulibre_cache_size)
if not ok then
self.error_message = self.doc -- will contain error message
return
end
self.is_open = true
self.info.has_pages = true
self:_readMetadata()
end
function DjvuDocument:getUsedBBox(pageno)
-- djvu does not support usedbbox, so fake it.
local used = {}
used.x, used.y, used.w, used.h = 0.01, 0.01, -0.01, -0.01
return used
end
function DjvuDocument:invertTextYAxel(pageno, text_table)
local _, height = self.doc:getOriginalPageSize(pageno)
for _,text in pairs(text_table) do
for _,line in ipairs(text) do
line.y0, line.y1 = (height - line.y1), (height - line.y0)
end
end
return text_table
end
DocumentRegistry:addProvider("djvu", "application/djvu", DjvuDocument)

@ -73,11 +73,34 @@ end
-- this might be overridden by a document implementation
function Document:close()
if self.is_open then
self.is_open = false
self._document:close()
end
end
-- this might be overridden by a document implementation
function Document:getNativePageDimensions(pageno)
return Geom:new{w=0, h=0}
local hash = "pgdim|"..self.file.."|"..pageno
local cached = Cache:check(hash)
if cached then
return cached[1]
end
local page = self._document:openPage(pageno)
local page_size_w, page_size_h = page:getSize(self.dc_null)
local page_size = Geom:new{ w = page_size_w, h = page_size_h }
Cache:insert(hash, CacheItem:new{ page_size })
page:close()
return page_size
end
function Document:_readMetadata()
if self.info.has_pages then
self.info.number_of_pages = self._document:getPages()
else
self.info.length = self._document:getFullHeight()
end
return true
end
-- calculates page dimensions
@ -96,6 +119,96 @@ function Document:getToc()
return self._document:getToc()
end
function Document:renderPage(pageno, rect, zoom, rotation)
local hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation
local page_size = self:getPageDimensions(pageno, zoom, rotation)
-- this will be the size we actually render
local size = page_size
-- we prefer to render the full page, if it fits into cache
if not Cache:willAccept(size.w * size.h / 2) then
-- whole page won't fit into cache
DEBUG("rendering only part of the page")
-- TODO: figure out how to better segment the page
if not rect then
DEBUG("aborting, since we do not have a specification for that part")
-- required part not given, so abort
return
end
-- only render required part
hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..tostring(rect)
size = rect
end
-- prepare cache item with contained blitbuffer
local tile = CacheItem:new{
size = size.w * size.h / 2 + 64, -- estimation
excerpt = size,
pageno = pageno,
bb = Blitbuffer.new(size.w, size.h)
}
-- create a draw context
local dc = DrawContext.new()
dc:setRotate(rotation)
-- correction of rotation
if rotation == 90 then
dc:setOffset(page_size.w, 0)
elseif rotation == 180 then
dc:setOffset(page_size.w, page_size.h)
elseif rotation == 270 then
dc:setOffset(0, page_size.h)
end
dc:setZoom(zoom)
-- render
local page = self._document:openPage(pageno)
page:draw(dc, tile.bb, size.x, size.y)
page:close()
Cache:insert(hash, tile)
return tile
end
-- a hint for the cache engine to paint a full page to the cache
-- TODO: this should trigger a background operation
function Document:hintPage(pageno, zoom, rotation)
self:renderPage(pageno, nil, zoom, rotation)
end
function Document:drawPage(target, x, y, rect, pageno, zoom, rotation)
local hash_full_page = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation
local hash_excerpt = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..tostring(rect)
local tile = Cache:check(hash_full_page)
if not tile then
tile = Cache:check(hash_excerpt)
if not tile then
DEBUG("rendering")
tile = self:renderPage(pageno, rect, zoom, rotation)
end
end
DEBUG("now painting", tile)
target:blitFrom(tile.bb, x, y, rect.x - tile.excerpt.x, rect.y - tile.excerpt.y, rect.w, rect.h)
end
function Document:drawCurrentView(target, x, y, rect, pos)
self._document:gotoPos(pos)
tile_bb = Blitbuffer.new(rect.w, rect.h)
self._document:drawCurrentView(tile_bb)
target:blitFrom(tile_bb, x, y, 0, 0, rect.w, rect.h)
end
function Document:getPageText(pageno)
-- is this worth caching? not done yet.
local page = self._document:openPage(pageno)
local text = page:getPageText()
page:close()
return text
end
-- load implementations:
require "document/pdfdocument"
require "document/djvudocument"
require "document/credocument"

@ -33,32 +33,6 @@ function PdfDocument:unlock(password)
return self:_readMetadata()
end
function PdfDocument:_readMetadata()
self.info.number_of_pages = self._document:getPages()
return true
end
function PdfDocument:close()
if self.is_open then
self.is_open = false
self._document:close()
end
end
function PdfDocument:getNativePageDimensions(pageno)
local hash = "pgdim|"..self.file.."|"..pageno
local cached = Cache:check(hash)
if cached then
return cached[1]
end
local page = self._document:openPage(pageno)
local page_size_w, page_size_h = page:getSize(self.dc_null)
local page_size = Geom:new{ w = page_size_w, h = page_size_h }
Cache:insert(hash, CacheItem:new{ page_size })
page:close()
return page_size
end
function PdfDocument:getUsedBBox(pageno)
local hash = "pgubbox|"..self.file.."|"..pageno
local cached = Cache:check(hash)
@ -73,84 +47,4 @@ function PdfDocument:getUsedBBox(pageno)
return used
end
function PdfDocument:getPageText(pageno)
-- is this worth caching? not done yet.
local page = self._document:openPage(pageno)
local text = page:getPageText()
page:close()
return text
end
function PdfDocument:renderPage(pageno, rect, zoom, rotation)
local hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation
local page_size = self:getPageDimensions(pageno, zoom, rotation)
-- this will be the size we actually render
local size = page_size
-- we prefer to render the full page, if it fits into cache
if not Cache:willAccept(size.w * size.h / 2) then
-- whole page won't fit into cache
DEBUG("rendering only part of the page")
-- TODO: figure out how to better segment the page
if not rect then
DEBUG("aborting, since we do not have a specification for that part")
-- required part not given, so abort
return
end
-- only render required part
hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..tostring(rect)
size = rect
end
-- prepare cache item with contained blitbuffer
local tile = CacheItem:new{
size = size.w * size.h / 2 + 64, -- estimation
excerpt = size,
pageno = pageno,
bb = Blitbuffer.new(size.w, size.h)
}
-- create a draw context
local dc = DrawContext.new()
dc:setRotate(rotation)
-- correction of rotation
if rotation == 90 then
dc:setOffset(page_size.w, 0)
elseif rotation == 180 then
dc:setOffset(page_size.w, page_size.h)
elseif rotation == 270 then
dc:setOffset(0, page_size.h)
end
dc:setZoom(zoom)
-- render
local page = self._document:openPage(pageno)
page:draw(dc, tile.bb, size.x, size.y)
page:close()
Cache:insert(hash, tile)
return tile
end
-- a hint for the cache engine to paint a full page to the cache
-- TODO: this should trigger a background operation
function PdfDocument:hintPage(pageno, zoom, rotation)
self:renderPage(pageno, nil, zoom, rotation)
end
function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation)
local hash_full_page = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation
local hash_excerpt = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..tostring(rect)
local tile = Cache:check(hash_full_page)
if not tile then
tile = Cache:check(hash_excerpt)
if not tile then
DEBUG("rendering")
tile = self:renderPage(pageno, rect, zoom, rotation)
end
end
DEBUG("now painting", tile)
target:blitFrom(tile.bb, x, y, rect.x - tile.excerpt.x, rect.y - tile.excerpt.y, rect.w, rect.h)
end
DocumentRegistry:addProvider("pdf", "application/pdf", PdfDocument)

@ -315,8 +315,8 @@ function Input:sequenceToString(sequence)
end
end
if #modifiers then
keystring[0] = table.concat(modifiers, "-")
keystring[1] = "-"
keystring[1] = table.concat(modifiers, "-")
keystring[2] = "-"
end
return table.concat(keystring)
end

@ -1,10 +1,10 @@
ReaderPanning = InputContainer:new{
key_events = {
-- these will all generate the same event, just with different arguments
MoveUp = { {"Up"}, doc = "move focus up", event = "Panning", args = {0, -1} },
MoveDown = { {"Down"}, doc = "move focus down", event = "Panning", args = {0, 1} },
MoveLeft = { {"Left"}, doc = "move focus left", event = "Panning", args = {-1, 0} },
MoveRight = { {"Right"}, doc = "move focus right", event = "Panning", args = {1, 0} },
MoveUp = { {"Up"}, doc = "move visible area up", event = "Panning", args = {0, -1} },
MoveDown = { {"Down"}, doc = "move visible area down", event = "Panning", args = {0, 1} },
MoveLeft = { {"Left"}, doc = "move visible area left", event = "Panning", args = {-1, 0} },
MoveRight = { {"Right"}, doc = "move visible area right", event = "Panning", args = {1, 0} },
},
-- defaults

@ -0,0 +1,64 @@
require "ui/reader/readerpanning"
ReaderRolling = InputContainer:new{
key_events = {
GotoNextView = { {Input.group.PgFwd}, doc = "go to next view", event = "GotoViewRel", args = 1 },
GotoPrevView = { {Input.group.PgBack}, doc = "go to previous view", event = "GotoViewRel", args = -1 },
MoveUp = { {"Up"}, doc = "move view up", event = "Panning", args = {0, -1} },
MoveDown = { {"Down"}, doc = "move view down", event = "Panning", args = {0, 1} },
GotoFirst = { {"1"}, doc = "go to start", event = "GotoPercent", args = 0},
Goto11 = { {"2"}, doc = "go to 11%", event = "GotoPercent", args = 11},
Goto22 = { {"3"}, doc = "go to 22%", event = "GotoPercent", args = 22},
Goto33 = { {"4"}, doc = "go to 33%", event = "GotoPercent", args = 33},
Goto44 = { {"5"}, doc = "go to 44%", event = "GotoPercent", args = 44},
Goto55 = { {"6"}, doc = "go to 55%", event = "GotoPercent", args = 55},
Goto66 = { {"7"}, doc = "go to 66%", event = "GotoPercent", args = 66},
Goto77 = { {"8"}, doc = "go to 77%", event = "GotoPercent", args = 77},
Goto88 = { {"9"}, doc = "go to 88%", event = "GotoPercent", args = 88},
GotoLast = { {"0"}, doc = "go to end", event = "GotoPercent", args = 100},
},
current_pos = 0,
length = nil,
panning_steps = ReaderPanning.panning_steps,
}
function ReaderRolling:init()
self.length = self.ui.document.info.length
end
function ReaderRolling:onPosUpdate(new_pos)
self.current_pos = new_pos
end
function ReaderRolling:gotoPos(new_pos)
if new_pos == self.current_pos then return end
if new_pos < 0 then new_pos = 0 end
if new_pos > self.length then new_pos = self.length end
self.ui:handleEvent(Event:new("PosUpdate", new_pos))
end
function ReaderRolling:onGotoPercent(percent)
DEBUG("goto document offset in percent:", percent)
self:gotoPos(percent * self.length / 10000)
return true
end
function ReaderRolling:onGotoViewRel(diff)
DEBUG("goto relative screen:", diff)
self:gotoPos(self.current_pos + diff * self.ui.dimen.h)
return true
end
function ReaderRolling:onPanning(args, key)
local _, dy = unpack(args)
DEBUG("key =", key)
self:gotoPos(self.current_pos + dy * self.panning_steps.normal)
return true
end
function ReaderRolling:onZoom()
--@TODO re-read length info after font or lineheight changes 05.06 2012 (houqp)
end

@ -3,6 +3,8 @@ ReaderToc = InputContainer:new{
ShowToc = { {"T"}, doc = "show Table of Content menu"},
},
dimen = Geom:new{ w = G_width-20, h = G_height-20},
current_page = 0,
current_pos = 0,
}
function ReaderToc:cleanUpTocTitle(title)
@ -13,6 +15,41 @@ function ReaderToc:onSetDimensions(dimen)
self.dimen = dimen
end
--function ReaderToc:fillToc()
--self.toc = self.doc:getToc()
--end
-- getTocTitleByPage wrapper, so specific reader
-- can tranform pageno according its need
function ReaderToc:getTocTitleByPage(pageno)
return self:_getTocTitleByPage(pageno)
end
function ReaderToc:_getTocTitleByPage(pageno)
if not self.toc then
-- build toc when needed.
self:fillToc()
end
-- no table of content
if #self.toc == 0 then
return ""
end
local pre_entry = self.toc[1]
for _k,_v in ipairs(self.toc) do
if _v.page > pageno then
break
end
pre_entry = _v
end
return self:cleanUpTocTitle(pre_entry.title)
end
function ReaderToc:getTocTitleOfCurrentPage()
return self:getTocTitleByPage(self.pageno)
end
function ReaderToc:onShowToc()
function callback(item)
self.ui:handleEvent(Event:new("PageUpdate", item.page))
@ -34,3 +71,12 @@ function ReaderToc:onShowToc()
UIManager:show(toc_menu)
end
function ReaderToc:onPageUpdate(new_page_no)
self.current_page = new_page_no
end
function ReaderToc:onPosUpdate(new_pos)
self.current_pos = new_pos
end

@ -3,6 +3,7 @@ ReaderView = WidgetContainer:new{
state = {
page = 0,
pos = 0,
zoom = 1.0,
rotation = 0,
offset = {},
@ -32,25 +33,38 @@ function ReaderView:paintTo(bb, x, y)
end
-- draw content
self.ui.document:drawPage(
bb,
x + inner_offset.x,
y + inner_offset.y,
self.visible_area,
self.state.page,
self.state.zoom,
self.state.rotation)
if self.ui.document.info.has_pages then
self.ui.document:drawPage(
bb,
x + inner_offset.x,
y + inner_offset.y,
self.visible_area,
self.state.page,
self.state.zoom,
self.state.rotation)
else
self.ui.document:drawCurrentView(
bb,
x + inner_offset.x,
y + inner_offset.y,
self.visible_area,
self.state.pos)
end
end
function ReaderView:recalculate()
local page_size = self.ui.document:getPageDimensions(self.state.page, self.state.zoom, self.state.rotation)
-- TODO: bbox
self.page_area = page_size
if self.ui.document.info.has_pages then
local page_size = self.ui.document:getPageDimensions(self.state.page, self.state.zoom, self.state.rotation)
-- TODO: bbox
self.page_area = page_size
-- reset our size
self.visible_area:setSizeTo(self.ui.dimen)
-- and recalculate it according to page size
self.visible_area:offsetWithin(self.page_area, 0, 0)
-- reset our size
self.visible_area:setSizeTo(self.ui.dimen)
-- and recalculate it according to page size
self.visible_area:offsetWithin(self.page_area, 0, 0)
else
self.visible_area:setSizeTo(self.ui.dimen)
end
-- flag a repaint
UIManager:setDirty(self.dialog)
end
@ -78,6 +92,11 @@ function ReaderView:onPageUpdate(new_page_no)
self:recalculate()
end
function ReaderView:onPosUpdate(new_pos)
self.state.pos = new_pos
self:recalculate()
end
function ReaderView:ZoomUpdate(zoom)
self.state.zoom = zoom
self:recalculate()

@ -4,6 +4,7 @@ require "ui/reader/readerzooming"
require "ui/reader/readerpanning"
require "ui/reader/readerrotation"
require "ui/reader/readerpaging"
require "ui/reader/readerrolling"
require "ui/reader/readertoc"
--[[
@ -37,32 +38,36 @@ function ReaderUI:init()
dialog = self.dialog,
ui = self
}
-- zooming controller
self[2] = ReaderZooming:new{
dialog = self.dialog,
view = self[1],
ui = self
}
-- panning controller
self[3] = ReaderPanning:new{
dialog = self.dialog,
view = self[1],
ui = self
}
-- rotation controller
self[4] = ReaderRotation:new{
self[2] = ReaderRotation:new{
dialog = self.dialog,
view = self[1],
ui = self
}
-- Toc menu controller
self[5] = ReaderToc:new{
self[3] = ReaderToc:new{
dialog = self.dialog,
view = self[1],
ui = self
}
-- if needed, insert a paging container
if self.document.info.has_pages then
-- for page specific controller
-- zooming controller
local zoomer = ReaderZooming:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, zoomer)
-- panning controller
local panner = ReaderPanning:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, panner)
-- if needed, insert a paging container
local pager = ReaderPaging:new{
dialog = self.dialog,
view = self[1],
@ -70,6 +75,14 @@ function ReaderUI:init()
}
table.insert(self, pager)
pager:gotoPage(1)
else
local roller = ReaderRolling:new{
dialog = self.dialog,
view = self[1],
ui = self
}
table.insert(self, roller)
roller:gotoPos(0)
end
-- notify childs of dimensions
self:handleEvent(Event:new("SetDimensions", self.dimen))

Binary file not shown.

@ -142,6 +142,8 @@ reader = ReaderUI:new{
dialog = readerwindow,
dimen = Geom:new{ w = G_width - 100, h = G_height - 100 },
document = DocumentRegistry:getProvider("test/2col.pdf")
--document = DocumentRegistry:getProvider("test/djvu3spec.djvu")
--document = DocumentRegistry:getProvider("./README.TXT")
}
readerwindow[1][1] = reader

Loading…
Cancel
Save