fbpdf.1: update and improve the manual page
[fbpdf.git] / poppler.c
blobd46685e344a495e9fefbc7d645ce21f0831ecb93
1 #include <stdlib.h>
2 #include <string.h>
3 #include <poppler/cpp/poppler-document.h>
4 #include <poppler/cpp/poppler-image.h>
5 #include <poppler/cpp/poppler-page.h>
6 #include <poppler/cpp/poppler-page-renderer.h>
8 #define MIN(a, b) ((a) < (b) ? (a) : (b))
10 extern "C" {
11 #include "draw.h"
12 #include "doc.h"
15 struct doc {
16 poppler::document *doc;
19 static poppler::rotation_enum rotation(int times)
21 if (times == 1)
22 return poppler::rotate_90;
23 if (times == 2)
24 return poppler::rotate_180;
25 if (times == 3)
26 return poppler::rotate_270;
27 return poppler::rotate_0;
30 void *doc_draw(struct doc *doc, int p, int zoom, int rotate, int *rows, int *cols)
32 poppler::page *page = doc->doc->create_page(p - 1);
33 poppler::page_renderer pr;
34 int x, y;
35 int h, w;
36 fbval_t *pbuf;
37 unsigned char *dat;
38 pr.set_render_hint(poppler::page_renderer::antialiasing, true);
39 pr.set_render_hint(poppler::page_renderer::text_antialiasing, true);
40 poppler::image img = pr.render_page(page,
41 (float) 72 * zoom / 100, (float) 72 * zoom / 100,
42 -1, -1, -1, -1, rotation((rotate + 89) / 90));
43 h = img.height();
44 w = img.width();
45 dat = (unsigned char *) img.data();
46 if (!(pbuf = (fbval_t *) malloc(h * w * sizeof(pbuf[0])))) {
47 delete page;
48 return NULL;
50 for (y = 0; y < h; y++) {
51 unsigned char *s = dat + img.bytes_per_row() * y;
52 for (x = 0; x < w; x++)
53 pbuf[y * w + x] = FB_VAL(s[x * 4 + 2],
54 s[x * 4 + 1], s[x * 4 + 0]);
56 *rows = h;
57 *cols = w;
58 delete page;
59 return pbuf;
62 int doc_pages(struct doc *doc)
64 return doc->doc->pages();
67 struct doc *doc_open(char *path)
69 struct doc *doc = (struct doc *) malloc(sizeof(*doc));
70 if (doc == NULL)
71 return NULL;
72 doc->doc = poppler::document::load_from_file(path);
73 if (!doc->doc) {
74 doc_close(doc);
75 return NULL;
77 return doc;
80 void doc_close(struct doc *doc)
82 delete doc->doc;
83 free(doc);