/* gcc -framework Cocoa -lobjc `pkg-config --cflags --libs cairo` new-font.m -o new-font */ #import #include @interface TestView: NSView { } @end @implementation TestView - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; return self; } - (void)drawRect:(NSRect)rect { NSRect bounds = [self bounds]; int width = bounds.size.width; int height = bounds.size.height; cairo_scaled_font_t *font; cairo_font_extents_t extents; 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_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size (cr, 12.0); font = cairo_get_scaled_font (cr); cairo_scaled_font_extents (font, &extents); printf ("height: %f\n", extents.height); cairo_move_to (cr, 10, 10); cairo_show_text (cr, "Testing a bit with some text here"); 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; }