fbpdf: C centers the page horizontally
[fbpdf.git] / fbpdf.c
blob5d288d2ea3210c1fc96dddde9ba6e6e39eb40fda
1 /*
2 * fbpdf - a small framebuffer pdf viewer using mupdf
4 * Copyright (C) 2009-2014 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
7 */
8 #include <ctype.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <pty.h>
15 #include "draw.h"
16 #include "doc.h"
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
21 #define PAGESTEPS 8
22 #define CTRLKEY(x) ((x) - 96)
23 #define ISMARK(x) (isalpha(x) || (x) == '\'' || (x) == '`')
24 #define MAXWIDTH 2
25 #define MAXHEIGHT 3
26 #define PDFCOLS (1 << 11)
27 #define PDFROWS (1 << 12)
28 #define MAXZOOM (100)
30 static struct doc *doc;
31 static fbval_t pbuf[PDFROWS * PDFCOLS]; /* current page */
32 static int prows, pcols; /* the dimensions of current page */
34 static struct termios termios;
35 static char filename[256];
36 static int mark[128]; /* mark page number */
37 static int mark_head[128]; /* mark head position */
38 static int num = 1; /* page number */
39 static int numdiff; /* G command page number difference */
40 static int zoom = 15;
41 static int zoom_def = 15; /* default zoom */
42 static int rotate;
43 static int head;
44 static int left;
45 static int count;
47 static void draw(void)
49 int i;
50 for (i = head; i < MIN(head + fb_rows(), PDFROWS); i++)
51 fb_set(i - head, 0, pbuf + i * PDFCOLS + left, fb_cols());
54 static int showpage(int p, int h)
56 if (p < 1 || p > doc_pages(doc))
57 return 0;
58 memset(pbuf, 0x00, sizeof(pbuf));
59 prows = PDFROWS;
60 pcols = PDFCOLS;
61 doc_draw(doc, p, zoom, rotate, pbuf, &prows, &pcols);
62 num = p;
63 head = h;
64 draw();
65 return 0;
68 static void zoom_page(int z)
70 int _zoom = zoom;
71 zoom = MIN(MAXZOOM, MAX(1, z));
72 showpage(num, MIN(PDFROWS - fb_rows(), head * zoom / _zoom));
75 static void setmark(int c)
77 if (ISMARK(c)) {
78 mark[c] = num;
79 mark_head[c] = head / zoom;
83 static void jmpmark(int c, int offset)
85 if (c == '`')
86 c = '\'';
87 if (ISMARK(c) && mark[c]) {
88 int dst = mark[c];
89 int dst_head = offset ? mark_head[c] * zoom : 0;
90 setmark('\'');
91 showpage(dst, dst_head);
95 static int readkey(void)
97 unsigned char b;
98 if (read(0, &b, 1) <= 0)
99 return -1;
100 return b;
103 static int getcount(int def)
105 int result = count ? count : def;
106 count = 0;
107 return result;
110 static void printinfo(void)
112 printf("\x1b[H");
113 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
114 filename, num, doc_pages(doc), zoom * 10);
115 fflush(stdout);
118 static void term_setup(void)
120 struct termios newtermios;
121 tcgetattr(0, &termios);
122 newtermios = termios;
123 newtermios.c_lflag &= ~ICANON;
124 newtermios.c_lflag &= ~ECHO;
125 tcsetattr(0, TCSAFLUSH, &newtermios);
128 static void term_cleanup(void)
130 tcsetattr(0, 0, &termios);
133 static void sigcont(int sig)
135 term_setup();
138 static void reload(void)
140 doc_close(doc);
141 doc = doc_open(filename);
142 showpage(num, head);
145 static int rightmost(int cont)
147 int ret = 0;
148 int i, j;
149 for (i = 0; i < prows; i++) {
150 j = PDFCOLS - 1;
151 while (j > ret && pbuf[i * PDFCOLS + j] == FB_VAL(0, 0, 0))
152 j--;
153 while (cont && j > ret &&
154 pbuf[i * PDFCOLS + j] == FB_VAL(255, 255, 255))
155 j--;
156 if (ret < j)
157 ret = j;
159 return ret;
162 static int leftmost(int cont)
164 int ret = PDFCOLS;
165 int i, j;
166 for (i = 0; i < prows; i++) {
167 j = 0;
168 while (j < ret && pbuf[i * PDFCOLS + j] == FB_VAL(0, 0, 0))
169 j++;
170 while (cont && j < ret &&
171 pbuf[i * PDFCOLS + j] == FB_VAL(255, 255, 255))
172 j++;
173 if (ret > j)
174 ret = j;
176 return ret;
179 static void mainloop(void)
181 int step = fb_rows() / PAGESTEPS;
182 int hstep = fb_cols() / PAGESTEPS;
183 int c;
184 term_setup();
185 signal(SIGCONT, sigcont);
186 showpage(num, 0);
187 while ((c = readkey()) != -1) {
188 switch (c) {
189 case CTRLKEY('f'):
190 case 'J':
191 showpage(num + getcount(1), 0);
192 break;
193 case CTRLKEY('b'):
194 case 'K':
195 showpage(num - getcount(1), 0);
196 break;
197 case 'G':
198 setmark('\'');
199 showpage(getcount(doc_pages(doc) - numdiff) + numdiff, 0);
200 break;
201 case 'O':
202 numdiff = num - getcount(num);
203 setmark('\'');
204 showpage(num + numdiff, 0);
205 break;
206 case 'o':
207 numdiff = num - getcount(num);
208 break;
209 case 'z':
210 zoom_page(getcount(zoom_def));
211 break;
212 case 'Z':
213 zoom_def = getcount(zoom);
214 break;
215 case 'w':
216 zoom_page(zoom * fb_cols() / pcols);
217 break;
218 case 'W':
219 if (leftmost(1) < rightmost(1))
220 zoom_page(zoom * (fb_cols() - hstep) /
221 (rightmost(1) - leftmost(1)));
222 break;
223 case 'f':
224 zoom_page(zoom * fb_rows() / prows);
225 break;
226 case 'r':
227 rotate = getcount(0);
228 showpage(num, 0);
229 break;
230 case 'i':
231 printinfo();
232 break;
233 case 'q':
234 term_cleanup();
235 return;
236 case 27:
237 count = 0;
238 break;
239 case 'm':
240 setmark(readkey());
241 break;
242 case 'e':
243 reload();
244 break;
245 case '`':
246 case '\'':
247 jmpmark(readkey(), c == '`');
248 break;
249 case 'd':
250 sleep(getcount(1));
251 break;
252 default:
253 if (isdigit(c))
254 count = count * 10 + c - '0';
256 switch (c) {
257 case 'j':
258 head += step * getcount(1);
259 break;
260 case 'k':
261 head -= step * getcount(1);
262 break;
263 case 'l':
264 left += hstep * getcount(1);
265 break;
266 case 'h':
267 left -= hstep * getcount(1);
268 break;
269 case 'H':
270 head = 0;
271 break;
272 case 'L':
273 head = MAX(0, prows - fb_rows());
274 break;
275 case 'M':
276 head = (prows - fb_rows()) / 2;
277 break;
278 case 'C':
279 left = (PDFCOLS - fb_cols()) / 2;
280 break;
281 case ' ':
282 case CTRL('d'):
283 head += fb_rows() * getcount(1) - step;
284 break;
285 case 127:
286 case CTRL('u'):
287 head -= fb_rows() * getcount(1) - step;
288 break;
289 case '[':
290 left = leftmost(0);
291 break;
292 case ']':
293 left = rightmost(0) - fb_cols();
294 break;
295 case '{':
296 left = leftmost(1) - hstep / 2;
297 break;
298 case '}':
299 left = rightmost(1) + hstep / 2 - fb_cols();
300 break;
301 case CTRLKEY('l'):
302 break;
303 default:
304 /* no need to redraw */
305 continue;
307 head = MAX(0, MIN(PDFROWS - fb_rows(), head));
308 left = MAX(0, MIN(PDFCOLS - fb_cols(), left));
309 draw();
313 static char *usage =
314 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
316 int main(int argc, char *argv[])
318 int i = 1;
319 if (argc < 2) {
320 printf(usage);
321 return 1;
323 strcpy(filename, argv[argc - 1]);
324 doc = doc_open(filename);
325 if (!doc) {
326 fprintf(stderr, "cannot open <%s>\n", filename);
327 return 1;
329 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
330 switch (argv[i][1]) {
331 case 'r':
332 rotate = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
333 break;
334 case 'z':
335 zoom = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
336 break;
337 case 'p':
338 num = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
339 break;
342 printf("\x1b[?25l"); /* hide the cursor */
343 printf("\x1b[2J"); /* clear the screen */
344 printinfo();
345 if (fb_init())
346 return 1;
347 left = (PDFCOLS - fb_cols()) / 2;
348 if (FBM_BPP(fb_mode()) != sizeof(fbval_t))
349 fprintf(stderr, "fbpdf: fbval_t doesn't match fb depth\n");
350 else
351 mainloop();
352 fb_free();
353 printf("\x1b[?25h\n"); /* show the cursor */
354 doc_close(doc);
355 return 0;