From f70ef06ea661c3ea712543203c2ffe3ccae7edd1 Mon Sep 17 00:00:00 2001 From: chrox Date: Mon, 5 Nov 2012 23:56:56 +0800 Subject: [PATCH] use koptcontext to keep dozons of parameters for k2pdfopt --- Makefile | 5 +- djvu.c | 48 +++-------- k2pdfopt.c | 92 ++++++++------------ k2pdfopt.h | 42 ++++++--- koptcontext.c | 234 ++++++++++++++++++++++++++++++++++++++++++++++++++ koptcontext.h | 53 ++++++++++++ kpdfview.c | 2 + pdf.c | 48 +++-------- 8 files changed, 381 insertions(+), 143 deletions(-) create mode 100644 koptcontext.c create mode 100644 koptcontext.h diff --git a/Makefile b/Makefile index 68fb3d9cb..40904367a 100644 --- a/Makefile +++ b/Makefile @@ -116,7 +116,7 @@ POPENNSLIB := $(POPENNSDIR)/libpopen_noshell.a all: kpdfview extr 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 $(CC) \ $(CFLAGS) \ @@ -126,6 +126,7 @@ kpdfview: kpdfview.o einkfb.o pdf.o k2pdfopt.o blitbuffer.o drawcontext.o input. k2pdfopt.o \ blitbuffer.o \ drawcontext.o \ + koptcontext.o \ input.o \ $(POPENNSLIB) \ util.o \ @@ -162,7 +163,7 @@ slider_watcher: slider_watcher.o $(POPENNSLIB) ft.o: %.o: %.c $(THIRDPARTYLIBS) $(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 $@ k2pdfopt.o: %.o: %.c diff --git a/djvu.c b/djvu.c index 78d9200e3..3874bdcfd 100644 --- a/djvu.c +++ b/djvu.c @@ -23,6 +23,7 @@ #include "blitbuffer.h" #include "drawcontext.h" +#include "koptcontext.h" #include "djvu.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -473,48 +474,21 @@ static int closePage(lua_State *L) { static int reflowPage(lua_State *L) { 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); - int width = luaL_checkint(L, 4); // framebuffer size - int height = luaL_checkint(L, 5); - double font_size = luaL_checknumber(L, 6); - double page_margin = luaL_checknumber(L, 7); - 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; + + k2pdfopt_djvu_reflow(kc, page->page_ref, page->doc->context, mode, page->doc->pixelformat); + + return 0; } static int drawReflowedPage(lua_State *L) { - uint8_t *pmptr = NULL; - 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"); + uint8_t *koptr = kc->data; uint8_t *bbptr = bb->data; - k2pdfopt_rfbmp_ptr(&pmptr); int x_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(x = x_offset/2; x < (bb->w/2); x++) { 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; - pmptr += bb->w; + koptr += bb->w; if (bb->w & 1) { - bbptr[x] = 255 - (pmptr[x*2] & 0xF0); + bbptr[x] = 255 - (koptr[x*2] & 0xF0); } } diff --git a/k2pdfopt.c b/k2pdfopt.c index 01a1bcbc9..56503f3f4 100644 --- a/k2pdfopt.c +++ b/k2pdfopt.c @@ -463,13 +463,9 @@ static void wpdfboxes_add_box(WPDFBOXES *boxes, WPDFBOX *box); static MASTERINFO _masterinfo, *masterinfo; 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_height_pix = 4000; 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) { 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 */ bmpregion_source_page_add(®ion, masterinfo, 1, pageinfo, (int) (0.25 * src_dpi + .5)); - master_bmp_width = masterinfo->bmp.width; - master_bmp_height = masterinfo->rows; - bmp_free(srcgrey); if (pageinfo != NULL) wpdfboxes_free(&pageinfo->boxes); } -void k2pdfopt_set_params(int bb_width, int bb_height, \ - double font_size, double page_margin, \ - double line_space, double word_space, \ - int wrapping, int straighten, \ - int justification, int detect_indent,\ - int columns, double contrast, \ - int rotation, double quality, \ - double defect_size, int trim_page) { - dst_userwidth = bb_width; // dst_width is adjusted in adjust_params_init - dst_userheight = bb_height; - zoom_value = font_size; - vertical_line_spacing = line_space; - word_spacing = word_space; - text_wrap = wrapping; - 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) { +static void k2pdfopt_init(KOPTContext *kctx) { + dst_userwidth = kctx->dev_width; // dst_width is adjusted in adjust_params_init + dst_userheight = kctx->dev_height; + vertical_line_spacing = kctx->line_spacing; + word_spacing = kctx->word_spacing; + text_wrap = kctx->wrap; + src_autostraighten = kctx->straighten; + preserve_indentation = kctx->indent; + max_columns = kctx->columns; + src_rot = kctx->rotate; + src_dpi = (int)300*kctx->quality; + defect_size_pts = kctx->defect_size; + + if (kctx->trim == 0) { mar_left = 0; mar_top = 0; mar_right = 0; @@ -588,19 +572,19 @@ void k2pdfopt_set_params(int bb_width, int bb_height, \ } // margin - dst_mar = page_margin; + dst_mar = kctx->margin; dst_martop = -1.0; dst_marbot = -1.0; dst_marleft = -1.0; dst_marright = -1.0; // justification - if (justification < 0) { + if (kctx->justification < 0) { dst_justify = -1; dst_fulljustify = -1; } - else if (justification <= 2) { - dst_justify = justification; + else if (kctx->justification <= 2) { + dst_justify = kctx->justification; dst_fulljustify = 0; } 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_pixmap *pix; fz_rect bounds,bounds2; @@ -617,8 +601,10 @@ void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) { fz_bbox bbox; WILLUSBITMAP _src, *src; + k2pdfopt_init(kctx); + double dpp,zoom; - zoom = zoom_value; + zoom = kctx->zoom; double dpi = 250*zoom*src_dpi/300; do { 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); 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); - zoom_value = zoom; + kctx->zoom = zoom; zoom *= shrink_factor; dpi *= shrink_factor; } 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_free_device(dev); - if(gamma_correction >= 0.0) { - fz_gamma_pixmap(ctx, pix, gamma_correction); + if(kctx->contrast >= 0.0) { + fz_gamma_pixmap(ctx, pix, kctx->contrast); } src = &_src; @@ -668,15 +654,22 @@ void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx) { bmp_free(src); 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) { WILLUSBITMAP _src, *src; ddjvu_rect_t prect; ddjvu_rect_t rrect; + + k2pdfopt_init(kctx); + int i, iw, ih, idpi, status; - double zoom = zoom_value; + double zoom = kctx->zoom; double dpi = 250*zoom; 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.h = ih * dpi / idpi; 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; dpi *= shrink_factor; } 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); 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) { - *zoom = zoom_value; + kctx->page_width = masterinfo->bmp.width; + kctx->page_height = masterinfo->rows; + kctx->data = masterinfo->bmp.data; } /* ansi.c */ diff --git a/k2pdfopt.h b/k2pdfopt.h index b3a6d6a08..72615efa1 100644 --- a/k2pdfopt.h +++ b/k2pdfopt.h @@ -26,19 +26,35 @@ #include #include -void k2pdfopt_set_params(int bb_width, int bb_height, \ - double font_size, double page_margin, \ - double line_space, double word_space, \ - int wrapping, int straighten, \ - int justification, int detect_indent, \ - int columns, double contrast, \ - int rotation, double quality, \ - double defect_size, int trim_page); -void k2pdfopt_mupdf_reflow(fz_document *doc, fz_page *page, fz_context *ctx); -void k2pdfopt_djvu_reflow(ddjvu_page_t *page, ddjvu_context_t *ctx, ddjvu_render_mode_t mode, ddjvu_format_t *fmt); -void k2pdfopt_rfbmp_size(int *width, int *height); -void k2pdfopt_rfbmp_ptr(unsigned char** bmp_ptr_ptr); -void k2pdfopt_rfbmp_zoom(double *zoom); +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; + +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 diff --git a/koptcontext.c b/koptcontext.c new file mode 100644 index 000000000..7ecc55198 --- /dev/null +++ b/koptcontext.c @@ -0,0 +1,234 @@ +/* + KindlePDFViewer: a KOPTContext abstraction + Copyright (C) 2012 Huang Xin + + 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 . +*/ + +#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; +} diff --git a/koptcontext.h b/koptcontext.h new file mode 100644 index 000000000..977f74210 --- /dev/null +++ b/koptcontext.h @@ -0,0 +1,53 @@ +/* + KindlePDFViewer: a KOPTContext abstraction + Copyright (C) 2012 Huang Xin + + 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 . +*/ +#ifndef _KOPTCONTEXT_H +#define _KOPTCONTEXT_H + +#include +#include +#include + +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 diff --git a/kpdfview.c b/kpdfview.c index 4a3bd7b33..6e31e3cc5 100644 --- a/kpdfview.c +++ b/kpdfview.c @@ -25,6 +25,7 @@ #include "blitbuffer.h" #include "drawcontext.h" +#include "koptcontext.h" #include "pdf.h" #include "mupdfimg.h" #include "djvu.h" @@ -94,6 +95,7 @@ int main(int argc, char **argv) { luaopen_blitbuffer(L); luaopen_drawcontext(L); + luaopen_koptcontext(L); luaopen_einkfb(L); luaopen_pdf(L); luaopen_djvu(L); diff --git a/pdf.c b/pdf.c index d51332927..528572565 100644 --- a/pdf.c +++ b/pdf.c @@ -19,6 +19,7 @@ #include "blitbuffer.h" #include "drawcontext.h" +#include "koptcontext.h" #include "pdf.h" #include #include @@ -514,47 +515,20 @@ static int closePage(lua_State *L) { static int reflowPage(lua_State *L) { PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage"); - DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext"); - int width = luaL_checkint(L, 4); // framebuffer size - int height = luaL_checkint(L, 5); - double font_size = luaL_checknumber(L, 6); - double page_margin = luaL_checknumber(L, 7); - 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; + KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext"); + + k2pdfopt_mupdf_reflow(kc, page->doc->xref, page->page, page->doc->context); + + return 0; } static int drawReflowedPage(lua_State *L) { - uint8_t *pmptr = NULL; - 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"); + uint8_t *koptr = kc->data; uint8_t *bbptr = bb->data; - k2pdfopt_rfbmp_ptr(&pmptr); int x_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(x = x_offset/2; x < (bb->w/2); x++) { 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; - pmptr += bb->w; + koptr += bb->w; if (bb->w & 1) { - bbptr[x] = 255 - (pmptr[x*2] & 0xF0); + bbptr[x] = 255 - (koptr[x*2] & 0xF0); } }