diff --git a/zathura/adjustment.c b/zathura/adjustment.c index fcd4d15..84fedf8 100644 --- a/zathura/adjustment.c +++ b/zathura/adjustment.c @@ -14,6 +14,8 @@ page_calc_height_width(zathura_document_t* document, double height, double scale = zathura_document_get_scale(document); + // TODO this just set all pages to the maximum. + // needs to adjust cell size based on the page size itself. if (rotate == true && zathura_document_get_rotation(document) % 180 != 0) { *page_width = round(height * scale); *page_height = round(width * scale); diff --git a/zathura/adjustment.h b/zathura/adjustment.h index 55110c7..033e67f 100644 --- a/zathura/adjustment.h +++ b/zathura/adjustment.h @@ -12,9 +12,9 @@ * desired. * * @param document the document - * @param height the original height + * @param height the original height * @param width the original width - * @param page_height the scaled and rotated height + * @param page_height the scaled and rotated height * @param page_width the scaled and rotated width * @param rotate honor page's rotation * @return real scale after rounding diff --git a/zathura/document.c b/zathura/document.c index 99f492f..818e2b6 100644 --- a/zathura/document.c +++ b/zathura/document.c @@ -451,6 +451,7 @@ 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; } @@ -648,6 +649,16 @@ 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, unsigned int first_page_column) 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 244169f..9955a22 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -967,6 +967,51 @@ document_open_password_dialog(gpointer data) return FALSE; } +typedef struct sample_s { + double h; + double w; + unsigned int freq; +} sample_t; + +static int document_page_size_comp(const void *a, const void *b) { + return ((sample_t *)a)->freq - ((sample_t *)b)->freq; +} + +static void document_open_page_most_frequent_size(zathura_document_t *document, + unsigned int *width, + unsigned int *height) { + sample_t samples[32] = {{0}}; /* a max heap */ + 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 (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); + unsigned int i = 0; + for (i = 0; i < last_sample; i++) { + if (samples[i].h == h && samples[i].w == w) { + samples[i].freq++; + break; + } + } + if (i == last_sample) { /* insert */ + samples[page_id].h = h; + samples[page_id].w = w; + samples[page_id].freq = 1; + } + } + + 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); */ + } + + *width = samples[31].w; + *height = samples[31].h; +} + bool document_open(zathura_t* zathura, const char* path, const char* uri, const char* password, int page_number) @@ -1169,12 +1214,23 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char* goto error_free; } + unsigned int most_freq_width, most_freq_height; + document_open_page_most_frequent_size(document, &most_freq_width, + &most_freq_height); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { zathura_page_t* page = zathura_document_get_page(document, page_id); if (page == NULL) { 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); + GtkWidget* page_widget = zathura_page_widget_new(zathura, page); if (page_widget == NULL) { goto error_free; @@ -1728,6 +1784,9 @@ 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;