2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/ui/reader/readerview.lua

641 lines
20 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local OverlapGroup = require("ui/widget/overlapgroup")
local ReaderFlipping = require("ui/reader/readerflipping")
local ReaderFooter = require("ui/reader/readerfooter")
local ReaderDogear = require("ui/reader/readerdogear")
2014-02-16 04:32:08 +00:00
local UIManager = require("ui/uimanager")
local Screen = require("ui/screen")
2013-10-18 20:38:07 +00:00
local Geom = require("ui/geometry")
local Event = require("ui/event")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
local ReaderView = OverlapGroup:new{
2014-03-13 13:52:43 +00:00
document = nil,
-- single page state
state = {
page = nil,
pos = 0,
zoom = 1.0,
rotation = 0,
gamma = 1.0,
offset = nil,
bbox = nil,
},
outer_page_color = DOUTER_PAGE_COLOR,
-- hightlight with "lighten" or "underscore" or "invert"
highlight = {
lighten_color = 0.2, -- color range [0.0, 1.0]
temp_drawer = "invert",
temp = {},
saved_drawer = "lighten",
saved = {},
},
highlight_visible = true,
-- PDF/DjVu continuous paging
page_scroll = nil,
page_bgcolor = DBACKGROUND_COLOR,
page_states = {},
scroll_mode = "vertical",
page_gap = {
width = Screen:scaleByDPI(8),
height = Screen:scaleByDPI(8),
color = 8,
},
-- DjVu page rendering mode (used in djvu.c:drawPage())
render_mode = DRENDER_MODE, -- default to COLOR
-- Crengine view mode
view_mode = DCREREADER_VIEW_MODE, -- default to page mode
hinting = true,
-- visible area within current viewing page
visible_area = Geom:new{x = 0, y = 0},
-- dimen for current viewing page
page_area = Geom:new{},
-- dimen for area to dim
dim_area = Geom:new{w = 0, h = 0},
-- has footer
footer_visible = nil,
-- has dogear
dogear_visible = false,
-- in flipping state
flipping_visible = false,
-- auto save settings after turning pages
auto_save_paging_count = 0,
}
function ReaderView:init()
2014-03-13 13:52:43 +00:00
self:resetLayout()
end
function ReaderView:resetLayout()
2014-03-13 13:52:43 +00:00
self.dogear = ReaderDogear:new{
view = self,
ui = self.ui,
}
self.footer = ReaderFooter:new{
view = self,
ui = self.ui,
visible = self.footer_visible,
}
self.flipping = ReaderFlipping:new{
view = self,
ui = self.ui,
}
self[1] = self.dogear
self[2] = self.footer
self[3] = self.flipping
end
function ReaderView:paintTo(bb, x, y)
2014-03-13 13:52:43 +00:00
DEBUG("painting", self.visible_area, "to", x, y)
if self.page_scroll then
self:drawPageBackground(bb, x, y)
else
self:drawPageSurround(bb, x, y)
end
-- draw page content
if self.ui.document.info.has_pages then
if self.page_scroll then
self:drawScrollPages(bb, x, y)
else
self:drawSinglePage(bb, x, y)
end
else
if self.view_mode == "page" then
self:drawPageView(bb, x, y)
elseif self.view_mode == "scroll" then
self:drawScrollView(bb, x, y)
end
end
-- dim last read area
if self.document.view_mode ~= "page"
and self.dim_area.w ~= 0 and self.dim_area.h ~= 0 then
bb:dimRect(
self.dim_area.x, self.dim_area.y,
self.dim_area.w, self.dim_area.h
)
end
-- draw saved highlight
if self.highlight_visible then
self:drawSavedHighlight(bb, x, y)
end
-- draw temporary highlight
if self.highlight.temp then
self:drawTempHighlight(bb, x, y)
end
-- paint dogear
if self.dogear_visible then
self.dogear:paintTo(bb, x, y)
end
-- paint footer
if self.footer_visible then
self.footer:paintTo(bb, x, y)
end
-- paint flipping
if self.flipping_visible then
self.flipping:paintTo(bb, x, y)
end
-- stop activity indicator
self.ui:handleEvent(Event:new("StopActivityIndicator"))
end
--[[
Given coordinates on the screen return position in original page
]]--
function ReaderView:screenToPageTransform(pos)
2014-03-13 13:52:43 +00:00
if self.ui.document.info.has_pages then
if self.page_scroll then
return self:getScrollPagePosition(pos)
else
return self:getSinglePagePosition(pos)
end
else
pos.page = self.ui.document:getCurrentPage()
local last_y = self.ui.document:getCurrentPos()
DEBUG("document has no pages at", pos)
return pos
end
end
--[[
Given rectangle in original page return rectangle on the screen
]]--
function ReaderView:pageToScreenTransform(page, rect)
2014-03-13 13:52:43 +00:00
if self.ui.document.info.has_pages then
if self.page_scroll then
return self:getScrollPageRect(page, rect)
else
return self:getSinglePageRect(rect)
end
else
return rect
end
end
2013-03-10 06:23:26 +00:00
function ReaderView:drawPageBackground(bb, x, y)
2014-03-13 13:52:43 +00:00
bb:paintRect(x, y, self.dimen.w, self.dimen.h, self.page_bgcolor)
2013-03-10 06:23:26 +00:00
end
function ReaderView:drawPageSurround(bb, x, y)
2014-03-13 13:52:43 +00:00
if self.dimen.h > self.visible_area.h then
bb:paintRect(x, y, self.dimen.w, self.state.offset.y, self.outer_page_color)
bb:paintRect(x, y + self.dimen.h - self.state.offset.y - 1,
self.dimen.w, self.state.offset.y + 1, self.outer_page_color)
end
if self.dimen.w > self.visible_area.w then
bb:paintRect(x, y, self.state.offset.x, self.dimen.h, self.outer_page_color)
bb:paintRect(x + self.dimen.w - self.state.offset.x - 1, y,
self.state.offset.x + 1, self.dimen.h, self.outer_page_color)
end
2013-03-10 06:23:26 +00:00
end
function ReaderView:drawScrollPages(bb, x, y)
2014-03-13 13:52:43 +00:00
local pos = Geom:new{x = x , y = y}
for page, state in ipairs(self.page_states) do
self.ui.document:drawPage(
bb,
pos.x + state.offset.x,
pos.y + state.offset.y,
state.visible_area,
state.page,
state.zoom,
state.rotation,
state.gamma,
self.render_mode)
pos.y = pos.y + state.visible_area.h
-- draw page gap if not the last part
if page ~= #self.page_states then
self:drawPageGap(bb, pos.x, pos.y)
pos.y = pos.y + self.page_gap.height
end
end
UIManager:scheduleIn(0, function()
self.ui:handleEvent(Event:new("HintPage", self.hinting))
end)
2013-03-10 06:23:26 +00:00
end
2013-06-15 15:13:19 +00:00
function ReaderView:getCurrentPageList()
2014-03-13 13:52:43 +00:00
local pages = {}
if self.ui.document.info.has_pages then
if self.page_scroll then
for _, state in ipairs(self.page_states) do
table.insert(pages, state.page)
end
else
table.insert(pages, self.state.page)
end
end
return pages
2013-06-15 15:13:19 +00:00
end
function ReaderView:getScrollPagePosition(pos)
2014-03-13 13:52:43 +00:00
local x_s, y_s = pos.x, pos.y
local x_p, y_p = nil, nil
for _, state in ipairs(self.page_states) do
if y_s < state.visible_area.h + state.offset.y then
y_p = (state.visible_area.y + y_s - state.offset.y) / state.zoom
x_p = (state.visible_area.x + x_s - state.offset.x) / state.zoom
return {
x = x_p,
y = y_p,
page = state.page,
zoom = state.zoom,
rotation = state.rotation,
}
else
y_s = y_s - state.visible_area.h - self.page_gap.height
end
end
end
function ReaderView:getScrollPageRect(page, rect_p)
2014-03-13 13:52:43 +00:00
local rect_s = Geom:new{}
for _, state in ipairs(self.page_states) do
local trans_p = Geom:new(rect_p):copy()
trans_p:transformByScale(state.zoom, state.zoom)
if page == state.page and state.visible_area:contains(trans_p) then
rect_s.x = rect_s.x + state.offset.x + trans_p.x - state.visible_area.x
rect_s.y = rect_s.y + state.offset.y + trans_p.y - state.visible_area.y
rect_s.w = trans_p.w
rect_s.h = trans_p.h
return rect_s
end
rect_s.y = rect_s.y + state.visible_area.h + self.page_gap.height
end
end
2013-03-10 06:23:26 +00:00
function ReaderView:drawPageGap(bb, x, y)
2014-03-13 13:52:43 +00:00
if self.scroll_mode == "vertical" then
bb:paintRect(x, y, self.dimen.w, self.page_gap.height, self.page_gap.color)
elseif self.scroll_mode == "horizontal" then
bb:paintRect(x, y, self.page_gap.width, self.dimen.h, self.page_gap.color)
end
2013-03-10 06:23:26 +00:00
end
function ReaderView:drawSinglePage(bb, x, y)
2014-03-13 13:52:43 +00:00
self.ui.document:drawPage(
bb,
x + self.state.offset.x,
y + self.state.offset.y,
self.visible_area,
self.state.page,
self.state.zoom,
self.state.rotation,
self.state.gamma,
self.render_mode)
UIManager:scheduleIn(0, function()
self.ui:handleEvent(Event:new("HintPage", self.hinting))
end)
2013-03-10 06:23:26 +00:00
end
function ReaderView:getSinglePagePosition(pos)
2014-03-13 13:52:43 +00:00
local x_s, y_s = pos.x, pos.y
return {
x = (self.visible_area.x + x_s - self.state.offset.x) / self.state.zoom,
y = (self.visible_area.y + y_s - self.state.offset.y) / self.state.zoom,
page = self.state.page,
zoom = self.state.zoom,
rotation = self.state.rotation,
}
end
function ReaderView:getSinglePageRect(rect_p)
2014-03-13 13:52:43 +00:00
local rect_s = Geom:new{}
local trans_p = Geom:new(rect_p):copy()
trans_p:transformByScale(self.state.zoom, self.state.zoom)
if self.visible_area:contains(trans_p) then
rect_s.x = self.state.offset.x + trans_p.x - self.visible_area.x
rect_s.y = self.state.offset.y + trans_p.y - self.visible_area.y
rect_s.w = trans_p.w
rect_s.h = trans_p.h
return rect_s
end
end
2013-03-10 06:23:26 +00:00
function ReaderView:drawPageView(bb, x, y)
2014-03-13 13:52:43 +00:00
self.ui.document:drawCurrentViewByPage(
bb,
x + self.state.offset.x,
y + self.state.offset.y,
self.visible_area,
self.state.page)
2013-03-10 06:23:26 +00:00
end
function ReaderView:drawScrollView(bb, x, y)
2014-03-13 13:52:43 +00:00
self.ui.document:drawCurrentViewByPos(
bb,
x + self.state.offset.x,
y + self.state.offset.y,
self.visible_area,
self.state.pos)
2013-03-10 06:23:26 +00:00
end
2013-06-15 15:13:19 +00:00
function ReaderView:drawTempHighlight(bb, x, y)
2014-03-13 13:52:43 +00:00
for page, boxes in pairs(self.highlight.temp) do
for i = 1, #boxes do
local rect = self:pageToScreenTransform(page, boxes[i])
if rect then
self:drawHighlightRect(bb, x, y, rect, self.highlight.temp_drawer)
end
end
end
2013-06-15 15:13:19 +00:00
end
function ReaderView:drawSavedHighlight(bb, x, y)
2014-03-13 13:52:43 +00:00
if self.ui.document.info.has_pages then
self:drawPageSavedHighlight(bb, x, y)
else
self:drawXPointerSavedHighlight(bb, x, y)
end
end
function ReaderView:drawPageSavedHighlight(bb, x, y)
2014-03-13 13:52:43 +00:00
local pages = self:getCurrentPageList()
for _, page in pairs(pages) do
local items = self.highlight.saved[page]
if not items then items = {} end
for i = 1, #items do
local item = items[i]
local pos0, pos1 = item.pos0, item.pos1
local boxes = self.ui.document:getPageBoxesFromPositions(page, pos0, pos1)
if boxes then
for _, box in pairs(boxes) do
local rect = self:pageToScreenTransform(page, box)
if rect then
self:drawHighlightRect(bb, x, y, rect, item.drawer or self.highlight.saved_drawer)
end
end -- end for each box
end -- end if boxes
end -- end for each highlight
end -- end for each page
2013-06-15 15:13:19 +00:00
end
function ReaderView:drawXPointerSavedHighlight(bb, x, y)
2014-03-13 13:52:43 +00:00
for page, _ in pairs(self.highlight.saved) do
local items = self.highlight.saved[page]
if not items then items = {} end
for j = 1, #items do
local item = items[j]
local pos0, pos1 = item.pos0, item.pos1
local boxes = self.ui.document:getScreenBoxesFromPositions(pos0, pos1)
if boxes then
for _, box in pairs(boxes) do
local rect = self:pageToScreenTransform(page, box)
if rect then
self:drawHighlightRect(bb, x, y, rect, item.drawer or self.highlight.saved_drawer)
end
end -- end for each box
end -- end if boxes
end -- end for each highlight
end -- end for all saved highlight
end
2013-06-15 15:13:19 +00:00
function ReaderView:drawHighlightRect(bb, x, y, rect, drawer)
2014-03-13 13:52:43 +00:00
local x, y, w, h = rect.x, rect.y, rect.w, rect.h
if drawer == "underscore" then
self.highlight.line_width = self.highlight.line_width or 2
self.highlight.line_color = self.highlight.line_color or 5
bb:paintRect(x, y+h-1, w,
self.highlight.line_width,
self.highlight.line_color)
elseif drawer == "lighten" then
bb:lightenRect(x, y, w, h, self.highlight.lighten_color)
elseif drawer == "invert" then
bb:invertRect(x, y, w, h)
end
end
2013-03-10 06:23:26 +00:00
function ReaderView:getPageArea(page, zoom, rotation)
2014-03-13 13:52:43 +00:00
if self.use_bbox then
return self.ui.document:getUsedBBoxDimensions(page, zoom, rotation)
else
return self.ui.document:getPageDimensions(page, zoom, rotation)
end
2013-03-10 06:23:26 +00:00
end
2012-12-08 06:05:10 +00:00
--[[
This method is supposed to be only used by ReaderPaging
--]]
function ReaderView:recalculate()
2014-03-13 13:52:43 +00:00
local page_size = nil
if self.ui.document.info.has_pages and self.state.page then
self.page_area = self:getPageArea(
self.state.page,
self.state.zoom,
self.state.rotation)
-- starts from left top of page_area
self.visible_area.x = self.page_area.x
self.visible_area.y = self.page_area.y
-- reset our size
self.visible_area:setSizeTo(self.dimen)
-- and recalculate it according to page size
self.visible_area:offsetWithin(self.page_area, 0, 0)
-- clear dim area
self.dim_area.w = 0
self.dim_area.h = 0
self.ui:handleEvent(
Event:new("ViewRecalculate", self.visible_area, self.page_area))
else
self.visible_area:setSizeTo(self.dimen)
end
self.state.offset = Geom:new{x = 0, y = 0}
if self.dimen.h > self.visible_area.h then
self.state.offset.y = (self.dimen.h - self.visible_area.h) / 2
end
if self.dimen.w > self.visible_area.w then
self.state.offset.x = (self.dimen.w - self.visible_area.w) / 2
end
-- flag a repaint so self:paintTo will be called
UIManager:setDirty(self.dialog)
end
function ReaderView:PanningUpdate(dx, dy)
2014-03-13 13:52:43 +00:00
DEBUG("pan by", dx, dy)
local old = self.visible_area:copy()
self.visible_area:offsetWithin(self.page_area, dx, dy)
if self.visible_area ~= old then
-- flag a repaint
UIManager:setDirty(self.dialog)
DEBUG("on pan: page_area", self.page_area)
DEBUG("on pan: visible_area", self.visible_area)
self.ui:handleEvent(
Event:new("ViewRecalculate", self.visible_area, self.page_area))
end
return true
end
function ReaderView:PanningStart(x, y)
2014-03-13 13:52:43 +00:00
DEBUG("panning start", x, y)
if not self.panning_visible_area then
self.panning_visible_area = self.visible_area:copy()
end
self.visible_area = self.panning_visible_area:copy()
self.visible_area:offsetWithin(self.page_area, x, y)
self.ui:handleEvent(Event:new("ViewRecalculate", self.visible_area, self.page_area))
UIManager:setDirty(self.dialog)
end
function ReaderView:PanningStop()
2014-03-13 13:52:43 +00:00
self.panning_visible_area = nil
end
function ReaderView:SetZoomCenter(x, y)
2014-03-13 13:52:43 +00:00
local old = self.visible_area:copy()
self.visible_area:centerWithin(self.page_area, x, y)
if self.visible_area ~= old then
self.ui:handleEvent(Event:new("ViewRecalculate", self.visible_area, self.page_area))
UIManager:setDirty(self.dialog)
end
end
2013-07-13 05:57:02 +00:00
function ReaderView:onSetScreenMode(new_mode, rotation)
2014-03-13 13:52:43 +00:00
if new_mode == "landscape" or new_mode == "portrait" then
self.screen_mode = new_mode
if rotation ~= nil then
Screen:setRotationMode(rotation)
else
Screen:setScreenMode(new_mode)
end
self.ui:handleEvent(Event:new("SetDimensions", Screen:getSize()))
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end
self.cur_rotation_mode = Screen.cur_rotation_mode
return true
end
-- for returning to FileManager
function ReaderView:onRestoreScreenMode(old_mode)
2014-03-13 13:52:43 +00:00
if old_mode == "landscape" or old_mode == "portrait" then
Screen:setScreenMode(old_mode)
self.ui:handleEvent(Event:new("SetDimensions", Screen:getSize()))
end
return true
end
2012-06-12 16:14:23 +00:00
function ReaderView:onSetDimensions(dimensions)
2014-03-13 13:52:43 +00:00
self:resetLayout()
self.dimen = dimensions
-- recalculate view
self:recalculate()
end
function ReaderView:onRestoreDimensions(dimensions)
2014-03-13 13:52:43 +00:00
--DEBUG("restore dimen", dimensions)
self:resetLayout()
self.dimen = dimensions
-- recalculate view
self:recalculate()
2012-06-12 16:14:23 +00:00
end
function ReaderView:onSetFullScreen(full_screen)
2014-03-13 13:52:43 +00:00
self.footer_visible = not full_screen
self.ui:handleEvent(Event:new("SetDimensions", Screen:getSize()))
end
2013-03-10 06:23:26 +00:00
function ReaderView:onToggleScrollMode(page_scroll)
2014-03-13 13:52:43 +00:00
self.page_scroll = page_scroll
self:recalculate()
self.ui:handleEvent(Event:new("InitScrollPageStates"))
2013-03-10 06:23:26 +00:00
end
function ReaderView:onReadSettings(config)
2014-03-13 13:52:43 +00:00
self.render_mode = config:readSetting("render_mode") or 0
local screen_mode = config:readSetting("screen_mode") or "portrait"
if screen_mode then
Screen:setScreenMode(screen_mode)
self:onSetScreenMode(screen_mode, config:readSetting("rotation_mode"))
end
self.state.gamma = config:readSetting("gamma") or DGLOBALGAMMA
local full_screen = config:readSetting("kopt_full_screen") or self.document.configurable.full_screen
local status_line = config:readSetting("copt_status_line") or self.document.configurable.status_line
self.footer_visible = (full_screen == 0 or status_line == 1) and true or false
self:resetLayout()
local page_scroll = config:readSetting("kopt_page_scroll") or self.document.configurable.page_scroll
self.page_scroll = page_scroll == 1 and true or false
self.highlight.saved = config:readSetting("highlight") or {}
end
function ReaderView:onPageUpdate(new_page_no)
2014-03-13 13:52:43 +00:00
self.state.page = new_page_no
self:recalculate()
self.highlight.temp = {}
self:autoSaveSettings()
end
2012-06-05 07:23:36 +00:00
function ReaderView:onPosUpdate(new_pos)
2014-03-13 13:52:43 +00:00
self.state.pos = new_pos
self:recalculate()
self.highlight.temp = {}
self:autoSaveSettings()
2012-06-05 07:23:36 +00:00
end
function ReaderView:onZoomUpdate(zoom)
2014-03-13 13:52:43 +00:00
self.state.zoom = zoom
self:recalculate()
self.highlight.temp = {}
end
2012-12-02 09:09:32 +00:00
function ReaderView:onBBoxUpdate(bbox)
2014-03-13 13:52:43 +00:00
self.use_bbox = bbox and true or false
2012-12-02 09:09:32 +00:00
end
function ReaderView:onRotationUpdate(rotation)
2014-03-13 13:52:43 +00:00
self.state.rotation = rotation
self:recalculate()
end
function ReaderView:onGammaUpdate(gamma)
2014-03-13 13:52:43 +00:00
self.state.gamma = gamma
if self.page_scroll then
self.ui:handleEvent(Event:new("UpdateScrollPageGamma", gamma))
end
end
function ReaderView:onFontSizeUpdate()
2014-03-13 13:52:43 +00:00
self.ui:handleEvent(Event:new("ReZoom"))
end
2013-04-14 07:16:42 +00:00
function ReaderView:onDefectSizeUpdate()
2014-03-13 13:52:43 +00:00
self.ui:handleEvent(Event:new("ReZoom"))
2013-04-14 07:16:42 +00:00
end
function ReaderView:onPageCrop()
2014-03-13 13:52:43 +00:00
self.ui:handleEvent(Event:new("ReZoom"))
2013-04-14 07:16:42 +00:00
end
function ReaderView:onMarginUpdate()
2014-03-13 13:52:43 +00:00
self.ui:handleEvent(Event:new("ReZoom"))
2013-04-14 07:16:42 +00:00
end
function ReaderView:onSetViewMode(new_mode)
2014-03-13 13:52:43 +00:00
self.view_mode = new_mode
self.ui.document:setViewMode(new_mode)
self.ui:handleEvent(Event:new("ChangeViewMode"))
return true
end
2013-12-27 15:18:16 +00:00
function ReaderView:onSaveSettings()
2014-03-13 13:52:43 +00:00
self.ui.doc_settings:saveSetting("render_mode", self.render_mode)
self.ui.doc_settings:saveSetting("screen_mode", self.screen_mode)
self.ui.doc_settings:saveSetting("rotation_mode", self.cur_rotation_mode)
self.ui.doc_settings:saveSetting("gamma", self.state.gamma)
self.ui.doc_settings:saveSetting("highlight", self.highlight.saved)
end
2013-10-18 20:38:07 +00:00
2013-12-27 15:18:16 +00:00
function ReaderView:autoSaveSettings()
2014-03-13 13:52:43 +00:00
if DAUTO_SAVE_PAGING_COUNT then
if self.auto_save_paging_count == DAUTO_SAVE_PAGING_COUNT then
self.ui:saveSettings()
self.auto_save_paging_count = 0
else
self.auto_save_paging_count = self.auto_save_paging_count + 1
end
end
2013-12-27 15:18:16 +00:00
end
2013-10-18 20:38:07 +00:00
return ReaderView