Skip to content

Commit 50d5604

Browse files
committed
mod: code cleanup for readers
closes koreader#26
1 parent b318ca0 commit 50d5604

File tree

3 files changed

+34
-72
lines changed

3 files changed

+34
-72
lines changed

djvureader.lua

+2-58
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,8 @@ function DJVUReader:init()
1212
end
1313

1414
-- open a DJVU file and its settings store
15+
-- DJVU does not support password yet
1516
function DJVUReader:open(filename)
1617
self.doc = djvu.openDocument(filename)
17-
if self.doc ~= nil then
18-
self.settings = DocSettings:open(filename)
19-
local gamma = self.settings:readsetting("gamma")
20-
if gamma then
21-
self.globalgamma = gamma
22-
end
23-
return true
24-
end
25-
return false
26-
end
27-
28-
--set viewer state according to zoom state
29-
function DJVUReader:setzoom(page)
30-
local dc = self.newDC()
31-
local pwidth, pheight = page:getSize(self.nulldc)
32-
33-
if self.globalzoommode == self.ZOOM_FIT_TO_PAGE then
34-
self.globalzoom = width / pwidth
35-
self.offset_x = 0
36-
self.offset_y = (height - (self.globalzoom * pheight)) / 2
37-
if height / pheight < self.globalzoom then
38-
self.globalzoom = height / pheight
39-
print(width, (self.globalzoom * pwidth))
40-
self.offset_x = (width - (self.globalzoom * pwidth)) / 2
41-
self.offset_y = 0
42-
end
43-
elseif self.globalzoommode == self.ZOOM_FIT_TO_PAGE_WIDTH then
44-
self.globalzoom = width / pwidth
45-
self.offset_x = 0
46-
self.offset_y = (height - (self.globalzoom * pheight)) / 2
47-
elseif self.globalzoommode == self.ZOOM_FIT_TO_PAGE_HEIGHT then
48-
self.globalzoom = height / pheight
49-
self.offset_x = (width - (self.globalzoom * pwidth)) / 2
50-
self.offset_y = 0
51-
end
52-
53-
dc:setZoom(self.globalzoom)
54-
-- record globalzoom for manual zoom in/out
55-
self.globalzoom_orig = self.globalzoom
56-
57-
dc:setRotate(self.globalrotate);
58-
dc:setOffset(self.offset_x, self.offset_y)
59-
self.fullwidth, self.fullheight = page:getSize(dc)
60-
self.min_offset_x = fb.bb:getWidth() - self.fullwidth
61-
self.min_offset_y = fb.bb:getHeight() - self.fullheight
62-
if(self.min_offset_x > 0) then
63-
self.min_offset_x = 0
64-
end
65-
if(self.min_offset_y > 0) then
66-
self.min_offset_y = 0
67-
end
68-
69-
-- set gamma here, we don't have any other good place for this right now:
70-
if self.globalgamma ~= self.GAMMA_NO_GAMMA then
71-
print("gamma correction: "..self.globalgamma)
72-
dc:setGamma(self.globalgamma)
73-
end
74-
return dc
18+
return self:loadSettings(filename)
7519
end

pdfreader.lua

+1-9
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,5 @@ end
1414
-- open a PDF file and its settings store
1515
function PDFReader:open(filename, password)
1616
self.doc = pdf.openDocument(filename, password or "")
17-
if self.doc ~= nil then
18-
self.settings = DocSettings:open(filename)
19-
local gamma = self.settings:readsetting("gamma")
20-
if gamma then
21-
self.globalgamma = gamma
22-
end
23-
return true
24-
end
25-
return false
17+
return self:loadSettings(filename)
2618
end

unireader.lua

+31-5
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,41 @@ function UniReader:new(o)
7272
return o
7373
end
7474

75+
--[[
76+
For a new specific reader,
77+
you must always overwrite following two methods:
78+
79+
* self:init()
80+
* self:open()
81+
82+
overwrite other methods if needed.
83+
--]]
7584
function UniReader:init()
7685
print("empty initialization method!")
7786
end
7887

88+
-- open a file and its settings store
89+
-- tips: you can use self:loadSettings in open() method.
90+
function UniReader:open(filename, password)
91+
return false
92+
end
93+
94+
95+
96+
--[ following are default methods ]--
97+
98+
function UniReader:loadSettings(filename)
99+
if self.doc ~= nil then
100+
self.settings = DocSettings:open(filename)
101+
local gamma = self.settings:readsetting("gamma")
102+
if gamma then
103+
self.globalgamma = gamma
104+
end
105+
return true
106+
end
107+
return false
108+
end
109+
79110
-- guarantee that we have enough memory in cache
80111
function UniReader:cacheclaim(size)
81112
if(size > self.cache_max_memsize) then
@@ -136,11 +167,6 @@ function UniReader:clearcache()
136167
self.cache_current_memsize = 0
137168
end
138169

139-
-- open a file and its settings store
140-
function UniReader:open(filename, password)
141-
return false
142-
end
143-
144170
-- set viewer state according to zoom state
145171
function UniReader:setzoom(page)
146172
local dc = self.newDC()

0 commit comments

Comments
 (0)