@@ -317,7 +317,18 @@ - (void)setWideFont:(NSFont *)newFont
317
317
// NOTE: No need to set point size etc. since this is taken from the
318
318
// regular font when drawing.
319
319
[fontWide release ];
320
- fontWide = [newFont retain ];
320
+
321
+ // Use 'Apple Color Emoji' font for rendering emoji
322
+ CGFloat size = [newFont pointSize ];
323
+ NSFontDescriptor *emojiDesc = [NSFontDescriptor
324
+ fontDescriptorWithName: @" Apple Color Emoji" size: size];
325
+ NSFontDescriptor *newFontDesc = [newFont fontDescriptor ];
326
+ NSDictionary *attrs = [NSDictionary
327
+ dictionaryWithObject: [NSArray arrayWithObject: newFontDesc]
328
+ forKey: NSFontCascadeListAttribute ];
329
+ NSFontDescriptor *desc =
330
+ [emojiDesc fontDescriptorByAddingAttributes: attrs];
331
+ fontWide = [[NSFont fontWithDescriptor: desc size: size] retain ];
321
332
}
322
333
}
323
334
@@ -1028,6 +1039,22 @@ - (void)batchDrawData:(NSData *)data
1028
1039
return newFontRef;
1029
1040
}
1030
1041
1042
+ static UniCharCount
1043
+ gatherGlyphs (CGGlyph glyphs[], UniCharCount count)
1044
+ {
1045
+ // Gather scattered glyphs that was happended by Surrogate pair chars
1046
+ UniCharCount glyphCount = 0 ;
1047
+ NSUInteger pos = 0 ;
1048
+ NSUInteger i;
1049
+ for (i = 0 ; i < count; ++i) {
1050
+ if (glyphs[i] != 0 ) {
1051
+ ++glyphCount;
1052
+ glyphs[pos++] = glyphs[i];
1053
+ }
1054
+ }
1055
+ return glyphCount;
1056
+ }
1057
+
1031
1058
static void
1032
1059
ligatureGlyphsForChars (const unichar *chars, CGGlyph *glyphs, CGPoint *positions, UniCharCount *length, CTFontRef font )
1033
1060
{
@@ -1093,15 +1120,13 @@ - (void)batchDrawData:(NSData *)data
1093
1120
{
1094
1121
if (CTFontGetGlyphsForCharacters (fontRef, chars, glyphs, length)) {
1095
1122
// All chars were mapped to glyphs, so draw all at once and return.
1123
+ length = gatherGlyphs (glyphs, length);
1096
1124
if (useLigatures) {
1097
1125
memset (glyphs, 0 , sizeof (CGGlyph) * length);
1098
1126
ligatureGlyphsForChars (chars, glyphs, positions, &length, fontRef);
1099
1127
}
1100
1128
1101
- CGFontRef cgFontRef = CTFontCopyGraphicsFont (fontRef, NULL );
1102
- CGContextSetFont (context, cgFontRef);
1103
- CGContextShowGlyphsAtPositions (context, glyphs, positions, length);
1104
- CGFontRelease (cgFontRef);
1129
+ CTFontDrawGlyphs (fontRef, glyphs, positions, length, context);
1105
1130
return ;
1106
1131
}
1107
1132
@@ -1113,23 +1138,34 @@ - (void)batchDrawData:(NSData *)data
1113
1138
// Draw as many consecutive glyphs as possible in the current font
1114
1139
// (if a glyph is 0 that means it does not exist in the current
1115
1140
// font).
1141
+ BOOL surrogatePair = NO ;
1116
1142
while (*g && g < glyphsEnd) {
1117
- ++g;
1118
- ++c;
1143
+ if (CFStringIsSurrogateHighCharacter (*c)) {
1144
+ surrogatePair = YES ;
1145
+ g += 2 ;
1146
+ c += 2 ;
1147
+ } else {
1148
+ ++g;
1149
+ ++c;
1150
+ }
1119
1151
++p;
1120
1152
}
1121
1153
1122
1154
int count = g-glyphs;
1123
- CGFontRef cgFontRef = CTFontCopyGraphicsFont (fontRef, NULL );
1124
- CGContextSetFont (context, cgFontRef);
1125
- CGContextShowGlyphsAtPositions (context, glyphs, positions, count);
1126
- CGFontRelease (cgFontRef);
1155
+ if (surrogatePair)
1156
+ count = gatherGlyphs (glyphs, count);
1157
+ CTFontDrawGlyphs (fontRef, glyphs, positions, count, context);
1127
1158
} else {
1128
1159
// Skip past as many consecutive chars as possible which cannot be
1129
1160
// drawn in the current font.
1130
1161
while (0 == *g && g < glyphsEnd) {
1131
- ++g;
1132
- ++c;
1162
+ if (CFStringIsSurrogateHighCharacter (*c)) {
1163
+ g += 2 ;
1164
+ c += 2 ;
1165
+ } else {
1166
+ ++g;
1167
+ ++c;
1168
+ }
1133
1169
++p;
1134
1170
}
1135
1171
0 commit comments