Skip to content

Commit 51fc70f

Browse files
vkarehraveit65
authored andcommitted
Scale HiDPI images correctly
1 parent e0be704 commit 51fc70f

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/eom-scroll-view.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ struct _EomScrollViewPrivate {
9797
GdkPixbuf *pixbuf;
9898
cairo_surface_t *surface;
9999

100+
/* scale factor */
101+
gint scale;
102+
100103
/* zoom mode, either ZOOM_MODE_FIT or ZOOM_MODE_FREE */
101104
ZoomMode zoom_mode;
102105

@@ -173,16 +176,10 @@ static cairo_surface_t *
173176
create_surface_from_pixbuf (EomScrollView *view, GdkPixbuf *pixbuf)
174177
{
175178
cairo_surface_t *surface;
176-
cairo_t *cr;
177179

178-
surface = gdk_window_create_similar_surface (gtk_widget_get_window (view->priv->display),
179-
CAIRO_CONTENT_COLOR | CAIRO_CONTENT_ALPHA,
180-
gdk_pixbuf_get_width (pixbuf),
181-
gdk_pixbuf_get_height (pixbuf));
182-
cr = cairo_create (surface);
183-
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
184-
cairo_paint (cr);
185-
cairo_destroy (cr);
180+
surface = gdk_cairo_surface_create_from_pixbuf (pixbuf,
181+
view->priv->scale,
182+
gtk_widget_get_window (view->priv->display));
186183

187184
return surface;
188185
}
@@ -230,8 +227,8 @@ compute_scaled_size (EomScrollView *view, double zoom, int *width, int *height)
230227
priv = view->priv;
231228

232229
if (priv->pixbuf) {
233-
*width = floor (gdk_pixbuf_get_width (priv->pixbuf) * zoom + 0.5);
234-
*height = floor (gdk_pixbuf_get_height (priv->pixbuf) * zoom + 0.5);
230+
*width = floor (gdk_pixbuf_get_width (priv->pixbuf) / priv->scale * zoom + 0.5);
231+
*height = floor (gdk_pixbuf_get_height (priv->pixbuf) / priv->scale * zoom + 0.5);
235232
} else
236233
*width = *height = 0;
237234
}
@@ -689,8 +686,8 @@ set_minimum_zoom_factor (EomScrollView *view)
689686
{
690687
g_return_if_fail (EOM_IS_SCROLL_VIEW (view));
691688

692-
view->priv->min_zoom = MAX (1.0 / gdk_pixbuf_get_width (view->priv->pixbuf),
693-
MAX(1.0 / gdk_pixbuf_get_height (view->priv->pixbuf),
689+
view->priv->min_zoom = MAX (1.0 / gdk_pixbuf_get_width (view->priv->pixbuf) / view->priv->scale,
690+
MAX(1.0 / gdk_pixbuf_get_height (view->priv->pixbuf) / view->priv->scale,
694691
MIN_ZOOM_FACTOR) );
695692
return;
696693
}
@@ -794,8 +791,8 @@ set_zoom_fit (EomScrollView *view)
794791
gtk_widget_get_allocation (GTK_WIDGET(priv->display), &allocation);
795792

796793
new_zoom = zoom_fit_scale (allocation.width, allocation.height,
797-
gdk_pixbuf_get_width (priv->pixbuf),
798-
gdk_pixbuf_get_height (priv->pixbuf),
794+
gdk_pixbuf_get_width (priv->pixbuf) / priv->scale,
795+
gdk_pixbuf_get_height (priv->pixbuf) / priv->scale,
799796
priv->upscale);
800797

801798
if (new_zoom > MAX_ZOOM_FACTOR)
@@ -1276,17 +1273,17 @@ display_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
12761273
switch (eom_transform_get_transform_type (transform)) {
12771274
case EOM_TRANSFORM_ROT_90:
12781275
case EOM_TRANSFORM_FLIP_HORIZONTAL:
1279-
image_offset_x = (double) gdk_pixbuf_get_width (priv->pixbuf);
1276+
image_offset_x = (double) gdk_pixbuf_get_width (priv->pixbuf) / priv->scale;
12801277
break;
12811278
case EOM_TRANSFORM_ROT_270:
12821279
case EOM_TRANSFORM_FLIP_VERTICAL:
1283-
image_offset_y = (double) gdk_pixbuf_get_height (priv->pixbuf);
1280+
image_offset_y = (double) gdk_pixbuf_get_height (priv->pixbuf) / priv->scale;
12841281
break;
12851282
case EOM_TRANSFORM_ROT_180:
12861283
case EOM_TRANSFORM_TRANSPOSE:
12871284
case EOM_TRANSFORM_TRANSVERSE:
1288-
image_offset_x = (double) gdk_pixbuf_get_width (priv->pixbuf);
1289-
image_offset_y = (double) gdk_pixbuf_get_height (priv->pixbuf);
1285+
image_offset_x = (double) gdk_pixbuf_get_width (priv->pixbuf) / priv->scale;
1286+
image_offset_y = (double) gdk_pixbuf_get_height (priv->pixbuf) / priv->scale;
12901287
break;
12911288
case EOM_TRANSFORM_NONE:
12921289
default:
@@ -1767,6 +1764,7 @@ eom_scroll_view_init (EomScrollView *view)
17671764
priv->display = g_object_new (GTK_TYPE_DRAWING_AREA,
17681765
"can-focus", TRUE,
17691766
NULL);
1767+
priv->scale = gtk_widget_get_scale_factor (GTK_WIDGET (priv->display));
17701768

17711769
gtk_widget_add_events (GTK_WIDGET (priv->display),
17721770
GDK_EXPOSURE_MASK
@@ -2088,8 +2086,8 @@ view_on_drag_begin_cb (GtkWidget *widget,
20882086
thumbnail = eom_image_get_thumbnail (image);
20892087

20902088
if (thumbnail) {
2091-
width = gdk_pixbuf_get_width (thumbnail);
2092-
height = gdk_pixbuf_get_height (thumbnail);
2089+
width = gdk_pixbuf_get_width (thumbnail) / view->priv->scale;
2090+
height = gdk_pixbuf_get_height (thumbnail) / view->priv->scale;
20932091
gtk_drag_set_icon_pixbuf (context, thumbnail, width/2, height/2);
20942092
g_object_unref (thumbnail);
20952093
}

0 commit comments

Comments
 (0)