From 1f49e0e59a0c55d3ab45b86d8e4d30263bc47d93 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Sun, 15 Apr 2012 00:03:03 +0200 Subject: [PATCH] Save scale level per page --- callbacks.c | 2 +- document.c | 9 ++++ page-widget.c | 2 +- page.c | 22 +++++++++ page.h | 16 ++++++ render.c | 2 +- shortcuts.c | 131 ++++++++++++++++++++++++++++++-------------------- utils.c | 4 +- 8 files changed, 130 insertions(+), 58 deletions(-) diff --git a/callbacks.c b/callbacks.c index 8337d98..b997882 100644 --- a/callbacks.c +++ b/callbacks.c @@ -73,12 +73,12 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi center.height = center.width = (2 * page_padding) + 1; unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); - double scale = zathura_document_get_scale(zathura->document); bool updated = false; /* find page that fits */ for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); + double scale = zathura_page_get_scale(page); GdkRectangle page_rect; GtkWidget* page_widget = zathura_page_get_widget(zathura, page); diff --git a/document.c b/document.c index 474d4b5..7223def 100644 --- a/document.c +++ b/document.c @@ -309,6 +309,15 @@ zathura_document_set_scale(zathura_document_t* document, double scale) return; } + for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(document, page_id); + if (page == NULL) { + continue; + } + + zathura_page_set_scale(page, scale); + } + document->scale = scale; } diff --git a/page-widget.c b/page-widget.c index b4c80fb..a6ae59c 100644 --- a/page-widget.c +++ b/page-widget.c @@ -599,7 +599,7 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b redraw_rect(ZATHURA_PAGE(widget), &priv->mouse.selection); zathura_rectangle_t tmp = priv->mouse.selection; - double scale = zathura_document_get_scale(document); + double scale = zathura_page_get_scale(priv->page); tmp.x1 /= scale; tmp.x2 /= scale; tmp.y1 /= scale; diff --git a/page.c b/page.c index ff70a10..f5416b1 100644 --- a/page.c +++ b/page.c @@ -17,6 +17,7 @@ struct zathura_page_s { unsigned int index; /**< Page number */ void* data; /**< Custom data */ bool visible; /**< Page is visible */ + double scale; /**< Scale */ zathura_document_t* document; /**< Document */ }; @@ -36,6 +37,7 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error page->index = index; page->visible = false; page->document = document; + page->scale = 1.0; /* init plugin */ zathura_plugin_t* plugin = zathura_document_get_plugin(document); @@ -171,6 +173,26 @@ zathura_page_set_visibility(zathura_page_t* page, bool visibility) page->visible = visibility; } +double +zathura_page_get_scale(zathura_page_t* page) +{ + if (page == NULL) { + return 1.0; + } + + return page->scale; +} + +void +zathura_page_set_scale(zathura_page_t* page, double scale) +{ + if (page == NULL) { + return; + } + + page->scale = scale; +} + void* zathura_page_get_data(zathura_page_t* page) { diff --git a/page.h b/page.h index dbac902..d0177cf 100644 --- a/page.h +++ b/page.h @@ -96,6 +96,22 @@ bool zathura_page_get_visibility(zathura_page_t* page); */ void zathura_page_set_visibility(zathura_page_t* page, bool visibility); +/** + * Returns the scale value of the page + * + * @param page The page + * @return The scale value + */ +double zathura_page_get_scale(zathura_page_t* page); + +/** + * Sets the new scale level of the page + * + * @param page The page + * @param scale The new scale value + */ +void zathura_page_set_scale(zathura_page_t* page, double scale); + /** * Returns the custom data * diff --git a/render.c b/render.c index a709f64..fa9b2c6 100644 --- a/render.c +++ b/render.c @@ -114,7 +114,7 @@ render(zathura_t* zathura, zathura_page_t* page) cairo_restore(cairo); cairo_save(cairo); - double scale = zathura_document_get_scale(zathura->document); + double scale = zathura_page_get_scale(page); if (fabs(scale - 1.0f) > FLT_EPSILON) { cairo_scale(cairo, scale, scale); } diff --git a/shortcuts.c b/shortcuts.c index 0e9c91c..3bbc30a 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include "callbacks.h" @@ -87,64 +89,87 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument, } /* calculate total width and max-height */ - double total_width = 0; - double total_height = 0; - double max_height = 0; - double max_width = 0; - unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); - for (unsigned int page_id = 0; page_id < pages_per_row; page_id++) { - if (page_id == number_of_pages) { - break; - } + unsigned int number_of_rows = ceil(number_of_pages / pages_per_row); + unsigned int rotation = zathura_document_get_rotation(zathura->document); - zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); - if (page == NULL) { - goto error_ret; - } + for (unsigned int row = 0; row < number_of_rows; row++) { + double total_width = 0; + double total_height = 0; + double max_height = 0; + double max_width = 0; - unsigned int page_height = zathura_page_get_height(page); - unsigned int page_width = zathura_page_get_width(page); - - if (page_height > max_height) { - max_height = page_height; - } - - if (page_width > max_width) { - max_width = page_width; - } - - total_width += page_width; - total_height += page_height; - } - - unsigned int rotation = zathura_document_get_rotation(zathura->document); - switch (argument->n) { - case ZATHURA_ADJUST_WIDTH: - if (rotation == 0 || rotation == 180) { - zathura_document_set_scale(zathura->document, width / total_width); - } else { - zathura_document_set_scale(zathura->document, width / total_height); - } - break; - case ZATHURA_ADJUST_BESTFIT: - if (total_width < total_height) { - if (rotation == 0 || rotation == 180) { - zathura_document_set_scale(zathura->document, height / max_height); - } else { - zathura_document_set_scale(zathura->document, width / total_height); - } - } else { - if (rotation == 0 || rotation == 180) { - zathura_document_set_scale(zathura->document, width / total_width); - } else { - zathura_document_set_scale(zathura->document, height / total_width); - } + /* calculate total width and max-height */ + for (unsigned int column = 0; column < pages_per_row; column++) { + unsigned int page_id = row * pages_per_row + column; + if (page_id == number_of_pages) { + break; } - break; - default: - goto error_ret; + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); + if (page == NULL) { + goto error_ret; + } + + unsigned int page_height = zathura_page_get_height(page); + unsigned int page_width = zathura_page_get_width(page); + + if (page_height > max_height) { + max_height = page_height; + } + + if (page_width > max_width) { + max_width = page_width; + } + + total_width += page_width; + total_height += page_height; + } + + double new_scale = 1.0; + + switch (argument->n) { + case ZATHURA_ADJUST_WIDTH: + if (rotation == 0 || rotation == 180) { + new_scale = width / total_width; + } else { + new_scale = width / total_height; + } + break; + case ZATHURA_ADJUST_BESTFIT: + if (total_width < total_height) { + if (rotation == 0 || rotation == 180) { + new_scale = height / max_height; + } else { + new_scale = width / total_height; + } + } else { + if (rotation == 0 || rotation == 180) { + new_scale = width / total_width; + } else { + new_scale = height / total_width; + } + } + + break; + default: + goto error_ret; + } + + /* apply new scale */ + for (unsigned int column = 0; column < pages_per_row; column++) { + unsigned int page_id = row * pages_per_row + column; + if (page_id == number_of_pages) { + break; + } + + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); + if (page == NULL) { + goto error_ret; + } + + zathura_page_set_scale(page, new_scale); + } } /* keep position */ diff --git a/utils.c b/utils.c index 6d24478..c9ed358 100644 --- a/utils.c +++ b/utils.c @@ -231,7 +231,7 @@ recalc_rectangle(zathura_page_t* page, zathura_rectangle_t rectangle) double page_height = zathura_page_get_height(page); double page_width = zathura_page_get_width(page); - double scale = zathura_document_get_scale(document); + double scale = zathura_page_get_scale(page); zathura_rectangle_t tmp; @@ -287,7 +287,7 @@ page_calc_height_width(zathura_page_t* page, unsigned int* page_height, unsigned double height = zathura_page_get_height(page); double width = zathura_page_get_width(page); - double scale = zathura_document_get_scale(document); + double scale = zathura_page_get_scale(page); if (rotate && zathura_document_get_rotation(document) % 180) { *page_width = ceil(height * scale);