]> granicus.if.org Git - graphviz/commitdiff
Quartz: simplify text drawing
authorGlen Low <glen.low@pixelglow.com>
Thu, 5 Dec 2013 14:54:56 +0000 (22:54 +0800)
committerGlen Low <glen.low@pixelglow.com>
Thu, 5 Dec 2013 16:22:40 +0000 (00:22 +0800)
* 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.

plugin/quartz/gvtextlayout_quartz.c

index 26bdfd4bacece51ea152b381fa02b512725e508d..d6a82e4d0e59b4a3daf622566c0a6eda50bf3ca1 100644 (file)
@@ -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)