diff --git a/README b/README index 0588c69..52115a0 100644 --- a/README +++ b/README @@ -8,7 +8,7 @@ Requirements ------------ meson (>= 0.43) -gtk3 (>= 3.10) +gtk3 (>= 3.22) glib (>= 2.50) girara (>= 0.2.8) sqlite3 (optional, >= 3.5.9) diff --git a/meson.build b/meson.build index 3a381cc..7566810 100644 --- a/meson.build +++ b/meson.build @@ -41,7 +41,7 @@ girara = dependency('girara-gtk3', version: '>=0.2.8') glib = dependency('glib-2.0', version: '>=2.50') gthread = dependency('gthread-2.0', version: '>=2.50') gmodule = dependency('gmodule-no-export-2.0', version: '>=2.50') -gtk3 = dependency('gtk+-3.0', version: '>=3.10') +gtk3 = dependency('gtk+-3.0', version: '>=3.22') build_dependencies = [libm, girara, glib, gthread, gmodule, gtk3] diff --git a/zathura/adjustment.c b/zathura/adjustment.c index 9a10da5..b4d70a7 100644 --- a/zathura/adjustment.c +++ b/zathura/adjustment.c @@ -13,6 +13,7 @@ page_calc_height_width(zathura_document_t* document, double height, g_return_val_if_fail(document != NULL && page_height != NULL && page_width != NULL, 0.0); double scale = zathura_document_get_scale(document); + if (rotate == true && zathura_document_get_rotation(document) % 180 != 0) { *page_width = round(height * scale); *page_height = round(width * scale); diff --git a/zathura/adjustment.h b/zathura/adjustment.h index 0ae967c..a1c3da4 100644 --- a/zathura/adjustment.h +++ b/zathura/adjustment.h @@ -8,7 +8,7 @@ #include "document.h" /** - * Calculate the page size according to the corrent scaling and rotation if + * Calculate the page size according to the current scaling and rotation if * desired. * * @param document the document diff --git a/zathura/callbacks.c b/zathura/callbacks.c index c7ba706..63024da 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -216,6 +216,57 @@ cb_refresh_view(GtkWidget* GIRARA_UNUSED(view), gpointer data) statusbar_page_number_update(zathura); } +void +cb_monitors_changed(GdkScreen* screen, gpointer data) +{ + girara_debug("signal received"); + + zathura_t* zathura = data; + if (screen == NULL || zathura == NULL) { + return; + } + + zathura_update_view_ppi(zathura); +} + +void +cb_widget_screen_changed(GtkWidget* widget, GdkScreen* previous_screen, gpointer data) +{ + girara_debug("called"); + + zathura_t* zathura = data; + if (widget == NULL || zathura == NULL) { + return; + } + + /* disconnect previous screen handler if present */ + if (previous_screen != NULL && zathura->signals.monitors_changed_handler > 0) { + g_signal_handler_disconnect(previous_screen, zathura->signals.monitors_changed_handler); + zathura->signals.monitors_changed_handler = 0; + } + + if (gtk_widget_has_screen(widget)) { + GdkScreen* screen = gtk_widget_get_screen(widget); + + /* connect to new screen */ + zathura->signals.monitors_changed_handler = g_signal_connect(G_OBJECT(screen), + "monitors-changed", G_CALLBACK(cb_monitors_changed), zathura); + } + + zathura_update_view_ppi(zathura); +} + +void +cb_widget_configured(GtkWidget* UNUSED(widget), GdkEvent* UNUSED(event), gpointer data) +{ + zathura_t* zathura = data; + if (zathura == NULL) { + return; + } + + zathura_update_view_ppi(zathura); +} + void cb_scale_factor(GObject* object, GParamSpec* UNUSED(pspec), gpointer data) { @@ -235,6 +286,7 @@ cb_scale_factor(GObject* object, GParamSpec* UNUSED(pspec), gpointer data) fabs(new_factor - current.y) >= DBL_EPSILON) { zathura_document_set_device_factors(zathura->document, new_factor, new_factor); girara_debug("New device scale factor: %d", new_factor); + zathura_update_view_ppi(zathura); render_all(zathura); } } diff --git a/zathura/callbacks.h b/zathura/callbacks.h index 95ee0fe..bee54e0 100644 --- a/zathura/callbacks.h +++ b/zathura/callbacks.h @@ -79,6 +79,46 @@ void cb_view_vadjustment_changed(GtkAdjustment *adjustment, gpointer data); */ void cb_refresh_view(GtkWidget* view, gpointer data); +/** + * This function gets called when the monitors associated with the GdkScreen + * change. + * + * It checks for a change of monitor PPI, storing the new value and triggering + * a refresh if appropriate. + * + * @param screen The GDK screen + * @param gpointer The zathura instance + */ +void cb_monitors_changed(GdkScreen* screen, gpointer data); + +/** + * This function gets called when the screen associated with the view widget + * changes. + * + * It updates the connection on the monitors-changed signal and checks for a + * change of monitor PPI, storing the new value and triggering a refresh if + * appropriate. + * + * @param widget The view widget + * @param previous_screen The widget's previous screen + * @param gpointer The zathura instance + */ +void cb_widget_screen_changed(GtkWidget* widget, GdkScreen* previous_screen, gpointer data); + +/** + * This function gets called when the main window's size, position or stacking + * changes. + * + * It checks for a change of monitor PPI (due to the window moving between + * different monitors), storing the new value and triggering a refresh if + * appropriate. + * + * @param widget The main window widget + * @param event The configure event + * @param gpointer The zathura instance + */ +void cb_widget_configured(GtkWidget* widget, GdkEvent* event, gpointer data); + /** * This function gets called when the view widget scale factor changes (e.g. * when moving from a regular to a HiDPI screen). diff --git a/zathura/database-plain.c b/zathura/database-plain.c index 17c8438..254a626 100644 --- a/zathura/database-plain.c +++ b/zathura/database-plain.c @@ -24,7 +24,7 @@ #define KEY_PAGE "page" #define KEY_OFFSET "offset" -#define KEY_SCALE "scale" +#define KEY_ZOOM "zoom" #define KEY_ROTATE "rotate" #define KEY_PAGES_PER_ROW "pages-per-row" #define KEY_FIRST_PAGE_COLUMN "first-page-column" @@ -550,7 +550,7 @@ plain_set_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t* g_key_file_set_integer(priv->history, name, KEY_PAGE, file_info->current_page); g_key_file_set_integer(priv->history, name, KEY_OFFSET, file_info->page_offset); - g_key_file_set_double (priv->history, name, KEY_SCALE, file_info->scale); + g_key_file_set_double (priv->history, name, KEY_ZOOM, file_info->zoom); g_key_file_set_integer(priv->history, name, KEY_ROTATE, file_info->rotation); g_key_file_set_integer(priv->history, name, KEY_PAGES_PER_ROW, file_info->pages_per_row); g_key_file_set_string(priv->history, name, KEY_FIRST_PAGE_COLUMN, file_info->first_page_column_list); @@ -586,7 +586,7 @@ plain_get_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t* file_info->current_page = g_key_file_get_integer(priv->history, name, KEY_PAGE, NULL); file_info->page_offset = g_key_file_get_integer(priv->history, name, KEY_OFFSET, NULL); - file_info->scale = g_key_file_get_double (priv->history, name, KEY_SCALE, NULL); + file_info->zoom = g_key_file_get_double (priv->history, name, KEY_ZOOM, NULL); file_info->rotation = g_key_file_get_integer(priv->history, name, KEY_ROTATE, NULL); /* the following flags got introduced at a later point */ diff --git a/zathura/database-sqlite.c b/zathura/database-sqlite.c index 7e01f57..cc44337 100644 --- a/zathura/database-sqlite.c +++ b/zathura/database-sqlite.c @@ -147,7 +147,7 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path) "file TEXT PRIMARY KEY," "page INTEGER," "offset INTEGER," - "scale FLOAT," + "zoom FLOAT," "rotation INTEGER," "pages_per_row INTEGER," "first_page_column TEXT," @@ -184,6 +184,10 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path) static const char SQL_FILEINFO_ALTER3[] = "ALTER TABLE fileinfo ADD COLUMN time TIMESTAMP;"; + /* update fileinfo table (part 4) */ + static const char SQL_FILEINFO_ALTER4[] = + "ALTER TABLE fileinfo ADD COLUMN zoom FLOAT;"; + /* update bookmark table */ static const char SQL_BOOKMARK_ALTER[] = "ALTER TABLE bookmarks ADD COLUMN hadj_ratio FLOAT;" @@ -234,6 +238,15 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path) } } + ret1 = check_column(session, "fileinfo", "zoom", &res1); + + if (ret1 == true && res1 == false) { + girara_debug("old database table layout detected; updating ..."); + if (sqlite3_exec(session, SQL_FILEINFO_ALTER4, NULL, 0, NULL) != SQLITE_OK) { + girara_warning("failed to update database table layout"); + } + } + ret1 = check_column(session, "bookmarks", "hadj_ratio", &res1); ret2 = check_column(session, "bookmarks", "vadj_ratio", &res2); @@ -625,7 +638,7 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db); static const char SQL_FILEINFO_SET[] = - "REPLACE INTO fileinfo (file, page, offset, scale, rotation, pages_per_row, first_page_column, position_x, position_y, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, DATETIME('now'));"; + "REPLACE INTO fileinfo (file, page, offset, zoom, rotation, pages_per_row, first_page_column, position_x, position_y, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, DATETIME('now'));"; sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_SET); if (stmt == NULL) { @@ -635,7 +648,7 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK || sqlite3_bind_int(stmt, 2, file_info->current_page) != SQLITE_OK || sqlite3_bind_int(stmt, 3, file_info->page_offset) != SQLITE_OK || - sqlite3_bind_double(stmt, 4, file_info->scale) != SQLITE_OK || + sqlite3_bind_double(stmt, 4, file_info->zoom) != SQLITE_OK || sqlite3_bind_int(stmt, 5, file_info->rotation) != SQLITE_OK || sqlite3_bind_int(stmt, 6, file_info->pages_per_row) != SQLITE_OK || sqlite3_bind_text(stmt, 7, file_info->first_page_column_list, -1, NULL) @@ -664,7 +677,7 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file, zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db); static const char SQL_FILEINFO_GET[] = - "SELECT page, offset, scale, rotation, pages_per_row, first_page_column, position_x, position_y FROM fileinfo WHERE file = ?;"; + "SELECT page, offset, zoom, rotation, pages_per_row, first_page_column, position_x, position_y FROM fileinfo WHERE file = ?;"; sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_GET); if (stmt == NULL) { @@ -685,7 +698,7 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file, file_info->current_page = sqlite3_column_int(stmt, 0); file_info->page_offset = sqlite3_column_int(stmt, 1); - file_info->scale = sqlite3_column_double(stmt, 2); + file_info->zoom = sqlite3_column_double(stmt, 2); file_info->rotation = sqlite3_column_int(stmt, 3); file_info->pages_per_row = sqlite3_column_int(stmt, 4); file_info->first_page_column_list = g_strdup((const char*) sqlite3_column_text(stmt, 5)); diff --git a/zathura/database.h b/zathura/database.h index 7c76407..1211a3d 100644 --- a/zathura/database.h +++ b/zathura/database.h @@ -12,7 +12,7 @@ typedef struct zathura_fileinfo_s { unsigned int current_page; unsigned int page_offset; - double scale; + double zoom; unsigned int rotation; unsigned int pages_per_row; char* first_page_column_list; diff --git a/zathura/document.c b/zathura/document.c index f463187..46bd56e 100644 --- a/zathura/document.c +++ b/zathura/document.c @@ -28,15 +28,16 @@ struct zathura_document_s { const char* password; /**< Password of the document */ unsigned int current_page_number; /**< Current page number */ unsigned int number_of_pages; /**< Number of pages */ - double scale; /**< Scale value */ + double zoom; /**< Zoom value */ unsigned int rotate; /**< Rotation */ void* data; /**< Custom data */ zathura_adjust_mode_t adjust_mode; /**< Adjust mode (best-fit, width) */ int page_offset; /**< Page offset */ - double cell_width; /**< width of a page cell in the document (not ransformed by scale and rotation) */ - double cell_height; /**< height of a page cell in the document (not ransformed by scale and rotation) */ + double cell_width; /**< width of a page cell in the document (not transformed by scale and rotation) */ + double cell_height; /**< height of a page cell in the document (not transformed by scale and rotation) */ unsigned int view_width; /**< width of current viewport */ unsigned int view_height; /**< height of current viewport */ + double view_ppi; /**< PPI of the current viewport */ zathura_device_factors_t device_factors; /**< x and y device scale factors (for e.g. HiDPI) */ unsigned int pages_per_row; /**< number of pages in a row */ unsigned int first_page_column; /**< column of the first page */ @@ -127,13 +128,14 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri, g_object_unref(gf); } document->password = password; - document->scale = 1.0; + document->zoom = 1.0; document->plugin = plugin; document->adjust_mode = ZATHURA_ADJUST_NONE; document->cell_width = 0.0; document->cell_height = 0.0; document->view_height = 0; document->view_width = 0; + document->view_ppi = 0.0; document->device_factors.x = 1.0; document->device_factors.y = 1.0; document->position_x = 0.0; @@ -388,6 +390,26 @@ zathura_document_set_position_y(zathura_document_t* document, double position_y) document->position_y = position_y; } +double +zathura_document_get_zoom(zathura_document_t* document) +{ + if (document == NULL) { + return 0; + } + + return document->zoom; +} + +void +zathura_document_set_zoom(zathura_document_t* document, double zoom) +{ + if (document == NULL) { + return; + } + + document->zoom = zoom; +} + double zathura_document_get_scale(zathura_document_t* document) { @@ -395,17 +417,14 @@ zathura_document_get_scale(zathura_document_t* document) return 0; } - return document->scale; -} - -void -zathura_document_set_scale(zathura_document_t* document, double scale) -{ - if (document == NULL) { - return; + double ppi = document->view_ppi; + if (ppi < DBL_EPSILON) { + /* No PPI information -> use a typical value */ + ppi = 100; } - document->scale = scale; + /* scale = pixels per point, and there are 72 points in one inch */ + return document->zoom * ppi / 72.0; } unsigned int @@ -495,6 +514,15 @@ zathura_document_set_viewport_height(zathura_document_t* document, unsigned int document->view_height = height; } +void +zathura_document_set_viewport_ppi(zathura_document_t* document, double ppi) +{ + if (document == NULL) { + return; + } + document->view_ppi = ppi; +} + void zathura_document_get_viewport_size(zathura_document_t* document, unsigned int *height, unsigned int* width) @@ -504,6 +532,15 @@ zathura_document_get_viewport_size(zathura_document_t* document, *width = document->view_width; } +double +zathura_document_get_viewport_ppi(zathura_document_t* document) +{ + if (document == NULL) { + return 0.0; + } + return document->view_ppi; +} + void zathura_document_set_device_factors(zathura_document_t* document, double x_factor, double y_factor) diff --git a/zathura/document.h b/zathura/document.h index a993411..42d7e77 100644 --- a/zathura/document.h +++ b/zathura/document.h @@ -142,20 +142,29 @@ void zathura_document_set_position_x(zathura_document_t* document, double positi void zathura_document_set_position_y(zathura_document_t* document, double position_y); /** - * Returns the current scale value of the document + * Returns the current zoom value of the document * * @param document The document - * @return The current scale value + * @return The current zoom value + */ +double zathura_document_get_zoom(zathura_document_t* document); + +/** + * Returns the current scale value of the document (based on zoom and screen + * PPI) + * + * @param document The document + * @return The current scale value, in pixels per point */ double zathura_document_get_scale(zathura_document_t* document); /** - * Sets the new scale value of the document + * Sets the new zoom value of the document * * @param document The document - * @param scale The new scale value + * @param zoom The new zoom value */ -void zathura_document_set_scale(zathura_document_t* document, double scale); +void zathura_document_set_zoom(zathura_document_t* document, double zoom); /** * Returns the rotation value of zathura (0..360) @@ -249,6 +258,26 @@ void zathura_document_get_viewport_size(zathura_document_t* document, unsigned int *height, unsigned int* width); +/** + Sets the viewport PPI (pixels per inch: the resolution of the monitor, after + scaling with the device factor). + * + * @param[in] document The document instance + * @param[in] height The viewport PPI + */ +void +zathura_document_set_viewport_ppi(zathura_document_t* document, double ppi); + +/** + * Return the viewport PPI (pixels per inch: the resolution of the monitor, + * after scaling with the device factor). + * + * @param[in] document The document instance + * @return The viewport PPI + */ +double +zathura_document_get_viewport_ppi(zathura_document_t* document); + /** * Set the device scale factors (e.g. for HiDPI). These are generally integers * and equal for x and y. These scaling factors are only used when rendering to diff --git a/zathura/links.c b/zathura/links.c index 47030e2..47c6c38 100644 --- a/zathura/links.c +++ b/zathura/links.c @@ -114,7 +114,7 @@ zathura_link_target_t zathura_link_get_target(zathura_link_t* link) { if (link == NULL) { - zathura_link_target_t target = { 0, NULL, 0, 0, 0, 0, 0, 0 }; + zathura_link_target_t target = { 0, NULL, 0, 0, 0, 0, 0, { 0 } }; return target; } @@ -134,9 +134,9 @@ zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link) switch (link->type) { case ZATHURA_LINK_GOTO_DEST: if (link->target.destination_type != ZATHURA_LINK_DESTINATION_UNKNOWN) { - if (link->target.scale >= DBL_EPSILON && link_zoom) { - zathura_document_set_scale(zathura->document, - zathura_correct_scale_value(zathura->ui.session, link->target.scale)); + if (link->target.zoom >= DBL_EPSILON && link_zoom) { + zathura_document_set_zoom(zathura->document, + zathura_correct_zoom_value(zathura->ui.session, link->target.zoom)); render_all(zathura); } diff --git a/zathura/marks.c b/zathura/marks.c index 0433c7f..12eee4f 100644 --- a/zathura/marks.c +++ b/zathura/marks.c @@ -24,7 +24,7 @@ struct zathura_mark_s { double position_x; /**> Horizontal adjustment */ double position_y; /**> Vertical adjustment */ unsigned int page; /**> Page number */ - double scale; /**> Zoom level */ + double zoom; /**> Zoom level */ }; bool @@ -202,7 +202,7 @@ mark_add(zathura_t* zathura, int key) double position_x = zathura_document_get_position_x(zathura->document); double position_y = zathura_document_get_position_y(zathura->document); - double scale = zathura_document_get_scale(zathura->document); + double zoom = zathura_document_get_zoom(zathura->document); /* search for existing mark */ GIRARA_LIST_FOREACH_BODY_WITH_ITER(zathura->global.marks, zathura_mark_t*, iter, mark, @@ -210,7 +210,7 @@ mark_add(zathura_t* zathura, int key) mark->page = page_id; mark->position_x = position_x; mark->position_y = position_y; - mark->scale = scale; + mark->zoom = zoom; girara_list_iterator_free(iter); return; } @@ -226,7 +226,7 @@ mark_add(zathura_t* zathura, int key) mark->page = page_id; mark->position_x = position_x; mark->position_y = position_y; - mark->scale = scale; + mark->zoom = zoom; girara_list_append(zathura->global.marks, mark); } @@ -241,8 +241,8 @@ mark_evaluate(zathura_t* zathura, int key) /* search for existing mark */ GIRARA_LIST_FOREACH_BODY(zathura->global.marks, zathura_mark_t*, mark, if (mark != NULL && mark->key == key) { - zathura_document_set_scale(zathura->document, - zathura_correct_scale_value(zathura->ui.session, mark->scale)); + zathura_document_set_zoom(zathura->document, + zathura_correct_zoom_value(zathura->ui.session, mark->zoom)); render_all(zathura); zathura_jumplist_add(zathura); diff --git a/zathura/page-widget.c b/zathura/page-widget.c index 606268f..0f95d2e 100644 --- a/zathura/page-widget.c +++ b/zathura/page-widget.c @@ -317,8 +317,8 @@ set_font_from_property(cairo_t* cairo, zathura_t* zathura, cairo_font_weight_t w const char* family = pango_font_description_get_family(descr); /* get font size: can be points or absolute. - * absolute units: value = 10*PANGO_SCALE = 10 (unscaled) device units (logical pixels) - * point units: value = 10*PANGO_SCALE = 10 points = 10*(font dpi config / 72) device units */ + * absolute units: example: value 10*PANGO_SCALE = 10 (unscaled) device units (logical pixels) + * point units: example: value 10*PANGO_SCALE = 10 points = 10*(font dpi config / 72) device units */ double size = pango_font_description_get_size(descr) / PANGO_SCALE; /* convert point size to device units */ @@ -496,7 +496,6 @@ zathura_page_widget_get_property(GObject* object, guint prop_id, GValue* value, } } -#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,14,0) static zathura_device_factors_t get_safe_device_factors(cairo_surface_t* surface) { @@ -512,13 +511,6 @@ get_safe_device_factors(cairo_surface_t* surface) return factors; } -#else -static zathura_device_factors_t -get_safe_device_factors(cairo_surface_t* UNUSED(surface)) -{ - return (zathura_device_factors_t){1.0, 1.0}; -} -#endif static gboolean zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) @@ -690,9 +682,9 @@ zathura_page_widget_redraw_canvas(ZathuraPage* pageview) } /* smaller than max to be replaced by actual renders */ -#define THUMBNAIL_INITIAL_SCALE 0.5 +#define THUMBNAIL_INITIAL_ZOOM 0.5 /* small enough to make bilinear downscaling fast */ -#define THUMBNAIL_MAX_SCALE 0.5 +#define THUMBNAIL_MAX_ZOOM 0.5 static bool surface_small_enough(cairo_surface_t* surface, size_t max_size, cairo_surface_t* old) @@ -712,7 +704,7 @@ surface_small_enough(cairo_surface_t* surface, size_t max_size, cairo_surface_t* const unsigned int width_old = cairo_image_surface_get_width(old); const unsigned int height_old = cairo_image_surface_get_height(old); const size_t old_size = width_old * height_old; - if (new_size < old_size && new_size >= old_size * THUMBNAIL_MAX_SCALE * THUMBNAIL_MAX_SCALE) { + if (new_size < old_size && new_size >= old_size * THUMBNAIL_MAX_ZOOM * THUMBNAIL_MAX_ZOOM) { return false; } } @@ -725,9 +717,9 @@ draw_thumbnail_image(cairo_surface_t* surface, size_t max_size) { unsigned int width = cairo_image_surface_get_width(surface); unsigned int height = cairo_image_surface_get_height(surface); - double scale = sqrt((double)max_size / (width * height)) * THUMBNAIL_INITIAL_SCALE; - if (scale > THUMBNAIL_MAX_SCALE) { - scale = THUMBNAIL_MAX_SCALE; + double scale = sqrt((double)max_size / (width * height)) * THUMBNAIL_INITIAL_ZOOM; + if (scale > THUMBNAIL_MAX_ZOOM) { + scale = THUMBNAIL_MAX_ZOOM; } width = width * scale; height = height * scale; diff --git a/zathura/render.c b/zathura/render.c index 5953de0..e7ddc6c 100644 --- a/zathura/render.c +++ b/zathura/render.c @@ -740,25 +740,25 @@ render(render_job_t* job, ZathuraRenderRequest* request, ZathuraRenderer* render const double height = zathura_page_get_height(page); const double width = zathura_page_get_width(page); - /* page size in user pixels base on document zoom: 100% results in 1 pixel per point */ + /* page size in user pixels based on document zoom: if PPI information is + * correct, 100% zoom will result in 72 documents points per inch of screen + * (i.e. document size on screen matching the physical paper size). */ const double real_scale = page_calc_height_width(document, height, width, &page_height, &page_width, false); -#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,14,0) zathura_device_factors_t device_factors = zathura_document_get_device_factors(document); page_width *= device_factors.x; page_height *= device_factors.y; -#endif cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height); if (surface == NULL) { return false; } -#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,14,0) + cairo_surface_set_device_scale(surface, device_factors.x, device_factors.y); -#endif + if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy(surface); return false; @@ -868,8 +868,10 @@ render_all(zathura_t* zathura) girara_debug("Queuing resize for page %u to %u x %u (%f x %f).", page_id, page_width, page_height, width, height); GtkWidget* widget = zathura_page_get_widget(zathura, page); - gtk_widget_set_size_request(widget, page_width, page_height); - gtk_widget_queue_resize(widget); + if (widget != NULL) { + gtk_widget_set_size_request(widget, page_width, page_height); + gtk_widget_queue_resize(widget); + } } } diff --git a/zathura/shortcuts.c b/zathura/shortcuts.c index 11ca04e..f14a00b 100644 --- a/zathura/shortcuts.c +++ b/zathura/shortcuts.c @@ -1304,8 +1304,8 @@ sc_toggle_presentation(girara_session_t* session, girara_argument_t* /* set full screen */ gtk_window_unfullscreen(GTK_WINDOW(session->gtk.window)); - /* reset scale */ - zathura_document_set_scale(zathura->document, zathura->shortcut.toggle_presentation_mode.zoom); + /* reset zoom */ + zathura_document_set_zoom(zathura->document, zathura->shortcut.toggle_presentation_mode.zoom); render_all(zathura); refresh_view(zathura); @@ -1326,7 +1326,7 @@ sc_toggle_presentation(girara_session_t* session, girara_argument_t* girara_setting_set(session, "pages-per-row", &int_value); /* back up zoom */ - zathura->shortcut.toggle_presentation_mode.zoom = zathura_document_get_scale(zathura->document); + zathura->shortcut.toggle_presentation_mode.zoom = zathura_document_get_zoom(zathura->document); /* adjust window */ girara_argument_t argument = { ZATHURA_ADJUST_BESTFIT, NULL }; @@ -1379,37 +1379,37 @@ sc_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t* const int nt = (t == 0) ? 1 : t; const double zoom_step = 1.0 + value / 100.0 * nt; - const double old_zoom = zathura_document_get_scale(zathura->document); + const double old_zoom = zathura_document_get_zoom(zathura->document); /* specify new zoom value */ if (argument->n == ZOOM_IN) { girara_debug("Increasing zoom by %f.", zoom_step - 1.0); - zathura_document_set_scale(zathura->document, old_zoom * zoom_step); + zathura_document_set_zoom(zathura->document, old_zoom * zoom_step); } else if (argument->n == ZOOM_OUT) { girara_debug("Decreasing zoom by %f.", zoom_step - 1.0); - zathura_document_set_scale(zathura->document, old_zoom / zoom_step); + zathura_document_set_zoom(zathura->document, old_zoom / zoom_step); } else if (argument->n == ZOOM_SPECIFIC) { if (t == 0) { girara_debug("Setting zoom to 1."); - zathura_document_set_scale(zathura->document, 1.0); + zathura_document_set_zoom(zathura->document, 1.0); } else { girara_debug("Setting zoom to %f.", t / 100.0); - zathura_document_set_scale(zathura->document, t / 100.0); + zathura_document_set_zoom(zathura->document, t / 100.0); } } else if (argument->n == ZOOM_SMOOTH) { const double dy = (event != NULL) ? event->y : 1.0; girara_debug("Increasing zoom by %f.", zoom_step * dy - 1.0); - zathura_document_set_scale(zathura->document, old_zoom + zoom_step * dy); + zathura_document_set_zoom(zathura->document, old_zoom + zoom_step * dy); } else { girara_debug("Setting zoom to 1."); - zathura_document_set_scale(zathura->document, 1.0); + zathura_document_set_zoom(zathura->document, 1.0); } /* zoom limitations */ - const double scale = zathura_document_get_scale(zathura->document); - zathura_document_set_scale(zathura->document, zathura_correct_scale_value(session, scale)); + const double zoom = zathura_document_get_zoom(zathura->document); + zathura_document_set_zoom(zathura->document, zathura_correct_zoom_value(session, zoom)); - const double new_zoom = zathura_document_get_scale(zathura->document); + const double new_zoom = zathura_document_get_zoom(zathura->document); if (fabs(new_zoom - old_zoom) <= DBL_EPSILON) { girara_debug("New and old zoom level are too close: %f vs. %f, diff = %f", new_zoom, old_zoom, fabs(new_zoom - old_zoom)); return false; diff --git a/zathura/types.h b/zathura/types.h index 0cf1f9b..006890b 100644 --- a/zathura/types.h +++ b/zathura/types.h @@ -179,7 +179,10 @@ typedef struct zathura_link_target_s double right; /**< Right coordinate */ double top; /**< Top coordinate */ double bottom; /**< Bottom coordinate */ - double scale; /**< Scale */ + union { + double scale; /**< @deprecated Scale */ + double zoom; /**< Zoom */ + }; } zathura_link_target_t; /** diff --git a/zathura/utils.c b/zathura/utils.c index bd19131..6c3c4ad 100644 --- a/zathura/utils.c +++ b/zathura/utils.c @@ -21,10 +21,10 @@ #include "content-type.h" double -zathura_correct_scale_value(girara_session_t* session, const double scale) +zathura_correct_zoom_value(girara_session_t* session, const double zoom) { if (session == NULL) { - return scale; + return zoom; } /* zoom limitations */ @@ -36,12 +36,12 @@ zathura_correct_scale_value(girara_session_t* session, const double scale) const double zoom_min = zoom_min_int * 0.01; const double zoom_max = zoom_max_int * 0.01; - if (scale < zoom_min) { + if (zoom < zoom_min) { return zoom_min; - } else if (scale > zoom_max) { + } else if (zoom > zoom_max) { return zoom_max; } else { - return scale; + return zoom; } } diff --git a/zathura/utils.h b/zathura/utils.h index 135eb2e..088b214 100644 --- a/zathura/utils.h +++ b/zathura/utils.h @@ -96,16 +96,16 @@ char* zathura_get_version_string(zathura_t* zathura, bool markup); GdkAtom* get_selection(zathura_t* zathura); /** - * Returns the valid scale value which needs to lie in the interval of zoom_min + * Returns the valid zoom value which needs to lie in the interval of zoom_min * and zoom_max specified in the girara session * * @param[in] session The session - * @param[in] scale The proposed scale value + * @param[in] zoom The proposed zoom value * - * @return The corrected scale value + * @return The corrected zoom value */ -double zathura_correct_scale_value(girara_session_t* session, const double - scale); +double zathura_correct_zoom_value(girara_session_t* session, const double + zoom); /** diff --git a/zathura/zathura.c b/zathura/zathura.c index 46aeb24..a7622d5 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -15,6 +15,10 @@ #include #include +#ifdef GDK_WINDOWING_WAYLAND +#include +#endif + #ifdef G_OS_UNIX #include #include @@ -131,6 +135,69 @@ create_directories(zathura_t* zathura) } } +void +zathura_update_view_ppi(zathura_t* zathura) +{ + if (zathura == NULL) { + return; + } + + /* get view widget GdkMonitor */ + GdkWindow* window = gtk_widget_get_window (zathura->ui.session->gtk.view); // NULL if not realized + if (window == NULL) { + return; + } + GdkDisplay* display = gtk_widget_get_display(zathura->ui.session->gtk.view); + if (display == NULL) { + return; + } + + double ppi = 0.0; + + GdkMonitor* monitor = gdk_display_get_monitor_at_window(display, window); + if (monitor == NULL) { + return; + } + + /* physical width of monitor */ + int width_mm = gdk_monitor_get_width_mm(monitor); + + /* size of monitor in pixels */ + GdkRectangle monitor_geom; + gdk_monitor_get_geometry(monitor, &monitor_geom); + + /* calculate ppi, knowing that 1 inch = 25.4 mm */ + if (width_mm == 0) { + girara_debug("cannot calculate PPI: monitor has zero width"); + } else { + ppi = monitor_geom.width * 25.4 / width_mm; + } + +#ifdef GDK_WINDOWING_WAYLAND + /* work around apparent bug in GDK: on Wayland, monitor geometry doesn't + * return values in application pixels as documented, but in device pixels. + * */ + if (GDK_IS_WAYLAND_DISPLAY(display)) + { + /* not using the cached value for the scale factor here to avoid issues + * if this function is called before the cached value is updated */ + int device_factor = gtk_widget_get_scale_factor(zathura->ui.session->gtk.view); + girara_debug("on Wayland, correcting PPI for device scale factor = %d", device_factor); + if (device_factor != 0) { + ppi /= device_factor; + } + } +#endif + + double current_ppi = zathura_document_get_viewport_ppi(zathura->document); + if (fabs(ppi - current_ppi) > DBL_EPSILON) { + girara_debug("monitor width: %d mm, pixels: %d, ppi: %f", width_mm, monitor_geom.width, ppi); + zathura_document_set_viewport_ppi(zathura->document, ppi); + render_all(zathura); + refresh_view(zathura); + } +} + static bool init_ui(zathura_t* zathura) { @@ -153,6 +220,15 @@ init_ui(zathura_t* zathura) g_signal_connect(G_OBJECT(zathura->ui.session->gtk.view), "notify::scale-factor", G_CALLBACK(cb_scale_factor), zathura); + g_signal_connect(G_OBJECT(zathura->ui.session->gtk.view), + "screen-changed", G_CALLBACK(cb_widget_screen_changed), zathura); + + g_signal_connect(G_OBJECT(zathura->ui.session->gtk.window), + "configure-event", G_CALLBACK(cb_widget_configured), zathura); + + /* initialize the screen-changed handler to 0 (i.e. invalid) */ + zathura->signals.monitors_changed_handler = 0; + /* page view */ zathura->ui.page_widget = gtk_grid_new(); gtk_grid_set_row_homogeneous(GTK_GRID(zathura->ui.page_widget), TRUE); @@ -802,7 +878,7 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char* zathura_fileinfo_t file_info = { .current_page = 0, .page_offset = 0, - .scale = 1, + .zoom = 1, .rotation = 0, .pages_per_row = 0, .first_page_column_list = NULL, @@ -817,12 +893,12 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char* /* set page offset */ zathura_document_set_page_offset(document, file_info.page_offset); - /* check for valid scale value */ - if (file_info.scale <= DBL_EPSILON) { - file_info.scale = 1; + /* check for valid zoom value */ + if (file_info.zoom <= DBL_EPSILON) { + file_info.zoom = 1; } - zathura_document_set_scale(document, - zathura_correct_scale_value(zathura->ui.session, file_info.scale)); + zathura_document_set_zoom(document, + zathura_correct_zoom_value(zathura->ui.session, file_info.zoom)); /* check current page number */ /* if it wasn't specified on the command-line, get it from file_info */ @@ -959,6 +1035,11 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char* const unsigned int view_height = (unsigned int)floor(gtk_adjustment_get_page_size(vadjustment)); zathura_document_set_viewport_height(zathura->document, view_height); + zathura_update_view_ppi(zathura); + + /* call screen-changed callback to connect monitors-changed signal on initial screen */ + cb_widget_screen_changed(zathura->ui.session->gtk.view, NULL, zathura); + /* get initial device scale */ int device_factor = gtk_widget_get_scale_factor(zathura->ui.session->gtk.view); zathura_document_set_device_factors(zathura->document, device_factor, device_factor); @@ -1188,7 +1269,7 @@ save_fileinfo_to_db(zathura_t* zathura) zathura_fileinfo_t file_info = { .current_page = zathura_document_get_current_page_number(zathura->document), .page_offset = zathura_document_get_page_offset(zathura->document), - .scale = zathura_document_get_scale(zathura->document), + .zoom = zathura_document_get_zoom(zathura->document), .rotation = zathura_document_get_rotation(zathura->document), .pages_per_row = 1, .first_page_column_list = "1:2", @@ -1482,31 +1563,31 @@ adjust_view(zathura_t* zathura) double page_ratio = (double)cell_height / (double)document_width; double view_ratio = (double)view_height / (double)view_width; - double scale = zathura_document_get_scale(zathura->document); - double newscale = scale; + double zoom = zathura_document_get_zoom(zathura->document); + double newzoom = zoom; if (adjust_mode == ZATHURA_ADJUST_WIDTH || (adjust_mode == ZATHURA_ADJUST_BESTFIT && page_ratio < view_ratio)) { - newscale *= (double)view_width / (double)document_width; + newzoom *= (double)view_width / (double)document_width; } else if (adjust_mode == ZATHURA_ADJUST_BESTFIT) { - newscale *= (double)view_height / (double)cell_height; + newzoom *= (double)view_height / (double)cell_height; } else { goto error_ret; } - /* save new scale and recompute cell size */ - zathura_document_set_scale(zathura->document, newscale); + /* save new zoom and recompute cell size */ + zathura_document_set_zoom(zathura->document, newzoom); unsigned int new_cell_height = 0, new_cell_width = 0; zathura_document_get_cell_size(zathura->document, &new_cell_height, &new_cell_width); - /* if the change in scale changes page cell dimensions by at least one pixel, render */ + /* if the change in zoom changes page cell dimensions by at least one pixel, render */ if (abs((int)new_cell_width - (int)cell_width) > 1 || abs((int)new_cell_height - (int)cell_height) > 1) { render_all(zathura); refresh_view(zathura); } else { - /* otherwise set the old scale and leave */ - zathura_document_set_scale(zathura->document, scale); + /* otherwise set the old zoom and leave */ + zathura_document_set_zoom(zathura->document, zoom); } error_ret: diff --git a/zathura/zathura.h b/zathura/zathura.h index 84e0cb6..22077cc 100644 --- a/zathura/zathura.h +++ b/zathura/zathura.h @@ -163,6 +163,8 @@ struct zathura_s #ifdef G_OS_UNIX guint sigterm; #endif + + gulong monitors_changed_handler; /**< Signal handler for monitors-changed */ } signals; struct @@ -287,6 +289,13 @@ void zathura_set_plugin_dir(zathura_t* zathura, const char* dir); */ void zathura_set_argv(zathura_t* zathura, char** argv); +/** + * Calculate and store the monitor PPI for the view widget + * + * @param zathura The zathura session + */ +void zathura_update_view_ppi(zathura_t* zathura); + /** * Opens a file *