README: zoom to fit page contents
authorAli Gholami Rudi <ali@rudi.ir>
Fri, 22 Mar 2013 20:50:20 +0000 (23 01:20 +0430)
committerAli Gholami Rudi <ali@rudi.ir>
Fri, 22 Mar 2013 20:55:06 +0000 (23 01:25 +0430)
This adds the following commands:
* W: zoom to fit page contents horizontally
* {: align with the left edge of the page
* }: align with the right edge of the page

README
fbpdf.c

diff --git a/README b/README
index 66de094..00f73f6 100644 (file)
--- a/README
+++ b/README
@@ -40,6 +40,8 @@ h             scroll left
 l              scroll right
 [              align with the leftmost character on the page
 ]              align with the rightmost character on the page
+{              align with the left edge of the page
+}              align with the right edge of the page
 H              show top
 M              show middle
 L              show bottom
@@ -47,6 +49,7 @@ L             show bottom
 ^H/^U          (backspace) page up
 ^L             redraw
 e              reload current file
-f              fit to height
-w              fit to width
+f              zoom to fit page height
+w              zoom to fit page width
+W              zoom to fit page contents horizontally
 ============== ================================================
diff --git a/fbpdf.c b/fbpdf.c
index a6725e1..2854f72 100644 (file)
--- a/fbpdf.c
+++ b/fbpdf.c
@@ -24,6 +24,7 @@
 #define MAXHEIGHT              3
 #define PDFCOLS                        (1 << 11)
 #define PDFROWS                        (1 << 12)
+#define MAXZOOM                        (100)
 
 static struct doc *doc;
 static fbval_t pbuf[PDFROWS * PDFCOLS];        /* current page */
@@ -64,7 +65,7 @@ static int showpage(int p, int h)
 static void zoom_page(int z)
 {
        int _zoom = zoom;
-       zoom = z;
+       zoom = MIN(MAXZOOM, MAX(1, z));
        showpage(num, MIN(PDFROWS - fb_rows(), head * zoom / _zoom));
 }
 
@@ -118,7 +119,7 @@ static void reload(void)
        showpage(num, head);
 }
 
-static int rightmost(void)
+static int rightmost(int cont)
 {
        int ret = 0;
        int i, j;
@@ -126,7 +127,8 @@ static int rightmost(void)
                j = PDFCOLS - 1;
                while (j > ret && pbuf[i * PDFCOLS + j] == FB_VAL(0, 0, 0))
                        j--;
-               while (j > ret && pbuf[i * PDFCOLS + j] == FB_VAL(255, 255, 255))
+               while (cont && j > ret &&
+                               pbuf[i * PDFCOLS + j] == FB_VAL(255, 255, 255))
                        j--;
                if (ret < j)
                        ret = j;
@@ -134,7 +136,7 @@ static int rightmost(void)
        return ret;
 }
 
-static int leftmost(void)
+static int leftmost(int cont)
 {
        int ret = PDFCOLS;
        int i, j;
@@ -142,7 +144,8 @@ static int leftmost(void)
                j = 0;
                while (j < ret && pbuf[i * PDFCOLS + j] == FB_VAL(0, 0, 0))
                        j++;
-               while (j < ret && pbuf[i * PDFCOLS + j] == FB_VAL(255, 255, 255))
+               while (cont && j < ret &&
+                               pbuf[i * PDFCOLS + j] == FB_VAL(255, 255, 255))
                        j++;
                if (ret > j)
                        ret = j;
@@ -177,6 +180,11 @@ static void mainloop(void)
                case 'w':
                        zoom_page(zoom * fb_cols() / pcols);
                        break;
+               case 'W':
+                       if (leftmost(1) < rightmost(1))
+                               zoom_page(zoom * (fb_cols() - hstep) /
+                                       (rightmost(1) - leftmost(1)));
+                       break;
                case 'f':
                        zoom_page(zoom * fb_rows() / prows);
                        break;
@@ -233,7 +241,7 @@ static void mainloop(void)
                        head = MAX(0, prows - fb_rows());
                        break;
                case 'M':
-                       head = prows / 2;
+                       head = (prows - fb_rows()) / 2;
                        break;
                case ' ':
                case CTRL('d'):
@@ -243,11 +251,17 @@ static void mainloop(void)
                case CTRL('u'):
                        head -= fb_rows() * getcount(1) - step;
                        break;
+               case '{':
+                       left = leftmost(0);
+                       break;
+               case '}':
+                       left = rightmost(0) - fb_cols();
+                       break;
                case '[':
-                       left = leftmost() - hstep / 2;
+                       left = leftmost(1) - hstep / 2;
                        break;
                case ']':
-                       left = rightmost() + hstep / 2 - fb_cols();
+                       left = rightmost(1) + hstep / 2 - fb_cols();
                        break;
                case CTRLKEY('l'):
                        break;