first demo for credocument

pull/2/merge
Qingping Hou 12 years ago
parent 75ee86b2cb
commit e7d16de4e2

@ -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}

@ -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)

@ -95,7 +95,11 @@ function Document:getNativePageDimensions(pageno)
end
function Document:_readMetadata()
self.info.number_of_pages = self._document:getPages()
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
@ -187,6 +191,13 @@ function Document:drawPage(target, x, y, rect, pageno, zoom, rotation)
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)
@ -200,3 +211,4 @@ end
require "document/pdfdocument"
require "document/djvudocument"
require "document/credocument"

@ -0,0 +1,50 @@
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 },
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,
}
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:onZoom()
--@TODO re-read length info after font or lineheight changes 05.06 2012 (houqp)
end

@ -4,6 +4,7 @@ ReaderToc = InputContainer:new{
},
dimen = Geom:new{ w = G_width-20, h = G_height-20},
current_page = 0,
current_pos = 0,
}
function ReaderToc:cleanUpTocTitle(title)
@ -74,4 +75,8 @@ 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))

Loading…
Cancel
Save