static void update_title(void) { static char buf[256]; size_t n = strlen(title); if (n > 50) sprintf(buf, "...%s - %d / %d", title + n - 50, currentpage + 1, fz_count_pages(ctx, doc)); else sprintf(buf, "%s - %d / %d", title, currentpage + 1, fz_count_pages(ctx, doc)); glfwSetWindowTitle(window, buf); }
static void merge_range(char *range) { int page, spage, epage, src_pagecount, des_pagecount; char *spec, *dash; pdf_graft_map *graft_map; src_pagecount = fz_count_pages(ctx, (fz_document*) doc_src); des_pagecount = fz_count_pages(ctx, (fz_document*) doc_des); spec = fz_strsep(&range, ","); graft_map = pdf_new_graft_map(ctx, doc_src); fz_try(ctx) { while (spec) { dash = strchr(spec, '-'); if (dash == spec) spage = epage = src_pagecount; else spage = epage = atoi(spec); if (dash) { if (strlen(dash) > 1) epage = atoi(dash + 1); else epage = src_pagecount; } spage = fz_clampi(spage, 1, src_pagecount); epage = fz_clampi(epage, 1, src_pagecount); if (spage < epage) for (page = spage; page <= epage; page++, des_pagecount++) page_merge(page, des_pagecount + 1, graft_map); else for (page = spage; page >= epage; page--, des_pagecount++) page_merge(page, des_pagecount + 1, graft_map); spec = fz_strsep(&range, ","); } } fz_always(ctx) { pdf_drop_graft_map(ctx, graft_map); } fz_catch(ctx) { fz_rethrow(ctx); } }
JNIEXPORT int JNICALL JNI_FN(MuPDF_countPagesInternal)(JNIEnv *env, jobject thiz) { globals *glo = get_globals(env, thiz); return fz_count_pages(glo->doc); }
static void reload(void) { fz_drop_outline(ctx, outline); fz_drop_document(ctx, doc); doc = fz_open_document(ctx, filename); if (fz_needs_password(ctx, doc)) { if (!fz_authenticate_password(ctx, doc, password)) { fprintf(stderr, "Invalid password.\n"); exit(1); } } fz_layout_document(ctx, doc, layout_w, layout_h, layout_em); fz_try(ctx) outline = fz_load_outline(ctx, doc); fz_catch(ctx) outline = NULL; pdf = pdf_specifics(ctx, doc); if (pdf) pdf_enable_js(ctx, pdf); currentpage = fz_clampi(currentpage, 0, fz_count_pages(ctx, doc) - 1); render_page(); update_title(); }
static void processpages(fz_context *ctx, fz_document *doc) { int page, pagecount; pagecount = fz_count_pages(ctx, doc); for (page = 1; page <= pagecount; ++page) processpage(ctx, doc, page); }
int main(int argc, char **argv) { /* get filename */ char *filename = NULL; if (argc != 2) { printf("usage: %s filename.pdf\n", argv[0]); return 1; } if (strcmp(argv[1], "--help") == 0) { printf("usage: %s filename.pdf\n", argv[0]); return 0; } filename = argv[1]; /* open document */ fz_context *context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); fz_document *document = fz_open_document(context, filename); /* print page count */ printf("page count: %d\n", fz_count_pages(document)); fz_close_document(document); fz_free_context(context); return 0; }
static void jump_to_page(int newpage) { newpage = fz_clampi(newpage, 0, fz_count_pages(ctx, doc) - 1); clear_future(); push_history(); currentpage = newpage; push_history(); }
VALUE doc_each_page(VALUE self){ Document *doc; Data_Get_Struct(self, Document, doc); for( int page=0; page < fz_count_pages(doc->fz_document); page++){ rb_yield( doc_page_at(self, INT2FIX(page)) ); } return self; }
static void doc_gc_mark(Document *doc){ VALUE page; printf("GC Mark Document: %016" PRIxPTR "\n", (uintptr_t)doc ); for ( int pg=0; pg < fz_count_pages(doc->fz_document); pg++ ){ page = doc->pages[pg]; if ( page ){ printf("GC Mark Document: %016" PRIxPTR "\n", (uintptr_t)doc ); rb_gc_mark(page); } } }
void Mpdf::loadPdf(QString path) { currentPath = removeBackRef(path); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); doc = fz_open_document(ctx, currentPath.toStdString().c_str()); pageCount = fz_count_pages(doc); currentPage = 1; currentZoom = 60; enablePageNumber(); showPage(); hideTop(); }
static void doc_gc_free( Document *doc ) { VALUE rb_page; printf("GC Free Document: %016" PRIxPTR "\n", (uintptr_t)doc ); for ( int pg=0; pg < fz_count_pages(doc->fz_document); pg++ ){ rb_page = doc->pages[pg]; if (rb_page){ page_free(rb_page); } } fz_close_document(doc->fz_document); free(doc->pages); free(doc); }
int open_pdf(fz_context *context, char *filename) { fz_stream *file; int faulty; unsigned int i; /* char * s; */ faulty = 0; /* Resolution */ printf("Opening: %s\n", filename); fz_try(context) { file = fz_open_file(context, filename); doc = (fz_document *) pdf_open_document_with_stream(file); /* TODO Password */ fz_close(file); } fz_catch (context) { fprintf(stderr, "Cannot open: %s\n", filename); faulty = 1; } if (faulty) return faulty; /* XXX need error handling */ pagec = fz_count_pages(doc); pages = malloc(sizeof(struct least_page_info) * pagec); #if 0 return 0; #endif for(i = 0; i < pagec; i++) { pages[i].rendering = 0; pages[i].texture = 0; /* page_to_texture(context, doc, i); */ } page_to_texture(context, doc, 0); printf("Done opening\n"); return 0; }
void ViewerWidget::load(const QByteArray &data) { scene->clear(); QList<QGraphicsItem *> items; QPixmap pixmap; if (pixmap.loadFromData(data)) { items << new QGraphicsPixmapItem(pixmap); } else if (data.startsWith("%PDF")) { fz_stream *stream = fz_open_memory(context, (unsigned char *)data.constData(), data.length()); fz_document *doc = fz_open_document_with_stream(context, ".pdf", stream); fz_close(stream); int pagecount = fz_count_pages(doc); for (int i = 0; i < pagecount; i++) { fz_page *page = fz_load_page(doc, i); fz_rect bounds; fz_bound_page(doc, page, &bounds); fz_display_list *list = fz_new_display_list(context); fz_device *dev = fz_new_list_device(context, list); fz_run_page(doc, page, dev, &fz_identity, NULL); fz_free_device(dev); fz_free_page(doc, page); PageItem *item = new PageItem(context, list, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0); item->setCacheMode(QGraphicsItem::DeviceCoordinateCache); items << item; } fz_close_document(doc); } else { scene->setSceneRect(0, 0, 0, 0); return; } int top = 0; QPen outline(Qt::lightGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin); outline.setCosmetic(true); foreach (QGraphicsItem *item, items) { QGraphicsRectItem *rim = new QGraphicsRectItem(item->boundingRect()); item->setPos(0, top); rim->setPos(0, top); rim->setPen(outline); rim->setBrush(Qt::NoBrush); scene->addItem(rim); scene->addItem(item); top += item->boundingRect().height() + SPACING; }
static void do_outline(fz_outline *node, int outline_w) { static char *id = "outline"; static int outline_scroll_y = 0; static int saved_outline_scroll_y = 0; static int saved_ui_y = 0; int outline_h; int total_h; outline_w -= ui.lineheight; outline_h = window_h; total_h = measure_outline_height(outline); if (ui.x >= 0 && ui.x < outline_w && ui.y >= 0 && ui.y < outline_h) { ui.hot = id; if (!ui.active && ui.middle) { ui.active = id; saved_ui_y = ui.y; saved_outline_scroll_y = outline_scroll_y; } } if (ui.active == id) outline_scroll_y = saved_outline_scroll_y + (saved_ui_y - ui.y) * 5; if (ui.hot == id) outline_scroll_y -= ui.scroll_y * ui.lineheight * 3; ui_scrollbar(outline_w, 0, outline_w+ui.lineheight, outline_h, &outline_scroll_y, outline_h, total_h); glScissor(0, 0, outline_w, outline_h); glEnable(GL_SCISSOR_TEST); glColor4f(1, 1, 1, 1); glRectf(0, 0, outline_w, outline_h); do_outline_imp(outline, fz_count_pages(ctx, doc), 0, outline_w, 10, -outline_scroll_y); glDisable(GL_SCISSOR_TEST); }
pdf::pdf(const char *filepath) { _filepath = filepath; _good = true; _image_name = nullptr; _needs_password = false; _size = 0; // Create a context to hold the exception stack and various caches. _ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!_ctx) { _good = false; return; } fz_try(_ctx) { // Register the default file types. fz_register_document_handlers(_ctx); // Open the PDF, XPS or CBZ document. _doc = fz_open_document(_ctx, filepath); if (fz_needs_password(_ctx, _doc)) { _needs_password = true; _good = false; } else { // Retrieve the number of pages (not used in this example). _size = fz_count_pages(_ctx, _doc); } } fz_catch(_ctx) { _doc = 0; _good = false; } }
VALUE doc_page_at(VALUE doc, VALUE pg){ VALUE page; int page_number; Document *document; Data_Get_Struct( doc, Document, document); page_number = FIX2INT(pg); if ( page_number < 0 || page_number > fz_count_pages(document->fz_document) ){ rb_raise(rb_eArgError, "Page Number must be between 0..%d",page_number); } page = document->pages[page_number]; if ( ! page ){ page = page_create(document, page_number); rb_gc_mark(page); document->pages[page_number] = page; } printf("Doc page_at: %016" PRIxPTR " %lu\n", (uintptr_t)document, page); return page; }
int wmupdf_numpages(char *filename) { fz_context *ctx; fz_document *doc; int np; doc=NULL; ctx = fz_new_context(NULL,NULL,FZ_STORE_DEFAULT); if (!ctx) return(-1); fz_try(ctx) { doc=fz_open_document(ctx,filename); } fz_catch(ctx) { fz_free_context(ctx); return(-2); } np=fz_count_pages(doc); fz_close_document(doc); fz_flush_warnings(ctx); fz_free_context(ctx); return(np); }
VALUE doc_open(VALUE klass, VALUE path){ Document *document; fz_document *doc; size_t pg_size; VALUE str = rb_funcall(path, to_s, 0); fz_try(FZ_CONTEXT) { doc = fz_open_document(FZ_CONTEXT, StringValuePtr(str)); } fz_catch(FZ_CONTEXT) { rb_raise(rb_eArgError, "Unable to open file"); } pg_size = sizeof(VALUE)*fz_count_pages(doc); document = malloc(sizeof(Document)); printf("New Document: %016" PRIxPTR "\n", (uintptr_t)document ); document->fz_document = doc; document->pages = malloc(pg_size); memset( document->pages, 0, pg_size); // important - set the pages to 0 so we can tell which are later allocated VALUE self = Data_Wrap_Struct( rb_Doc, &doc_gc_mark, &doc_gc_free, document ); return self; }
static void smart_move_forward(void) { if (scroll_y + canvas_h >= page_tex.h) { if (scroll_x + canvas_w >= page_tex.w) { if (currentpage + 1 < fz_count_pages(ctx, doc)) { scroll_x = 0; scroll_y = 0; currentpage += 1; } } else { scroll_y = 0; scroll_x += canvas_w * 9 / 10; } } else { scroll_y += canvas_h * 9 / 10; } }
void render(char *filename, int pagenumber, int zoom, int rotation) { // Create a context to hold the exception stack and various caches. fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); // Open the PDF, XPS or CBZ document. fz_document *doc = fz_open_document(ctx, filename); // Retrieve the number of pages (not used in this example). int pagecount = fz_count_pages(doc); // Load the page we want. Page numbering starts from zero. fz_page *page = fz_load_page(doc, pagenumber - 1); // Calculate a transform to use when rendering. This transform // contains the scale and rotation. Convert zoom percentage to a // scaling factor. Without scaling the resolution is 72 dpi. fz_matrix transform; fz_rotate(&transform, rotation); fz_pre_scale(&transform, zoom / 100.0f, zoom / 100.0f); // Take the page bounds and transform them by the same matrix that // we will use to render the page. fz_rect bounds; fz_bound_page(doc, page, &bounds); fz_transform_rect(&bounds, &transform); // Create a blank pixmap to hold the result of rendering. The // pixmap bounds used here are the same as the transformed page // bounds, so it will contain the entire page. The page coordinate // space has the origin at the top left corner and the x axis // extends to the right and the y axis extends down. fz_irect bbox; fz_round_rect(&bbox, &bounds); fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox); fz_clear_pixmap_with_value(ctx, pix, 0xff); // A page consists of a series of objects (text, line art, images, // gradients). These objects are passed to a device when the // interpreter runs the page. There are several devices, used for // different purposes: // // draw device -- renders objects to a target pixmap. // // text device -- extracts the text in reading order with styling // information. This text can be used to provide text search. // // list device -- records the graphic objects in a list that can // be played back through another device. This is useful if you // need to run the same page through multiple devices, without // the overhead of parsing the page each time. // Create a draw device with the pixmap as its target. // Run the page with the transform. fz_device *dev = fz_new_draw_device(ctx, pix); fz_run_page(doc, page, dev, &transform, NULL); fz_free_device(dev); // Save the pixmap to a file. fz_write_png(ctx, pix, "out.png", 0); // Clean up. fz_drop_pixmap(ctx, pix); fz_free_page(doc, page); fz_close_document(doc); fz_free_context(ctx); }
static void drawrange(fz_context *ctx, fz_document *doc, char *range) { int page, spage, epage, pagecount; char *spec, *dash; pagecount = fz_count_pages(doc); spec = fz_strsep(&range, ","); while (spec) { dash = strchr(spec, '-'); if (dash == spec) spage = epage = pagecount; else spage = epage = atoi(spec); if (dash) { if (strlen(dash) > 1) epage = atoi(dash + 1); else epage = pagecount; } spage = fz_clampi(spage, 1, pagecount); epage = fz_clampi(epage, 1, pagecount); current_doc = doc; current_ctx = ctx; current_epage = epage; for (page = spage; page <= epage; ) { if (page == 0) page = 1; current_page = page; drawpage(ctx, doc, page); draw_decors(page, epage); yutani_flip(yctx, window); yutani_msg_t * m = NULL; while (1) { m = yutani_poll(yctx); if (m) { switch (m->type) { case YUTANI_MSG_KEY_EVENT: { struct yutani_msg_key_event * ke = (void*)m->data; if (ke->event.action == KEY_ACTION_DOWN) { switch (ke->event.keycode) { case KEY_ESCAPE: case 'q': yutani_close(yctx, window); exit(0); break; case KEY_ARROW_LEFT: case 'a': page--; goto _continue; case KEY_ARROW_RIGHT: case 's': page++; if (page > epage) page = epage; goto _continue; case KEY_F12: toggle_decorations(); goto _continue; default: break; } } } break; case YUTANI_MSG_SESSION_END: yutani_close(yctx, window); exit(0); break; case YUTANI_MSG_WINDOW_FOCUS_CHANGE: { struct yutani_msg_window_focus_change * wf = (void*)m->data; yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid); if (win) { win->focused = wf->focused; goto _continue; } } break; case YUTANI_MSG_RESIZE_OFFER: { struct yutani_msg_window_resize * wr = (void*)m->data; resize_finish(wr->width, wr->height); goto _continue; } break; case YUTANI_MSG_WINDOW_MOUSE_EVENT: if (decor_handle_event(yctx, m) == DECOR_CLOSE) { yutani_close(yctx, window); exit(0); } break; default: break; } } free(m); } _continue: free(m); } spec = fz_strsep(&range, ","); } }
static void do_app(void) { if (ui.key == KEY_F4 && ui.mod == GLFW_MOD_ALT) quit(); if (ui.down || ui.middle || ui.right || ui.key) showinfo = 0; if (!ui.focus && ui.key) { switch (ui.key) { case 'q': quit(); break; case 'm': if (number == 0) push_history(); else if (number > 0 && number < nelem(marks)) marks[number] = currentpage; break; case 't': if (number == 0) { if (history_count > 0) pop_history(); } else if (number > 0 && number < nelem(marks)) { jump_to_page(marks[number]); } break; case 'T': if (number == 0) { if (future_count > 0) pop_future(); } break; case 'N': search_dir = -1; if (search_hit_page == currentpage) search_page = currentpage + search_dir; else search_page = currentpage; if (search_page >= 0 && search_page < fz_count_pages(ctx, doc)) { search_hit_page = -1; if (search_needle) search_active = 1; } break; case 'n': search_dir = 1; if (search_hit_page == currentpage) search_page = currentpage + search_dir; else search_page = currentpage; if (search_page >= 0 && search_page < fz_count_pages(ctx, doc)) { search_hit_page = -1; if (search_needle) search_active = 1; } break; case 'f': toggle_fullscreen(); break; case 'w': shrinkwrap(); break; case 'r': reload(); break; case 'o': toggle_outline(); break; case 'W': auto_zoom_w(); break; case 'H': auto_zoom_h(); break; case 'Z': auto_zoom(); break; case 'z': currentzoom = number > 0 ? number : DEFRES; break; case '<': currentpage -= 10 * fz_maxi(number, 1); break; case '>': currentpage += 10 * fz_maxi(number, 1); break; case ',': case KEY_PAGE_UP: currentpage -= fz_maxi(number, 1); break; case '.': case KEY_PAGE_DOWN: currentpage += fz_maxi(number, 1); break; case 'b': number = fz_maxi(number, 1); while (number--) smart_move_backward(); break; case ' ': number = fz_maxi(number, 1); while (number--) smart_move_forward(); break; case 'g': jump_to_page(number - 1); break; case 'G': jump_to_page(fz_count_pages(ctx, doc) - 1); break; case '+': currentzoom = zoom_in(currentzoom); break; case '-': currentzoom = zoom_out(currentzoom); break; case '[': currentrotate += 90; break; case ']': currentrotate -= 90; break; case 'l': showlinks = !showlinks; break; case 'i': showinfo = !showinfo; break; case '/': search_dir = 1; showsearch = 1; search_input.p = search_input.text; search_input.q = search_input.end; break; case '?': search_dir = -1; showsearch = 1; search_input.p = search_input.text; search_input.q = search_input.end; break; case KEY_UP: scroll_y -= 10; break; case KEY_DOWN: scroll_y += 10; break; case KEY_LEFT: scroll_x -= 10; break; case KEY_RIGHT: scroll_x += 10; break; } if (ui.key >= '0' && ui.key <= '9') number = number * 10 + ui.key - '0'; else number = 0; currentpage = fz_clampi(currentpage, 0, fz_count_pages(ctx, doc) - 1); currentzoom = fz_clamp(currentzoom, MINRES, MAXRES); while (currentrotate < 0) currentrotate += 360; while (currentrotate >= 360) currentrotate -= 360; if (search_hit_page != currentpage) search_hit_page = -1; /* clear highlights when navigating */ ui_needs_update = 1; ui.key = 0; /* we ate the key event, so zap it */ } }
int DrPDFExtractor::GetPageCount() { return fz_count_pages(m_doc); }
VALUE doc_page_count(VALUE self){ Document *doc; Data_Get_Struct(self,Document, doc); return INT2FIX( fz_count_pages(doc->fz_document) ); }
static void run_main_loop(void) { glViewport(0, 0, window_w, window_h); glClearColor(0.3f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, window_w, window_h, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ui_begin(); if (search_active) { float start_time = glfwGetTime(); if (ui.key == KEY_ESCAPE) search_active = 0; /* ignore events during search */ ui.key = ui.mod = 0; ui.down = ui.middle = ui.right = 0; while (glfwGetTime() < start_time + 0.2) { search_hit_count = fz_search_page_number(ctx, doc, search_page, search_needle, search_hit_bbox, nelem(search_hit_bbox)); if (search_hit_count) { search_active = 0; search_hit_page = search_page; jump_to_page(search_hit_page); break; } else { search_page += search_dir; if (search_page < 0 || search_page == fz_count_pages(ctx, doc)) { search_active = 0; break; } } } /* keep searching later */ if (search_active) ui_needs_update = 1; } do_app(); canvas_w = window_w - canvas_x; canvas_h = window_h - canvas_y; do_canvas(); if (showinfo) do_info(); if (showoutline) do_outline(outline, canvas_x); if (showsearch) { int state = ui_input(canvas_x, 0, canvas_x + canvas_w, ui.lineheight+4, &search_input); if (state == -1) { ui.focus = NULL; showsearch = 0; } else if (state == 1) { ui.focus = NULL; showsearch = 0; search_page = -1; if (search_needle) { fz_free(ctx, search_needle); search_needle = NULL; } if (search_input.end > search_input.text) { search_needle = fz_strdup(ctx, search_input.text); search_active = 1; search_page = currentpage; } } ui_needs_update = 1; } if (search_active) { char buf[256]; sprintf(buf, "Searching page %d of %d.", search_page + 1, fz_count_pages(ctx, doc)); ui_label_draw(canvas_x, 0, canvas_x + canvas_w, ui.lineheight+4, buf); } ui_end(); glfwSwapBuffers(window); ogl_assert(ctx, "swap buffers"); }
int bmpmupdf_pdffile_to_bmp(WILLUSBITMAP *bmp,char *filename,int pageno,double dpi, int bpp) { fz_context *ctx; fz_colorspace *colorspace; fz_document *doc; fz_page *page; fz_display_list *list; fz_device *dev; fz_pixmap *pix; double dpp; fz_rect bounds,bounds2; fz_matrix ctm; fz_irect bbox; // fz_glyph_cache *glyphcache; // fz_error error; int np,status; dev=NULL; list=NULL; page=NULL; doc=NULL; status=0; if (pageno<1) return(-99); ctx = fz_new_context(NULL,NULL,FZ_STORE_DEFAULT); if (!ctx) return(-1); fz_try(ctx) { fz_register_document_handlers(ctx); fz_set_aa_level(ctx,8); /* Sumatra version of MuPDF v1.4 -- use locally installed fonts */ pdf_install_load_system_font_funcs(ctx); // fz_accelerate(); // glyphcache=fz_new_glyph_cache(); colorspace=(bpp==8 ? fz_device_gray(ctx) : fz_device_rgb(ctx)); fz_try(ctx) { doc=fz_open_document(ctx,filename); } fz_catch(ctx) { fz_free_context(ctx); return(-1); } /* if (fz_needs_password(doc) && !fz_authenticate_password(doc,password)) return(-2); */ // error=pdf_load_page_tree(xref); // if (error) // { // pdf_free_xref(xref); // return(-2); // } np=fz_count_pages(doc); if (pageno>np) return(-99); fz_try(ctx) { page = fz_load_page(doc,pageno-1); } fz_catch(ctx) { fz_close_document(doc); fz_free_context(ctx); return(-3); } fz_try(ctx) { list=fz_new_display_list(ctx); dev=fz_new_list_device(ctx,list); fz_run_page(doc,page,dev,&fz_identity,NULL); } fz_catch(ctx) { fz_free_device(dev); fz_drop_display_list(ctx,list); fz_free_page(doc,page); fz_close_document(doc); fz_free_context(ctx); return(-4); } fz_free_device(dev); dev=NULL; dpp=dpi/72.; pix=NULL; fz_var(pix); fz_bound_page(doc,page,&bounds); ctm=fz_identity; fz_scale(&ctm,dpp,dpp); // ctm=fz_concat(ctm,fz_rotate(rotation)); bounds2=bounds; fz_transform_rect(&bounds2,&ctm); fz_round_rect(&bbox,&bounds2); // ctm=fz_translate(0,-page->mediabox.y1); // ctm=fz_concat(ctm,fz_scale(dpp,-dpp)); // ctm=fz_concat(ctm,fz_rotate(page->rotate)); // ctm=fz_concat(ctm,fz_rotate(0)); // bbox=fz_round_rect(fz_transform_rect(ctm,page->mediabox)); // pix=fz_new_pixmap_with_rect(colorspace,bbox); fz_try(ctx) { pix=fz_new_pixmap_with_bbox(ctx,colorspace,&bbox); fz_clear_pixmap_with_value(ctx,pix,255); dev=fz_new_draw_device(ctx,pix); if (list) fz_run_display_list(list,dev,&ctm,&bounds2,NULL); else fz_run_page(doc,page,dev,&ctm,NULL); fz_free_device(dev); dev=NULL; status=bmpmupdf_pixmap_to_bmp(bmp,ctx,pix); fz_drop_pixmap(ctx,pix); } fz_catch(ctx) { fz_free_device(dev); fz_drop_pixmap(ctx,pix); fz_drop_display_list(ctx,list); fz_free_page(doc,page); fz_close_document(doc); fz_free_context(ctx); return(-5); } if (list) fz_drop_display_list(ctx,list); fz_free_page(doc,page); // pdf_free_xref(xref); fz_close_document(doc); fz_flush_warnings(ctx); } /* fz_catch before registering handlers */ fz_catch(ctx) /* Error registering */ { status = -10; } fz_free_context(ctx); // fz_free_glyph_cache(glyphcache); // fz_flush_warnings(); if (status<0) return(status-10); return(0); }
int MupdfBackend::numPages() { return m_doc ? fz_count_pages(m_ctx, m_doc) : 0; }
int main(int argc, char **argv) { char *filename = argv[1]; pthread_t *thread = NULL; fz_locks_context locks; pthread_mutex_t mutex[FZ_LOCK_MAX]; int i; // Initialize FZ_LOCK_MAX number of non-recursive mutexes. for (i = 0; i < FZ_LOCK_MAX; i++) { if (pthread_mutex_init(&mutex[i], NULL) < 0) fail("pthread_mutex_init()"); } // Initialize the locking structure with function pointers to // the locking functions and to the user data. In this case // the user data is a pointer to the array of mutexes so the // locking functions can find the relevant lock to change when // they are called. This way we avoid global variables. locks.user = mutex; locks.lock = lock_mutex; locks.unlock = unlock_mutex; // This is the main threads context function, so supply the // locking structure. This context will be used to parse all // the pages from the document. fz_context *ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); // Open the PDF, XPS or CBZ document. fz_document *doc = fz_open_document(ctx, filename); // Retrieve the number of pages, which translates to the // number of threads used for rendering pages. int threads = fz_count_pages(doc); fprintf(stderr, "spawning %d threads, one per page...\n", threads); thread = malloc(threads * sizeof (pthread_t)); for (i = 0; i < threads; i++) { // Load the relevant page for each thread. fz_page *page = fz_load_page(doc, i); // Compute the bounding box for each page. fz_rect rect = fz_bound_page(doc, page); fz_bbox bbox = fz_round_rect(rect); // Create a display list that will hold the drawing // commands for the page. fz_display_list *list = fz_new_display_list(ctx); // Run the loaded page through a display list device // to populate the page's display list. fz_device *dev = fz_new_list_device(ctx, list); fz_run_page(doc, page, dev, fz_identity, NULL); fz_free_device(dev); // The page is no longer needed, all drawing commands // are now in the display list. fz_free_page(doc, page); // Create a white pixmap using the correct dimensions. fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb, bbox); fz_clear_pixmap_with_value(ctx, pix, 0xff); // Populate the data structure to be sent to the // rendering thread for this page. struct data *data = malloc(sizeof (struct data)); data->pagenumber = i + 1; data->ctx = ctx; data->list = list; data->bbox = bbox; data->pix = pix; // Create the thread and pass it the data structure. if (pthread_create(&thread[i], NULL, renderer, data) < 0) fail("pthread_create()"); } // Now each thread is rendering pages, so wait for each thread // to complete its rendering. fprintf(stderr, "joining %d threads...\n", threads); for (i = threads - 1; i >= 0; i--) { char filename[42]; struct data *data; if (pthread_join(thread[i], (void **) &data) < 0) fail("pthread_join"); sprintf(filename, "out%04d.png", i); fprintf(stderr, "\tSaving %s...\n", filename); // Write the rendered image to a PNG file fz_write_png(ctx, data->pix, filename, 0); // Free the thread's pixmap and display list since // they were allocated by the main thread above. fz_drop_pixmap(ctx, data->pix); fz_free_display_list(ctx, data->list); // Free the data structured passed back and forth // between the main thread and rendering thread. free(data); } fprintf(stderr, "finally!\n"); fflush(NULL); free(thread); // Finally the document is closed and the main thread's // context is freed. fz_close_document(doc); fz_free_context(ctx); return 0; }
/* ** Returns 0 if got dimensions. */ int bmpmupdf_pdffile_width_and_height(char *filename,int pageno,double *width_in,double *height_in) { fz_context *ctx; fz_document *doc; fz_page *page; fz_display_list *list; fz_device *dev; fz_rect bounds; int np; dev=NULL; list=NULL; page=NULL; doc=NULL; if (pageno<1) return(-99); ctx = fz_new_context(NULL,NULL,FZ_STORE_DEFAULT); if (!ctx) return(-1); fz_try(ctx) { fz_register_document_handlers(ctx); fz_set_aa_level(ctx,8); /* Sumatra version of MuPDF v1.4 -- use locally installed fonts */ pdf_install_load_system_font_funcs(ctx); fz_try(ctx) { doc=fz_open_document(ctx,filename); } fz_catch(ctx) { fz_free_context(ctx); return(-1); } np=fz_count_pages(doc); if (pageno>np) return(-99); fz_try(ctx) { page = fz_load_page(doc,pageno-1); } fz_catch(ctx) { fz_close_document(doc); fz_free_context(ctx); return(-3); } fz_try(ctx) { list=fz_new_display_list(ctx); dev=fz_new_list_device(ctx,list); fz_run_page(doc,page,dev,&fz_identity,NULL); } fz_catch(ctx) { fz_free_device(dev); fz_drop_display_list(ctx,list); fz_free_page(doc,page); fz_close_document(doc); fz_free_context(ctx); return(-4); } fz_free_device(dev); dev=NULL; fz_bound_page(doc,page,&bounds); if (width_in!=NULL) (*width_in)=fabs(bounds.x1-bounds.x0)/72.; if (height_in!=NULL) (*height_in)=fabs(bounds.y1-bounds.y0)/72.; fz_drop_display_list(ctx,list); fz_free_page(doc,page); fz_close_document(doc); } fz_catch(ctx) /* Error registering */ { fz_free_context(ctx); return(-20); } fz_free_context(ctx); return(0); }
int Pdf::numPages() const { return fz_count_pages(doc); }