From 5a01d672b9dc2113aa1677d552bbe2f235173e64 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Tue, 29 Aug 2017 03:16:52 +0300 Subject: [PATCH] Optimize and simplify hasGlyph. --- ios/Text/RNSVGTSpan.m | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/ios/Text/RNSVGTSpan.m b/ios/Text/RNSVGTSpan.m index 79b519ae..7cbac8ab 100644 --- a/ios/Text/RNSVGTSpan.m +++ b/ios/Text/RNSVGTSpan.m @@ -671,6 +671,19 @@ } } int i = -1; + CFDictionaryRef ligattributes; + NSNumber *lig = [NSNumber numberWithInt:allowOptionalLigatures ? 2 : 1]; + + if (fontRef != nil) { + ligattributes = (__bridge CFDictionaryRef)@{ + (NSString *)kCTFontAttributeName: (__bridge id)fontRef, + (NSString *)NSLigatureAttributeName: lig + }; + } else { + ligattributes = (__bridge CFDictionaryRef)@{ + (NSString *)NSLigatureAttributeName: lig + }; + } for(CFIndex g = 0; g < runGlyphCount; g++) { i++; bool alreadyRenderedGraphemeCluster = ligature[i]; @@ -719,7 +732,7 @@ bool hasLigature = false; while (++nextIndex < n) { NSString* nextLigature = [str substringWithRange:NSMakeRange(i, len++)]; - bool hasNextLigature = hasGlyph(fontRef, nextLigature, &glyph, allowOptionalLigatures); + bool hasNextLigature = hasGlyph(fontRef, nextLigature, &glyph, ligattributes); if (hasNextLigature) { ligature[nextIndex] = true; hasLigature = true; @@ -836,23 +849,9 @@ CGFloat getTextAnchorOffset(enum TextAnchor textAnchor, CGFloat width) } } -bool hasGlyph(CTFontRef fontRef, NSString * str, CGGlyph* glyph, bool allowOptionalLigatures) +bool hasGlyph(CTFontRef fontRef, NSString * str, CGGlyph* glyph, CFDictionaryRef attributes) { CFStringRef string = (__bridge CFStringRef)str; - CFDictionaryRef attributes; - NSNumber *lig = [NSNumber numberWithInt:allowOptionalLigatures ? 2 : 1]; - - if (fontRef != nil) { - attributes = (__bridge CFDictionaryRef)@{ - (NSString *)kCTFontAttributeName: (__bridge id)fontRef, - (NSString *)NSLigatureAttributeName: lig - }; - } else { - attributes = (__bridge CFDictionaryRef)@{ - (NSString *)NSLigatureAttributeName: lig - }; - } - CFAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes); CTLineRef line = CTLineCreateWithAttributedString(attrString); CFArrayRef runs = CTLineGetGlyphRuns(line); @@ -863,14 +862,10 @@ bool hasGlyph(CTFontRef fontRef, NSString * str, CGGlyph* glyph, bool allowOptio } CTRunRef run = CFArrayGetValueAtIndex(runs, 0); CFIndex runGlyphCount = CTRunGetGlyphCount(run); - - CGGlyph glyphs[runGlyphCount]; - CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs); - bool hasGlyph = runGlyphCount == 1; if (hasGlyph) { - *glyph = glyphs[0]; + CTRunGetGlyphs(run, CFRangeMake(0, 1), glyph); } return hasGlyph;