Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ios PathMeasure problem with index out of bounds exception #2307

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ColorTest from './src/ColorTest';
import PointerEventsBoxNone from './src/PointerEventsBoxNone';
import Test1374 from './src/Test1374';
import Test1718 from './src/Test1718';
import Test1740 from './src/Test1740';
import Test1813 from './src/Test1813';
import Test1845 from './src/Test1845';
import Test1986 from './src/Test1986';
Expand Down
34 changes: 34 additions & 0 deletions TestsExample/src/Test1740.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import {SvgXml} from 'react-native-svg';

const CircleSvg = `
<svg width="290" height="500" viewBox="0 0 290 500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink='http://www.w3.org/1999/xlink'>
<defs>
<path id="text-path-a" d="M40 12 H250 A28 28 0 0 1 278 40 V460 A28 28 0 0 1 250 488 H40 A28 28 0 0 1 12 460 V40 A28 28 0 0 1 40 12 z" />
</defs>
<text>
<textPath startOffset="1%" xlink:href="#text-path-a">
ahshjkdasjklsadjkldasjkldsajklkljsdakjlcsakljdjklaskljdjsakldjaksl
</textPath>
<textPath startOffset="1%" xlink:href="#text-path-a">
ahshjkdasjklsadjkldasjkldsajklkljsdakjlcsakljdjklaskljdjsakldjaksl
</textPath>
<textPath startOffset="1%" xlink:href="#text-path-a">
ahshjkdasjklsadjkldasjkldsajklkljsdakjlcsakljdjklaskljdjsakldjaksl
</textPath>
<textPath startOffset="1%" xlink:href="#text-path-a">
ahshjkdasjklsadjkldasjkldsajklkljsdakjlcsakljdjklaskljdjsakldjaksl
</textPath>
<textPath startOffset="1%" xlink:href="#text-path-a">
ahshjkdasjklsadjkldasjkldsajklkljsdakjlcsakljdjklaskljdjsakldjaksl
</textPath>
<textPath startOffset="1%" xlink:href="#text-path-a">
ahshjkdasjklsadjkldasjkldsajklkljsdakjlcsakljdjklaskljdjsakldjaksl
</textPath>
</text>
</svg>
`;

export default function Test1740() {
return <SvgXml xml={CircleSvg} width="100%" height="100%" />;
}
25 changes: 14 additions & 11 deletions apple/Utils/RNSVGPathMeasure.mm
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ - (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint x:(CGFloat *)x
{
// Investigation suggests binary search is faster at lineCount >= 16
// https://gist.github.com/msand/4c7993319425f9d7933be58ad9ada1a4
NSUInteger i = _lineCount < 16
NSUInteger index = _lineCount < 16
? [_lengths indexOfObjectPassingTest:^(NSNumber *length, NSUInteger index, BOOL *_Nonnull stop) {
BOOL contains = midPoint <= [length doubleValue];
return contains;
Expand All @@ -205,22 +205,25 @@ - (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint x:(CGFloat *)x
usingComparator:^(NSNumber *obj1, NSNumber *obj2) {
return [obj1 compare:obj2];
}];
if (index >= _lineCount) {
index = _lineCount - 1;
}

CGFloat totalLength = (CGFloat)[_lengths[i] doubleValue];
CGFloat prevLength = i == 0 ? 0 : (CGFloat)[_lengths[i - 1] doubleValue];
CGFloat totalLength = (CGFloat)[_lengths[index] doubleValue];
CGFloat prevLength = index == 0 ? 0 : (CGFloat)[_lengths[index - 1] doubleValue];

CGFloat length = totalLength - prevLength;
CGFloat percent = (midPoint - prevLength) / length;

NSArray *points = [_lines objectAtIndex:i];
CGPoint p1 = [[points objectAtIndex:0] CGPointValue];
CGPoint p2 = [[points objectAtIndex:1] CGPointValue];
NSArray *points = [_lines objectAtIndex:index];
CGPoint startPoint = [[points objectAtIndex:0] CGPointValue];
CGPoint endPoint = [[points objectAtIndex:1] CGPointValue];

CGFloat ldx = p2.x - p1.x;
CGFloat ldy = p2.y - p1.y;
*angle = atan2(ldy, ldx);
*x = p1.x + ldx * percent;
*y = p1.y + ldy * percent;
CGFloat deltaX = endPoint.x - startPoint.x;
CGFloat deltaY = endPoint.y - startPoint.y;
*angle = atan2(deltaY, deltaX);
*x = startPoint.x + deltaX * percent;
*y = startPoint.y + deltaY * percent;
}

@end
Loading