mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-05-29 21:25:00 +00:00
Add binary search for text paths with lineCount >= 16
This commit is contained in:
+18
-13
@@ -785,20 +785,25 @@ NSCharacterSet *separators = nil;
|
||||
continue;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
CGFloat totalLength = 0;
|
||||
CGFloat prevLength = 0;
|
||||
// Investigation suggests binary search is faster at lineCount >= 16
|
||||
// https://gist.github.com/msand/4c7993319425f9d7933be58ad9ada1a4
|
||||
NSUInteger i = lineCount < 16 ?
|
||||
[lengths
|
||||
indexOfObjectPassingTest:^(NSNumber* length, NSUInteger index, BOOL * _Nonnull stop) {
|
||||
BOOL contains = midPoint <= [length doubleValue];
|
||||
return contains;
|
||||
}]
|
||||
:
|
||||
[lengths
|
||||
indexOfObject:[NSNumber numberWithDouble:midPoint]
|
||||
inSortedRange:NSMakeRange(0, lineCount)
|
||||
options:NSBinarySearchingInsertionIndex
|
||||
usingComparator:^(NSNumber* obj1, NSNumber* obj2) {
|
||||
return [obj1 compare:obj2];
|
||||
}];
|
||||
|
||||
// TODO investigate at what lineCount a binary search is faster
|
||||
while (i < lineCount - 1) {
|
||||
prevLength = totalLength;
|
||||
totalLength = [[lengths objectAtIndex: i] floatValue];
|
||||
if (totalLength < midPoint) {
|
||||
i++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
};
|
||||
CGFloat totalLength = [lengths[i] doubleValue];
|
||||
CGFloat prevLength = i == 0 ? 0 : [lengths[i - 1] doubleValue];
|
||||
|
||||
CGFloat length = totalLength - prevLength;
|
||||
CGFloat targetPercent = (midPoint - prevLength) / length;
|
||||
|
||||
Reference in New Issue
Block a user