@@ -149,7 +149,6 @@ - (void)dealloc
149149 [helper release ]; helper = nil ;
150150
151151 if (glyphs) { free (glyphs); glyphs = NULL ; }
152- if (advances) { free (advances); advances = NULL ; }
153152 if (positions) { free (positions); positions = NULL ; }
154153
155154 [super dealloc ];
@@ -1054,47 +1053,29 @@ - (void)batchDrawData:(NSData *)data
10541053}
10551054
10561055 static void
1057- drawGlyphs (CTFontRef fontRef, const CGGlyph glyphs[], const CGSize advances[],
1058- CGPoint positions[], size_t count, CGContextRef context)
1059- {
1060- CGAffineTransform matrix =
1061- CGAffineTransformInvert (CGContextGetTextMatrix (context));
1062- positions[0 ] = CGPointZero;
1063- NSUInteger i;
1064- for (i = 1 ; i < count; ++i) {
1065- CGSize advance = CGSizeApplyAffineTransform (advances[i - 1 ], matrix);
1066- positions[i].x = positions[i - 1 ].x + advance.width ;
1067- positions[i].y = positions[i - 1 ].y + advance.height ;
1068- }
1069- CTFontDrawGlyphs (fontRef, glyphs, positions, count, context);
1070- }
1071-
1072- static void
1073- recurseDraw (const unichar *chars, CGGlyph *glyphs, CGSize *advances,
1074- CGPoint *positions, UniCharCount length, CGContextRef context,
1075- CTFontRef fontRef, float x, float y, NSMutableArray *fontCache,
1076- BOOL emojiEnabled)
1056+ recurseDraw (const unichar *chars, CGGlyph *glyphs, CGPoint *positions,
1057+ UniCharCount length, CGContextRef context, CTFontRef fontRef,
1058+ NSMutableArray *fontCache, BOOL emojiEnabled)
10771059{
10781060
10791061 if (CTFontGetGlyphsForCharacters (fontRef, chars, glyphs, length)) {
10801062 // All chars were mapped to glyphs, so draw all at once and return.
10811063 CGContextSetTextPosition (context, x, y);
10821064 if (emojiEnabled) {
10831065 length = gatherGlyphs (glyphs, length);
1084- drawGlyphs (fontRef, glyphs, advances , positions, length, context);
1066+ CTFontDrawGlyphs (fontRef, glyphs, positions, length, context);
10851067 } else {
10861068 CGFontRef cgFontRef = CTFontCopyGraphicsFont (fontRef, NULL );
10871069 CGContextSetFont (context, cgFontRef);
1088- CGContextShowGlyphsWithAdvances (context, glyphs, advances , length);
1070+ CGContextShowGlyphsAtPositions (context, glyphs, positions , length);
10891071 CGFontRelease (cgFontRef);
10901072 }
10911073 return ;
10921074 }
10931075
10941076 CGGlyph *glyphsEnd = glyphs+length, *g = glyphs;
1095- CGSize *a = advances ;
1077+ CGPoint *p = positions ;
10961078 const unichar *c = chars;
1097- float x0 = x;
10981079 while (glyphs < glyphsEnd) {
10991080 if (*g) {
11001081 // Draw as many consecutive glyphs as possible in the current font
@@ -1106,26 +1087,25 @@ - (void)batchDrawData:(NSData *)data
11061087 surrogatePair = YES ;
11071088 g += 2 ;
11081089 c += 2 ;
1090+ p += 2 ;
11091091 } else {
11101092 ++g;
11111093 ++c;
1094+ ++p;
11121095 }
1113- x += a->width ;
1114- ++a;
11151096 }
11161097
11171098 int count = g-glyphs;
11181099 CGContextSetTextPosition (context, x0, y);
11191100 if (emojiEnabled) {
11201101 if (surrogatePair)
11211102 count = gatherGlyphs (glyphs, count);
1122- drawGlyphs (fontRef,
1123- glyphs, advances, positions, count, context);
1103+ CTFontDrawGlyphs (fontRef, glyphs, positions, count, context);
11241104 } else {
11251105 CGFontRef cgFontRef = CTFontCopyGraphicsFont (fontRef, NULL );
11261106 CGContextSetFont (context, cgFontRef);
1127- CGContextShowGlyphsWithAdvances (
1128- context, glyphs, advances, count);
1107+ CGContextShowGlyphsAtPositions (context, glyphs, positions,
1108+ count);
11291109 CGFontRelease (cgFontRef);
11301110 }
11311111 } else {
@@ -1135,12 +1115,12 @@ - (void)batchDrawData:(NSData *)data
11351115 if (emojiEnabled && CFStringIsSurrogateHighCharacter (*c)) {
11361116 g += 2 ;
11371117 c += 2 ;
1118+ p += 2 ;
11381119 } else {
11391120 ++g;
11401121 ++c;
1122+ ++p;
11411123 }
1142- x += a->width ;
1143- ++a;
11441124 }
11451125
11461126 // Figure out which font to draw these chars with.
@@ -1149,16 +1129,15 @@ - (void)batchDrawData:(NSData *)data
11491129 if (!newFontRef)
11501130 return ;
11511131
1152- recurseDraw (chars, glyphs, advances, positions, count, context,
1153- newFontRef, x0, y, fontCache, emojiEnabled);
1132+ recurseDraw (chars, glyphs, positions, count, context, newFontRef ,
1133+ fontCache, emojiEnabled);
11541134
11551135 CFRelease (newFontRef);
11561136 }
11571137
11581138 chars = c;
11591139 glyphs = g;
1160- advances = a;
1161- x0 = x;
1140+ positions = p;
11621141 }
11631142}
11641143
@@ -1237,10 +1216,8 @@ - (void)drawString:(const UniChar *)chars length:(UniCharCount)length
12371216
12381217 if (length > maxlen) {
12391218 if (glyphs) free (glyphs);
1240- if (advances) free (advances);
12411219 if (positions) free (positions);
12421220 glyphs = (CGGlyph*)malloc (length*sizeof (CGGlyph));
1243- advances = (CGSize*)calloc (length, sizeof (CGSize));
12441221 positions = (CGPoint*)calloc (length, sizeof (CGPoint));
12451222 maxlen = length;
12461223 }
@@ -1250,9 +1227,13 @@ - (void)drawString:(const UniChar *)chars length:(UniCharCount)length
12501227 CGContextSetRGBFillColor (context, RED (fg), GREEN (fg), BLUE (fg), ALPHA (fg));
12511228 CGContextSetFontSize (context, [font pointSize ]);
12521229
1230+ // Calculate position of each glyph relative to (x,y).
12531231 NSUInteger i;
1254- for (i = 0 ; i < length; ++i)
1255- advances[i].width = w;
1232+ float xrel = 0 ;
1233+ for (i = 0 ; i < length; ++i) {
1234+ positions[i].x = xrel;
1235+ xrel += w;
1236+ }
12561237
12571238 CTFontRef fontRef = (CTFontRef)(flags & DRAW_WIDE ? [fontWide retain ]
12581239 : [font retain ]);
@@ -1271,7 +1252,8 @@ - (void)drawString:(const UniChar *)chars length:(UniCharCount)length
12711252 }
12721253 }
12731254
1274- recurseDraw (chars, glyphs, advances, positions, length, context, fontRef, x,
1255+ CGContextSetTextPosition (context, x, y+fontDescent);
1256+ recurseDraw (chars, glyphs, positions, length, context, fontRef, fontCache,
12751257 y+fontDescent, fontCache, (flags & DRAW_WIDE) && emojiEnabled);
12761258
12771259 CFRelease (fontRef);
0 commit comments