@@ -2767,15 +2767,32 @@ static CGFloat _get_device_scale(CGContextRef cr)
2767
2767
float height;
2768
2768
2769
2769
CGRect rect;
2770
-
2771
2770
CGPoint point;
2771
+
2772
2772
CTFontRef font;
2773
2773
2774
+ char data[8 ];
2775
+
2774
2776
CGContextRef cr = self->cr ;
2775
2777
if (!cr)
2776
2778
{
2777
- PyErr_SetString (PyExc_RuntimeError, " CGContextRef is NULL" );
2778
- return NULL ;
2779
+ CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray ();
2780
+ if (!colorspace) {
2781
+ PyErr_SetString (PyExc_MemoryError, " Failed to create color space" );
2782
+ goto exit;
2783
+ }
2784
+ cr = CGBitmapContextCreate (data,
2785
+ 1 ,
2786
+ 1 ,
2787
+ 8 ,
2788
+ 1 ,
2789
+ colorspace,
2790
+ 0 );
2791
+ CGColorSpaceRelease (colorspace);
2792
+ if (!cr) {
2793
+ PyErr_SetString (PyExc_MemoryError, " Failed to create bitmap context" );
2794
+ goto exit;
2795
+ }
2779
2796
}
2780
2797
2781
2798
#if PY33
@@ -2785,7 +2802,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
2785
2802
&family,
2786
2803
&size,
2787
2804
&weight,
2788
- &italic)) return NULL ;
2805
+ &italic)) goto exit ;
2789
2806
CFStringRef s = CFStringCreateWithCString (kCFAllocatorDefault , text, kCFStringEncodingUTF8 );
2790
2807
#else
2791
2808
if (!PyArg_ParseTuple (args, " u#Ofss" ,
@@ -2801,7 +2818,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
2801
2818
if (!(font = setfont (cr, family, size, weight, italic)))
2802
2819
{
2803
2820
CFRelease (s);
2804
- return NULL ;
2821
+ goto exit ;
2805
2822
};
2806
2823
2807
2824
CFStringRef keys[1 ];
@@ -2830,18 +2847,22 @@ static CGFloat _get_device_scale(CGContextRef cr)
2830
2847
{
2831
2848
PyErr_SetString (PyExc_RuntimeError,
2832
2849
" CTLineCreateWithAttributedString failed" );
2833
- return NULL ;
2850
+ goto exit ;
2834
2851
}
2835
2852
2836
2853
point = CGContextGetTextPosition (cr);
2837
2854
rect = CTLineGetImageBounds (line, cr);
2838
2855
CFRelease (line);
2856
+ if (!self->cr ) CGContextRelease (cr);
2839
2857
2840
2858
width = rect.size .width ;
2841
2859
height = rect.size .height ;
2842
2860
descent = point.y - rect.origin .y ;
2843
2861
2844
2862
return Py_BuildValue (" fff" , width, height, descent);
2863
+ exit:
2864
+ if (cr && !self->cr ) CGContextRelease (cr);
2865
+ return NULL ;
2845
2866
}
2846
2867
2847
2868
#else // Text drawing for OSX versions <10.5
@@ -2956,19 +2977,33 @@ static CGFloat _get_device_scale(CGContextRef cr)
2956
2977
ATSFontRef atsfont;
2957
2978
Rect rect;
2958
2979
2980
+ char data[8 ];
2981
+
2959
2982
CGContextRef cr = self->cr ;
2960
2983
if (!cr)
2961
2984
{
2962
- PyErr_SetString (PyExc_RuntimeError, " CGContextRef is NULL" );
2963
- return NULL ;
2985
+ CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray ();
2986
+ if (!colorspace) {
2987
+ PyErr_SetString (PyExc_MemoryError, " Failed to create color space" );
2988
+ goto exit;
2989
+ }
2990
+ cr = CGBitmapContextCreate (data,
2991
+ 1 ,
2992
+ 1 ,
2993
+ 8 ,
2994
+ 1 ,
2995
+ colorspace,
2996
+ 0 );
2997
+ CGColorSpaceRelease (colorspace);
2998
+ if (!cr) {
2999
+ PyErr_SetString (PyExc_MemoryError, " Failed to create bitmap context" );
3000
+ goto exit;
3001
+ }
2964
3002
}
2965
3003
2966
- if (!PyArg_ParseTuple (args, " u#Ofss" , &text, &n, &family, &size, &weight, &italic)) return NULL ;
3004
+ if (!PyArg_ParseTuple (args, " u#Ofss" , &text, &n, &family, &size, &weight, &italic)) goto exit ;
2967
3005
2968
- if (!(atsfont = setfont (cr, family, size, weight, italic)))
2969
- {
2970
- return NULL ;
2971
- }
3006
+ if (!(atsfont = setfont (cr, family, size, weight, italic))) goto exit;
2972
3007
2973
3008
OSStatus status = noErr;
2974
3009
ATSUAttributeTag tags[] = {kATSUFontTag ,
@@ -2988,7 +3023,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
2988
3023
if (status!=noErr)
2989
3024
{
2990
3025
PyErr_SetString (PyExc_RuntimeError, " ATSUSetAttributes failed" );
2991
- return NULL ;
3026
+ goto exit ;
2992
3027
}
2993
3028
2994
3029
status = ATSUSetTextPointerLocation (layout,
@@ -3000,7 +3035,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
3000
3035
{
3001
3036
PyErr_SetString (PyExc_RuntimeError,
3002
3037
" ATSUCreateTextLayoutWithTextPtr failed" );
3003
- return NULL ;
3038
+ goto exit ;
3004
3039
}
3005
3040
3006
3041
status = ATSUSetRunStyle (layout,
@@ -3010,7 +3045,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
3010
3045
if (status!=noErr)
3011
3046
{
3012
3047
PyErr_SetString (PyExc_RuntimeError, " ATSUSetRunStyle failed" );
3013
- return NULL ;
3048
+ goto exit ;
3014
3049
}
3015
3050
3016
3051
ATSUAttributeTag tag = kATSUCGContextTag ;
@@ -3020,7 +3055,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
3020
3055
if (status!=noErr)
3021
3056
{
3022
3057
PyErr_SetString (PyExc_RuntimeError, " ATSUSetLayoutControls failed" );
3023
- return NULL ;
3058
+ goto exit ;
3024
3059
}
3025
3060
3026
3061
status = ATSUMeasureTextImage (layout,
@@ -3029,14 +3064,17 @@ static CGFloat _get_device_scale(CGContextRef cr)
3029
3064
if (status!=noErr)
3030
3065
{
3031
3066
PyErr_SetString (PyExc_RuntimeError, " ATSUMeasureTextImage failed" );
3032
- return NULL ;
3067
+ goto exit ;
3033
3068
}
3034
3069
3035
3070
const float width = rect.right -rect.left ;
3036
3071
const float height = rect.bottom -rect.top ;
3037
3072
const float descent = rect.bottom ;
3038
3073
3039
3074
return Py_BuildValue (" fff" , width, height, descent);
3075
+ exit:
3076
+ if (cr && !self->cr ) CGContextRelease (cr);
3077
+ return NULL ;
3040
3078
}
3041
3079
#endif
3042
3080
0 commit comments