Code

gtk-tpdfv: Center PDF page vertically and horizontally.
authorSebastian Harl <sh@tokkee.org>
Fri, 21 Oct 2011 13:02:25 +0000 (15:02 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 21 Oct 2011 13:02:25 +0000 (15:02 +0200)
src/gtk-tpdfv.c

index 4398719385557f7144ca42c674bdf66acfd47630..771dff99871ebd2f2490207790760175caa684e9 100644 (file)
@@ -179,6 +179,14 @@ gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event)
        gtk_tpdfv_t *pdf;
        cairo_t *cr;
 
+       gdouble width, height;
+
+       gdouble page_width  = 0.0;
+       gdouble page_height = 0.0;
+
+       double zoom_factor  = 1.0;
+       double x, y;
+
        pdf = GTK_TPDFV_GET_PRIVATE(tpdfv);
 
        if (! pdf)
@@ -189,23 +197,15 @@ gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event)
                        event->area.width, event->area.height);
        cairo_clip(cr);
 
-       /* zoom, scrolling */
+       poppler_page_get_size(pdf->current_page, &page_width, &page_height);
+       width  = (gdouble)tpdfv->allocation.width;
+       height = (gdouble)tpdfv->allocation.height;
+
+       /* zoom */
        if (pdf->zoom_mode == TPDFV_ZOOM_CUSTOM) {
-               cairo_scale(cr, pdf->zoom_factor, pdf->zoom_factor);
+               zoom_factor = pdf->zoom_factor;
        }
        else {
-               gdouble width;
-               gdouble height;
-
-               gdouble page_width  = 0.0;
-               gdouble page_height = 0.0;
-
-               double zoom_factor  = 1.0;
-
-               poppler_page_get_size(pdf->current_page, &page_width, &page_height);
-               width  = (gdouble)tpdfv->allocation.width;
-               height = (gdouble)tpdfv->allocation.height;
-
                if (pdf->zoom_mode == TPDFV_ZOOM_WIDTH) {
                        zoom_factor = (double)(width / page_width);
                }
@@ -219,11 +219,14 @@ gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event)
                else {
                        assert(0);
                }
-
-               cairo_scale(cr, zoom_factor, zoom_factor);
        }
+       cairo_scale(cr, zoom_factor, zoom_factor);
+
+       /* centered position / scrolling */
+       x = (double)(width - page_width * zoom_factor) / 2.0 / zoom_factor;
+       y = (double)(height - page_height * zoom_factor) / 2.0 / zoom_factor;
 
-       cairo_translate(cr, pdf->delta_x, pdf->delta_y);
+       cairo_translate(cr, x + pdf->delta_x, y + pdf->delta_y);
 
        poppler_page_render(pdf->current_page, cr);