Commit 70dbf391 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Merge branch 'develop' of pwmt.org:zathura into develop

Conflicts:
	document.c
parents 1d606ef0 7003df76
......@@ -15,6 +15,17 @@
#include <girara/utils.h>
#include <girara/datastructures.h>
static int
compare_case_insensitive(const char* str1, const char* str2)
{
char* ustr1 = g_utf8_casefold(str1, -1);
char* ustr2 = g_utf8_casefold(str2, -1);
int res = g_utf8_collate(ustr1, ustr2);
g_free(ustr1);
g_free(ustr2);
return res;
}
static girara_list_t*
list_files(zathura_t* zathura, const char* current_path, const char* current_file, int current_file_length, bool is_dir)
{
......@@ -24,7 +35,7 @@ list_files(zathura_t* zathura, const char* current_path, const char* current_fil
return NULL;
}
girara_list_t* res = girara_sorted_list_new2((girara_compare_function_t)g_strcmp0,
girara_list_t* res = girara_sorted_list_new2((girara_compare_function_t)compare_case_insensitive,
(girara_free_function_t)g_free);
/* read files */
......
......@@ -19,6 +19,7 @@
#define KEY_PAGE "page"
#define KEY_OFFSET "offset"
#define KEY_SCALE "scale"
#define KEY_ROTATE "rotate"
#define file_lock_set(fd, cmd) \
{ \
......@@ -226,7 +227,7 @@ zathura_db_load_bookmarks(zathura_database_t* db, const char* file)
bool
zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
page, int offset, double scale)
page, int offset, double scale, int rotation)
{
if (db == NULL || db->history == NULL || file == NULL) {
return false;
......@@ -240,6 +241,7 @@ zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
g_key_file_set_integer(db->history, file, KEY_PAGE, page);
g_key_file_set_integer(db->history, file, KEY_OFFSET, offset);
g_key_file_set_string (db->history, file, KEY_SCALE, tmp);
g_key_file_set_integer(db->history, file, KEY_ROTATE, rotation);
g_free(tmp);
......@@ -250,10 +252,10 @@ zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
bool
zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
page, int* offset, double* scale)
page, int* offset, double* scale, int* rotation)
{
if (db == NULL || db->history == NULL || file == NULL || page == NULL ||
offset == NULL || scale == NULL) {
offset == NULL || scale == NULL || rotation == NULL) {
return false;
}
......@@ -264,6 +266,7 @@ zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
*page = g_key_file_get_integer(db->history, file, KEY_PAGE, NULL);
*offset = g_key_file_get_integer(db->history, file, KEY_OFFSET, NULL);
*scale = strtod(g_key_file_get_string(db->history, file, KEY_SCALE, NULL), NULL);
*rotation = g_key_file_get_integer(db->history, file, KEY_ROTATE, NULL);
return true;
}
......
......@@ -41,7 +41,8 @@ zathura_db_init(const char* dir)
"file TEXT PRIMARY KEY,"
"page INTEGER,"
"offset INTEGER,"
"scale FLOAT);";
"scale FLOAT,"
"rotation INTEGER);";
if (sqlite3_open(path, &(db->session)) != SQLITE_OK) {
girara_error("Could not open database: %s\n", path);
......@@ -198,12 +199,12 @@ zathura_db_load_bookmarks(zathura_database_t* db, const char* file)
bool
zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
page, int offset, double scale)
page, int offset, double scale, int rotation)
{
g_return_val_if_fail(db && file, false);
static const char SQL_FILEINFO_SET[] =
"REPLACE INTO fileinfo (file, page, offset, scale) VALUES (?, ?, ?, ?);";
"REPLACE INTO fileinfo (file, page, offset, scale, rotation) VALUES (?, ?, ?, ?, ?);";
sqlite3_stmt* stmt = prepare_statement(db->session, SQL_FILEINFO_SET);
if (stmt == NULL) {
......@@ -213,7 +214,8 @@ zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK ||
sqlite3_bind_int(stmt, 2, page) != SQLITE_OK ||
sqlite3_bind_int(stmt, 3, offset) != SQLITE_OK ||
sqlite3_bind_double(stmt, 4, scale) != SQLITE_OK) {
sqlite3_bind_double(stmt, 4, scale) != SQLITE_OK ||
sqlite3_bind_int(stmt, 5, rotation) != SQLITE_OK) {
sqlite3_finalize(stmt);
girara_error("Failed to bind arguments.");
return false;
......@@ -226,12 +228,12 @@ zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
bool
zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
page, int* offset, double* scale)
page, int* offset, double* scale, int* rotation)
{
g_return_val_if_fail(db && file && page && offset && scale, false);
g_return_val_if_fail(db && file && page && offset && scale && rotation, false);
static const char SQL_FILEINFO_GET[] =
"SELECT page, offset, scale FROM fileinfo WHERE file = ?;";
"SELECT page, offset, scale, rotation FROM fileinfo WHERE file = ?;";
sqlite3_stmt* stmt = prepare_statement(db->session, SQL_FILEINFO_GET);
if (stmt == NULL) {
......@@ -253,6 +255,7 @@ zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
*page = sqlite3_column_int(stmt, 0);
*offset = sqlite3_column_int(stmt, 1);
*scale = sqlite3_column_double(stmt, 2);
*rotation = sqlite3_column_int(stmt, 3);
sqlite3_finalize(stmt);
return true;
}
......
......@@ -64,10 +64,11 @@ girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char*
* @param page The last page.
* @param offset The last offset.
* @param scale The last scale.
* @param rotation The last rotation.
* @return true on success, false otherwise.
*/
bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned
int page, int offset, double scale);
int page, int offset, double scale, int rotation);
/* Get file info (last site, ...) from the database.
*
......@@ -76,9 +77,10 @@ bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned
* @param page The last page.
* @param offset The last offset.
* @param scale The last scale.
* @param rotation The rotation.
* @return true on success, false otherwise.
*/
bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned
int* page, int* offset, double* scale);
int* page, int* offset, double* scale, int* rotation);
#endif // DATABASE_H
......@@ -295,14 +295,16 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
/* read history file */
int offset = 0;
zathura_db_get_fileinfo(zathura->database, document->file_path,
&document->current_page_number, &offset, &document->scale);
&document->current_page_number, &offset, &document->scale, &document->rotate);
/* check for valid scale value */
if (document->scale <= FLT_EPSILON) {
girara_warning("document info: '%s' has non positive scale", document->file_path);
document->scale = 1;
}
if (document->current_page_number == 0 || document->current_page_number > document->number_of_pages) {
/* check current page number */
if (document->current_page_number < 1 || document->current_page_number > document->number_of_pages) {
girara_warning("document info: '%s' has an invalid page number", document->file_path);
document->current_page_number = 1;
}
......@@ -498,7 +500,11 @@ zathura_page_get(zathura_document_t* document, unsigned int page_id, zathura_plu
page->drawing_area = zathura_page_widget_new(page);
page->document = document;
gtk_widget_set_size_request(page->drawing_area, page->width * document->scale, page->height * document->scale);
unsigned int page_height = 0;
unsigned int page_width = 0;
page_calc_height_width(page, &page_height, &page_width, true);
gtk_widget_set_size_request(page->drawing_area, page_width, page_height);
}
return page;
......@@ -606,6 +612,26 @@ zathura_page_image_save(zathura_page_t* page, zathura_image_t* image, const char
return page->document->functions.page_image_save(page, image, file);
}
char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_plugin_error_t* error)
{
if (page == NULL || page->document == NULL) {
if (error) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
return NULL;
}
if (page->document->functions.page_get_text == NULL) {
girara_error("%s not implemented", __FUNCTION__);
if (error) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
return NULL;
}
return page->document->functions.page_get_text(page, rectangle, error);
}
zathura_plugin_error_t
zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing)
{
......
......@@ -274,6 +274,11 @@ struct zathura_document_s
*/
zathura_plugin_error_t (*page_image_save)(zathura_page_t* page, zathura_image_t* image, const char* file);
/**
* Get text for selection
*/
char* (*page_get_text)(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_plugin_error_t* error);
/**
* Renders the page
*/
......@@ -481,6 +486,16 @@ girara_list_t* zathura_page_images_get(zathura_page_t* page, zathura_plugin_erro
*/
zathura_plugin_error_t zathura_page_image_save(zathura_page_t* page, zathura_image_t* image, const char* file);
/**
* Get text for selection
* @param page Page
* @param rectangle Selection
* @error Set to an error value (see \ref zathura_plugin_error_t) if an error
* occured
* @return The selected text (needs to be deallocated with g_free)
*/
char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_plugin_error_t* error);
/**
* Render page
*
......
......@@ -97,9 +97,7 @@ zathura_page_widget_init(ZathuraPage* widget)
/* we want mouse events */
gtk_widget_add_events(GTK_WIDGET(widget),
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK);
/* widgets can focus */
gtk_widget_set_can_focus(GTK_WIDGET(widget), TRUE);
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
}
GtkWidget*
......
......@@ -10,22 +10,11 @@
#include "zathura.h"
#include "document.h"
#include "page_widget.h"
#include "utils.h"
void* render_job(void* data);
bool render(zathura_t* zathura, zathura_page_t* page);
static void
page_calc_height_width(zathura_page_t* page, unsigned int* page_height, unsigned int* page_width, bool rotate)
{
if (rotate && page->document->rotate % 180) {
*page_width = ceil(page->height * page->document->scale);
*page_height = ceil(page->width * page->document->scale);
} else {
*page_width = ceil(page->width * page->document->scale);
*page_height = ceil(page->height * page->document->scale);
}
}
void*
render_job(void* data)
{
......
......@@ -320,13 +320,16 @@ sc_rotate(girara_session_t* session, girara_argument_t* UNUSED(argument),
zathura_t* zathura = session->global.data;
g_return_val_if_fail(zathura->document != NULL, false);
unsigned int page_number = zathura->document->current_page_number;
/* update rotate value */
zathura->document->rotate = (zathura->document->rotate + 90) % 360;
/* render all pages again */
/* XXX: we don't need to rerender, only to resize the widgets and redraw */
render_all(zathura);
page_set_delayed(zathura, page_number);
return false;
}
......
......@@ -7,6 +7,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "math.h"
#include "utils.h"
#include "zathura.h"
......@@ -189,6 +190,38 @@ page_calculate_offset(zathura_page_t* page, page_offset_t* offset)
zathura->ui.page_widget, 0, 0, &(offset->x), &(offset->y)) == true);
}
zathura_rectangle_t rotate_rectangle(zathura_rectangle_t rectangle, unsigned int degree, int height, int width)
{
zathura_rectangle_t tmp;
switch (degree) {
case 90:
tmp.x1 = height - rectangle.y2;
tmp.x2 = height - rectangle.y1;
tmp.y1 = rectangle.x1;
tmp.y2 = rectangle.x2;
break;
case 180:
tmp.x1 = width - rectangle.x2;
tmp.x2 = width - rectangle.x1;
tmp.y1 = height - rectangle.y2;
tmp.y2 = height - rectangle.y1;
break;
case 270:
tmp.x1 = rectangle.y1;
tmp.x2 = rectangle.y2;
tmp.y1 = width - rectangle.x2;
tmp.y2 = width - rectangle.x1;
break;
default:
tmp.x1 = rectangle.x1;
tmp.x2 = rectangle.x2;
tmp.y1 = rectangle.y1;
tmp.y2 = rectangle.y2;
}
return tmp;
}
zathura_rectangle_t
recalc_rectangle(zathura_page_t* page, zathura_rectangle_t rectangle)
{
......@@ -232,3 +265,17 @@ set_adjustment(GtkAdjustment* adjustment, gdouble value)
{
gtk_adjustment_set_value(adjustment, MAX(adjustment->lower, MIN(adjustment->upper - adjustment->page_size, value)));
}
void
page_calc_height_width(zathura_page_t* page, unsigned int* page_height, unsigned int* page_width, bool rotate)
{
g_return_if_fail(page != NULL && page_height != NULL && page_width != NULL);
if (rotate && page->document->rotate % 180) {
*page_width = ceil(page->height * page->document->scale);
*page_height = ceil(page->width * page->document->scale);
} else {
*page_width = ceil(page->width * page->document->scale);
*page_height = ceil(page->height * page->document->scale);
}
}
......@@ -72,6 +72,16 @@ void document_index_build(GtkTreeModel* model, GtkTreeIter* parent, girara_tree_
*/
void page_calculate_offset(zathura_page_t* page, page_offset_t* offset);
/**
* Rotate a rectangle by 0, 90, 180 or 270 degree
* @param rect the rectangle to rotate
* @param degree rotation degree
* @param height the height of the enclosing rectangle
* @param width the width of the enclosing rectangle
* @return the rotated rectangle
*/
zathura_rectangle_t rotate_rectangle(zathura_rectangle_t rectangle, unsigned int degree, int height, int width);
/**
* Calculates the new coordinates based on the rotation and scale level of the
* document for the given rectangle
......@@ -89,4 +99,16 @@ zathura_rectangle_t recalc_rectangle(zathura_page_t* page, zathura_rectangle_t r
*/
void set_adjustment(GtkAdjustment* adjust, gdouble value);
/**
* 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
*/
void
page_calc_height_width(zathura_page_t* page, unsigned int* page_height, unsigned int* page_width, bool rotate);
#endif // UTILS_H
......@@ -502,7 +502,8 @@ document_close(zathura_t* zathura)
/* store last seen page */
zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, zathura->document->current_page_number + 1,
/* zathura->document->offset TODO */ 0, zathura->document->scale);
/* zathura->document->offset TODO */ 0, zathura->document->scale,
zathura->document->rotate);
render_free(zathura->sync.render_thread);
zathura->sync.render_thread = NULL;
......
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