static int pdfLoadPage(PDFContext* ctx) { if (fullPageBuffer != NULL) { fz_droppixmap(fullPageBuffer); fullPageBuffer = NULL; } strcpy(lastPageError, "No error"); fz_error *error; fz_obj *obj; if (ctx->page) pdf_droppage(ctx->page); ctx->page = 0; // empty store to save memory if (ctx->xref->store) pdf_emptystore(ctx->xref->store); bk_pdf_resetbufferssize(); /* pdf_item *item; fz_obj *key; list<fz_obj *key> keys; for (item = ctx->xref->store->root; item; item = item->next) { if (item->kind == PDF_KIMAGE) keys << key; } list<fz_obj *key>::iterator it(keys.begin); while (it != keys.end()) { ++it; } */ obj = pdf_getpageobject(ctx->pages, ctx->pageno - 1, ctx->xref); error = pdf_loadpage(&ctx->page, ctx->xref, obj); if (error) { printf("errLP1: %s\n", error->msg); strcpy(lastPageError, error->msg); return -1; } //printf("\n\n debug tree ------------------------------------------------\n"); //fz_debugtree(ctx->page->tree); //optimizeNode(ctx->page->tree->root); //printf("\n\n OPT debug tree OPT ------------------------------------------------\n"); //fz_debugtree(ctx->page->tree); recalcScreenMediaBox(ctx); if (BKUser::options.pdfFastScroll) { pdfRenderFullPage(ctx); } return 0; }
void pdfmoz_loadpage(pdfmoz_t *moz, int pagenum) { page_t *page = moz->pages + pagenum; fz_error error; if (page->page) return; error = pdf_loadpage(&page->page, moz->xref, page->obj); if (error) pdfmoz_error(moz, error); }
void drawloadpage(int pagenum, struct benchmark *loadtimes) { fz_error *error; fz_obj *pageobj; long start; long end; long elapsed; fprintf(stderr, "draw %s page %d ", srcname, pagenum); if (benchmark && loadtimes) { fflush(stderr); gettime(&start); } pageobj = pdf_getpageobject(srcpages, pagenum - 1); error = pdf_loadpage(&drawpage, src, pageobj); if (error) die(error); if (benchmark && loadtimes) { gettime(&end); elapsed = end - start; if (elapsed < loadtimes->min) { loadtimes->min = elapsed; loadtimes->minpage = pagenum; } if (elapsed > loadtimes->max) { loadtimes->max = elapsed; loadtimes->maxpage = pagenum; } loadtimes->avg += elapsed; loadtimes->pages++; } fprintf(stderr, "mediabox [ %g %g %g %g ] rotate %d%s", drawpage->mediabox.x0, drawpage->mediabox.y0, drawpage->mediabox.x1, drawpage->mediabox.y1, drawpage->rotate, benchmark ? "" : "\n"); if (benchmark) fflush(stderr); }
JNIEXPORT void JNICALL Java_com_artifex_mupdf_MuPDFCore_gotoPageInternal( JNIEnv *env, jobject thiz, int page) { float zoom; fz_matrix ctm; fz_obj *pageobj; fz_bbox bbox; fz_error error; fz_device *dev; pdf_page *currentPage; /* In the event of an error, ensure we give a non-empty page */ pageWidth = 100; pageHeight = 100; LOGE("Goto page %d...", page); if (currentPageList != NULL) { fz_freedisplaylist(currentPageList); currentPageList = NULL; } pagenum = page; pageobj = pdf_getpageobject(xref, pagenum); if (pageobj == NULL) return; error = pdf_loadpage(¤tPage, xref, pageobj); if (error) return; zoom = resolution / 72; currentMediabox = currentPage->mediabox; currentRotate = currentPage->rotate; ctm = fz_translate(0, -currentMediabox.y1); ctm = fz_concat(ctm, fz_scale(zoom, -zoom)); ctm = fz_concat(ctm, fz_rotate(currentRotate)); bbox = fz_roundrect(fz_transformrect(ctm, currentMediabox)); pageWidth = bbox.x1-bbox.x0; pageHeight = bbox.y1-bbox.y0; /* Render to list */ currentPageList = fz_newdisplaylist(); dev = fz_newlistdevice(currentPageList); error = pdf_runpage(xref, currentPage, dev, fz_identity); pdf_freepage(currentPage); if (error) LOGE("cannot make displaylist from page %d", pagenum); fz_freedevice(dev); }
void drawloadpage(int pagenum) { fz_error *error; fz_obj *pageobj; pageobj = pdf_getpageobject(srcpages, pagenum - 1); error = pdf_loadpage(&drawpage, src, pageobj); if (error) die(error); fprintf(stderr, "draw %s page %d mediabox [ %g %g %g %g ] rotate %d\n", srcname, pagenum, drawpage->mediabox.x0, drawpage->mediabox.y0, drawpage->mediabox.x1, drawpage->mediabox.y1, drawpage->rotate); }
static void drawloadpage(int pagenum, struct benchmark *loadtimes) { fz_error error; fz_obj *pageobj; long start; long end; long elapsed; fprintf(stderr, "draw %s:%03d ", basename, pagenum); if (benchmark && loadtimes) { fflush(stderr); gettime(&start); } error = pdf_getpageobject(xref, pagenum, &pageobj); if (error) die(error); error = pdf_loadpage(&drawpage, xref, pageobj); if (error) die(error); if (benchmark && loadtimes) { gettime(&end); elapsed = end - start; if (elapsed < loadtimes->min) { loadtimes->min = elapsed; loadtimes->minpage = pagenum; } if (elapsed > loadtimes->max) { loadtimes->max = elapsed; loadtimes->maxpage = pagenum; } loadtimes->avg += elapsed; loadtimes->pages++; } if (benchmark) fflush(stderr); }
fz_error* processPage( soPdfFile* inFile, int pageNo, fz_rect *bbRect, int rectCount ) { fz_error *error; fz_obj *pageRef; pdf_page *pdfPage; fz_rect contentBox; fz_rect mediaBox; // Initialize for (int ctr = 0; ctr < rectCount; ctr++) bbRect[ctr] = fz_emptyrect; // Get the page reference and load the page contents pageRef = pdf_getpageobject(inFile->pageTree, pageNo); error = pdf_loadpage(&pdfPage, inFile->xref, pageRef); if (error != NULL) { // Ideally pdf_loadpage should render all the pages // and this should never happen return processErrorPage(inFile, pageRef, pageNo, bbRect, error); } // Get the bounding box for the page mediaBox = pdfPage->mediabox; float mbHeight = mediaBox.y1 - mediaBox.y0; // calculate the bounding box for all the elements in the page contentBox = fz_boundnode(pdfPage->tree->root, fz_identity()); float cbHeight = contentBox.y1 - contentBox.y0; // If there is nothing on the page we return nothing. // should we return an empty page instead ??? if (fz_isemptyrect(contentBox)) goto Cleanup; // if contentBox is bigger than mediaBox then there are some // elements that should not be display and hence we reset the // content box to media box if ((cbHeight > mbHeight) || ((contentBox.x1 - contentBox.x0) > (mediaBox.x1 - mediaBox.x0))) { // Calculate the new content box based on the content that is // inside the the media box and recalculate cbHeight contentBox = getContainingRect(pdfPage->tree->root, mediaBox); cbHeight = contentBox.y1 - contentBox.y0; } #ifdef _blahblah printf("-->Page %d\n", pageNo); bbdump(pdfPage->tree->root, 1); #endif // The rotation takes place when we insert the page into destination // If only the mupdf renderer could give accurate values of the // bounding box of all the elements in a page, the splitting would // be so much more easier without overlapping, cutting text, etc switch(p_mode) { case FitHeight: case FitWidth: bbRect[0] = contentBox; goto Cleanup; case Fit2xHeight: case Fit2xWidth: // Let the processing happen. break; case SmartFitHeight: case SmartFitWidth: default: return fz_throw("Mode(%d) not yet implemented.", p_mode); break; } // If the contentBox is 60% of mediaBox then do not split if (((cbHeight / mbHeight) * 100) <= 55) { bbRect[0] = contentBox; goto Cleanup; } // Get the first split contents from top. The box we specify is // top 55% (bottom + 45) of the contents bbRect[0] = contentBox; bbRect[0].y0 = bbRect[0].y0 + (float)(0.45 * cbHeight); bbRect[0] = getContainingRect(pdfPage->tree->root, bbRect[0]); // Check if the contents we got in first split is more than 40% // of the total contents float bbRect0Height = bbRect[0].y1 - bbRect[0].y0; if (((bbRect0Height / cbHeight) * 100) >= 40) { // The content is more than 40%. Put the rest of the // content in the second split and exit bbRect[1] = contentBox; bbRect[1].y1 = bbRect[1].y1 - bbRect0Height; // Adjust the split box height by X points to make sure // we get everything and dont have annoying tail cuts bbRect[0].y0 -= 2; goto Cleanup; } // Since the contents we got in first split is less than 40% // of the total contents, split the content in half with overlap float overlap = (cbHeight * (float)(p_overlap / 100)) / 2; bbRect[0] = contentBox; bbRect[0].y0 = bbRect[0].y0 + (float)(0.5 * cbHeight) - overlap; bbRect[1] = contentBox; bbRect[1].y1 = bbRect[1].y1 - (float)(0.5 * cbHeight) + overlap; Cleanup: // This function can overflow the stack when the pdf page // to be rendered is very complex. I had to increase the // stack size reserved for exe using compiler option pdf_droppage(pdfPage); return error; }
void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage) { char buf[256]; fz_error *error; fz_matrix ctm; fz_rect bbox; fz_obj *obj; if (loadpage) { wincursor(app, WAIT); if (app->page) pdf_droppage(app->page); app->page = nil; obj = pdf_getpageobject(app->pages, app->pageno - 1); error = pdf_loadpage(&app->page, app->xref, obj); if (error) pdfapp_error(app, error); sprintf(buf, "%s - %d/%d", app->doctitle, app->pageno, pdf_getpagecount(app->pages)); wintitle(app, buf); } if (drawpage) { wincursor(app, WAIT); if (app->image) fz_droppixmap(app->image); app->image = nil; ctm = pdfapp_viewctm(app); bbox = fz_transformaabb(ctm, app->page->mediabox); error = fz_rendertree(&app->image, app->rast, app->page->tree, ctm, fz_roundrect(bbox), 1); if (error) pdfapp_error(app, error); winconvert(app, app->image); } pdfapp_panview(app, app->panx, app->pany); if (app->shrinkwrap) { int w = app->image->w; int h = app->image->h; if (app->winw == w) app->panx = 0; if (app->winh == h) app->pany = 0; if (w > app->scrw * 90 / 100) w = app->scrw * 90 / 100; if (h > app->scrh * 90 / 100) h = app->scrh * 90 / 100; if (w != app->winw || h != app->winh) winresize(app, w, h); } winrepaint(app); wincursor(app, ARROW); }
static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage) { char buf[256]; fz_error error; fz_device *idev, *tdev, *mdev; fz_displaylist *list; fz_matrix ctm; fz_bbox bbox; fz_obj *obj; if (loadpage) { wincursor(app, WAIT); if (app->page) pdf_droppage(app->page); app->page = nil; //code change by kakai kno_clearselect(app); //code change by kakai pdf_flushxref(app->xref, 0); obj = pdf_getpageobject(app->xref, app->pageno); error = pdf_loadpage(&app->page, app->xref, obj); if (error) pdfapp_error(app, error); sprintf(buf, "%s - %d/%d", app->doctitle, app->pageno, app->pagecount); wintitle(app, buf); } if (drawpage) { wincursor(app, WAIT); ctm = pdfapp_viewctm(app); bbox = fz_roundrect(fz_transformrect(ctm, app->page->mediabox)); list = fz_newdisplaylist(); mdev = fz_newlistdevice(list); error = pdf_runcontentstream(mdev, fz_identity(), app->xref, app->page->resources, app->page->contents); if (error) pdfapp_error(app, error); fz_freedevice(mdev); if (app->image) fz_droppixmap(app->image); app->image = fz_newpixmapwithrect(pdf_devicergb, bbox); fz_clearpixmap(app->image, 0xFF); idev = fz_newdrawdevice(app->cache, app->image); fz_executedisplaylist(list, idev, ctm); fz_freedevice(idev); if (app->text) fz_freetextspan(app->text); app->text = fz_newtextspan(); tdev = fz_newtextdevice(app->text); fz_executedisplaylist(list, tdev, ctm); fz_freedevice(tdev); fz_freedisplaylist(list); //code change by kakai kno_allocselection(app); kno_applyselect(app); //code change by kakai winconvert(app, app->image); } pdfapp_panview(app, app->panx, app->pany); if (app->shrinkwrap) { int w = app->image->w; int h = app->image->h; if (app->winw == w) app->panx = 0; if (app->winh == h) app->pany = 0; if (w > app->scrw * 90 / 100) w = app->scrw * 90 / 100; if (h > app->scrh * 90 / 100) h = app->scrh * 90 / 100; if (w != app->winw || h != app->winh) winresize(app, w, h); } winrepaint(app); wincursor(app, ARROW); }