aboutsummaryrefslogtreecommitdiffstats
path: root/zoom.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-10-11 23:01:38 +0200
committerMattias Andrée <m@maandree.se>2026-10-11 23:29:28 +0200
commit94659550993f454e12e4bcb8cf93a5ff258bc08c (patch)
treef8e1cfe9fbcdec6f09b6cc18c761684dc34505d2 /zoom.c
parentFirst commit (diff)
downloadgbild-94659550993f454e12e4bcb8cf93a5ff258bc08c.tar.gz
gbild-94659550993f454e12e4bcb8cf93a5ff258bc08c.tar.bz2
gbild-94659550993f454e12e4bcb8cf93a5ff258bc08c.tar.xz
Second commit
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'zoom.c')
-rw-r--r--zoom.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/zoom.c b/zoom.c
new file mode 100644
index 0000000..caa9884
--- /dev/null
+++ b/zoom.c
@@ -0,0 +1,82 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#include <math.h> /* just for HUGE_VAL, no -lm needed */
+
+Zoom zoom = {1.0};
+enum zoom_mode zoom_mode = ZOOM_BEST_FIT;
+
+int
+zoom_eq(Zoom a, Zoom b)
+{
+ return FLOAT_EQ(a, b);
+}
+
+double
+get_zoom(Zoom z)
+{
+ return z;
+}
+
+Zoom
+make_zoom(double s)
+{
+ return s;
+}
+
+Zoom
+zoom_in(Zoom z)
+{
+ return get_zoom(z) * 1.25;
+}
+
+Zoom
+zoom_out(Zoom z)
+{
+ return get_zoom(z) * (1.0 / 1.25);
+}
+
+Zoom
+calculate_fit(GdkPixbuf *image, int xfit, int yfit, double cap)
+{
+ GtkAdjustment *adjustment;
+ double s = 1.0, y;
+ int w, h;
+
+ if (xfit) {
+ adjustment = logical_scroll_pane_get_hadjustment(LOGICAL_SCROLL_PANE(scroll));
+ w = gdk_pixbuf_get_width(image);
+ s = adjustment->page_size / (double)w;
+ }
+
+ if (yfit) {
+ adjustment = logical_scroll_pane_get_vadjustment(LOGICAL_SCROLL_PANE(scroll));
+ h = gdk_pixbuf_get_height(image);
+ y = adjustment->page_size / (double)h;
+ s = xfit ? MIN(s, y) : y;
+ }
+
+ if (s > cap)
+ s = cap;
+
+ return make_zoom(s);
+}
+
+Zoom
+calculate_zoom_mode(GdkPixbuf *image, enum zoom_mode mode)
+{
+ switch (mode) {
+ case ZOOM_CUSTOM:
+ break;
+ case ZOOM_NORMAL:
+ return calculate_fit(image, 0, 0, 1.0);
+ case ZOOM_FIT_WIDTH:
+ return calculate_fit(image, 1, 0, HUGE_VAL);
+ case ZOOM_FIT_HEIGHT:
+ return calculate_fit(image, 0, 1, HUGE_VAL);
+ case ZOOM_FIT_WINDOW:
+ return calculate_fit(image, 1, 1, HUGE_VAL);
+ case ZOOM_BEST_FIT:
+ return calculate_fit(image, 1, 1, 1.0);
+ }
+ return zoom;
+}