Commit 1f49e0e5 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Save scale level per page

parent 122ea70e
......@@ -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);
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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)
{
......
......@@ -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
*
......
......@@ -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);
}
......
......@@ -6,6 +6,8 @@
#include <girara/shortcuts.h>
#include <girara/utils.h>
#include <gtk/gtk.h>
#include <libgen.h>
#include <math.h>
#include <glib/gi18n.h>
#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);
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;
/* 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;
}
zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
if (page == NULL) {
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);
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_height > max_height) {
max_height = page_height;
}
if (page_width > max_width) {
max_width = page_width;
if (page_width > max_width) {
max_width = page_width;
}
total_width += page_width;
total_height += page_height;
}
total_width += page_width;
total_height += page_height;
}
double new_scale = 1.0;
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) {
switch (argument->n) {
case ZATHURA_ADJUST_WIDTH:
if (rotation == 0 || rotation == 180) {
zathura_document_set_scale(zathura->document, height / max_height);
new_scale = width / total_width;
} else {
zathura_document_set_scale(zathura->document, width / total_height);
new_scale = width / total_height;
}
} else {
if (rotation == 0 || rotation == 180) {
zathura_document_set_scale(zathura->document, width / total_width);
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 {
zathura_document_set_scale(zathura->document, height / total_width);
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;
}
break;
default:
goto error_ret;
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 */
......
......@@ -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);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment