mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-06 08:22:23 +00:00
Improve text subtree advance calculation caching
This commit is contained in:
@@ -108,6 +108,9 @@ class TSpanView extends TextView {
|
||||
}
|
||||
|
||||
double getSubtreeTextChunksTotalAdvance(Paint paint) {
|
||||
if (!Double.isNaN(cachedAdvance)) {
|
||||
return cachedAdvance;
|
||||
}
|
||||
double advance = 0;
|
||||
|
||||
if (mContent == null) {
|
||||
@@ -118,6 +121,7 @@ class TSpanView extends TextView {
|
||||
advance += text.getSubtreeTextChunksTotalAdvance(paint);
|
||||
}
|
||||
}
|
||||
cachedAdvance = advance;
|
||||
return advance;
|
||||
}
|
||||
|
||||
@@ -125,6 +129,7 @@ class TSpanView extends TextView {
|
||||
final int length = line.length();
|
||||
|
||||
if (length == 0) {
|
||||
cachedAdvance = 0;
|
||||
return advance;
|
||||
}
|
||||
|
||||
@@ -149,7 +154,8 @@ class TSpanView extends TextView {
|
||||
paint.setLetterSpacing((float)(letterSpacing / (font.fontSize * mScale)));
|
||||
}
|
||||
|
||||
return paint.measureText(line);
|
||||
cachedAdvance = paint.measureText(line);
|
||||
return cachedAdvance;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
|
||||
@@ -50,11 +50,15 @@ class TextView extends GroupView {
|
||||
if (mPath == null) {
|
||||
return;
|
||||
}
|
||||
cachedAdvance = Double.NaN;
|
||||
super.invalidate();
|
||||
clearChildCache();
|
||||
}
|
||||
|
||||
void clearCache() {
|
||||
cachedAdvance = Double.NaN;
|
||||
super.clearCache();
|
||||
}
|
||||
|
||||
@ReactProp(name = "textLength")
|
||||
public void setTextLength(Dynamic length) {
|
||||
mTextLength = SVGLength.from(length);
|
||||
|
||||
@@ -101,7 +101,7 @@ abstract public class VirtualView extends ReactViewGroup {
|
||||
super.invalidate();
|
||||
}
|
||||
|
||||
private void clearCache() {
|
||||
void clearCache() {
|
||||
canvasDiagonal = -1;
|
||||
canvasHeight = -1;
|
||||
canvasWidth = -1;
|
||||
|
||||
@@ -118,4 +118,6 @@ extern CGFloat const RNSVG_DEFAULT_FONT_SIZE;
|
||||
|
||||
- (void)clearChildCache;
|
||||
|
||||
- (void)clearPath;
|
||||
|
||||
@end
|
||||
|
||||
@@ -25,6 +25,7 @@ static CGFloat RNSVGTSpan_radToDeg = 180 / (CGFloat)M_PI;
|
||||
BOOL isClosed;
|
||||
NSMutableArray *emoji;
|
||||
NSMutableArray *emojiTransform;
|
||||
CGFloat cachedAdvance;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
@@ -41,6 +42,12 @@ static CGFloat RNSVGTSpan_radToDeg = 180 / (CGFloat)M_PI;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)clearPath
|
||||
{
|
||||
[super clearPath];
|
||||
cachedAdvance = NAN;
|
||||
}
|
||||
|
||||
- (void)setContent:(NSString *)content
|
||||
{
|
||||
if ([content isEqualToString:_content]) {
|
||||
@@ -102,6 +109,9 @@ static CGFloat RNSVGTSpan_radToDeg = 180 / (CGFloat)M_PI;
|
||||
|
||||
- (CGFloat)getSubtreeTextChunksTotalAdvance
|
||||
{
|
||||
if (!isnan(cachedAdvance)) {
|
||||
return cachedAdvance;
|
||||
}
|
||||
CGFloat advance = 0;
|
||||
|
||||
NSString *str = self.content;
|
||||
@@ -112,6 +122,7 @@ static CGFloat RNSVGTSpan_radToDeg = 180 / (CGFloat)M_PI;
|
||||
advance += [text getSubtreeTextChunksTotalAdvance];
|
||||
}
|
||||
}
|
||||
cachedAdvance = advance;
|
||||
return advance;
|
||||
}
|
||||
|
||||
@@ -155,6 +166,7 @@ static CGFloat RNSVGTSpan_radToDeg = 180 / (CGFloat)M_PI;
|
||||
|
||||
CGRect textBounds = CTLineGetBoundsWithOptions(line, 0);
|
||||
CGFloat textMeasure = CGRectGetWidth(textBounds);
|
||||
cachedAdvance = textMeasure;
|
||||
return textMeasure;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,16 @@
|
||||
if (self.dirty || self.merging) {
|
||||
return;
|
||||
}
|
||||
cachedAdvance = NAN;
|
||||
[super invalidate];
|
||||
[self clearChildCache];
|
||||
}
|
||||
|
||||
- (void)clearPath
|
||||
{
|
||||
[super clearPath];
|
||||
cachedAdvance = NAN;
|
||||
}
|
||||
|
||||
- (void)setTextLength:(RNSVGLength *)textLength
|
||||
{
|
||||
if ([textLength isEqualTo:_textLength]) {
|
||||
|
||||
Reference in New Issue
Block a user