/* gcc -framework Cocoa -lobjc `pkg-config --cflags --libs cairo` text-gradient.m -o text-gradient */ #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 { NSRect bounds = [self bounds]; int width = bounds.size.width; int height = bounds.size.height; CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; CGContextTranslateCTM (ctx, 0.0, height); CGContextScaleCTM (ctx, 1.0, -1.0); cairo_surface_t *surface = cairo_quartz_surface_create_for_cg_context (ctx, width, height); cairo_t *cr = cairo_create (surface); cairo_pattern_t *pattern; pattern = cairo_pattern_create_linear (0, 0, width, height); cairo_pattern_add_color_stop_rgb (pattern, 0, 0, 0, 0); cairo_pattern_add_color_stop_rgb (pattern, 1, 1, 1, 1); /* Not using the pattern results in a nice readable text, using it * garbles things up somehow. */ cairo_set_source (cr, pattern); cairo_move_to (cr, 10, 10); cairo_show_text (cr, "Testing a bit with some text and a gradient"); cairo_pattern_destroy (pattern); cairo_destroy (cr); cairo_surface_destroy (surface); } @end int main (int argc, char **argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *window; NSView *view; [NSApplication sharedApplication]; window = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 200, 100) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]; view = [[TestView alloc] init]; [window setContentView:view]; [window makeKeyAndOrderFront:nil]; [NSApp run]; [pool release]; return 0; }