From 6d66bc88af5c9c322b1f15974d0eceb67a7a73f8 Mon Sep 17 00:00:00 2001 From: Viktor Walter Date: Mon, 29 Nov 2021 19:12:45 +0100 Subject: [PATCH] Fixing the issue of the 'best-fit' zoom being way too low if one page is larger that the rest. The zoom was trying to accomodate for the large page, but it should instead accomodate for the most frequent size. This is still not ideal, since as it is now Zathura crops the larger pages to the size of the most frequent size, but this typically only affects things like larger first pages in scans. I am building this on thop of the Ailrk-fork, since that version already implemented a way how to estimate the most frequent page size, from the first 32 pages. --- zathura/document.c | 9 +++++++++ zathura/document.h | 12 ++++++++++++ zathura/zathura.c | 16 +++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/zathura/document.c b/zathura/document.c index ffa6376..3e0e6e4 100644 --- a/zathura/document.c +++ b/zathura/document.c @@ -427,6 +427,7 @@ void zathura_document_set_zoom(zathura_document_t *document, double zoom) { return; } + /* fprintf(stderr, "orig_zoom: %f\t new_zoom: %f\n", document->zoom, zoom); */ document->zoom = zoom; } @@ -603,6 +604,14 @@ void zathura_document_get_document_size(zathura_document_t *document, *height = nrow * cell_height + (nrow - 1) * pad; } +void zathura_document_set_cell_size(zathura_document_t *document, + unsigned int cell_height, + unsigned int cell_width) { + document->cell_width = cell_width; + document->cell_height = cell_height; +} + + void zathura_document_set_page_layout(zathura_document_t *document, unsigned int page_padding, unsigned int pages_per_row, diff --git a/zathura/document.h b/zathura/document.h index 90c1c0f..f972f6f 100644 --- a/zathura/document.h +++ b/zathura/document.h @@ -328,6 +328,18 @@ ZATHURA_PLUGIN_API void zathura_document_get_cell_size(zathura_document_t* docum ZATHURA_PLUGIN_API void zathura_document_get_document_size(zathura_document_t* document, unsigned int* height, unsigned int* width); + +/** + * Sets the cell height and width of the document + * + * @param[in] document The document instance + * @param[in] cell_height The desired cell height + * @param[in] cell_width The desired cell width + */ +ZATHURA_PLUGIN_API void zathura_document_set_cell_size(zathura_document_t *document, + unsigned int cell_height, + unsigned int cell_width); + /** * Sets the layout of the pages in the document * diff --git a/zathura/zathura.c b/zathura/zathura.c index b576d06..8023c3e 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -931,11 +931,6 @@ static void document_open_page_most_frequent_size(zathura_document_t *document, unsigned int number_of_pages = zathura_document_get_number_of_pages(document); unsigned int last_sample = (number_of_pages > 32) ? 32 : number_of_pages; - for (int i = 0; i < 32; ++i) { - fprintf(stderr, "i: %d, w: %f, h: %f, freq: %d\n", i, samples[i].h, - samples[i].w, samples[i].freq); - } - for (unsigned int page_id = 0; page_id < last_sample; page_id++) { zathura_page_t *page = zathura_document_get_page(document, page_id); double w = zathura_page_get_width(page), h = zathura_page_get_height(page); @@ -956,8 +951,8 @@ static void document_open_page_most_frequent_size(zathura_document_t *document, qsort((void *)samples, 32, sizeof(sample_t), document_page_size_comp); for (int i = 0; i < 32; ++i) { - fprintf(stderr, "i: %d, w: %f, h: %f, freq: %d\n", i, samples[i].h, - samples[i].w, samples[i].freq); + /* fprintf(stderr, "i: %d, w: %f, h: %f, freq: %d\n", i, samples[i].h, + samples[i].w, samples[i].freq); */ } *width = samples[31].w; @@ -1189,6 +1184,10 @@ bool document_open(zathura_t *zathura, const char *path, const char *uri, goto error_free; } + unsigned int cell_height = 0, cell_width = 0; + zathura_document_get_cell_size(document, &cell_height, &cell_width); + /* fprintf(stderr, "new_cell_height: %d \t new_cell_width: %d\n", most_freq_height, most_freq_width); */ + zathura_document_set_cell_size(document, most_freq_height, most_freq_width); zathura_page_set_width(page, most_freq_width); zathura_page_set_height(page, most_freq_height); @@ -1741,6 +1740,9 @@ bool adjust_view(zathura_t *zathura) { double zoom = zathura_document_get_zoom(zathura->document); double newzoom = zoom; + /* fprintf(stderr, "cell_height: %d \t cell_width: %d \t page_ratio: %f\n", cell_height, cell_width, page_ratio); */ + /* fprintf(stderr, "view_height: %d \t view_width: %d \t view_ratio: %f\n", view_height, view_width, view_ratio); */ + if (adjust_mode == ZATHURA_ADJUST_WIDTH || (adjust_mode == ZATHURA_ADJUST_BESTFIT && page_ratio < view_ratio)) { newzoom *= (double)view_width / (double)document_width;