@@ -1035,22 +1035,26 @@ - (void)batchDrawData:(NSData *)data
10351035}
10361036
10371037 static CFAttributedStringRef
1038- attributedStringForString (NSString *string, const CTFontRef font, BOOL useLigatures)
1038+ attributedStringForString (NSString *string, const CTFontRef font,
1039+ BOOL useLigatures)
10391040{
10401041 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
10411042 (id )font, kCTFontAttributeName ,
10421043 // 2 - full ligatures including rare
10431044 // 1 - basic ligatures
10441045 // 0 - no ligatures
1045- [NSNumber numberWithInteger: (useLigatures) ? 1 : 0 ], kCTLigatureAttributeName ,
1046+ [NSNumber numberWithInteger: (useLigatures ? 1 : 0 )],
1047+ kCTLigatureAttributeName ,
10461048 nil
10471049 ];
10481050
1049- return CFAttributedStringCreate (NULL , (CFStringRef)string, (CFDictionaryRef)attrs);
1051+ return CFAttributedStringCreate (NULL , (CFStringRef)string,
1052+ (CFDictionaryRef)attrs);
10501053}
10511054
10521055 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)
10541058{
10551059 NSArray *glyphRuns = (NSArray *)CTLineGetGlyphRuns (line);
10561060
@@ -1060,24 +1064,21 @@ - (void)batchDrawData:(NSData *)data
10601064 CTRunRef run = (CTRunRef)item;
10611065 CFIndex count = CTRunGetGlyphCount (run);
10621066
1063- if (count > 0 && count - offset > length) {
1067+ if (count > 0 && count - offset > length)
10641068 count = length - offset;
1065- }
10661069
10671070 CFRange range = CFRangeMake (0 , count);
10681071
1069- if ( glyphs != NULL ) {
1072+ if ( glyphs != NULL )
10701073 CTRunGetGlyphs (run, range, &glyphs[offset]);
1071- }
1072- if ( advances != NULL ) {
1074+ if (advances != NULL )
10731075 CTRunGetAdvances (run, range, &advances[offset]);
1074- }
10751076
10761077 offset += count;
1077- if (offset >= length) {
1078+ if (offset >= length)
10781079 break ;
1079- }
10801080 }
1081+
10811082 return offset;
10821083}
10831084
@@ -1097,52 +1098,58 @@ - (void)batchDrawData:(NSData *)data
10971098 return glyphCount;
10981099}
10991100
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)
11021104{
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];
11091111
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);
11121114
1113- memset (glyphs, 0 , sizeof (CGGlyph) * (* length) );
1115+ memset (glyphs, 0 , sizeof (CGGlyph) * length);
11141116
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 );
11171120
11181121 CTLineRef ligature = CTLineCreateWithAttributedString (ligatureText);
11191122
1120- CGSize ligatureRanges[* length], regularRanges[* length];
1123+ CGSize ligatureRanges[length], regularRanges[length];
11211124
11221125 // 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);
11241128 // fetch the advances for the base text
1125- CTFontGetAdvancesForGlyphs (font, kCTFontOrientationDefault , refGlyphs, regularRanges, *length);
1129+ CTFontGetAdvancesForGlyphs (font, kCTFontOrientationDefault , refGlyphs,
1130+ regularRanges, length);
11261131
11271132 CFRelease (ligatureText);
11281133 CFRelease (ligature);
11291134
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)
11341140
11351141 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) {
11371143 memcpy (&positions[i], &refPositions[skip + i], sizeof (CGSize));
11381144
1139- if ( fequal (ligatureRanges[i].width , regularRanges[skip + i].width ) ) {
1145+ if ( fequal (ligatureRanges[i].width , regularRanges[skip + i].width )) {
11401146 // [mostly] same width
11411147 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 )) {
11441150 // 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')
11461153 glyphs[i] = refGlyphs[skip + i];
11471154 continue ;
11481155 }
@@ -1152,9 +1159,8 @@ - (void)batchDrawData:(NSData *)data
11521159 CFIndex j = 0 ;
11531160 float width = ceil (regularRanges[skip + i].width );
11541161
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) {
11581164 width += ceil (regularRanges[++j + skip + i].width );
11591165 }
11601166 skip += j;
@@ -1165,7 +1171,7 @@ - (void)batchDrawData:(NSData *)data
11651171
11661172 // as ligatures combine characters it is required to adjust the
11671173 // original length value
1168- *length = offset;
1174+ return offset;
11691175}
11701176
11711177 static void
@@ -1176,10 +1182,11 @@ - (void)batchDrawData:(NSData *)data
11761182 if (CTFontGetGlyphsForCharacters (fontRef, chars, glyphs, length)) {
11771183 // All chars were mapped to glyphs, so draw all at once and return.
11781184 if (useLigatures) {
1179- ligatureGlyphsForChars (chars, glyphs, positions, &length, fontRef);
1185+ length = ligatureGlyphsForChars (chars, glyphs, positions, length,
1186+ fontRef);
11801187 } 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);
11831190 }
11841191
11851192 CTFontDrawGlyphs (fontRef, glyphs, positions, length, context);
@@ -1232,14 +1239,14 @@ - (void)batchDrawData:(NSData *)data
12321239 UniCharCount attemptedCount = count;
12331240 CTFontRef fallback = nil ;
12341241 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 ;
12381246 }
12391247
1240- if (!fallback) {
1241- return ;
1242- }
1248+ if (!fallback)
1249+ return ;
12431250
12441251 recurseDraw (chars, glyphs, positions, attemptedCount, context,
12451252 fallback, fontCache, useLigatures);
0 commit comments