From 48183d67172e79835dcd8cfd377d727fdfba3621 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 27 Oct 2014 11:55:21 +0100 Subject: [PATCH] Correct the given scale value --- links.c | 3 ++- marks.c | 3 ++- shortcuts.c | 14 +------------- utils.c | 29 +++++++++++++++++++++++++++++ utils.h | 12 ++++++++++++ zathura.c | 8 ++------ 6 files changed, 48 insertions(+), 21 deletions(-) diff --git a/links.c b/links.c index 1e8fe97..c3bb3e3 100644 --- a/links.c +++ b/links.c @@ -135,7 +135,8 @@ zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link) case ZATHURA_LINK_GOTO_DEST: if (link->target.destination_type != ZATHURA_LINK_DESTINATION_UNKNOWN) { if (link->target.scale != 0 && link_zoom) { - zathura_document_set_scale(zathura->document, link->target.scale); + zathura_document_set_scale(zathura->document, + zathura_correct_scale_value(zathura->ui.session, link->target.scale)); render_all(zathura); } diff --git a/marks.c b/marks.c index ac77422..5ec6bf2 100644 --- a/marks.c +++ b/marks.c @@ -239,7 +239,8 @@ mark_evaluate(zathura_t* zathura, int key) /* search for existing mark */ GIRARA_LIST_FOREACH(zathura->global.marks, zathura_mark_t*, iter, mark) if (mark != NULL && mark->key == key) { - zathura_document_set_scale(zathura->document, mark->scale); + zathura_document_set_scale(zathura->document, + zathura_correct_scale_value(zathura->ui.session, mark->scale)); render_all(zathura); zathura_jumplist_add(zathura); diff --git a/shortcuts.c b/shortcuts.c index 23c9ed8..e0fc53f 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -1390,20 +1390,8 @@ sc_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t* } /* zoom limitations */ - int zoom_min_int = 10; - int zoom_max_int = 1000; - girara_setting_get(session, "zoom-min", &zoom_min_int); - girara_setting_get(session, "zoom-max", &zoom_max_int); - - const double zoom_min = zoom_min_int * 0.01; - const double zoom_max = zoom_max_int * 0.01; - const double scale = zathura_document_get_scale(zathura->document); - if (scale < zoom_min) { - zathura_document_set_scale(zathura->document, zoom_min); - } else if (scale > zoom_max) { - zathura_document_set_scale(zathura->document, zoom_max); - } + zathura_document_set_scale(zathura->document, zathura_correct_scale_value(session, scale)); const double new_zoom = zathura_document_get_scale(zathura->document); if (fabs(new_zoom - old_zoom) <= DBL_EPSILON) { diff --git a/utils.c b/utils.c index 79ff6bc..288d056 100644 --- a/utils.c +++ b/utils.c @@ -23,6 +23,35 @@ #include "plugin.h" #include "content-type.h" +double +zathura_correct_scale_value(girara_session_t* session, const double scale) +{ + if (scale <= FLT_EPSILON) { + return 1; + } + + if (session == NULL) { + return scale; + } + + /* zoom limitations */ + int zoom_min_int = 10; + int zoom_max_int = 1000; + girara_setting_get(session, "zoom-min", &zoom_min_int); + girara_setting_get(session, "zoom-max", &zoom_max_int); + + const double zoom_min = zoom_min_int * 0.01; + const double zoom_max = zoom_max_int * 0.01; + + if (scale < zoom_min) { + return zoom_min; + } else if (scale > zoom_max) { + return zoom_max; + } else { + return scale; + } +} + bool file_valid_extension(zathura_t* zathura, const char* path) { diff --git a/utils.h b/utils.h index c09b71b..141468f 100644 --- a/utils.h +++ b/utils.h @@ -95,4 +95,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 + * and zoom_max specified in the girara session + * + * @param[in] session The session + * @param[in] scale The proposed scale value + * + * @return The corrected scale value + */ +double zathura_correct_scale_value(girara_session_t* session, const double + scale); + #endif // UTILS_H diff --git a/zathura.c b/zathura.c index 1e3a533..d630085 100644 --- a/zathura.c +++ b/zathura.c @@ -607,12 +607,8 @@ document_open(zathura_t* zathura, const char* path, const char* password, zathura_document_set_page_offset(document, file_info.page_offset); /* check for valid scale value */ - if (file_info.scale <= FLT_EPSILON) { - girara_warning("document info: '%s' has non positive scale", file_path); - zathura_document_set_scale(document, 1); - } else { - zathura_document_set_scale(document, file_info.scale); - } + zathura_document_set_scale(document, + zathura_correct_scale_value(zathura->ui.session, file_info.scale)); /* check current page number */ /* if it wasn't specified on the command-line, get it from file_info */