mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
separate DC out of pdf.c and djvu.c (cleanup)
This commit is contained in:
100
djvu.c
100
djvu.c
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "string.h"
|
||||
#include "blitbuffer.h"
|
||||
#include "drawcontext.h"
|
||||
#include "djvu.h"
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
@@ -40,14 +41,6 @@ typedef struct DjvuPage {
|
||||
DjvuDocument *doc;
|
||||
} DjvuPage;
|
||||
|
||||
typedef struct DrawContext {
|
||||
int rotate;
|
||||
double zoom;
|
||||
double gamma;
|
||||
int offset_x;
|
||||
int offset_y;
|
||||
} DrawContext;
|
||||
|
||||
|
||||
static int handle(lua_State *L, ddjvu_context_t *ctx, int wait)
|
||||
{
|
||||
@@ -177,77 +170,6 @@ static int getTableOfContent(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newDrawContext(lua_State *L) {
|
||||
int rotate = luaL_optint(L, 1, 0);
|
||||
double zoom = luaL_optnumber(L, 2, (double) 1.0);
|
||||
int offset_x = luaL_optint(L, 3, 0);
|
||||
int offset_y = luaL_optint(L, 4, 0);
|
||||
double gamma = luaL_optnumber(L, 5, (double) -1.0);
|
||||
|
||||
DrawContext *dc = (DrawContext*) lua_newuserdata(L, sizeof(DrawContext));
|
||||
dc->rotate = rotate;
|
||||
dc->zoom = zoom;
|
||||
dc->offset_x = offset_x;
|
||||
dc->offset_y = offset_y;
|
||||
dc->gamma = gamma;
|
||||
|
||||
|
||||
luaL_getmetatable(L, "drawcontext");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcSetOffset(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->offset_x = luaL_checkint(L, 2);
|
||||
dc->offset_y = luaL_checkint(L, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetOffset(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushinteger(L, dc->offset_x);
|
||||
lua_pushinteger(L, dc->offset_y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int dcSetRotate(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->rotate = luaL_checkint(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcSetZoom(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->zoom = luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetRotate(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushinteger(L, dc->rotate);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcGetZoom(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushnumber(L, dc->zoom);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcSetGamma(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->gamma = luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetGamma(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushnumber(L, dc->gamma);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int openPage(lua_State *L) {
|
||||
ddjvu_status_t r;
|
||||
DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument");
|
||||
@@ -407,7 +329,6 @@ static int drawPage(lua_State *L) {
|
||||
|
||||
static const struct luaL_reg djvu_func[] = {
|
||||
{"openDocument", openDocument},
|
||||
{"newDC", newDrawContext},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -429,18 +350,6 @@ static const struct luaL_reg djvupage_meth[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const struct luaL_reg drawcontext_meth[] = {
|
||||
{"setRotate", dcSetRotate},
|
||||
{"getRotate", dcGetRotate},
|
||||
{"setZoom", dcSetZoom},
|
||||
{"getZoom", dcGetZoom},
|
||||
{"setOffset", dcSetOffset},
|
||||
{"getOffset", dcGetOffset},
|
||||
{"setGamma", dcSetGamma},
|
||||
{"getGamma", dcGetGamma},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_djvu(lua_State *L) {
|
||||
luaL_newmetatable(L, "djvudocument");
|
||||
lua_pushstring(L, "__index");
|
||||
@@ -456,13 +365,6 @@ int luaopen_djvu(lua_State *L) {
|
||||
luaL_register(L, NULL, djvupage_meth);
|
||||
lua_pop(L, 1);
|
||||
|
||||
luaL_newmetatable(L, "drawcontext");
|
||||
lua_pushstring(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
luaL_register(L, NULL, drawcontext_meth);
|
||||
lua_pop(L, 1);
|
||||
|
||||
luaL_register(L, "djvu", djvu_func);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
require "unireader"
|
||||
|
||||
DJVUReader = UniReader:new{
|
||||
newDC = function()
|
||||
print("djvu.newDC")
|
||||
return djvu.newDC()
|
||||
end,
|
||||
}
|
||||
|
||||
function DJVUReader:init()
|
||||
self.nulldc = self.newDC()
|
||||
end
|
||||
DJVUReader = UniReader:new{}
|
||||
|
||||
-- open a DJVU file and its settings store
|
||||
-- DJVU does not support password yet
|
||||
|
||||
117
drawcontext.c
Normal file
117
drawcontext.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
KindlePDFViewer: a DC abstraction
|
||||
Copyright (C) 2012 Hans-Werner Hilse <hilse@web.de>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "drawcontext.h"
|
||||
|
||||
static int newDrawContext(lua_State *L) {
|
||||
int rotate = luaL_optint(L, 1, 0);
|
||||
double zoom = luaL_optnumber(L, 2, (double) 1.0);
|
||||
int offset_x = luaL_optint(L, 3, 0);
|
||||
int offset_y = luaL_optint(L, 4, 0);
|
||||
double gamma = luaL_optnumber(L, 5, (double) -1.0);
|
||||
|
||||
DrawContext *dc = (DrawContext*) lua_newuserdata(L, sizeof(DrawContext));
|
||||
dc->rotate = rotate;
|
||||
dc->zoom = zoom;
|
||||
dc->offset_x = offset_x;
|
||||
dc->offset_y = offset_y;
|
||||
dc->gamma = gamma;
|
||||
|
||||
luaL_getmetatable(L, "drawcontext");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcSetOffset(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->offset_x = luaL_checkint(L, 2);
|
||||
dc->offset_y = luaL_checkint(L, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetOffset(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushinteger(L, dc->offset_x);
|
||||
lua_pushinteger(L, dc->offset_y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int dcSetRotate(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->rotate = luaL_checkint(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcSetZoom(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->zoom = luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetRotate(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushinteger(L, dc->rotate);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcGetZoom(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushnumber(L, dc->zoom);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcSetGamma(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->gamma = luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetGamma(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushnumber(L, dc->gamma);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const struct luaL_reg drawcontext_meth[] = {
|
||||
{"setRotate", dcSetRotate},
|
||||
{"getRotate", dcGetRotate},
|
||||
{"setZoom", dcSetZoom},
|
||||
{"getZoom", dcGetZoom},
|
||||
{"setOffset", dcSetOffset},
|
||||
{"getOffset", dcGetOffset},
|
||||
{"setGamma", dcSetGamma},
|
||||
{"getGamma", dcGetGamma},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const struct luaL_reg drawcontext_func[] = {
|
||||
{"new", newDrawContext},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_drawcontext(lua_State *L) {
|
||||
luaL_newmetatable(L, "drawcontext");
|
||||
lua_pushstring(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
luaL_register(L, NULL, drawcontext_meth);
|
||||
lua_pop(L, 1);
|
||||
luaL_register(L, "DrawContext", drawcontext_func);
|
||||
return 1;
|
||||
}
|
||||
35
drawcontext.h
Normal file
35
drawcontext.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
KindlePDFViewer: a DC abstraction
|
||||
Copyright (C) 2012 Hans-Werner Hilse <hilse@web.de>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef _DRAWCONTEXT_H
|
||||
#define _DRAWCONTEXT_H
|
||||
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
typedef struct DrawContext {
|
||||
int rotate;
|
||||
double zoom;
|
||||
double gamma;
|
||||
int offset_x;
|
||||
int offset_y;
|
||||
} DrawContext;
|
||||
|
||||
int luaopen_drawcontext(lua_State *L);
|
||||
#endif
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include "blitbuffer.h"
|
||||
#include "drawcontext.h"
|
||||
#include "pdf.h"
|
||||
#include "einkfb.h"
|
||||
#include "input.h"
|
||||
@@ -48,6 +49,7 @@ int main(int argc, char **argv) {
|
||||
luaL_openlibs(L);
|
||||
|
||||
luaopen_blitbuffer(L);
|
||||
luaopen_drawcontext(L);
|
||||
luaopen_einkfb(L);
|
||||
luaopen_pdf(L);
|
||||
luaopen_djvu(L);
|
||||
|
||||
98
pdf.c
98
pdf.c
@@ -19,6 +19,7 @@
|
||||
#include <pdf/mupdf.h>
|
||||
|
||||
#include "blitbuffer.h"
|
||||
#include "drawcontext.h"
|
||||
#include "pdf.h"
|
||||
|
||||
typedef struct PdfDocument {
|
||||
@@ -36,14 +37,6 @@ typedef struct PdfPage {
|
||||
PdfDocument *doc;
|
||||
} PdfPage;
|
||||
|
||||
typedef struct DrawContext {
|
||||
int rotate;
|
||||
double zoom;
|
||||
double gamma;
|
||||
int offset_x;
|
||||
int offset_y;
|
||||
} DrawContext;
|
||||
|
||||
static int openDocument(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *password = luaL_checkstring(L, 2);
|
||||
@@ -140,76 +133,6 @@ static int getTableOfContent(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newDrawContext(lua_State *L) {
|
||||
int rotate = luaL_optint(L, 1, 0);
|
||||
double zoom = luaL_optnumber(L, 2, (double) 1.0);
|
||||
int offset_x = luaL_optint(L, 3, 0);
|
||||
int offset_y = luaL_optint(L, 4, 0);
|
||||
double gamma = luaL_optnumber(L, 5, (double) -1.0);
|
||||
|
||||
DrawContext *dc = (DrawContext*) lua_newuserdata(L, sizeof(DrawContext));
|
||||
dc->rotate = rotate;
|
||||
dc->zoom = zoom;
|
||||
dc->offset_x = offset_x;
|
||||
dc->offset_y = offset_y;
|
||||
dc->gamma = gamma;
|
||||
|
||||
luaL_getmetatable(L, "drawcontext");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcSetOffset(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->offset_x = luaL_checkint(L, 2);
|
||||
dc->offset_y = luaL_checkint(L, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetOffset(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushinteger(L, dc->offset_x);
|
||||
lua_pushinteger(L, dc->offset_y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int dcSetRotate(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->rotate = luaL_checkint(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcSetZoom(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->zoom = luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetRotate(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushinteger(L, dc->rotate);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcGetZoom(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushnumber(L, dc->zoom);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dcSetGamma(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
dc->gamma = luaL_checknumber(L, 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dcGetGamma(lua_State *L) {
|
||||
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 1, "drawcontext");
|
||||
lua_pushnumber(L, dc->gamma);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int openPage(lua_State *L) {
|
||||
fz_device *dev;
|
||||
|
||||
@@ -353,7 +276,6 @@ static int drawPage(lua_State *L) {
|
||||
|
||||
static const struct luaL_reg pdf_func[] = {
|
||||
{"openDocument", openDocument},
|
||||
{"newDC", newDrawContext},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -375,18 +297,6 @@ static const struct luaL_reg pdfpage_meth[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const struct luaL_reg drawcontext_meth[] = {
|
||||
{"setRotate", dcSetRotate},
|
||||
{"getRotate", dcGetRotate},
|
||||
{"setZoom", dcSetZoom},
|
||||
{"getZoom", dcGetZoom},
|
||||
{"setOffset", dcSetOffset},
|
||||
{"getOffset", dcGetOffset},
|
||||
{"setGamma", dcSetGamma},
|
||||
{"getGamma", dcGetGamma},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_pdf(lua_State *L) {
|
||||
luaL_newmetatable(L, "pdfdocument");
|
||||
lua_pushstring(L, "__index");
|
||||
@@ -400,12 +310,6 @@ int luaopen_pdf(lua_State *L) {
|
||||
lua_settable(L, -3);
|
||||
luaL_register(L, NULL, pdfpage_meth);
|
||||
lua_pop(L, 1);
|
||||
luaL_newmetatable(L, "drawcontext");
|
||||
lua_pushstring(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
luaL_register(L, NULL, drawcontext_meth);
|
||||
lua_pop(L, 1);
|
||||
luaL_register(L, "pdf", pdf_func);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
require "unireader"
|
||||
|
||||
PDFReader = UniReader:new{
|
||||
newDC = function()
|
||||
print("pdf.newDC")
|
||||
return pdf.newDC()
|
||||
end,
|
||||
}
|
||||
|
||||
function PDFReader:init()
|
||||
self.nulldc = self.newDC();
|
||||
end
|
||||
PDFReader = UniReader:new{}
|
||||
|
||||
-- open a PDF file and its settings store
|
||||
function PDFReader:open(filename, password)
|
||||
|
||||
@@ -55,10 +55,8 @@ UniReader = {
|
||||
-- the document's setting store:
|
||||
settings = nil,
|
||||
|
||||
-- you have to initialize newDC, nulldc in specific reader
|
||||
newDC = function() return nil end,
|
||||
-- we will use this one often, so keep it "static":
|
||||
nulldc = nil,
|
||||
nulldc = DrawContext.new(),
|
||||
|
||||
-- tile cache configuration:
|
||||
cache_max_memsize = 1024*1024*5, -- 5MB tile cache
|
||||
@@ -87,13 +85,11 @@ end
|
||||
For a new specific reader,
|
||||
you must always overwrite following two methods:
|
||||
|
||||
* self:init()
|
||||
* self:open()
|
||||
|
||||
overwrite other methods if needed.
|
||||
--]]
|
||||
function UniReader:init()
|
||||
print("empty initialization method!")
|
||||
end
|
||||
|
||||
-- open a file and its settings store
|
||||
@@ -286,7 +282,7 @@ end
|
||||
|
||||
-- set viewer state according to zoom state
|
||||
function UniReader:setzoom(page)
|
||||
local dc = self.newDC()
|
||||
local dc = DrawContext.new()
|
||||
local pwidth, pheight = page:getSize(self.nulldc)
|
||||
print("# page::getSize "..pwidth.."*"..pheight);
|
||||
local x0, y0, x1, y1 = page:getUsedBBox()
|
||||
|
||||
Reference in New Issue
Block a user