add reload command with e
[fbpdf.git] / mupdf.c
blob79f1b1ce37952d01b95c07e1b6940ecda4cedad4
1 #include <stdlib.h>
2 #include <string.h>
3 #include "fitz.h"
4 #include "mupdf.h"
5 #include "draw.h"
6 #include "doc.h"
8 struct doc {
9 fz_glyph_cache *glyphcache;
10 pdf_xref *xref;
13 int doc_draw(struct doc *doc, fbval_t *bitmap, int p, int rows, int cols, int zoom, int rotate)
15 fz_matrix ctm;
16 fz_bbox bbox;
17 fz_pixmap *pix;
18 fz_device *dev;
19 fz_display_list *list;
20 pdf_page *page;
21 int x, y;
23 if (pdf_load_page(&page, doc->xref, p - 1))
24 return 1;
25 list = fz_new_display_list();
26 dev = fz_new_list_device(list);
27 if (pdf_run_page(doc->xref, page, dev, fz_identity))
28 return 1;
29 fz_free_device(dev);
31 ctm = fz_translate(0, -page->mediabox.y1);
32 ctm = fz_concat(ctm, fz_scale((float) zoom / 10, (float) -zoom / 10));
33 if (rotate)
34 ctm = fz_concat(ctm, fz_rotate(rotate));
35 bbox = fz_round_rect(fz_transform_rect(ctm, page->mediabox));
37 pix = fz_new_pixmap_with_rect(fz_device_rgb, bbox);
38 fz_clear_pixmap_with_color(pix, 0xff);
40 dev = fz_new_draw_device(doc->glyphcache, pix);
41 fz_execute_display_list(list, dev, ctm, bbox);
42 fz_free_device(dev);
44 for (y = 0; y < MIN(pix->h, rows); y++) {
45 for (x = 0; x < MIN(pix->w, cols); x++) {
46 unsigned char *s = pix->samples + y * pix->w * 4 + x * 4;
47 bitmap[y * cols + x] = FB_VAL(s[0], s[1], s[2]);
51 fz_drop_pixmap(pix);
52 fz_free_display_list(list);
53 pdf_free_page(page);
54 pdf_age_store(doc->xref->store, 3);
55 return 0;
58 int doc_pages(struct doc *doc)
60 return pdf_count_pages(doc->xref);
63 struct doc *doc_open(char *path)
65 struct doc *doc = malloc(sizeof(*doc));
66 fz_accelerate();
67 doc->glyphcache = fz_new_glyph_cache();
68 if (pdf_open_xref(&doc->xref, path, NULL)) {
69 free(doc);
70 return NULL;
72 if (pdf_load_page_tree(doc->xref)) {
73 free(doc);
74 return NULL;
76 return doc;
79 void doc_close(struct doc *doc)
81 pdf_free_xref(doc->xref);
82 fz_free_glyph_cache(doc->glyphcache);
83 free(doc);