3adf31ee46f34839ecadeb247034f653dfdad22f
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 */
44 static int first
; /* first_page - 1 */
46 static void draw(void)
49 for (i
= head
; i
< MIN(head
+ fb_rows(), PDFROWS
); i
++)
50 fb_set(i
- head
, 0, pbuf
+ i
* PDFCOLS
+ left
, fb_cols());
53 static int showpage(int p
, int h
)
55 if (p
< 1 || p
> doc_pages(doc
))
57 memset(pbuf
, 0x00, sizeof(pbuf
));
60 doc_draw(doc
, p
, zoom
, rotate
, pbuf
, &prows
, &pcols
);
67 static void zoom_page(int z
)
70 zoom
= MIN(MAXZOOM
, MAX(1, z
));
71 showpage(num
, MIN(PDFROWS
- fb_rows(), head
* zoom
/ _zoom
));
74 static void setmark(int c
)
78 mark_head
[c
] = head
/ zoom
;
82 static void jmpmark(int c
, int offset
)
86 if (ISMARK(c
) && mark
[c
]) {
88 int dst_head
= offset
? mark_head
[c
] * zoom
: 0;
90 showpage(dst
, dst_head
);
94 static int readkey(void)
97 if (read(0, &b
, 1) <= 0)
102 static int getcount(int def
)
104 int result
= count
? count
: def
;
109 static void printinfo(void)
112 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
113 filename
, num
, doc_pages(doc
), zoom
* 10);
117 static void term_setup(void)
119 struct termios newtermios
;
120 tcgetattr(0, &termios
);
121 newtermios
= termios
;
122 newtermios
.c_lflag
&= ~ICANON
;
123 newtermios
.c_lflag
&= ~ECHO
;
124 tcsetattr(0, TCSAFLUSH
, &newtermios
);
127 static void term_cleanup(void)
129 tcsetattr(0, 0, &termios
);
132 static void sigcont(int sig
)
137 static void reload(void)
140 doc
= doc_open(filename
);
144 static int rightmost(int cont
)
148 for (i
= 0; i
< prows
; i
++) {
150 while (j
> ret
&& pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(0, 0, 0))
152 while (cont
&& j
> ret
&&
153 pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(255, 255, 255))
161 static int leftmost(int cont
)
165 for (i
= 0; i
< prows
; i
++) {
167 while (j
< ret
&& pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(0, 0, 0))
169 while (cont
&& j
< ret
&&
170 pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(255, 255, 255))
178 static void mainloop(void)
180 int step
= fb_rows() / PAGESTEPS
;
181 int hstep
= fb_cols() / PAGESTEPS
;
184 signal(SIGCONT
, sigcont
);
186 while ((c
= readkey()) != -1) {
190 showpage(num
+ getcount(1), 0);
194 showpage(num
- getcount(1), 0);
198 showpage(getcount(doc_pages(doc
) - first
) + first
, 0);
201 zoom_page(getcount(15));
204 zoom_page(zoom
* fb_cols() / pcols
);
207 if (leftmost(1) < rightmost(1))
208 zoom_page(zoom
* (fb_cols() - hstep
) /
209 (rightmost(1) - leftmost(1)));
212 zoom_page(zoom
* fb_rows() / prows
);
215 rotate
= getcount(0);
235 jmpmark(readkey(), c
== '`');
238 first
= getcount(1) - 1;
242 count
= count
* 10 + c
- '0';
246 head
+= step
* getcount(1);
249 head
-= step
* getcount(1);
252 left
+= hstep
* getcount(1);
255 left
-= hstep
* getcount(1);
261 head
= MAX(0, prows
- fb_rows());
264 head
= (prows
- fb_rows()) / 2;
268 head
+= fb_rows() * getcount(1) - step
;
272 head
-= fb_rows() * getcount(1) - step
;
278 left
= rightmost(0) - fb_cols();
281 left
= leftmost(1) - hstep
/ 2;
284 left
= rightmost(1) + hstep
/ 2 - fb_cols();
289 /* no need to redraw */
292 head
= MAX(0, MIN(PDFROWS
- fb_rows(), head
));
293 left
= MAX(0, MIN(PDFCOLS
- fb_cols(), left
));
299 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] [-o first page] filename\n";
301 int main(int argc
, char *argv
[])
308 strcpy(filename
, argv
[argc
- 1]);
309 doc
= doc_open(filename
);
311 fprintf(stderr
, "cannot open <%s>\n", filename
);
314 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
315 switch (argv
[i
][1]) {
317 rotate
= atoi(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
320 zoom
= atoi(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
323 num
= atoi(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
326 first
= atoi(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]) - 1;
330 printf("\x1b[?25l"); /* hide the cursor */
331 printf("\x1b[2J"); /* clear the screen */
335 left
= (PDFCOLS
- fb_cols()) / 2;
336 if (FBM_BPP(fb_mode()) != sizeof(fbval_t
))
337 fprintf(stderr
, "fbpdf: fbval_t doesn't match fb depth\n");
341 printf("\x1b[?25h\n"); /* show the cursor */