From: Sebastian Harl Date: Fri, 21 Oct 2011 12:42:59 +0000 (+0200) Subject: gtk-tpdfv/tpdfview: Implemented 'width', 'height', 'fit' zoom modes. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=53047b071ba51bbb5974b80599217d041dac5533;p=tpdfview.git gtk-tpdfv/tpdfview: Implemented 'width', 'height', 'fit' zoom modes. Currently, when leaving those modes, the zoom will be reset to the value before entering any of those modes. This is done on purpose ;-) --- diff --git a/src/gtk-tpdfv.c b/src/gtk-tpdfv.c index b04d2c6..4398719 100644 --- a/src/gtk-tpdfv.c +++ b/src/gtk-tpdfv.c @@ -43,6 +43,16 @@ #include #include +#define TPDFV_MIN(a, b) ((a) <= (b) ? (a) : (b)) +#define TPDFV_MAX(a, b) ((a) >= (b) ? (a) : (b)) + +typedef enum { + TPDFV_ZOOM_CUSTOM = 0, + TPDFV_ZOOM_WIDTH, + TPDFV_ZOOM_HEIGHT, + TPDFV_ZOOM_FIT, +} tpdfv_zoommode_t; + typedef struct { char *filename; @@ -52,6 +62,7 @@ typedef struct { int current_page_no; int total_pages; + tpdfv_zoommode_t zoom_mode; double zoom_factor; double delta_x; @@ -111,6 +122,7 @@ tpdfv_init(gtk_tpdfv_t *pdf, const char *filename) pdf->total_pages = poppler_document_get_n_pages(pdf->doc); + pdf->zoom_mode = TPDFV_ZOOM_CUSTOM; pdf->zoom_factor = 1.0; pdf->delta_x = pdf->delta_y = 0.0; return TRUE; @@ -178,7 +190,39 @@ gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event) cairo_clip(cr); /* zoom, scrolling */ - cairo_scale(cr, pdf->zoom_factor, pdf->zoom_factor); + if (pdf->zoom_mode == TPDFV_ZOOM_CUSTOM) { + cairo_scale(cr, pdf->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); + } + else if (pdf->zoom_mode == TPDFV_ZOOM_HEIGHT) { + zoom_factor = (double)(height / page_height); + } + else if (pdf->zoom_mode == TPDFV_ZOOM_FIT) { + zoom_factor = (double)TPDFV_MIN(width / page_width, + height / page_height); + } + else { + assert(0); + } + + cairo_scale(cr, zoom_factor, zoom_factor); + } + cairo_translate(cr, pdf->delta_x, pdf->delta_y); poppler_page_render(pdf->current_page, cr); @@ -291,6 +335,7 @@ gtk_tpdfv_zoom_in(GtkWidget *widget) pdf = GTK_TPDFV_GET_PRIVATE(widget); pdf->zoom_factor *= 1.2; + pdf->zoom_mode = TPDFV_ZOOM_CUSTOM; do_redraw(widget); } /* gtk_tpdfv_zoom_in */ @@ -303,6 +348,7 @@ gtk_tpdfv_zoom_out(GtkWidget *widget) if (pdf->zoom_factor > DBL_EPSILON * 2.0) { pdf->zoom_factor /= 1.2; + pdf->zoom_mode = TPDFV_ZOOM_CUSTOM; do_redraw(widget); } } /* gtk_tpdfv_zoom_out */ @@ -314,9 +360,40 @@ gtk_tpdfv_zoom_1(GtkWidget *widget) pdf = GTK_TPDFV_GET_PRIVATE(widget); pdf->zoom_factor = 1.0; + pdf->zoom_mode = TPDFV_ZOOM_CUSTOM; do_redraw(widget); } /* gtk_tpdfv_zoom_1 */ +void +gtk_tpdfv_zoom_width(GtkWidget *widget) +{ + gtk_tpdfv_t *pdf; + + pdf = GTK_TPDFV_GET_PRIVATE(widget); + pdf->zoom_mode = TPDFV_ZOOM_WIDTH; + do_redraw(widget); +} /* gtk_tpdfv_zoom_width */ + +void +gtk_tpdfv_zoom_height(GtkWidget *widget) +{ + gtk_tpdfv_t *pdf; + + pdf = GTK_TPDFV_GET_PRIVATE(widget); + pdf->zoom_mode = TPDFV_ZOOM_HEIGHT; + do_redraw(widget); +} /* gtk_tpdfv_zoom_width */ + +void +gtk_tpdfv_zoom_fit(GtkWidget *widget) +{ + gtk_tpdfv_t *pdf; + + pdf = GTK_TPDFV_GET_PRIVATE(widget); + pdf->zoom_mode = TPDFV_ZOOM_FIT; + do_redraw(widget); +} /* gtk_tpdfv_zoom_width */ + void gtk_tpdfv_scroll_up(GtkWidget *widget) { diff --git a/src/gtk-tpdfv.h b/src/gtk-tpdfv.h index c4e0599..7d260bd 100644 --- a/src/gtk-tpdfv.h +++ b/src/gtk-tpdfv.h @@ -90,6 +90,12 @@ void gtk_tpdfv_zoom_out(GtkWidget *widget); void gtk_tpdfv_zoom_1(GtkWidget *widget); +void +gtk_tpdfv_zoom_width(GtkWidget *widget); +void +gtk_tpdfv_zoom_height(GtkWidget *widget); +void +gtk_tpdfv_zoom_fit(GtkWidget *widget); /* * gtk_tpdfv_scroll_up, gtk_tpdfv_scroll_down, diff --git a/src/tpdfview.c b/src/tpdfview.c index 8c55600..d46aef0 100644 --- a/src/tpdfview.c +++ b/src/tpdfview.c @@ -135,6 +135,15 @@ key_press(GtkWidget __attribute__((unused)) *widget, case GDK_1: gtk_tpdfv_zoom_1(tpdfv); break; + case GDK_w: + gtk_tpdfv_zoom_width(tpdfv); + break; + case GDK_h: + gtk_tpdfv_zoom_height(tpdfv); + break; + case GDK_z: + gtk_tpdfv_zoom_fit(tpdfv); + break; /* scrolling */ case GDK_Up: