7 #include <cairo/cairo.h>
8 #include <glib/poppler.h>
12 #define CTRLKEY(x) ((x) - 96)
15 static PopplerDocument
*doc
;
16 static PopplerPage
*page
;
18 static cairo_t
*cairo
;
19 static cairo_surface_t
*surface
;
27 unsigned char *img
= cairo_image_surface_get_data(surface
);
28 fbval_t slice
[1 << 14];
30 int h
= MIN(fb_rows(), cairo_image_surface_get_height(surface
));
31 int w
= MIN(fb_cols(), cairo_image_surface_get_width(surface
));
32 int cols
= cairo_image_surface_get_width(surface
);
33 for (i
= head
; i
< h
+ head
; i
++) {
34 for (j
= left
; j
< w
+ left
; j
++) {
35 unsigned char *p
= img
+ (i
* cols
+ j
) * 4;
36 slice
[j
- left
] = fb_color(*p
, *(p
+ 1), *(p
+ 2));
38 fb_set(i
- head
, 0, slice
, w
);
42 static int load_document(char *filename
)
44 char abspath
[PATH_MAX
];
45 char uri
[PATH_MAX
+ 16];
46 realpath(filename
, abspath
);
47 snprintf(uri
, sizeof(uri
), "file://%s", abspath
);
48 doc
= poppler_document_new_from_file(uri
, NULL
, NULL
);
52 static void cleanup_page(void)
57 cairo_surface_destroy(surface
);
59 g_object_unref(G_OBJECT(page
));
62 static void showpage(int p
)
65 surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,
66 fb_cols() * MAXAREA
, fb_rows() * MAXAREA
);
67 cairo
= cairo_create(surface
);
68 cairo_scale(cairo
, (float) zoom
/ 10, (float) zoom
/ 10);
69 cairo_set_source_rgb(cairo
, 1.0, 1.0, 1.0);
71 if (p
< 0 || p
>= poppler_document_get_n_pages(doc
))
74 page
= poppler_document_get_page(doc
, p
);
75 poppler_page_render(page
, cairo
);
81 static int readkey(void)
84 if (read(STDIN_FILENO
, &b
, 1) <= 0)
89 static int getcount(int def
)
91 int result
= count
? count
: def
;
96 static void mainloop()
98 int step
= fb_rows() / PAGESTEPS
;
99 int hstep
= fb_cols() / PAGESTEPS
;
100 struct termios oldtermios
, termios
;
101 int maxhead
, maxleft
;
103 tcgetattr(STDIN_FILENO
, &termios
);
104 oldtermios
= termios
;
106 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &termios
);
108 while ((c
= readkey()) != -1) {
109 maxhead
= cairo_image_surface_get_height(surface
) - fb_rows();
110 maxleft
= cairo_image_surface_get_height(surface
) - fb_cols();
113 head
+= step
* getcount(1);
116 head
-= step
* getcount(1);
119 left
+= hstep
* getcount(1);
122 left
-= hstep
* getcount(1);
134 head
+= fb_rows() * getcount(1) - step
;
137 head
-= fb_rows() * getcount(1) - step
;
141 showpage(num
+ getcount(1));
145 showpage(num
- getcount(1));
148 showpage(getcount(0));
155 tcsetattr(STDIN_FILENO
, 0, &oldtermios
);
162 count
= count
* 10 + c
- '0';
164 head
= MAX(0, MIN(maxhead
, head
));
165 left
= MAX(0, MIN(maxleft
, left
));
170 int main(int argc
, char* argv
[])
173 printf("usage: fbpdf filename\n");
177 if (load_document(argv
[1])) {
178 printf("cannot open file\n");
186 g_object_unref(G_OBJECT(doc
));