From 80d1d10643d6ae9ae223378136e7621bd2b7fb4e Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Sat, 31 Mar 2012 23:48:23 +0430 Subject: [PATCH] mupdf: support version 1.0rc1 --- Makefile | 2 +- mupdf.c | 162 ++++++++++++++++++++++++++++++--------------------------------- 2 files changed, 79 insertions(+), 85 deletions(-) rewrite mupdf.c (67%) diff --git a/Makefile b/Makefile index 06a2511..eaca2a8 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ clean: # pdf support using mupdf fbpdf: fbpdf.o mupdf.o draw.o - $(CC) -o $@ $^ $(LDFLAGS) -lmupdf -lfitz -lfreetype \ + $(CC) -o $@ $^ $(LDFLAGS) -lfitz -lfreetype \ -ljbig2dec -ljpeg -lz -lopenjpeg -lm # djvu support fbdjvu: fbpdf.o djvulibre.o draw.o diff --git a/mupdf.c b/mupdf.c dissimilarity index 67% index 79f1b1c..36ba996 100644 --- a/mupdf.c +++ b/mupdf.c @@ -1,84 +1,78 @@ -#include -#include -#include "fitz.h" -#include "mupdf.h" -#include "draw.h" -#include "doc.h" - -struct doc { - fz_glyph_cache *glyphcache; - pdf_xref *xref; -}; - -int doc_draw(struct doc *doc, fbval_t *bitmap, int p, int rows, int cols, int zoom, int rotate) -{ - fz_matrix ctm; - fz_bbox bbox; - fz_pixmap *pix; - fz_device *dev; - fz_display_list *list; - pdf_page *page; - int x, y; - - if (pdf_load_page(&page, doc->xref, p - 1)) - return 1; - list = fz_new_display_list(); - dev = fz_new_list_device(list); - if (pdf_run_page(doc->xref, page, dev, fz_identity)) - return 1; - fz_free_device(dev); - - ctm = fz_translate(0, -page->mediabox.y1); - ctm = fz_concat(ctm, fz_scale((float) zoom / 10, (float) -zoom / 10)); - if (rotate) - ctm = fz_concat(ctm, fz_rotate(rotate)); - bbox = fz_round_rect(fz_transform_rect(ctm, page->mediabox)); - - pix = fz_new_pixmap_with_rect(fz_device_rgb, bbox); - fz_clear_pixmap_with_color(pix, 0xff); - - dev = fz_new_draw_device(doc->glyphcache, pix); - fz_execute_display_list(list, dev, ctm, bbox); - fz_free_device(dev); - - for (y = 0; y < MIN(pix->h, rows); y++) { - for (x = 0; x < MIN(pix->w, cols); x++) { - unsigned char *s = pix->samples + y * pix->w * 4 + x * 4; - bitmap[y * cols + x] = FB_VAL(s[0], s[1], s[2]); - - } - } - fz_drop_pixmap(pix); - fz_free_display_list(list); - pdf_free_page(page); - pdf_age_store(doc->xref->store, 3); - return 0; -} - -int doc_pages(struct doc *doc) -{ - return pdf_count_pages(doc->xref); -} - -struct doc *doc_open(char *path) -{ - struct doc *doc = malloc(sizeof(*doc)); - fz_accelerate(); - doc->glyphcache = fz_new_glyph_cache(); - if (pdf_open_xref(&doc->xref, path, NULL)) { - free(doc); - return NULL; - } - if (pdf_load_page_tree(doc->xref)) { - free(doc); - return NULL; - } - return doc; -} - -void doc_close(struct doc *doc) -{ - pdf_free_xref(doc->xref); - fz_free_glyph_cache(doc->glyphcache); - free(doc); -} +#include +#include +#include "fitz.h" +#include "draw.h" +#include "doc.h" + +struct doc { + fz_context *ctx; + fz_document *pdf; +}; + +int doc_draw(struct doc *doc, fbval_t *bitmap, int p, int rows, int cols, int zoom, int rotate) +{ + fz_matrix ctm; + fz_bbox bbox; + fz_pixmap *pix; + fz_device *dev; + fz_page *page; + fz_rect rect; + int h, w; + int x, y; + + if (!(page = fz_load_page(doc->pdf, p - 1))) + return 1; + ctm = fz_scale((float) zoom / 10, (float) -zoom / 10); + ctm = fz_concat(ctm, fz_translate(0, -100)); + if (rotate) + ctm = fz_concat(ctm, fz_rotate(rotate)); + rect = fz_bound_page(doc->pdf, page); + rect = fz_transform_rect(ctm, rect); + bbox = fz_round_rect(rect); + w = MIN(cols, rect.x1 - rect.x0); + h = MIN(rows, rect.y1 - rect.y0); + + pix = fz_new_pixmap_with_bbox(doc->ctx, fz_device_rgb, bbox); + fz_clear_pixmap_with_value(doc->ctx, pix, 0xff); + + dev = fz_new_draw_device(doc->ctx, pix); + fz_run_page(doc->pdf, page, dev, fz_identity, NULL); + fz_run_page(doc->pdf, page, dev, ctm, NULL); + fz_free_device(dev); + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + unsigned char *s = fz_pixmap_samples(doc->ctx, pix) + + y * fz_pixmap_width(doc->ctx, pix) * 4 + x * 4; + bitmap[(h - y - 1) * cols + x] = FB_VAL(s[0], s[1], s[2]); + + } + } + fz_drop_pixmap(doc->ctx, pix); + fz_free_page(doc->pdf, page); + return 0; +} + +int doc_pages(struct doc *doc) +{ + return fz_count_pages(doc->pdf); +} + +struct doc *doc_open(char *path) +{ + struct doc *doc = malloc(sizeof(*doc)); + doc->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); + doc->pdf = fz_open_document(doc->ctx, path); + if (!doc->pdf) { + free(doc); + return NULL; + } + return doc; +} + +void doc_close(struct doc *doc) +{ + fz_close_document(doc->pdf); + fz_free_context(doc->ctx); + free(doc); +} -- 2.11.4.GIT