use koptcontext to keep dozons of parameters for k2pdfopt

pull/2/merge
chrox 12 years ago
parent 3a7535d350
commit f70ef06ea6

@ -116,7 +116,7 @@ POPENNSLIB := $(POPENNSDIR)/libpopen_noshell.a
all: kpdfview extr all: kpdfview extr
VERSION?=$(shell git describe HEAD) VERSION?=$(shell git describe HEAD)
kpdfview: kpdfview.o einkfb.o pdf.o k2pdfopt.o blitbuffer.o drawcontext.o input.o $(POPENNSLIB) util.o ft.o lfs.o mupdfimg.o $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) djvu.o $(DJVULIBS) cre.o $(CRENGINELIBS) pic.o pic_jpeg.o kpdfview: kpdfview.o einkfb.o pdf.o k2pdfopt.o blitbuffer.o drawcontext.o koptcontext.o input.o $(POPENNSLIB) util.o ft.o lfs.o mupdfimg.o $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) djvu.o $(DJVULIBS) cre.o $(CRENGINELIBS) pic.o pic_jpeg.o
echo $(VERSION) > git-rev echo $(VERSION) > git-rev
$(CC) \ $(CC) \
$(CFLAGS) \ $(CFLAGS) \
@ -126,6 +126,7 @@ kpdfview: kpdfview.o einkfb.o pdf.o k2pdfopt.o blitbuffer.o drawcontext.o input.
k2pdfopt.o \ k2pdfopt.o \
blitbuffer.o \ blitbuffer.o \
drawcontext.o \ drawcontext.o \
koptcontext.o \
input.o \ input.o \
$(POPENNSLIB) \ $(POPENNSLIB) \
util.o \ util.o \
@ -162,7 +163,7 @@ slider_watcher: slider_watcher.o $(POPENNSLIB)
ft.o: %.o: %.c $(THIRDPARTYLIBS) ft.o: %.o: %.c $(THIRDPARTYLIBS)
$(CC) -c $(KPDFREADER_CFLAGS) -I$(FREETYPEDIR)/include -I$(MUPDFDIR)/fitz $< -o $@ $(CC) -c $(KPDFREADER_CFLAGS) -I$(FREETYPEDIR)/include -I$(MUPDFDIR)/fitz $< -o $@
kpdfview.o pdf.o blitbuffer.o util.o drawcontext.o einkfb.o input.o mupdfimg.o: %.o: %.c kpdfview.o pdf.o blitbuffer.o util.o drawcontext.o koptcontext.o einkfb.o input.o mupdfimg.o: %.o: %.c
$(CC) -c $(KPDFREADER_CFLAGS) $(EMU_CFLAGS) -I$(LFSDIR)/src $< -o $@ $(CC) -c $(KPDFREADER_CFLAGS) $(EMU_CFLAGS) -I$(LFSDIR)/src $< -o $@
k2pdfopt.o: %.o: %.c k2pdfopt.o: %.o: %.c

@ -23,6 +23,7 @@
#include "blitbuffer.h" #include "blitbuffer.h"
#include "drawcontext.h" #include "drawcontext.h"
#include "koptcontext.h"
#include "djvu.h" #include "djvu.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
@ -473,48 +474,21 @@ static int closePage(lua_State *L) {
static int reflowPage(lua_State *L) { static int reflowPage(lua_State *L) {
DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage"); DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext"); KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
ddjvu_render_mode_t mode = (int) luaL_checkint(L, 3); ddjvu_render_mode_t mode = (int) luaL_checkint(L, 3);
int width = luaL_checkint(L, 4); // framebuffer size
int height = luaL_checkint(L, 5); k2pdfopt_djvu_reflow(kc, page->page_ref, page->doc->context, mode, page->doc->pixelformat);
double font_size = luaL_checknumber(L, 6);
double page_margin = luaL_checknumber(L, 7); return 0;
double line_spacing = luaL_checknumber(L, 8);
double word_spacing = luaL_checknumber(L, 9);
int text_wrap = luaL_checkint(L, 10);
int straighten = luaL_checkint(L, 11);
int justification = luaL_checkint(L, 12);
int detect_indent = luaL_checkint(L, 13);
int columns = luaL_checkint(L, 14);
double contrast = luaL_checknumber(L, 15);
int rotation = luaL_checkint(L, 16);
double quality = luaL_checknumber(L, 17);
double defect_size = luaL_checknumber(L, 18);
int trim_page = luaL_checkint(L, 19);
k2pdfopt_set_params(width, height, font_size, page_margin, line_spacing, word_spacing, \
text_wrap, straighten, justification, detect_indent, columns, contrast, rotation, \
quality, defect_size, trim_page);
k2pdfopt_djvu_reflow(page->page_ref, page->doc->context, mode, page->doc->pixelformat);
k2pdfopt_rfbmp_size(&width, &height);
k2pdfopt_rfbmp_zoom(&dc->zoom);
lua_pushnumber(L, (double)width);
lua_pushnumber(L, (double)height);
lua_pushnumber(L, (double)dc->zoom);
return 3;
} }
static int drawReflowedPage(lua_State *L) { static int drawReflowedPage(lua_State *L) {
uint8_t *pmptr = NULL;
DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage"); DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext"); KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer"); BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
uint8_t *koptr = kc->data;
uint8_t *bbptr = bb->data; uint8_t *bbptr = bb->data;
k2pdfopt_rfbmp_ptr(&pmptr);
int x_offset = 0; int x_offset = 0;
int y_offset = 0; int y_offset = 0;
@ -524,12 +498,12 @@ static int drawReflowedPage(lua_State *L) {
for(y = y_offset; y < bb->h; y++) { for(y = y_offset; y < bb->h; y++) {
for(x = x_offset/2; x < (bb->w/2); x++) { for(x = x_offset/2; x < (bb->w/2); x++) {
int p = x*2 - x_offset; int p = x*2 - x_offset;
bbptr[x] = (((pmptr[p + 1] & 0xF0) >> 4) | (pmptr[p] & 0xF0)) ^ 0xFF; bbptr[x] = (((koptr[p + 1] & 0xF0) >> 4) | (koptr[p] & 0xF0)) ^ 0xFF;
} }
bbptr += bb->pitch; bbptr += bb->pitch;
pmptr += bb->w; koptr += bb->w;
if (bb->w & 1) { if (bb->w & 1) {
bbptr[x] = 255 - (pmptr[x*2] & 0xF0); bbptr[x] = 255 - (koptr[x*2] & 0xF0);
} }
} }

@ -463,13 +463,9 @@ static void wpdfboxes_add_box(WPDFBOXES *boxes, WPDFBOX *box);
static MASTERINFO _masterinfo, *masterinfo; static MASTERINFO _masterinfo, *masterinfo;
static int master_bmp_inited = 0; static int master_bmp_inited = 0;
static int master_bmp_width = 0;
static int master_bmp_height = 0;
static int max_page_width_pix = 3000; static int max_page_width_pix = 3000;
static int max_page_height_pix = 4000; static int max_page_height_pix = 4000;
static double shrink_factor = 0.9; static double shrink_factor = 0.9;
static double zoom_value = 1.0;
static double gamma_correction = 1.0;
static void k2pdfopt_reflow_bmp(MASTERINFO *masterinfo, WILLUSBITMAP *src) { static void k2pdfopt_reflow_bmp(MASTERINFO *masterinfo, WILLUSBITMAP *src) {
WPDFPAGEINFO _pageinfo, *pageinfo; WPDFPAGEINFO _pageinfo, *pageinfo;
@ -545,37 +541,25 @@ static void k2pdfopt_reflow_bmp(MASTERINFO *masterinfo, WILLUSBITMAP *src) {
/* Check to see if master bitmap might need more room */ /* Check to see if master bitmap might need more room */
bmpregion_source_page_add(&region, masterinfo, 1, pageinfo, (int) (0.25 * src_dpi + .5)); bmpregion_source_page_add(&region, masterinfo, 1, pageinfo, (int) (0.25 * src_dpi + .5));
master_bmp_width = masterinfo->bmp.width;
master_bmp_height = masterinfo->rows;
bmp_free(srcgrey); bmp_free(srcgrey);
if (pageinfo != NULL) if (pageinfo != NULL)
wpdfboxes_free(&pageinfo->boxes); wpdfboxes_free(&pageinfo->boxes);
} }
void k2pdfopt_set_params(int bb_width, int bb_height, \ static void k2pdfopt_init(KOPTContext *kctx) {
double font_size, double page_margin, \ dst_userwidth = kctx->dev_width; // dst_width is adjusted in adjust_params_init
double line_space, double word_space, \ dst_userheight = kctx->dev_height;
int wrapping, int straighten, \ vertical_line_spacing = kctx->line_spacing;
int justification, int detect_indent,\ word_spacing = kctx->word_spacing;
int columns, double contrast, \ text_wrap = kctx->wrap;
int rotation, double quality, \ src_autostraighten = kctx->straighten;
double defect_size, int trim_page) { preserve_indentation = kctx->indent;
dst_userwidth = bb_width; // dst_width is adjusted in adjust_params_init max_columns = kctx->columns;
dst_userheight = bb_height; src_rot = kctx->rotate;
zoom_value = font_size; src_dpi = (int)300*kctx->quality;
vertical_line_spacing = line_space; defect_size_pts = kctx->defect_size;
word_spacing = word_space;
text_wrap = wrapping; if (kctx->trim == 0) {
src_autostraighten = straighten;
preserve_indentation = detect_indent;
max_columns = columns;
gamma_correction = contrast; // contrast is only used by k2pdfopt_mupdf_reflow
src_rot = rotation;
src_dpi = (int)300*quality;
defect_size_pts = defect_size;
if (trim_page == 0) {
mar_left = 0; mar_left = 0;
mar_top = 0; mar_top = 0;
mar_right = 0; mar_right = 0;
@ -588,19 +572,19 @@ void k2pdfopt_set_params(int bb_width, int bb_height, \
} }
// margin // margin
dst_mar = page_margin; dst_mar = kctx->margin;
dst_martop = -1.0; dst_martop = -1.0;
dst_marbot = -1.0; dst_marbot = -1.0;
dst_marleft = -1.0; dst_marleft = -1.0;
dst_marright = -1.0; dst_marright = -1.0;
// justification // justification
if (justification < 0) { if (kctx->justification < 0) {
dst_justify = -1; dst_justify = -1;
dst_fulljustify = -1; dst_fulljustify = -1;
} }
else if (justification <= 2) { else if (kctx->justification <= 2) {
dst_justify = justification; dst_justify = kctx->justification;
dst_fulljustify = 0; dst_fulljustify = 0;
} }
else { else {
@ -609,7 +593,7 @@ void k2pdfopt_set_params(int bb_width, int bb_height, \
} }
} }
void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) { void k2pdfopt_mupdf_reflow(KOPTContext *kctx, fz_document *doc, fz_page *page, fz_context *ctx) {
fz_device *dev; fz_device *dev;
fz_pixmap *pix; fz_pixmap *pix;
fz_rect bounds,bounds2; fz_rect bounds,bounds2;
@ -617,8 +601,10 @@ void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) {
fz_bbox bbox; fz_bbox bbox;
WILLUSBITMAP _src, *src; WILLUSBITMAP _src, *src;
k2pdfopt_init(kctx);
double dpp,zoom; double dpp,zoom;
zoom = zoom_value; zoom = kctx->zoom;
double dpi = 250*zoom*src_dpi/300; double dpi = 250*zoom*src_dpi/300;
do { do {
dpp = dpi / 72.; dpp = dpi / 72.;
@ -630,7 +616,7 @@ void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) {
bounds2 = fz_transform_rect(ctm, bounds); bounds2 = fz_transform_rect(ctm, bounds);
bbox = fz_round_rect(bounds2); bbox = fz_round_rect(bounds2);
printf("reading page:%d,%d,%d,%d zoom:%.2f dpi:%.0f\n",bbox.x0,bbox.y0,bbox.x1,bbox.y1,zoom,dpi); printf("reading page:%d,%d,%d,%d zoom:%.2f dpi:%.0f\n",bbox.x0,bbox.y0,bbox.x1,bbox.y1,zoom,dpi);
zoom_value = zoom; kctx->zoom = zoom;
zoom *= shrink_factor; zoom *= shrink_factor;
dpi *= shrink_factor; dpi *= shrink_factor;
} while (bbox.x1 > max_page_width_pix | bbox.y1 > max_page_height_pix); } while (bbox.x1 > max_page_width_pix | bbox.y1 > max_page_height_pix);
@ -656,8 +642,8 @@ void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) {
fz_run_page(doc, page, dev, ctm, NULL); fz_run_page(doc, page, dev, ctm, NULL);
fz_free_device(dev); fz_free_device(dev);
if(gamma_correction >= 0.0) { if(kctx->contrast >= 0.0) {
fz_gamma_pixmap(ctx, pix, gamma_correction); fz_gamma_pixmap(ctx, pix, kctx->contrast);
} }
src = &_src; src = &_src;
@ -668,15 +654,22 @@ void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) {
bmp_free(src); bmp_free(src);
fz_drop_pixmap(ctx, pix); fz_drop_pixmap(ctx, pix);
kctx->page_width = masterinfo->bmp.width;
kctx->page_height = masterinfo->rows;
kctx->data = masterinfo->bmp.data;
} }
void k2pdfopt_djvu_reflow(ddjvu_page_t *page, ddjvu_context_t *ctx, \ void k2pdfopt_djvu_reflow(KOPTContext *kctx, ddjvu_page_t *page, ddjvu_context_t *ctx, \
ddjvu_render_mode_t mode, ddjvu_format_t *fmt) { ddjvu_render_mode_t mode, ddjvu_format_t *fmt) {
WILLUSBITMAP _src, *src; WILLUSBITMAP _src, *src;
ddjvu_rect_t prect; ddjvu_rect_t prect;
ddjvu_rect_t rrect; ddjvu_rect_t rrect;
k2pdfopt_init(kctx);
int i, iw, ih, idpi, status; int i, iw, ih, idpi, status;
double zoom = zoom_value; double zoom = kctx->zoom;
double dpi = 250*zoom; double dpi = 250*zoom;
while (!ddjvu_page_decoding_done(page)) while (!ddjvu_page_decoding_done(page))
@ -690,7 +683,7 @@ void k2pdfopt_djvu_reflow(ddjvu_page_t *page, ddjvu_context_t *ctx, \
prect.w = iw * dpi / idpi; prect.w = iw * dpi / idpi;
prect.h = ih * dpi / idpi; prect.h = ih * dpi / idpi;
printf("reading page:%d,%d,%d,%d dpi:%.0f\n",prect.x,prect.y,prect.w,prect.h,dpi); printf("reading page:%d,%d,%d,%d dpi:%.0f\n",prect.x,prect.y,prect.w,prect.h,dpi);
zoom_value = zoom; kctx->zoom = zoom;
zoom *= shrink_factor; zoom *= shrink_factor;
dpi *= shrink_factor; dpi *= shrink_factor;
} while (prect.w > max_page_width_pix | prect.h > max_page_height_pix); } while (prect.w > max_page_width_pix | prect.h > max_page_height_pix);
@ -718,19 +711,10 @@ void k2pdfopt_djvu_reflow(ddjvu_page_t *page, ddjvu_context_t *ctx, \
k2pdfopt_reflow_bmp(masterinfo, src); k2pdfopt_reflow_bmp(masterinfo, src);
bmp_free(src); bmp_free(src);
}
void k2pdfopt_rfbmp_size(int *width, int *height) {
*width = master_bmp_width;
*height = master_bmp_height;
}
void k2pdfopt_rfbmp_ptr(unsigned char** bmp_ptr_ptr) {
*bmp_ptr_ptr = masterinfo->bmp.data;
}
void k2pdfopt_rfbmp_zoom(double *zoom) { kctx->page_width = masterinfo->bmp.width;
*zoom = zoom_value; kctx->page_height = masterinfo->rows;
kctx->data = masterinfo->bmp.data;
} }
/* ansi.c */ /* ansi.c */

@ -26,19 +26,35 @@
#include <fitz/fitz-internal.h> #include <fitz/fitz-internal.h>
#include <libdjvu/ddjvuapi.h> #include <libdjvu/ddjvuapi.h>
void k2pdfopt_set_params(int bb_width, int bb_height, \ typedef unsigned char uint8_t;
double font_size, double page_margin, \ typedef struct KOPTContext {
double line_space, double word_space, \ int trim;
int wrapping, int straighten, \ int wrap;
int justification, int detect_indent, \ int indent;
int columns, double contrast, \ int rotate;
int rotation, double quality, \ int columns;
double defect_size, int trim_page); int offset_x;
void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx); int offset_y;
void k2pdfopt_djvu_reflow(ddjvu_page_t *page, ddjvu_context_t *ctx, ddjvu_render_mode_t mode, ddjvu_format_t *fmt); int dev_width;
void k2pdfopt_rfbmp_size(int *width, int *height); int dev_height;
void k2pdfopt_rfbmp_ptr(unsigned char** bmp_ptr_ptr); int page_width;
void k2pdfopt_rfbmp_zoom(double *zoom); int page_height;
int straighten;
int justification;
double zoom;
double margin;
double quality;
double contrast;
double defect_size;
double line_spacing;
double word_spacing;
uint8_t *data;
} KOPTContext;
void k2pdfopt_mupdf_reflow(KOPTContext *kc, fz_document *doc, fz_page *page, fz_context *ctx);
void k2pdfopt_djvu_reflow(KOPTContext *kc, ddjvu_page_t *page, ddjvu_context_t *ctx, ddjvu_render_mode_t mode, ddjvu_format_t *fmt);
#endif #endif

@ -0,0 +1,234 @@
/*
KindlePDFViewer: a KOPTContext abstraction
Copyright (C) 2012 Huang Xin <chrox.huang@gmail.com>
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 "koptcontext.h"
static int newKOPTContext(lua_State *L) {
int trim = luaL_optint(L, 1, 1);
int wrap = luaL_optint(L, 2, 1);
int indent = luaL_optint(L, 3, 1);
int rotate = luaL_optint(L, 4, 0);
int columns = luaL_optint(L, 5, 2);
int offset_x = luaL_optint(L, 6, 0);
int offset_y = luaL_optint(L, 7, 0);
int dev_width = luaL_optint(L, 8, 600);
int dev_height = luaL_optint(L, 9, 800);
int page_width = luaL_optint(L, 10, 600);
int page_height = luaL_optint(L, 11, 800);
int straighten = luaL_optint(L, 12, 0);
int justification = luaL_optint(L, 13, -1);
double zoom = luaL_optnumber(L, 14, (double) 1.0);
double margin = luaL_optnumber(L, 15, (double) 0.06);
double quality = luaL_optnumber(L, 16, (double) 1.0);
double contrast = luaL_optnumber(L, 17, (double) 1.0);
double defect_size = luaL_optnumber(L, 18, (double) 1.0);
double line_spacing = luaL_optnumber(L, 19, (double) 1.2);
double word_spacing = luaL_optnumber(L, 20, (double) 1.375);
uint8_t *data = NULL;
KOPTContext *kc = (KOPTContext*) lua_newuserdata(L, sizeof(KOPTContext));
kc->trim = trim;
kc->wrap = wrap;
kc->indent = indent;
kc->rotate = rotate;
kc->columns = columns;
kc->offset_x = offset_x;
kc->offset_y = offset_y;
kc->dev_width = dev_width;
kc->dev_height = dev_height;
kc->page_width = page_width;
kc->page_height = page_height;
kc->straighten = straighten;
kc->justification = justification;
kc->zoom = zoom;
kc->margin = margin;
kc->quality = quality;
kc->contrast = contrast;
kc->defect_size = defect_size;
kc->line_spacing = line_spacing;
kc->word_spacing = word_spacing;
kc->data = data;
luaL_getmetatable(L, "koptcontext");
lua_setmetatable(L, -2);
return 1;
}
static int kcSetTrim(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->trim = luaL_checkint(L, 2);
return 0;
}
static int kcSetWrap(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->wrap = luaL_checkint(L, 2);
return 0;
}
static int kcSetIndent(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->indent = luaL_checkint(L, 2);
return 0;
}
static int kcSetRotate(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->rotate = luaL_checkint(L, 2);
return 0;
}
static int kcSetColumns(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->columns = luaL_checkint(L, 2);
return 0;
}
static int kcSetOffset(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->offset_x = luaL_checkint(L, 2);
kc->offset_y = luaL_checkint(L, 3);
return 0;
}
static int kcGetOffset(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
lua_pushinteger(L, kc->offset_x);
lua_pushinteger(L, kc->offset_y);
return 2;
}
static int kcSetDeviceDim(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->dev_width = luaL_checkint(L, 2);
kc->dev_height = luaL_checkint(L, 3);
return 0;
}
static int kcGetPageDim(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
lua_pushinteger(L, kc->page_width);
lua_pushinteger(L, kc->page_height);
return 2;
}
static int kcSetStraighten(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->straighten = luaL_checkint(L, 2);
return 0;
}
static int kcSetJustification(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->justification = luaL_checkint(L, 2);
return 0;
}
static int kcSetZoom(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->zoom = luaL_checknumber(L, 2);
return 0;
}
static int kcGetZoom(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
lua_pushnumber(L, kc->zoom);
return 1;
}
static int kcSetMargin(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->margin = luaL_checknumber(L, 2);
return 0;
}
static int kcSetQuality(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->quality = luaL_checknumber(L, 2);
return 0;
}
static int kcSetContrast(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->contrast = luaL_checknumber(L, 2);
return 0;
}
static int kcSetDefectSize(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->defect_size = luaL_checknumber(L, 2);
return 0;
}
static int kcSetLineSpacing(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->line_spacing = luaL_checknumber(L, 2);
return 0;
}
static int kcSetWordSpacing(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
kc->word_spacing = luaL_checknumber(L, 2);
return 0;
}
static const struct luaL_Reg koptcontext_meth[] = {
{"setTrim", kcSetTrim},
{"setWrap", kcSetWrap},
{"setIndent", kcSetIndent},
{"setRotate", kcSetRotate},
{"setColumns", kcSetColumns},
{"setOffset", kcSetOffset},
{"getOffset", kcGetOffset},
{"setDeviceDim", kcSetDeviceDim},
{"getPageDim", kcGetPageDim},
{"setStraighten", kcSetStraighten},
{"setJustification", kcSetJustification},
{"setZoom", kcSetZoom},
{"getZoom", kcGetZoom},
{"setMargin", kcSetMargin},
{"setQuality", kcSetQuality},
{"setContrast", kcSetContrast},
{"setDefectSize", kcSetDefectSize},
{"setLineSpacing", kcSetLineSpacing},
{"setWordSpacing", kcSetWordSpacing},
{NULL, NULL}
};
static const struct luaL_Reg koptcontext_func[] = {
{"new", newKOPTContext},
{NULL, NULL}
};
int luaopen_koptcontext(lua_State *L) {
luaL_newmetatable(L, "koptcontext");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);
luaL_register(L, NULL, koptcontext_meth);
lua_pop(L, 1);
luaL_register(L, "KOPTContext", koptcontext_func);
return 1;
}

@ -0,0 +1,53 @@
/*
KindlePDFViewer: a KOPTContext abstraction
Copyright (C) 2012 Huang Xin <chrox.huang@gmail.com>
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 _KOPTCONTEXT_H
#define _KOPTCONTEXT_H
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
typedef unsigned char uint8_t;
typedef struct KOPTContext {
int trim;
int wrap;
int indent;
int rotate;
int columns;
int offset_x;
int offset_y;
int dev_width;
int dev_height;
int page_width;
int page_height;
int straighten;
int justification;
double zoom;
double margin;
double quality;
double contrast;
double defect_size;
double line_spacing;
double word_spacing;
uint8_t *data;
} KOPTContext;
int luaopen_koptcontext(lua_State *L);
#endif

@ -25,6 +25,7 @@
#include "blitbuffer.h" #include "blitbuffer.h"
#include "drawcontext.h" #include "drawcontext.h"
#include "koptcontext.h"
#include "pdf.h" #include "pdf.h"
#include "mupdfimg.h" #include "mupdfimg.h"
#include "djvu.h" #include "djvu.h"
@ -94,6 +95,7 @@ int main(int argc, char **argv) {
luaopen_blitbuffer(L); luaopen_blitbuffer(L);
luaopen_drawcontext(L); luaopen_drawcontext(L);
luaopen_koptcontext(L);
luaopen_einkfb(L); luaopen_einkfb(L);
luaopen_pdf(L); luaopen_pdf(L);
luaopen_djvu(L); luaopen_djvu(L);

48
pdf.c

@ -19,6 +19,7 @@
#include "blitbuffer.h" #include "blitbuffer.h"
#include "drawcontext.h" #include "drawcontext.h"
#include "koptcontext.h"
#include "pdf.h" #include "pdf.h"
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
@ -514,47 +515,20 @@ static int closePage(lua_State *L) {
static int reflowPage(lua_State *L) { static int reflowPage(lua_State *L) {
PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage"); PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext"); KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
int width = luaL_checkint(L, 4); // framebuffer size
int height = luaL_checkint(L, 5); k2pdfopt_mupdf_reflow(kc, page->doc->xref, page->page, page->doc->context);
double font_size = luaL_checknumber(L, 6);
double page_margin = luaL_checknumber(L, 7); return 0;
double line_spacing = luaL_checknumber(L, 8);
double word_spacing = luaL_checknumber(L, 9);
int text_wrap = luaL_checkint(L, 10);
int straighten = luaL_checkint(L, 11);
int justification = luaL_checkint(L, 12);
int detect_indent = luaL_checkint(L, 13);
int columns = luaL_checkint(L, 14);
double contrast = luaL_checknumber(L, 15);
int rotation = luaL_checkint(L, 16);
double quality = luaL_checknumber(L, 17);
double defect_size = luaL_checknumber(L, 18);
int trim_page = luaL_checkint(L, 19);
k2pdfopt_set_params(width, height, font_size, page_margin, line_spacing, word_spacing, \
text_wrap, straighten, justification, detect_indent, columns, contrast, rotation, \
quality, defect_size, trim_page);
k2pdfopt_mupdf_reflow(page->doc->xref, page->page, page->doc->context);
k2pdfopt_rfbmp_size(&width, &height);
k2pdfopt_rfbmp_zoom(&dc->zoom);
lua_pushnumber(L, (double)width);
lua_pushnumber(L, (double)height);
lua_pushnumber(L, (double)dc->zoom);
return 3;
} }
static int drawReflowedPage(lua_State *L) { static int drawReflowedPage(lua_State *L) {
uint8_t *pmptr = NULL;
PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage"); PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext"); KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer"); BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
uint8_t *koptr = kc->data;
uint8_t *bbptr = bb->data; uint8_t *bbptr = bb->data;
k2pdfopt_rfbmp_ptr(&pmptr);
int x_offset = 0; int x_offset = 0;
int y_offset = 0; int y_offset = 0;
@ -564,12 +538,12 @@ static int drawReflowedPage(lua_State *L) {
for(y = y_offset; y < bb->h; y++) { for(y = y_offset; y < bb->h; y++) {
for(x = x_offset/2; x < (bb->w/2); x++) { for(x = x_offset/2; x < (bb->w/2); x++) {
int p = x*2 - x_offset; int p = x*2 - x_offset;
bbptr[x] = (((pmptr[p + 1] & 0xF0) >> 4) | (pmptr[p] & 0xF0)) ^ 0xFF; bbptr[x] = (((koptr[p + 1] & 0xF0) >> 4) | (koptr[p] & 0xF0)) ^ 0xFF;
} }
bbptr += bb->pitch; bbptr += bb->pitch;
pmptr += bb->w; koptr += bb->w;
if (bb->w & 1) { if (bb->w & 1) {
bbptr[x] = 255 - (pmptr[x*2] & 0xF0); bbptr[x] = 255 - (koptr[x*2] & 0xF0);
} }
} }

Loading…
Cancel
Save