@@ -2996,6 +2996,18 @@ static void _data_provider_release(void* info, const void* data, size_t size)
29962996 Py_DECREF (image);
29972997}
29982998
2999+ /* Consider the drawing origin to be in user coordinates
3000+ * but the image size to be in device coordinates */
3001+ static void draw_image_user_coords_device_size (CGContextRef cr, CGImageRef im,
3002+ float x, float y, npy_intp ncols, npy_intp nrows)
3003+ {
3004+ CGRect dst;
3005+ dst.origin = CGPointMake (x,y);
3006+ dst.size = CGContextConvertSizeToUserSpace (cr, CGSizeMake (ncols,nrows));
3007+ dst.size .height = fabs (dst.size .height ); /* believe it or not... */
3008+ CGContextDrawImage (cr, dst, im);
3009+ }
3010+
29993011static PyObject*
30003012GraphicsContext_draw_mathtext (GraphicsContext* self, PyObject* args)
30013013{
@@ -3078,14 +3090,14 @@ static void _data_provider_release(void* info, const void* data, size_t size)
30783090
30793091 if (angle==0.0 )
30803092 {
3081- CGContextDrawImage (cr, CGRectMake (x,y, ncols,nrows), bitmap );
3093+ draw_image_user_coords_device_size (cr, bitmap, x, y, ncols, nrows );
30823094 }
30833095 else
30843096 {
30853097 CGContextSaveGState (cr);
30863098 CGContextTranslateCTM (cr, x, y);
30873099 CGContextRotateCTM (cr, angle*M_PI/180 );
3088- CGContextDrawImage (cr, CGRectMake ( 0 , 0 , ncols,nrows), bitmap );
3100+ draw_image_user_coords_device_size (cr, bitmap, 0 , 0 , ncols, nrows );
30893101 CGContextRestoreGState (cr);
30903102 }
30913103 CGImageRelease (bitmap);
@@ -3175,13 +3187,27 @@ static void _data_provider_release(void* info, const void* data, size_t size)
31753187 return NULL ;
31763188 }
31773189
3178- CGContextDrawImage (cr, CGRectMake (x,y, ncols,nrows), bitmap );
3190+ draw_image_user_coords_device_size (cr, bitmap, x, y, ncols, nrows );
31793191 CGImageRelease (bitmap);
31803192
31813193 Py_INCREF (Py_None);
31823194 return Py_None;
31833195}
31843196
3197+ static PyObject*
3198+ GraphicsContext_get_image_magnification (GraphicsContext* self)
3199+ {
3200+ CGContextRef cr = self->cr ;
3201+ if (!cr)
3202+ {
3203+ PyErr_SetString (PyExc_RuntimeError, " CGContextRef is NULL" );
3204+ return NULL ;
3205+ }
3206+
3207+ CGSize pixelSize = CGContextConvertSizeToDeviceSpace (cr, CGSizeMake (1 ,1 ));
3208+ return PyFloat_FromDouble (pixelSize.width );
3209+ }
3210+
31853211
31863212static PyMethodDef GraphicsContext_methods[] = {
31873213 {" save" ,
@@ -3294,6 +3320,11 @@ static void _data_provider_release(void* info, const void* data, size_t size)
32943320 METH_VARARGS,
32953321 " Draw an image at (x,y) in the graphics context."
32963322 },
3323+ {" get_image_magnification" ,
3324+ (PyCFunction)GraphicsContext_get_image_magnification,
3325+ METH_NOARGS,
3326+ " Returns the scale factor between user and device coordinates."
3327+ },
32973328 {NULL } /* Sentinel */
32983329};
32993330
0 commit comments