Merge remote-tracking branch 'ViktorWalter/develop' into develop

This commit is contained in:
Sebastian Ramacher 2022-12-03 14:56:40 +01:00
commit da33077766
5 changed files with 86 additions and 2 deletions

View file

@ -14,6 +14,8 @@ page_calc_height_width(zathura_document_t* document, double height,
double scale = zathura_document_get_scale(document); 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) { if (rotate == true && zathura_document_get_rotation(document) % 180 != 0) {
*page_width = round(height * scale); *page_width = round(height * scale);
*page_height = round(width * scale); *page_height = round(width * scale);

View file

@ -12,9 +12,9 @@
* desired. * desired.
* *
* @param document the document * @param document the document
* @param height the original height * @param height the original height
* @param width the original width * @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 page_width the scaled and rotated width
* @param rotate honor page's rotation * @param rotate honor page's rotation
* @return real scale after rounding * @return real scale after rounding

View file

@ -451,6 +451,7 @@ zathura_document_set_zoom(zathura_document_t* document, double zoom)
return; return;
} }
/* fprintf(stderr, "orig_zoom: %f\t new_zoom: %f\n", document->zoom, zoom); */
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; *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 void
zathura_document_set_page_layout(zathura_document_t* document, unsigned int page_padding, zathura_document_set_page_layout(zathura_document_t* document, unsigned int page_padding,
unsigned int pages_per_row, unsigned int first_page_column) unsigned int pages_per_row, unsigned int first_page_column)

View file

@ -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, ZATHURA_PLUGIN_API void zathura_document_get_document_size(zathura_document_t* document,
unsigned int* height, unsigned int* width); 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 * Sets the layout of the pages in the document
* *

View file

@ -967,6 +967,51 @@ document_open_password_dialog(gpointer data)
return FALSE; 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 bool
document_open(zathura_t* zathura, const char* path, const char* uri, const char* password, document_open(zathura_t* zathura, const char* path, const char* uri, const char* password,
int page_number) int page_number)
@ -1169,12 +1214,23 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char*
goto error_free; 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++) { for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
zathura_page_t* page = zathura_document_get_page(document, page_id); zathura_page_t* page = zathura_document_get_page(document, page_id);
if (page == NULL) { if (page == NULL) {
goto error_free; 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); GtkWidget* page_widget = zathura_page_widget_new(zathura, page);
if (page_widget == NULL) { if (page_widget == NULL) {
goto error_free; goto error_free;
@ -1728,6 +1784,9 @@ adjust_view(zathura_t* zathura)
double zoom = zathura_document_get_zoom(zathura->document); double zoom = zathura_document_get_zoom(zathura->document);
double newzoom = zoom; 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 || if (adjust_mode == ZATHURA_ADJUST_WIDTH ||
(adjust_mode == ZATHURA_ADJUST_BESTFIT && page_ratio < view_ratio)) { (adjust_mode == ZATHURA_ADJUST_BESTFIT && page_ratio < view_ratio)) {
newzoom *= (double)view_width / (double)document_width; newzoom *= (double)view_width / (double)document_width;