Save scale level per page

This commit is contained in:
Moritz Lipp 2012-04-15 00:03:03 +02:00
parent 122ea70e16
commit 1f49e0e59a
8 changed files with 130 additions and 58 deletions

View File

@ -73,12 +73,12 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi
center.height = center.width = (2 * page_padding) + 1; center.height = center.width = (2 * page_padding) + 1;
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
double scale = zathura_document_get_scale(zathura->document);
bool updated = false; bool updated = false;
/* find page that fits */ /* find page that fits */
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(zathura->document, page_id); zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
double scale = zathura_page_get_scale(page);
GdkRectangle page_rect; GdkRectangle page_rect;
GtkWidget* page_widget = zathura_page_get_widget(zathura, page); GtkWidget* page_widget = zathura_page_get_widget(zathura, page);

View File

@ -309,6 +309,15 @@ zathura_document_set_scale(zathura_document_t* document, double scale)
return; 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; document->scale = scale;
} }

View File

@ -599,7 +599,7 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b
redraw_rect(ZATHURA_PAGE(widget), &priv->mouse.selection); redraw_rect(ZATHURA_PAGE(widget), &priv->mouse.selection);
zathura_rectangle_t tmp = 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.x1 /= scale;
tmp.x2 /= scale; tmp.x2 /= scale;
tmp.y1 /= scale; tmp.y1 /= scale;

22
page.c
View File

@ -17,6 +17,7 @@ struct zathura_page_s {
unsigned int index; /**< Page number */ unsigned int index; /**< Page number */
void* data; /**< Custom data */ void* data; /**< Custom data */
bool visible; /**< Page is visible */ bool visible; /**< Page is visible */
double scale; /**< Scale */
zathura_document_t* document; /**< Document */ 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->index = index;
page->visible = false; page->visible = false;
page->document = document; page->document = document;
page->scale = 1.0;
/* init plugin */ /* init plugin */
zathura_plugin_t* plugin = zathura_document_get_plugin(document); 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; 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* void*
zathura_page_get_data(zathura_page_t* page) zathura_page_get_data(zathura_page_t* page)
{ {

16
page.h
View File

@ -96,6 +96,22 @@ bool zathura_page_get_visibility(zathura_page_t* page);
*/ */
void zathura_page_set_visibility(zathura_page_t* page, bool visibility); 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 * Returns the custom data
* *

View File

@ -114,7 +114,7 @@ render(zathura_t* zathura, zathura_page_t* page)
cairo_restore(cairo); cairo_restore(cairo);
cairo_save(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) { if (fabs(scale - 1.0f) > FLT_EPSILON) {
cairo_scale(cairo, scale, scale); cairo_scale(cairo, scale, scale);
} }

View File

@ -6,6 +6,8 @@
#include <girara/shortcuts.h> #include <girara/shortcuts.h>
#include <girara/utils.h> #include <girara/utils.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <libgen.h>
#include <math.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include "callbacks.h" #include "callbacks.h"
@ -87,13 +89,19 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
} }
/* calculate total width and max-height */ /* calculate total width and max-height */
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
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_width = 0;
double total_height = 0; double total_height = 0;
double max_height = 0; double max_height = 0;
double max_width = 0; double max_width = 0;
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); /* calculate total width and max-height */
for (unsigned int page_id = 0; page_id < pages_per_row; page_id++) { 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) { if (page_id == number_of_pages) {
break; break;
} }
@ -118,27 +126,28 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
total_height += page_height; total_height += page_height;
} }
unsigned int rotation = zathura_document_get_rotation(zathura->document); double new_scale = 1.0;
switch (argument->n) { switch (argument->n) {
case ZATHURA_ADJUST_WIDTH: case ZATHURA_ADJUST_WIDTH:
if (rotation == 0 || rotation == 180) { if (rotation == 0 || rotation == 180) {
zathura_document_set_scale(zathura->document, width / total_width); new_scale = width / total_width;
} else { } else {
zathura_document_set_scale(zathura->document, width / total_height); new_scale = width / total_height;
} }
break; break;
case ZATHURA_ADJUST_BESTFIT: case ZATHURA_ADJUST_BESTFIT:
if (total_width < total_height) { if (total_width < total_height) {
if (rotation == 0 || rotation == 180) { if (rotation == 0 || rotation == 180) {
zathura_document_set_scale(zathura->document, height / max_height); new_scale = height / max_height;
} else { } else {
zathura_document_set_scale(zathura->document, width / total_height); new_scale = width / total_height;
} }
} else { } else {
if (rotation == 0 || rotation == 180) { if (rotation == 0 || rotation == 180) {
zathura_document_set_scale(zathura->document, width / total_width); new_scale = width / total_width;
} else { } else {
zathura_document_set_scale(zathura->document, height / total_width); new_scale = height / total_width;
} }
} }
@ -147,6 +156,22 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
goto error_ret; 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;
}
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 */ /* keep position */
readjust_view_after_zooming(zathura, old_zoom); readjust_view_after_zooming(zathura, old_zoom);

View File

@ -231,7 +231,7 @@ recalc_rectangle(zathura_page_t* page, zathura_rectangle_t rectangle)
double page_height = zathura_page_get_height(page); double page_height = zathura_page_get_height(page);
double page_width = zathura_page_get_width(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; 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 height = zathura_page_get_height(page);
double width = zathura_page_get_width(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) { if (rotate && zathura_document_get_rotation(document) % 180) {
*page_width = ceil(height * scale); *page_width = ceil(height * scale);