Add internal support for image scaling

This commit is contained in:
Přemysl Eric Janouch 2021-09-17 20:40:11 +02:00
parent be6f3dfe99
commit aae6fd4d8b
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 31 additions and 12 deletions

View File

@ -40,12 +40,30 @@
struct _FastivView { struct _FastivView {
GtkWidget parent_instance; GtkWidget parent_instance;
cairo_surface_t *surface; cairo_surface_t *surface;
// TODO(p): Eventually, we'll want to have the zoom level (scale) here, // TODO(p): Zoom-to-fit indication.
// and some zoom-to-fit indication. double scale;
}; };
G_DEFINE_TYPE(FastivView, fastiv_view, GTK_TYPE_WIDGET) G_DEFINE_TYPE(FastivView, fastiv_view, GTK_TYPE_WIDGET)
static int
get_display_width(FastivView *self)
{
if (!self->surface)
return 0;
return ceil(cairo_image_surface_get_width(self->surface) * self->scale);
}
static int
get_display_height(FastivView *self)
{
if (!self->surface)
return 0;
return ceil(cairo_image_surface_get_height(self->surface) * self->scale);
}
static void static void
fastiv_view_finalize(GObject *gobject) fastiv_view_finalize(GObject *gobject)
{ {
@ -62,10 +80,8 @@ fastiv_view_get_preferred_height(GtkWidget *widget,
*minimum = 0; *minimum = 0;
*natural = 0; *natural = 0;
// TODO(p): Times the zoom.
FastivView *self = FASTIV_VIEW(widget); FastivView *self = FASTIV_VIEW(widget);
if (self->surface) *natural = get_display_height(self);
*natural = cairo_image_surface_get_height(self->surface);
} }
static void static void
@ -75,10 +91,8 @@ fastiv_view_get_preferred_width(GtkWidget *widget,
*minimum = 0; *minimum = 0;
*natural = 0; *natural = 0;
// TODO(p): Times the zoom.
FastivView *self = FASTIV_VIEW(widget); FastivView *self = FASTIV_VIEW(widget);
if (self->surface) *natural = get_display_width(self);
*natural = cairo_image_surface_get_width(self->surface);
} }
static gboolean static gboolean
@ -95,8 +109,8 @@ fastiv_view_draw(GtkWidget *widget, cairo_t *cr)
GtkAllocation allocation; GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation); gtk_widget_get_allocation(widget, &allocation);
int w = cairo_image_surface_get_width(self->surface); int w = get_display_width(self);
int h = cairo_image_surface_get_height(self->surface); int h = get_display_height(self);
double x = 0; double x = 0;
double y = 0; double y = 0;
@ -105,8 +119,10 @@ fastiv_view_draw(GtkWidget *widget, cairo_t *cr)
if (h < allocation.height) if (h < allocation.height)
y = (allocation.height - h) / 2; y = (allocation.height - h) / 2;
// TODO(p): Times the zoom. cairo_scale(cr, self->scale, self->scale);
cairo_set_source_surface(cr, self->surface, x, y); cairo_set_source_surface(cr, self->surface,
x / self->scale, y / self->scale);
cairo_paint(cr); cairo_paint(cr);
return TRUE; return TRUE;
} }
@ -126,6 +142,8 @@ fastiv_view_class_init(FastivViewClass *klass)
static void static void
fastiv_view_init(FastivView *self) fastiv_view_init(FastivView *self)
{ {
self->scale = 1.0;
gtk_widget_set_has_window(GTK_WIDGET(self), FALSE); gtk_widget_set_has_window(GTK_WIDGET(self), FALSE);
} }

View File

@ -6,6 +6,7 @@ dependencies = [
dependency('gtk+-3.0'), dependency('gtk+-3.0'),
dependency('libturbojpeg'), dependency('libturbojpeg'),
libraw, libraw,
meson.get_compiler('c').find_library('m', required : false),
] ]
conf = configuration_data() conf = configuration_data()