/* gcc -framework Cocoa -lobjc `pkg-config --cflags --libs cairo` cairo.m -o cairo * * The cairo code is adapted from the clearlooks theme, to reproduce an error: * : CGBitmapContextGetBitsPerPixel: invalid context * */ #import #include @interface TestView: NSView { } @end @implementation TestView - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; return self; } - (BOOL)isOpaque { return NO; } - (void)drawRect:(NSRect)rect { int width, height; CGContextRef context; cairo_surface_t *surface; cairo_t *cr; cairo_pattern_t *pattern; cairo_matrix_t matrix; width = [self bounds].size.width; height = [self bounds].size.height; context = [[NSGraphicsContext currentContext] graphicsPort]; CGContextTranslateCTM (context, 0.0, height); CGContextScaleCTM (context, 1.0, -1.0); surface = cairo_quartz_surface_create_for_cg_context (context, width, height); cr = cairo_create (surface); /* This transformation seems to be what triggers the error: */ cairo_matrix_init (&matrix, 0, 1, 1, 0, 0, 0); cairo_transform (cr, &matrix); pattern = cairo_pattern_create_linear (1, 1, 1, height - 2); cairo_pattern_add_color_stop_rgb (pattern, 0, 0, 0, 0); cairo_pattern_add_color_stop_rgb (pattern, 0.5, 0.5, 0.5, 0.5); cairo_pattern_add_color_stop_rgb (pattern, 1, 1, 1, 1); cairo_rectangle (cr, 1, 1, width - 2, height - 2); cairo_set_source (cr, pattern); cairo_fill (cr); cairo_pattern_destroy (pattern); cairo_destroy (cr); cairo_surface_destroy (surface); } @end int main (int argc, char **argv) { NSAutoreleasePool *pool; int style; NSWindow *window; NSView *view; pool = [[NSAutoreleasePool alloc] init]; [NSApplication sharedApplication]; style = NSTitledWindowMask | NSMiniaturizableWindowMask; window = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 300, 300) styleMask:style backing:NSBackingStoreBuffered defer:NO]; view = [[TestView alloc] init]; [window setContentView: view]; [window makeKeyAndOrderFront:window]; [NSApp run]; [pool release]; return 0; }