From 87cc194fc2cd2ab69917ef939c937b41ef14ec43 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Thu, 9 Aug 2018 22:02:09 +0300 Subject: [PATCH] [ios] Fix substring range length calculation for unicode emoji. --- ios/Text/RNSVGTSpan.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ios/Text/RNSVGTSpan.m b/ios/Text/RNSVGTSpan.m index 90f0d5c7..ef8f1b89 100644 --- a/ios/Text/RNSVGTSpan.m +++ b/ios/Text/RNSVGTSpan.m @@ -689,6 +689,14 @@ static double RNSVGTSpan_radToDeg = 180 / M_PI; CTRunGetStringIndices(run, CFRangeMake(0, 0), indices); CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName); CTFontGetAdvancesForGlyphs(runFont, kCTFontOrientationHorizontal, glyphs, advances, runGlyphCount); + CFIndex nextOrEndRunIndex = n - 1; + if (r + 1 < runEnd) { + CTRunRef nextRun = CFArrayGetValueAtIndex(runs, r + 1); + CFIndex nextRunGlyphCount = CTRunGetGlyphCount(nextRun); + CFIndex nextIndices[nextRunGlyphCount]; + CTRunGetStringIndices(nextRun, CFRangeMake(0, 0), nextIndices); + nextOrEndRunIndex = nextIndices[0]; + } for(CFIndex g = 0; g < runGlyphCount; g++) { CGGlyph glyph = glyphs[g]; @@ -724,7 +732,7 @@ static double RNSVGTSpan_radToDeg = 180 / M_PI; continue; } - CFIndex endIndex = g + 1 == runGlyphCount ? currIndex : indices[g + 1]; + CFIndex endIndex = g + 1 == runGlyphCount ? nextOrEndRunIndex : indices[g + 1]; while (++currIndex < endIndex) { // Skip rendering other grapheme clusters of ligatures (already rendered), // And, make sure to increment index positions by making gc.next() calls. @@ -815,8 +823,9 @@ static double RNSVGTSpan_radToDeg = 180 / M_PI; if (width == 0) { // Render unicode emoji UILabel *label = [[UILabel alloc] init]; CFIndex startIndex = indices[g]; - NSUInteger len = endIndex == startIndex ? n - startIndex : MAX(1, endIndex - startIndex); - NSString* currChars = [str substringWithRange:NSMakeRange(startIndex, len)]; + long len = MAX(1, endIndex - startIndex); + NSRange range = NSMakeRange(startIndex, len); + NSString* currChars = [str substringWithRange:range]; label.text = currChars; label.opaque = NO; label.backgroundColor = UIColor.clearColor;