2 * fbpdf - a small framebuffer pdf viewer using mupdf
4 * Copyright (C) 2009-2013 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22 #define CTRLKEY(x) ((x) - 96)
23 #define ISMARK(x) (isalpha(x) || (x) == '\'' || (x) == '`')
26 #define PDFCOLS (1 << 11)
27 #define PDFROWS (1 << 12)
30 static struct doc
*doc
;
31 static fbval_t pbuf
[PDFROWS
* PDFCOLS
]; /* current page */
32 static int prows
, pcols
; /* the dimensions of current page */
35 static struct termios termios
;
36 static char filename
[256];
37 static int mark
[128]; /* mark page number */
38 static int mark_head
[128]; /* mark head position */
45 static void draw(void)
48 for (i
= head
; i
< MIN(head
+ fb_rows(), PDFROWS
); i
++)
49 fb_set(i
- head
, 0, pbuf
+ i
* PDFCOLS
+ left
, fb_cols());
52 static int showpage(int p
, int h
)
54 if (p
< 1 || p
> doc_pages(doc
))
56 memset(pbuf
, 0x00, sizeof(pbuf
));
59 doc_draw(doc
, p
, zoom
, rotate
, pbuf
, &prows
, &pcols
);
66 static void zoom_page(int z
)
69 zoom
= MIN(MAXZOOM
, MAX(1, z
));
70 showpage(num
, MIN(PDFROWS
- fb_rows(), head
* zoom
/ _zoom
));
73 static void setmark(int c
)
77 mark_head
[c
] = head
/ zoom
;
81 static void jmpmark(int c
, int offset
)
85 if (ISMARK(c
) && mark
[c
]) {
87 int dst_head
= offset
? mark_head
[c
] * zoom
: 0;
89 showpage(dst
, dst_head
);
93 static int readkey(void)
96 if (read(0, &b
, 1) <= 0)
101 static int getcount(int def
)
103 int result
= count
? count
: def
;
108 static void printinfo(void)
111 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
112 filename
, num
, doc_pages(doc
), zoom
* 10);
116 static void term_setup(void)
118 struct termios newtermios
;
119 tcgetattr(0, &termios
);
120 newtermios
= termios
;
121 newtermios
.c_lflag
&= ~ICANON
;
122 newtermios
.c_lflag
&= ~ECHO
;
123 tcsetattr(0, TCSAFLUSH
, &newtermios
);
126 static void term_cleanup(void)
128 tcsetattr(0, 0, &termios
);
131 static void sigcont(int sig
)
136 static void reload(void)
139 doc
= doc_open(filename
);
143 static int rightmost(int cont
)
147 for (i
= 0; i
< prows
; i
++) {
149 while (j
> ret
&& pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(0, 0, 0))
151 while (cont
&& j
> ret
&&
152 pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(255, 255, 255))
160 static int leftmost(int cont
)
164 for (i
= 0; i
< prows
; i
++) {
166 while (j
< ret
&& pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(0, 0, 0))
168 while (cont
&& j
< ret
&&
169 pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(255, 255, 255))
177 static void mainloop(void)
179 int step
= fb_rows() / PAGESTEPS
;
180 int hstep
= fb_cols() / PAGESTEPS
;
183 signal(SIGCONT
, sigcont
);
185 while ((c
= readkey()) != -1) {
189 showpage(num
+ getcount(1), 0);
193 showpage(num
- getcount(1), 0);
197 showpage(getcount(doc_pages(doc
)), 0);
200 zoom_page(getcount(15));
203 zoom_page(zoom
* fb_cols() / pcols
);
206 if (leftmost(1) < rightmost(1))
207 zoom_page(zoom
* (fb_cols() - hstep
) /
208 (rightmost(1) - leftmost(1)));
211 zoom_page(zoom
* fb_rows() / prows
);
214 rotate
= getcount(0);
234 jmpmark(readkey(), c
== '`');
238 count
= count
* 10 + c
- '0';
242 head
+= step
* getcount(1);
245 head
-= step
* getcount(1);
248 left
+= hstep
* getcount(1);
251 left
-= hstep
* getcount(1);
257 head
= MAX(0, prows
- fb_rows());
260 head
= (prows
- fb_rows()) / 2;
264 head
+= fb_rows() * getcount(1) - step
;
268 head
-= fb_rows() * getcount(1) - step
;
274 left
= rightmost(0) - fb_cols();
277 left
= leftmost(1) - hstep
/ 2;
280 left
= rightmost(1) + hstep
/ 2 - fb_cols();
285 /* no need to redraw */
288 head
= MAX(0, MIN(PDFROWS
- fb_rows(), head
));
289 left
= MAX(0, MIN(PDFCOLS
- fb_cols(), left
));
295 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
297 int main(int argc
, char *argv
[])
304 strcpy(filename
, argv
[argc
- 1]);
305 doc
= doc_open(filename
);
307 fprintf(stderr
, "cannot open <%s>\n", filename
);
310 while (i
+ 2 < argc
&& argv
[i
][0] == '-') {
311 if (argv
[i
][1] == 'r')
312 rotate
= atoi(argv
[i
+ 1]);
313 if (argv
[i
][1] == 'z')
314 zoom
= atoi(argv
[i
+ 1]);
315 if (argv
[i
][1] == 'p')
316 num
= atoi(argv
[i
+ 1]);
319 printf("\x1b[?25l"); /* hide the cursor */
320 printf("\x1b[2J"); /* clear the screen */
324 left
= (PDFCOLS
- fb_cols()) / 2;
325 if (FBM_BPP(fb_mode()) != sizeof(fbval_t
))
326 fprintf(stderr
, "fbpdf: fbval_t doesn't match fb depth\n");
330 printf("\x1b[?25h\n"); /* show the cursor */