Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32cf4ce

Browse files
committed
Merge pull request #2939 from cimarronm/macosx_device_scale
Fixes a bug in drawing bitmap images in the macosx backend for handling device scaling
2 parents adae051 + d1e520d commit 32cf4ce

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/_macosx.m

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,14 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
22402240
return 0;
22412241
}
22422242

2243+
2244+
static CGFloat _get_device_scale(CGContextRef cr)
2245+
{
2246+
CGSize pixelSize = CGContextConvertSizeToDeviceSpace(cr, CGSizeMake(1,1));
2247+
return pixelSize.width;
2248+
}
2249+
2250+
22432251
static PyObject*
22442252
GraphicsContext_draw_gouraud_triangle (GraphicsContext* self, PyObject* args)
22452253

@@ -2998,17 +3006,8 @@ static void _data_provider_release(void* info, const void* data, size_t size)
29983006
Py_DECREF(image);
29993007
}
30003008

3001-
/* Consider the drawing origin to be in user coordinates
3002-
* but the image size to be in device coordinates */
3003-
static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
3004-
float x, float y, npy_intp ncols, npy_intp nrows)
3005-
{
3006-
CGRect dst;
3007-
dst.origin = CGPointMake(x,y);
3008-
dst.size = CGContextConvertSizeToUserSpace(cr, CGSizeMake(ncols,nrows));
3009-
dst.size.height = fabs(dst.size.height); /* believe it or not... */
3010-
CGContextDrawImage(cr, dst, im);
3011-
}
3009+
3010+
30123011

30133012
static PyObject*
30143013
GraphicsContext_draw_mathtext(GraphicsContext* self, PyObject* args)
@@ -3090,16 +3089,18 @@ static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
30903089
return NULL;
30913090
}
30923091

3092+
CGFloat deviceScale = _get_device_scale(cr);
3093+
30933094
if (angle==0.0)
30943095
{
3095-
draw_image_user_coords_device_size(cr, bitmap, x, y, ncols, nrows);
3096+
CGContextDrawImage(cr, CGRectMake(x, y, ncols/deviceScale, nrows/deviceScale), bitmap);
30963097
}
30973098
else
30983099
{
30993100
CGContextSaveGState(cr);
31003101
CGContextTranslateCTM(cr, x, y);
31013102
CGContextRotateCTM(cr, angle*M_PI/180);
3102-
draw_image_user_coords_device_size(cr, bitmap, 0, 0, ncols, nrows);
3103+
CGContextDrawImage(cr, CGRectMake(0, 0, ncols/deviceScale, nrows/deviceScale), bitmap);
31033104
CGContextRestoreGState(cr);
31043105
}
31053106
CGImageRelease(bitmap);
@@ -3189,7 +3190,9 @@ static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
31893190
return NULL;
31903191
}
31913192

3192-
draw_image_user_coords_device_size(cr, bitmap, x, y, ncols, nrows);
3193+
CGFloat deviceScale = _get_device_scale(cr);
3194+
3195+
CGContextDrawImage(cr, CGRectMake(x, y, ncols/deviceScale, nrows/deviceScale), bitmap);
31933196
CGImageRelease(bitmap);
31943197

31953198
Py_INCREF(Py_None);
@@ -3206,8 +3209,7 @@ static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
32063209
return NULL;
32073210
}
32083211

3209-
CGSize pixelSize = CGContextConvertSizeToDeviceSpace(cr, CGSizeMake(1,1));
3210-
return PyFloat_FromDouble(pixelSize.width);
3212+
return PyFloat_FromDouble(_get_device_scale(cr));
32113213
}
32123214

32133215

0 commit comments

Comments
 (0)