From: Glen Low Date: Thu, 5 Dec 2013 14:54:56 +0000 (+0800) Subject: Quartz: simplify text drawing X-Git-Tag: 2.38.0~132^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7356ff886462e469c14c87e29b784cb27cbc8b5f;p=graphviz Quartz: simplify text drawing * Text attribute takes foreground color from context and allows one call to draw entire line, rather than having to draw each glyph run. * We can now draw multicolored fonts such as emoji. --- diff --git a/plugin/quartz/gvtextlayout_quartz.c b/plugin/quartz/gvtextlayout_quartz.c index 26bdfd4ba..d6a82e4d0 100644 --- a/plugin/quartz/gvtextlayout_quartz.c +++ b/plugin/quartz/gvtextlayout_quartz.c @@ -32,12 +32,13 @@ void *quartz_new_layout(char* fontname, double fontsize, char* text) if (fontnameref && textref) { /* set up the Core Text line */ CTFontRef font = CTFontCreateWithName(fontnameref, fontsize, NULL); - + CFTypeRef attributeNames[] = { kCTFontAttributeName, kCTForegroundColorFromContextAttributeName }; + CFTypeRef attributeValues[] = { font, kCFBooleanTrue }; CFDictionaryRef attributes = CFDictionaryCreate( kCFAllocatorDefault, - (const void**)&kCTFontAttributeName, - (const void**)&font, - 1, + (const void**)attributeNames, + (const void**)attributeValues, + 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFAttributedStringRef attributed = CFAttributedStringCreate(kCFAllocatorDefault, textref, attributes); @@ -72,28 +73,7 @@ void quartz_size_layout(void *layout, double* width, double* height, double* yof void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position) { CGContextSetTextPosition(context, position.x, position.y); - - CFArrayRef runs = CTLineGetGlyphRuns((CTLineRef)layout); - CFIndex run_count = CFArrayGetCount(runs); - CFIndex run_index; - for (run_index = 0; run_index < run_count; ++run_index) - { - CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, run_index); - CTFontRef run_font = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName); - CGFontRef glyph_font = CTFontCopyGraphicsFont(run_font, NULL); - CFIndex glyph_count = CTRunGetGlyphCount(run); - CGGlyph glyphs[glyph_count]; - CGPoint positions[glyph_count]; - CFRange everything = CFRangeMake(0, 0); - CTRunGetGlyphs(run, everything, glyphs); - CTRunGetPositions(run, everything, positions); - - CGContextSetFont(context, glyph_font); - CGContextSetFontSize(context, CTFontGetSize(run_font)); - CGContextShowGlyphsAtPositions(context, glyphs, positions, glyph_count); - - CGFontRelease(glyph_font); - } + CTLineDraw((CTLineRef)layout, context); } void quartz_free_layout(void *layout)