From dc7c3d86eb59462f1a287164369e56a9d3b9fa9c Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Tue, 19 Apr 2011 19:24:03 +0200 Subject: [PATCH] Calculate page offset --- config.c | 2 ++ utils.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- utils.h | 17 +++++++++++++++ zathura.c | 5 +++++ zathura.h | 5 +++++ 5 files changed, 93 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index b847456..287b991 100644 --- a/config.c +++ b/config.c @@ -21,6 +21,8 @@ config_load_default(zathura_t* zathura) /* zathura settings */ int_value = 10; girara_setting_add(gsession, "zoom-step", &int_value, INT, false, "Zoom step", NULL); + int_value = 1; + girara_setting_add(gsession, "page-padding", &int_value, INT, true, "Padding between pages", NULL); int_value = 2; girara_setting_add(gsession, "pages-per-row", &int_value, INT, false, "Number of pages per row", NULL); diff --git a/utils.c b/utils.c index 9183924..42aad30 100644 --- a/utils.c +++ b/utils.c @@ -178,7 +178,8 @@ document_index_build(GtkTreeModel* model, GtkTreeIter* parent, girara_tree_node_ } while ((it = girara_list_iterator_next(it))); } -char* string_concat(const char* string1, ...) +char* +string_concat(const char* string1, ...) { if(!string1) { return NULL; @@ -234,3 +235,65 @@ char* string_concat(const char* string1, ...) return c; } + + +page_offset_t* +page_offset_top(zathura_page_t* page) +{ + if (page == NULL || page->document == NULL || page->document->zathura == NULL) { + return NULL; + } + + page_offset_t* offset = malloc(sizeof(page_offset_t)); + + if (offset == NULL) { + return NULL; + } + + zathura_document_t* document = page->document; + zathura_t* zathura = document->zathura; + + int* tmp = girara_setting_get(zathura->ui.session, "pages-per-row"); + + unsigned int page_padding = zathura->global.page_padding; + unsigned int pages_per_row = (tmp && *tmp != 0) ? *tmp : 1; + unsigned int number_of_rows = (document->number_of_pages / pages_per_row) + 1; + + free(tmp); + + for (unsigned int row = 0; row < number_of_rows; row++) { + unsigned int tmp = -1; + for (unsigned int column = 0; column < pages_per_row; column++) + { + unsigned int page_id = row * pages_per_row + column; + double page_height = document->pages[page_id]->height * document->scale; + + if (tmp == -1) { + tmp = page_height; + } else if (page_height > tmp) { + tmp = page_height; + } + } + + offset->y += tmp + (row == (number_of_rows - 1)) ? 0 : page_padding; + } + + for (unsigned int column = 0; column < pages_per_row; column++) { + unsigned int tmp = -1; + for (unsigned int row = 0; row < number_of_rows; row++) + { + unsigned int page_id = row * pages_per_row + column; + double page_width = document->pages[page_id]->width * document->scale; + + if (tmp == -1) { + tmp = page_width; + } else if (page_width > tmp) { + tmp = page_width; + } + } + + offset->x += tmp + (column == (pages_per_row - 1)) ? 0 : page_padding; + } + + return offset; +} diff --git a/utils.h b/utils.h index 735317a..8c10724 100644 --- a/utils.h +++ b/utils.h @@ -7,6 +7,14 @@ #include #include +#include "document.h" + +typedef struct page_offset_s +{ + int x; + int y; +} page_offset_t; + /** * Checks if the given file exists * @@ -61,4 +69,13 @@ void document_index_build(GtkTreeModel* model, GtkTreeIter* parent, girara_tree_ */ char* string_concat(const char* string1, ...); +/** + * Calculates the offset of the page to the top of the viewing area as + * well as to the left side of it. The result has to be freed. + * + * @param page The Page + * @return The calculated offset or NULL if an error occured + */ +page_offset_t* page_calculate_offset(zathura_page_t* page); + #endif // UTILS_H diff --git a/zathura.c b/zathura.c index a2e9055..5e823c7 100644 --- a/zathura.c +++ b/zathura.c @@ -148,6 +148,11 @@ zathura_init(int argc, char* argv[]) /* configuration */ config_load_default(zathura); + /* save page padding */ + int* page_padding = girara_setting_get(zathura->ui.session, "page-padding"); + zathura->global.page_padding = (page_padding) ? *page_padding : 1; + + /* open document if passed */ if (argc > 1) { zathura_document_info_t* document_info = malloc(sizeof(zathura_document_info_t)); diff --git a/zathura.h b/zathura.h index 86c17a9..262ee74 100644 --- a/zathura.h +++ b/zathura.h @@ -65,6 +65,11 @@ typedef struct zathura_s gchar* data_dir; } config; + struct + { + unsigned int page_padding; + } global; + zathura_document_t* document; /**> The current document */ } zathura_t;