From 3e7481766429955636c4969d8f306c2b4305f3cd Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 21 Oct 2011 15:02:25 +0200 Subject: [PATCH] gtk-tpdfv: Center PDF page vertically and horizontally. --- src/gtk-tpdfv.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/gtk-tpdfv.c b/src/gtk-tpdfv.c index 4398719..771dff9 100644 --- a/src/gtk-tpdfv.c +++ b/src/gtk-tpdfv.c @@ -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); -- 2.30.2