add functions to compute page positions

The plan is to put in adjustment.c every piece of code that has to do
with document positioning, either computing it from data on the document
side, or talking to GTK.

We want to have at our disposal functions to compute sizes and positions
without having to ask for it to a GTK widget. The new functions are:

 - move page_calc_height_width to adjustment.c

 - add page_calc_position that rotates a position relative to a page
   according to the rotation settings.

 - add position_to_page_number that computes the number of a page
   sitting at a given position (given in document-relative coordinates)

 - add page_number_to_position that computes the position (in document
   relative coordinates) that will be placed at the center of the viewport.

 - add page_is_visible that checks whether the given page intersects the
   viewport.
This commit is contained in:
Abdo Roig-Maranges 2013-10-23 09:46:00 +02:00
parent bca5bcc571
commit 030a8c65c1
7 changed files with 208 additions and 37 deletions

View file

@ -2,6 +2,144 @@
#include "adjustment.h"
#include "utils.h"
#include <math.h>
double
page_calc_height_width(zathura_document_t* document, double height, double width,
unsigned int* page_height, unsigned int* page_width, bool rotate)
{
g_return_val_if_fail(document != NULL && page_height != NULL && page_width != NULL, 0.0);
double scale = zathura_document_get_scale(document);
double real_scale;
if (rotate && zathura_document_get_rotation(document) % 180) {
*page_width = round(height * scale);
*page_height = round(width * scale);
real_scale = MAX(*page_width / height, *page_height / width);
} else {
*page_width = round(width * scale);
*page_height = round(height * scale);
real_scale = MAX(*page_width / width, *page_height / height);
}
return real_scale;
}
void
page_calc_position(zathura_document_t* document, double x, double y,
double *xn, double *yn) {
g_return_if_fail(document != NULL && xn != NULL && yn != NULL);
unsigned int rot = zathura_document_get_rotation(document);
if (rot == 90) {
*xn = 1 - y;
*yn = x;
}else if (rot == 180) {
*xn = 1 - x;
*yn = 1 - y;
} else if (rot == 270) {
*xn = y;
*yn = 1 - x;
} else {
*xn = x;
*yn = y;
}
}
unsigned int
position_to_page_number(zathura_document_t* document,
double pos_x, double pos_y){
g_return_val_if_fail(document != NULL, 0);
unsigned int doc_width, doc_height;
zathura_document_get_document_size(document, &doc_height, &doc_width);
unsigned int cell_width, cell_height;
zathura_document_get_cell_size(document, &cell_height, &cell_width);
unsigned int c0 = zathura_document_get_first_page_column(document);
unsigned int npag = zathura_document_get_number_of_pages(document);
unsigned int ncol = zathura_document_get_pages_per_row(document);
unsigned int nrow = (npag + c0 - 1 + ncol - 1) / ncol; /* number of rows */
unsigned int pad = zathura_document_get_page_padding(document);
unsigned int col = floor(pos_x * (double)doc_width / (double)(cell_width + pad));
unsigned int row = floor(pos_y * (double)doc_height / (double)(cell_height + pad));
return ncol * (row % nrow) + (col % ncol) - (c0 - 1);
}
void
page_number_to_position(zathura_document_t* document, unsigned int page_number,
double xalign, double yalign, double *pos_x, double *pos_y) {
g_return_if_fail(document != NULL);
unsigned int c0 = zathura_document_get_first_page_column(document);
unsigned int npag = zathura_document_get_number_of_pages(document);
unsigned int ncol = zathura_document_get_pages_per_row(document);
unsigned int nrow = (npag + c0 - 1 + ncol - 1) / ncol; /* number of rows */
/* row and column for page_number indexed from 0 */
unsigned int row = (page_number + c0 - 1) / ncol;
unsigned int col = (page_number + c0 - 1) % ncol;
/* sizes of page cell, viewport and document */
unsigned int cell_height = 0, cell_width = 0;
zathura_document_get_cell_size(document, &cell_height, &cell_width);
unsigned int view_height = 0, view_width = 0;
zathura_document_get_viewport_size(document, &view_height, &view_width);
unsigned int doc_height = 0, doc_width = 0;
zathura_document_get_document_size(document, &doc_height, &doc_width);
/* compute the shift to align to the viewport. If the page fits to viewport, just center it. */
double shift_x = 0.5, shift_y = 0.5;
if (cell_width > view_width) {
shift_x = 0.5 + (xalign - 0.5) * ((double)cell_width - (double)view_width) / (double)cell_width;
}
if (cell_height > view_height) {
shift_y = 0.5 + (yalign - 0.5) * ((double)cell_height - (double)view_height) / (double)cell_height;
}
/* compute the position */
*pos_x = ((double)col + shift_x) / (double)ncol;
*pos_y = ((double)row + shift_y) / (double)nrow;
}
bool
page_is_visible(zathura_document_t *document, unsigned int page_number) {
g_return_val_if_fail(document != NULL, false);
/* position at the center of the viewport */
double pos_x = zathura_document_get_position_x(document);
double pos_y = zathura_document_get_position_y(document);
/* get the center of page page_number */
double page_x, page_y;
page_number_to_position(document, page_number, 0.5, 0.5, &page_x, &page_y);
unsigned int cell_width, cell_height;
zathura_document_get_cell_size(document, &cell_height, &cell_width);
unsigned int doc_width, doc_height;
zathura_document_get_document_size(document, &doc_height, &doc_width);
unsigned int view_width, view_height;
zathura_document_get_viewport_size(document, &view_height, &view_width);
return ( fabs(pos_x - page_x) < 0.5 * (double)(view_width + cell_width) / (double)doc_width &&
fabs(pos_y - page_y) < 0.5 * (double)(view_height + cell_height) / (double)doc_height);
}
GtkAdjustment*
zathura_adjustment_clone(GtkAdjustment* adjustment)

View file

@ -4,8 +4,73 @@
#define ZATHURA_ADJUSTMENT_H
#include <gtk/gtk.h>
#include <stdbool.h>
#include "document.h"
/* Clone a GtkAdjustment
/**
* Calculate the page size according to the corrent scaling and rotation if
* desired.
*
* @param document the document
* @param height width the original height and width
* @return page_height page_width the scaled and rotated height and width
* @param rotate honor page's rotation
* @return real scale after rounding
*/
double page_calc_height_width(zathura_document_t* document, double height, double width,
unsigned int* page_height, unsigned int* page_width, bool rotate);
/**
* Calculate a page relative position after a rotation. The positions x y are
* relative to a page, i.e. 0=top of page, 1=bottom of page. They are NOT
* relative to the entire document.
*
* @param document the document
* @param x y the x y coordinates on the unrotated page
* @param xn yn the x y coordinates after rotation
*/
void page_calc_position(zathura_document_t* document, double x, double y,
double *xn, double *yn);
/**
* Converts a relative position within the document to a page number.
*
* @param document The document
* @param pos_x pos_y the position relative to the document
* @return page sitting in that position
*/
unsigned int position_to_page_number(zathura_document_t* document,
double pos_x, double pos_y);
/**
* Converts a page number to a position in units relative to the document
*
* We can specify where to aliwn the viewport and the page. For instance, xalign
* = 0 means align them on the left margin, xalign = 0.5 means centered, and
* xalign = 1.0 align them on the right margin.
*
* The return value is the position in in units relative to the document (0=top
* 1=bottom) of the point thet will lie at the center of the viewport.
*
* @param document The document
* @param page_number the given page number
* @param xalign yalign where to align the viewport and the page
* @return pos_x pos_y position that will lie at the center of the viewport.
*/
void page_number_to_position(zathura_document_t* document, unsigned int page_number,
double xalign, double yalign, double *pos_x, double *pos_y);
/**
* Checks whether a given page falls within the viewport
*
* @param document The document
* @param page_number the page number
* @return true if the page intersects the viewport
*/
bool page_is_visible(zathura_document_t *document, unsigned int page_number);
/**
* Clone a GtkAdjustment
*
* Creates a new adjustment with the same value, lower and upper bounds, step
* and page increments and page_size as the original adjustment.

View file

@ -22,6 +22,7 @@
#include <girara/session.h>
#include <girara/settings.h>
#include "adjustment.h"
#include "document.h"
#include "utils.h"
#include "zathura.h"

View file

@ -6,6 +6,7 @@
#include <girara/session.h>
#include <girara/settings.h>
#include "adjustment.h"
#include "links.h"
#include "zathura.h"
#include "document.h"

View file

@ -4,7 +4,9 @@
#include <girara/datastructures.h>
#include <girara/utils.h>
#include "glib-compat.h"
#include "render.h"
#include "adjustment.h"
#include "zathura.h"
#include "document.h"
#include "page.h"

23
utils.c
View file

@ -240,29 +240,6 @@ error_ret:
return rectangle;
}
double
page_calc_height_width(zathura_document_t* document, double height, double width,
unsigned int* page_height, unsigned int* page_width, bool rotate)
{
g_return_val_if_fail(document != NULL && page_height != NULL && page_width != NULL, 0.0);
double scale = zathura_document_get_scale(document);
double real_scale;
if (rotate && zathura_document_get_rotation(document) % 180) {
*page_width = ceil(height * scale);
*page_height = ceil(width * scale);
real_scale = MAX(*page_width / height, *page_height / width);
} else {
*page_width = ceil(width * scale);
*page_height = ceil(height * scale);
real_scale = MAX(*page_width / width, *page_height / height);
}
return real_scale;
}
GtkWidget*
zathura_page_get_widget(zathura_t* zathura, zathura_page_t* page)
{

13
utils.h
View file

@ -86,19 +86,6 @@ zathura_rectangle_t rotate_rectangle(zathura_rectangle_t rectangle, unsigned int
*/
zathura_rectangle_t recalc_rectangle(zathura_page_t* page, zathura_rectangle_t rectangle);
/**
* Calculate the page size according to the corrent scaling and rotation if
* desired.
* @param page the page
* @param page_height the resulting page height
* @param page_width the resultung page width
* @param rotate honor page's rotation
* @return real scale after rounding
*/
double
page_calc_height_width(zathura_document_t* document, double height, double width,
unsigned int* page_height, unsigned int* page_width, bool rotate);
/**
* Returns the page widget of the page
*