From 8041d3a33e6ae4425b44b65e930bbf93243abe01 Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Sat, 26 Oct 2013 19:20:34 +0200 Subject: [PATCH] Stop using 10.9 deprecated CGContext function --- src/MacVim/MMCoreTextView.h | 2 +- src/MacVim/MMCoreTextView.m | 44 ++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/MacVim/MMCoreTextView.h b/src/MacVim/MMCoreTextView.h index c40523d4df..489ae87142 100644 --- a/src/MacVim/MMCoreTextView.h +++ b/src/MacVim/MMCoreTextView.h @@ -37,7 +37,7 @@ unsigned maxlen; CGGlyph *glyphs; - CGSize *advances; + CGPoint *positions; NSMutableArray *fontCache; // These are used in MMCoreTextView+ToolTip.m diff --git a/src/MacVim/MMCoreTextView.m b/src/MacVim/MMCoreTextView.m index 0ca4d8d7d2..c582c46518 100644 --- a/src/MacVim/MMCoreTextView.m +++ b/src/MacVim/MMCoreTextView.m @@ -148,7 +148,7 @@ defaultAdvanceForFont(NSFont *font) [helper release]; helper = nil; if (glyphs) { free(glyphs); glyphs = NULL; } - if (advances) { free(advances); advances = NULL; } + if (positions) { free(positions); positions = NULL; } [super dealloc]; } @@ -1013,25 +1013,23 @@ lookupFont(NSMutableArray *fontCache, const unichar *chars, } static void -recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, +recurseDraw(const unichar *chars, CGGlyph *glyphs, CGPoint *positions, UniCharCount length, CGContextRef context, CTFontRef fontRef, - float x, float y, NSMutableArray *fontCache) + NSMutableArray *fontCache) { if (CTFontGetGlyphsForCharacters(fontRef, chars, glyphs, length)) { // All chars were mapped to glyphs, so draw all at once and return. CGFontRef cgFontRef = CTFontCopyGraphicsFont(fontRef, NULL); CGContextSetFont(context, cgFontRef); - CGContextSetTextPosition(context, x, y); - CGContextShowGlyphsWithAdvances(context, glyphs, advances, length); + CGContextShowGlyphsAtPositions(context, glyphs, positions, length); CGFontRelease(cgFontRef); return; } CGGlyph *glyphsEnd = glyphs+length, *g = glyphs; - CGSize *a = advances; + CGPoint *p = positions; const unichar *c = chars; - float x0 = x; while (glyphs < glyphsEnd) { if (*g) { // Draw as many consecutive glyphs as possible in the current font @@ -1040,15 +1038,13 @@ recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, while (*g && g < glyphsEnd) { ++g; ++c; - x += a->width; - ++a; + ++p; } int count = g-glyphs; CGFontRef cgFontRef = CTFontCopyGraphicsFont(fontRef, NULL); CGContextSetFont(context, cgFontRef); - CGContextSetTextPosition(context, x0, y); - CGContextShowGlyphsWithAdvances(context, glyphs, advances, count); + CGContextShowGlyphsAtPositions(context, glyphs, positions, count); CGFontRelease(cgFontRef); } else { // Skip past as many consecutive chars as possible which cannot be @@ -1056,8 +1052,7 @@ recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, while (0 == *g && g < glyphsEnd) { ++g; ++c; - x += a->width; - ++a; + ++p; } // Figure out which font to draw these chars with. @@ -1066,16 +1061,15 @@ recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, if (!newFontRef) return; - recurseDraw(chars, glyphs, advances, count, context, newFontRef, - x0, y, fontCache); + recurseDraw(chars, glyphs, positions, count, context, newFontRef, + fontCache); CFRelease(newFontRef); } chars = c; glyphs = g; - advances = a; - x0 = x; + positions = p; } } @@ -1148,9 +1142,9 @@ recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, if (length > maxlen) { if (glyphs) free(glyphs); - if (advances) free(advances); + if (positions) free(positions); glyphs = (CGGlyph*)malloc(length*sizeof(CGGlyph)); - advances = (CGSize*)calloc(length, sizeof(CGSize)); + positions = (CGPoint*)calloc(length, sizeof(CGPoint)); maxlen = length; } @@ -1159,9 +1153,13 @@ recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, CGContextSetRGBFillColor(context, RED(fg), GREEN(fg), BLUE(fg), ALPHA(fg)); CGContextSetFontSize(context, [font pointSize]); + // Calculate position of each glyph relative to (x,y). NSUInteger i; - for (i = 0; i < length; ++i) - advances[i].width = w; + float xrel = 0; + for (i = 0; i < length; ++i) { + positions[i].x = xrel; + xrel += w; + } CTFontRef fontRef = (CTFontRef)(flags & DRAW_WIDE ? [fontWide retain] : [font retain]); @@ -1180,8 +1178,8 @@ recurseDraw(const unichar *chars, CGGlyph *glyphs, CGSize *advances, } } - recurseDraw(chars, glyphs, advances, length, context, fontRef, x, - y+fontDescent, fontCache); + CGContextSetTextPosition(context, x, y+fontDescent); + recurseDraw(chars, glyphs, positions, length, context, fontRef, fontCache); CFRelease(fontRef); CGContextRestoreGState(context);