[cairo] [PATCH] Fix problem with NULL CG context for empty surface

Christophe de Dinechin christophe at taodyne.com
Thu Mar 4 23:59:12 PST 2010


I'm new to the list, I apologize if I'm not following the right process.

I noticed that on MacOSX, my programs very often emit messages like:

<Error>: CGContextSetFont: invalid context 0x0
<Error>: CGContextSetFontSize: invalid context 0x0
<Error>: CGContextSetTextMatrix: invalid context 0x0
<Error>: CGContextClearRect: invalid context 0x0
<Error>: CGContextSetRGBFillColor: invalid context 0x0
<Error>: CGContextShowGlyphsAtPoint: invalid context 0x0

The source of the problem is that no CG context is returned when the surface is empty (e.g. width==0). The patch below fixes it.


Thanks
Christophe

---
 src/cairo-quartz-font.c |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/cairo-quartz-font.c b/src/cairo-quartz-font.c
index 0ed2a50..71d2c25 100644
--- a/src/cairo-quartz-font.c
+++ b/src/cairo-quartz-font.c
@@ -669,19 +669,22 @@ _cairo_quartz_init_glyph_surface (cairo_quartz_scaled_font_t *font,
 				       kCGImageAlphaNone);
     CGColorSpaceRelease (gray);
 
-    CGContextSetFont (cgContext, font_face->cgFont);
-    CGContextSetFontSize (cgContext, 1.0);
-    CGContextSetTextMatrix (cgContext, textMatrix);
+    if (cgContext) // May be NULL if surface is empty (width*height = 0)
+    {
+	CGContextSetFont (cgContext, font_face->cgFont);
+	CGContextSetFontSize (cgContext, 1.0);
+	CGContextSetTextMatrix (cgContext, textMatrix);
 
-    CGContextClearRect (cgContext, CGRectMake (0.0f, 0.0f, width, height));
+	CGContextClearRect (cgContext, CGRectMake (0.0f, 0.0f, width, height));
 
-    if (font->base.options.antialias == CAIRO_ANTIALIAS_NONE)
-	CGContextSetShouldAntialias (cgContext, false);
+	if (font->base.options.antialias == CAIRO_ANTIALIAS_NONE)
+	    CGContextSetShouldAntialias (cgContext, false);
 
-    CGContextSetRGBFillColor (cgContext, 1.0, 1.0, 1.0, 1.0);
-    CGContextShowGlyphsAtPoint (cgContext, - glyphOrigin.x, - glyphOrigin.y, &glyph, 1);
+	CGContextSetRGBFillColor (cgContext, 1.0, 1.0, 1.0, 1.0);
+	CGContextShowGlyphsAtPoint (cgContext, - glyphOrigin.x, - glyphOrigin.y, &glyph, 1);
 
-    CGContextRelease (cgContext);
+	CGContextRelease (cgContext);
+    }
 
     cairo_surface_set_device_offset (&surface->base,
 				     - glyphOrigin.x,
-- 
1.6.6.1



More information about the cairo mailing list