@@ -1035,22 +1035,26 @@ - (void)batchDrawData:(NSData *)data
1035
1035
}
1036
1036
1037
1037
static CFAttributedStringRef
1038
- attributedStringForString (NSString *string, const CTFontRef font, BOOL useLigatures)
1038
+ attributedStringForString (NSString *string, const CTFontRef font,
1039
+ BOOL useLigatures)
1039
1040
{
1040
1041
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
1041
1042
(id )font, kCTFontAttributeName ,
1042
1043
// 2 - full ligatures including rare
1043
1044
// 1 - basic ligatures
1044
1045
// 0 - no ligatures
1045
- [NSNumber numberWithInteger: (useLigatures) ? 1 : 0 ], kCTLigatureAttributeName ,
1046
+ [NSNumber numberWithInteger: (useLigatures ? 1 : 0 )],
1047
+ kCTLigatureAttributeName ,
1046
1048
nil
1047
1049
];
1048
1050
1049
- return CFAttributedStringCreate (NULL , (CFStringRef)string, (CFDictionaryRef)attrs);
1051
+ return CFAttributedStringCreate (NULL , (CFStringRef)string,
1052
+ (CFDictionaryRef)attrs);
1050
1053
}
1051
1054
1052
1055
static UniCharCount
1053
- fetchGlyphsAndAdvances (const CTLineRef line, CGGlyph *glyphs, CGSize *advances, UniCharCount length)
1056
+ fetchGlyphsAndAdvances (const CTLineRef line, CGGlyph *glyphs, CGSize *advances,
1057
+ UniCharCount length)
1054
1058
{
1055
1059
NSArray *glyphRuns = (NSArray *)CTLineGetGlyphRuns (line);
1056
1060
@@ -1060,24 +1064,21 @@ - (void)batchDrawData:(NSData *)data
1060
1064
CTRunRef run = (CTRunRef)item;
1061
1065
CFIndex count = CTRunGetGlyphCount (run);
1062
1066
1063
- if (count > 0 && count - offset > length) {
1067
+ if (count > 0 && count - offset > length)
1064
1068
count = length - offset;
1065
- }
1066
1069
1067
1070
CFRange range = CFRangeMake (0 , count);
1068
1071
1069
- if ( glyphs != NULL ) {
1072
+ if ( glyphs != NULL )
1070
1073
CTRunGetGlyphs (run, range, &glyphs[offset]);
1071
- }
1072
- if ( advances != NULL ) {
1074
+ if (advances != NULL )
1073
1075
CTRunGetAdvances (run, range, &advances[offset]);
1074
- }
1075
1076
1076
1077
offset += count;
1077
- if (offset >= length) {
1078
+ if (offset >= length)
1078
1079
break ;
1079
- }
1080
1080
}
1081
+
1081
1082
return offset;
1082
1083
}
1083
1084
@@ -1097,52 +1098,58 @@ - (void)batchDrawData:(NSData *)data
1097
1098
return glyphCount;
1098
1099
}
1099
1100
1100
- static void
1101
- ligatureGlyphsForChars (const unichar *chars, CGGlyph *glyphs, CGPoint *positions, UniCharCount *length, CTFontRef font )
1101
+ static UniCharCount
1102
+ ligatureGlyphsForChars (const unichar *chars, CGGlyph *glyphs,
1103
+ CGPoint *positions, UniCharCount length, CTFontRef font)
1102
1104
{
1103
- /* CoreText has no simple wait of retrieving a ligature for a set of UniChars.
1104
- * The way proposed on the CoreText ML is to convert the text to an attributed
1105
- * string, create a CTLine from it and retrieve the Glyphs from the CTRuns in it.
1106
- */
1107
- CGGlyph refGlyphs[* length];
1108
- CGPoint refPositions[* length];
1105
+ // CoreText has no simple wait of retrieving a ligature for a set of
1106
+ // UniChars. The way proposed on the CoreText ML is to convert the text to
1107
+ // an attributed string, create a CTLine from it and retrieve the Glyphs
1108
+ // from the CTRuns in it.
1109
+ CGGlyph refGlyphs[length];
1110
+ CGPoint refPositions[length];
1109
1111
1110
- memcpy (refGlyphs, glyphs, sizeof (CGGlyph) * (* length) );
1111
- memcpy (refPositions, positions, sizeof (CGSize) * (* length) );
1112
+ memcpy (refGlyphs, glyphs, sizeof (CGGlyph) * length);
1113
+ memcpy (refPositions, positions, sizeof (CGSize) * length);
1112
1114
1113
- memset (glyphs, 0 , sizeof (CGGlyph) * (* length) );
1115
+ memset (glyphs, 0 , sizeof (CGGlyph) * length);
1114
1116
1115
- NSString *plainText = [NSString stringWithCharacters: chars length: *length];
1116
- CFAttributedStringRef ligatureText = attributedStringForString (plainText, font, YES );
1117
+ NSString *plainText = [NSString stringWithCharacters: chars length: length];
1118
+ CFAttributedStringRef ligatureText = attributedStringForString (plainText,
1119
+ font, YES );
1117
1120
1118
1121
CTLineRef ligature = CTLineCreateWithAttributedString (ligatureText);
1119
1122
1120
- CGSize ligatureRanges[* length], regularRanges[* length];
1123
+ CGSize ligatureRanges[length], regularRanges[length];
1121
1124
1122
1125
// get the (ligature)glyphs and advances for the new text
1123
- UniCharCount offset = fetchGlyphsAndAdvances (ligature, glyphs, ligatureRanges, *length);
1126
+ UniCharCount offset = fetchGlyphsAndAdvances (ligature, glyphs,
1127
+ ligatureRanges, length);
1124
1128
// fetch the advances for the base text
1125
- CTFontGetAdvancesForGlyphs (font, kCTFontOrientationDefault , refGlyphs, regularRanges, *length);
1129
+ CTFontGetAdvancesForGlyphs (font, kCTFontOrientationDefault , refGlyphs,
1130
+ regularRanges, length);
1126
1131
1127
1132
CFRelease (ligatureText);
1128
1133
CFRelease (ligature);
1129
1134
1130
- // tricky part: compare both advance ranges and chomp positions which
1131
- // are covered by a single ligature while keeping glyphs not in the ligature font.
1132
- #define fequal (a, b ) (fabs( (a) - (b) ) < FLT_EPSILON)
1133
- #define fless (a, b )((a) - (b) < FLT_EPSILON) && (fabs( (a) - (b) ) > FLT_EPSILON)
1135
+ // tricky part: compare both advance ranges and chomp positions which are
1136
+ // covered by a single ligature while keeping glyphs not in the ligature
1137
+ // font.
1138
+ #define fequal (a, b ) (fabs((a) - (b)) < FLT_EPSILON)
1139
+ #define fless (a, b )((a) - (b) < FLT_EPSILON) && (fabs((a) - (b)) > FLT_EPSILON)
1134
1140
1135
1141
CFIndex skip = 0 ;
1136
- for ( CFIndex i = 0 ; i < offset && skip + i < * length; ++i ) {
1142
+ for ( CFIndex i = 0 ; i < offset && skip + i < length; ++i) {
1137
1143
memcpy (&positions[i], &refPositions[skip + i], sizeof (CGSize));
1138
1144
1139
- if ( fequal (ligatureRanges[i].width , regularRanges[skip + i].width ) ) {
1145
+ if ( fequal (ligatureRanges[i].width , regularRanges[skip + i].width )) {
1140
1146
// [mostly] same width
1141
1147
continue ;
1142
-
1143
- } else if ( fless (ligatureRanges[i]. width , regularRanges[skip + i].width ) ) {
1148
+ } else if ( fless (ligatureRanges[i]. width ,
1149
+ regularRanges[skip + i].width )) {
1144
1150
// original is wider than our result - use the original glyph
1145
- // FIXME: this is currently the only way to detect emoji (except for 'glyph[i] == 5')
1151
+ // FIXME: this is currently the only way to detect emoji (except
1152
+ // for 'glyph[i] == 5')
1146
1153
glyphs[i] = refGlyphs[skip + i];
1147
1154
continue ;
1148
1155
}
@@ -1152,9 +1159,8 @@ - (void)batchDrawData:(NSData *)data
1152
1159
CFIndex j = 0 ;
1153
1160
float width = ceil (regularRanges[skip + i].width );
1154
1161
1155
- while ( (int )width < (int )ligatureRanges[i].width
1156
- && skip + i + j < *length )
1157
- {
1162
+ while ((int )width < (int )ligatureRanges[i].width
1163
+ && skip + i + j < length) {
1158
1164
width += ceil (regularRanges[++j + skip + i].width );
1159
1165
}
1160
1166
skip += j;
@@ -1165,7 +1171,7 @@ - (void)batchDrawData:(NSData *)data
1165
1171
1166
1172
// as ligatures combine characters it is required to adjust the
1167
1173
// original length value
1168
- *length = offset;
1174
+ return offset;
1169
1175
}
1170
1176
1171
1177
static void
@@ -1176,10 +1182,11 @@ - (void)batchDrawData:(NSData *)data
1176
1182
if (CTFontGetGlyphsForCharacters (fontRef, chars, glyphs, length)) {
1177
1183
// All chars were mapped to glyphs, so draw all at once and return.
1178
1184
if (useLigatures) {
1179
- ligatureGlyphsForChars (chars, glyphs, positions, &length, fontRef);
1185
+ length = ligatureGlyphsForChars (chars, glyphs, positions, length,
1186
+ fontRef);
1180
1187
} else {
1181
- // only fixup surrogate pairs if we're not using ligatures
1182
- length = gatherGlyphs (glyphs, length);
1188
+ // only fixup surrogate pairs if we're not using ligatures
1189
+ length = gatherGlyphs (glyphs, length);
1183
1190
}
1184
1191
1185
1192
CTFontDrawGlyphs (fontRef, glyphs, positions, length, context);
@@ -1232,14 +1239,14 @@ - (void)batchDrawData:(NSData *)data
1232
1239
UniCharCount attemptedCount = count;
1233
1240
CTFontRef fallback = nil ;
1234
1241
while (fallback == nil && attemptedCount > 0 ) {
1235
- fallback = lookupFont (fontCache, chars, attemptedCount, fontRef);
1236
- if (!fallback)
1237
- attemptedCount /= 2 ;
1242
+ fallback = lookupFont (fontCache, chars, attemptedCount,
1243
+ fontRef);
1244
+ if (!fallback)
1245
+ attemptedCount /= 2 ;
1238
1246
}
1239
1247
1240
- if (!fallback) {
1241
- return ;
1242
- }
1248
+ if (!fallback)
1249
+ return ;
1243
1250
1244
1251
recurseDraw (chars, glyphs, positions, attemptedCount, context,
1245
1252
fallback, fontCache, useLigatures);
0 commit comments