Skip to content

Commit

Permalink
Refactor usages of TextLayoutManager.sTextPaintInstance
Browse files Browse the repository at this point in the history
Summary:
Refactor usages of TextLayoutManager.sTextPaintInstance

changelog: [internal] internal

Reviewed By: javache

Differential Revision: D43888689

fbshipit-source-id: 64a198f3ca07e32666701204d94e6e75b3ab53ad
  • Loading branch information
mdvacca authored and OlimpiaZurek committed May 22, 2023
1 parent ad2c39a commit 378437b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ private static ColorStateList getDefaultTextAttribute(Context context, int attri
TypedArray textAppearances = null;
try {
textAppearances = theme.obtainStyledAttributes(new int[] {attribute});
ColorStateList textColor = textAppearances.getColorStateList(0);
return textColor;
return textAppearances.getColorStateList(0);
} finally {
if (textAppearances != null) {
textAppearances.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ private static Layout createLayout(
Layout layout;
int spanLength = text.length();
boolean unconstrainedWidth = widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || width < 0;
TextPaint textPaint = sTextPaintInstance;
float desiredWidth = boring == null ? Layout.getDesiredWidth(text, textPaint) : Float.NaN;
float desiredWidth =
boring == null ? Layout.getDesiredWidth(text, sTextPaintInstance) : Float.NaN;

if (boring == null
&& (unconstrainedWidth
Expand All @@ -252,15 +252,15 @@ private static Layout createLayout(
layout =
new StaticLayout(
text,
textPaint,
sTextPaintInstance,
hintWidth,
Layout.Alignment.ALIGN_NORMAL,
1.f,
0.f,
includeFontPadding);
} else {
layout =
StaticLayout.Builder.obtain(text, 0, spanLength, textPaint, hintWidth)
StaticLayout.Builder.obtain(text, 0, spanLength, sTextPaintInstance, hintWidth)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0.f, 1.f)
.setIncludePad(includeFontPadding)
Expand All @@ -281,7 +281,7 @@ private static Layout createLayout(
layout =
BoringLayout.make(
text,
textPaint,
sTextPaintInstance,
boringLayoutWidth,
Layout.Alignment.ALIGN_NORMAL,
1.f,
Expand All @@ -295,15 +295,15 @@ private static Layout createLayout(
layout =
new StaticLayout(
text,
textPaint,
sTextPaintInstance,
(int) width,
Layout.Alignment.ALIGN_NORMAL,
1.f,
0.f,
includeFontPadding);
} else {
StaticLayout.Builder builder =
StaticLayout.Builder.obtain(text, 0, spanLength, textPaint, (int) width)
StaticLayout.Builder.obtain(text, 0, spanLength, sTextPaintInstance, (int) width)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0.f, 1.f)
.setIncludePad(includeFontPadding)
Expand Down Expand Up @@ -332,7 +332,6 @@ public static long measureText(
@Nullable float[] attachmentsPositions) {

// TODO(5578671): Handle text direction (see View#getTextDirectionHeuristic)
TextPaint textPaint = sTextPaintInstance;
Spannable text;
if (attributedString.hasKey("cacheId")) {
int cacheId = attributedString.getInt("cacheId");
Expand All @@ -342,7 +341,7 @@ public static long measureText(
if (sTagToSpannableCache.containsKey(cacheId)) {
text = sTagToSpannableCache.get(cacheId);
if (ENABLE_MEASURE_LOGGING) {
FLog.e(TAG, "Text for spannable found for cacheId[" + cacheId + "]: " + text.toString());
FLog.e(TAG, "Text for spannable found for cacheId[" + cacheId + "]: " + text);
}
} else {
if (ENABLE_MEASURE_LOGGING) {
Expand All @@ -369,7 +368,7 @@ public static long measureText(
throw new IllegalStateException("Spannable element has not been prepared in onBeforeLayout");
}

BoringLayout.Metrics boring = BoringLayout.isBoring(text, textPaint);
BoringLayout.Metrics boring = BoringLayout.isBoring(text, sTextPaintInstance);

Layout layout =
createLayout(
Expand Down Expand Up @@ -527,9 +526,8 @@ public static WritableArray measureLines(
ReadableMap attributedString,
ReadableMap paragraphAttributes,
float width) {
TextPaint textPaint = sTextPaintInstance;
Spannable text = getOrCreateSpannableForText(context, attributedString, null);
BoringLayout.Metrics boring = BoringLayout.isBoring(text, textPaint);
BoringLayout.Metrics boring = BoringLayout.isBoring(text, sTextPaintInstance);

int textBreakStrategy =
TextAttributeProps.getTextBreakStrategy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class TextLayoutManagerMapBuffer {

private static final String INLINE_VIEW_PLACEHOLDER = "0";

private static final Object sSpannableCacheLock = new Object();
private static final boolean DEFAULT_INCLUDE_FONT_PADDING = true;
private static final LruCache<MapBuffer, Spannable> sSpannableCache =
new LruCache<>(spannableCacheSize);
Expand Down Expand Up @@ -253,8 +252,8 @@ private static Layout createLayout(
Layout layout;
int spanLength = text.length();
boolean unconstrainedWidth = widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || width < 0;
TextPaint textPaint = sTextPaintInstance;
float desiredWidth = boring == null ? Layout.getDesiredWidth(text, textPaint) : Float.NaN;
float desiredWidth =
boring == null ? Layout.getDesiredWidth(text, sTextPaintInstance) : Float.NaN;

if (boring == null
&& (unconstrainedWidth
Expand All @@ -267,15 +266,15 @@ private static Layout createLayout(
layout =
new StaticLayout(
text,
textPaint,
sTextPaintInstance,
hintWidth,
Layout.Alignment.ALIGN_NORMAL,
1.f,
0.f,
includeFontPadding);
} else {
layout =
StaticLayout.Builder.obtain(text, 0, spanLength, textPaint, hintWidth)
StaticLayout.Builder.obtain(text, 0, spanLength, sTextPaintInstance, hintWidth)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0.f, 1.f)
.setIncludePad(includeFontPadding)
Expand All @@ -296,7 +295,7 @@ private static Layout createLayout(
layout =
BoringLayout.make(
text,
textPaint,
sTextPaintInstance,
boringLayoutWidth,
Layout.Alignment.ALIGN_NORMAL,
1.f,
Expand All @@ -310,15 +309,15 @@ private static Layout createLayout(
layout =
new StaticLayout(
text,
textPaint,
sTextPaintInstance,
(int) width,
Layout.Alignment.ALIGN_NORMAL,
1.f,
0.f,
includeFontPadding);
} else {
StaticLayout.Builder builder =
StaticLayout.Builder.obtain(text, 0, spanLength, textPaint, (int) width)
StaticLayout.Builder.obtain(text, 0, spanLength, sTextPaintInstance, (int) width)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0.f, 1.f)
.setIncludePad(includeFontPadding)
Expand Down Expand Up @@ -357,7 +356,7 @@ public static long measureText(
if (sTagToSpannableCache.containsKey(cacheId)) {
text = sTagToSpannableCache.get(cacheId);
if (ENABLE_MEASURE_LOGGING) {
FLog.e(TAG, "Text for spannable found for cacheId[" + cacheId + "]: " + text.toString());
FLog.e(TAG, "Text for spannable found for cacheId[" + cacheId + "]: " + text);
}
} else {
if (ENABLE_MEASURE_LOGGING) {
Expand Down Expand Up @@ -385,11 +384,6 @@ public static long measureText(
}

BoringLayout.Metrics boring = BoringLayout.isBoring(text, textPaint);
float desiredWidth = boring == null ? Layout.getDesiredWidth(text, textPaint) : Float.NaN;

// technically, width should never be negative, but there is currently a bug in
boolean unconstrainedWidth = widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || width < 0;

Layout layout =
createLayout(
text,
Expand Down Expand Up @@ -551,9 +545,8 @@ public static WritableArray measureLines(
MapBuffer paragraphAttributes,
float width) {

TextPaint textPaint = sTextPaintInstance;
Spannable text = getOrCreateSpannableForText(context, attributedString, null);
BoringLayout.Metrics boring = BoringLayout.isBoring(text, textPaint);
BoringLayout.Metrics boring = BoringLayout.isBoring(text, sTextPaintInstance);

int textBreakStrategy =
TextAttributeProps.getTextBreakStrategy(
Expand Down

0 comments on commit 378437b

Please sign in to comment.