aboutsummaryrefslogtreecommitdiffstats
path: root/zoom.c
blob: 7fa617ac1a23e36244f25100692d9acd75d011ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* See LICENSE file for copyright and license details. */
#include "common.h"

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;
}