Skip to content

Commit

Permalink
add native to reflow transform in koptcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
chrox committed Oct 12, 2013
1 parent f798639 commit aba8fee
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
26 changes: 21 additions & 5 deletions djvu.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,27 @@ static int getPagePix(lua_State *L) {
ddjvu_rect_t rrect;
int px, py, pw, ph, rx, ry, rw, rh, status;

prect.x = 0;
prect.y = 0;
prect.w = ddjvu_page_get_width(page->page_ref);
prect.h = ddjvu_page_get_height(page->page_ref);
rrect = prect;
px = 0;
py = 0;
pw = ddjvu_page_get_width(page->page_ref);
ph = ddjvu_page_get_height(page->page_ref);
prect.x = px;
prect.y = py;

rx = (int)kctx->bbox.x0;
ry = (int)kctx->bbox.y0;
rw = (int)(kctx->bbox.x1 - kctx->bbox.x0);
rh = (int)(kctx->bbox.y1 - kctx->bbox.y0);

float scale = kctx->zoom;

prect.w = pw * scale;
prect.h = ph * scale;
rrect.x = rx * scale;
rrect.y = ry * scale;
rrect.w = rw * scale;
rrect.h = rh * scale;
printf("rendering page:%d,%d,%d,%d\n",rrect.x,rrect.y,rrect.w,rrect.h);

WILLUSBITMAP *dst = &kctx->dst;
bmp_init(dst);
Expand Down
35 changes: 32 additions & 3 deletions koptcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,40 @@ static int kcReflowToNativePosTransform(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
int x = luaL_checknumber(L, 2);
int y = luaL_checknumber(L, 3);
int i, x0, y0;
int i, x0, y0, w0, h0;
for (i = 0; i < kc->rectmaps.n; i++) {
WRECTMAP * rectmap = &kc->rectmaps.wrectmap[i];
if (wrectmap_inside(rectmap, x, y)) {
x0 = (rectmap->coords[0].x + rectmap->coords[2].x/2)/kc->zoom + kc->bbox.x0;
y0 = (rectmap->coords[0].y + rectmap->coords[2].y/2)/kc->zoom + kc->bbox.y0;
w0 = rectmap->coords[2].x*rectmap->srcdpiw/kc->dev_dpi;
h0 = rectmap->coords[2].y*rectmap->srcdpih/kc->dev_dpi;
x0 = (rectmap->coords[0].x + w0/2)/kc->zoom + kc->bbox.x0;
y0 = (rectmap->coords[0].y + h0/2)/kc->zoom + kc->bbox.y0;
lua_pushinteger(L, x0);
lua_pushinteger(L, y0);
return 2;
}
}
return 0;
}

int wrectmap_native_inside(WRECTMAP *wrmap, int xc, int yc) {
return(wrmap->coords[0].x <= xc && wrmap->coords[0].y <= yc
&& wrmap->coords[0].x + wrmap->coords[2].x >= xc
&& wrmap->coords[0].y + wrmap->coords[2].y >= yc);
}

static int kcNativeToReflowPosTransform(lua_State *L) {
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
int x = luaL_checknumber(L, 2);
int y = luaL_checknumber(L, 3);
int i, x0, y0;
x = (x - kc->bbox.x0) * kc->zoom;
y = (y - kc->bbox.y0) * kc->zoom;
for (i = 0; i < kc->rectmaps.n; i++) {
WRECTMAP * rectmap = &kc->rectmaps.wrectmap[i];
if (wrectmap_native_inside(rectmap, x, y)) {
x0 = rectmap->coords[1].x + rectmap->coords[2].x/2;
y0 = rectmap->coords[1].y + rectmap->coords[2].y/2;
lua_pushinteger(L, x0);
lua_pushinteger(L, y0);
return 2;
Expand Down Expand Up @@ -482,6 +510,7 @@ static const struct luaL_Reg koptcontext_meth[] = {
{"getReflowedWordBoxes", kcGetReflowedWordBoxes},
{"getNativeWordBoxes", kcGetNativeWordBoxes},
{"reflowToNativePosTransform", kcReflowToNativePosTransform},
{"nativeToReflowPosTransform", kcNativeToReflowPosTransform},
{"getTOCRWord", kcGetTOCRWord},

{"freeOCR", kcFreeOCREngine},
Expand Down
2 changes: 1 addition & 1 deletion libk2pdfopt
Submodule libk2pdfopt updated 1 files
+4 −4 koptreflow.c
15 changes: 10 additions & 5 deletions pdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,22 +772,27 @@ static int getPagePix(lua_State *L) {
PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage");
KOPTContext *kctx = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
fz_device *dev;
fz_pixmap *pix = NULL;
fz_pixmap *pix;
fz_matrix ctm;
fz_rect bounds;
fz_irect bbox;

pix = NULL;
fz_var(pix);
bounds.x0 = kctx->bbox.x0;
bounds.y0 = kctx->bbox.y0;
bounds.x1 = kctx->bbox.x1;
bounds.y1 = kctx->bbox.y1;

fz_bound_page(page->doc->xref, page->page, &bounds);
fz_transform_rect(&bounds, &fz_identity);
fz_round_rect(&bbox, &bounds);
fz_scale(&ctm, kctx->zoom, kctx->zoom);
fz_transform_rect(&bounds, &ctm);
fz_round_rect(&bbox, &bounds);

fz_try(page->doc->context) {
pix = fz_new_pixmap_with_bbox(page->doc->context, fz_device_gray(page->doc->context), &bbox);
fz_clear_pixmap_with_value(page->doc->context, pix, 0xff);
dev = fz_new_draw_device(page->doc->context, pix);
fz_run_page(page->doc->xref, page->page, dev, &fz_identity, NULL);
fz_run_page(page->doc->xref, page->page, dev, &ctm, NULL);
}
fz_always(page->doc->context) {
fz_free_device(dev);
Expand Down

0 comments on commit aba8fee

Please sign in to comment.