Skip to content

Commit 2eaf0b0

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Add an overload for createLayout to reduce code duplication (facebook#45083)
Summary: Adds an overload for `createLayout` method that also handles extracting paragraph attributes and scaling font size if necessary. ## Changelog: [ANDROID] [CHANGED] - Extracted common parts related to calculating text layout to a helper Pull Request resolved: facebook#45083 Test Plan: Tried out on RNTester Reviewed By: robhogan Differential Revision: D58818560 Pulled By: cortinico fbshipit-source-id: a42b5de04c4a70edb88cdd734387d7e4cee94032
1 parent 8a6b88e commit 2eaf0b0

File tree

2 files changed

+75
-104
lines changed

2 files changed

+75
-104
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7628,6 +7628,7 @@ public class com/facebook/react/views/text/TextLayoutManager {
76287628
public static final field PA_KEY_TEXT_BREAK_STRATEGY S
76297629
public fun <init> ()V
76307630
public static fun adjustSpannableFontToFit (Landroid/text/Spannable;FLcom/facebook/yoga/YogaMeasureMode;FLcom/facebook/yoga/YogaMeasureMode;DIZIILandroid/text/Layout$Alignment;)V
7631+
public static fun createLayout (Landroid/content/Context;Lcom/facebook/react/common/mapbuffer/MapBuffer;Lcom/facebook/react/common/mapbuffer/MapBuffer;FFLcom/facebook/react/views/text/ReactTextViewManagerCallback;)Landroid/text/Layout;
76317632
public static fun deleteCachedSpannableForTag (I)V
76327633
public static fun getOrCreateSpannableForText (Landroid/content/Context;Lcom/facebook/react/common/mapbuffer/MapBuffer;Lcom/facebook/react/views/text/ReactTextViewManagerCallback;)Landroid/text/Spannable;
76337634
public static fun getTextAlignment (Lcom/facebook/react/common/mapbuffer/MapBuffer;Landroid/text/Spannable;)Landroid/text/Layout$Alignment;

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java

Lines changed: 74 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,69 @@ private static Layout createLayout(
422422
return layout;
423423
}
424424

425+
public static Layout createLayout(
426+
@NonNull Context context,
427+
MapBuffer attributedString,
428+
MapBuffer paragraphAttributes,
429+
float width,
430+
float height,
431+
ReactTextViewManagerCallback reactTextViewManagerCallback) {
432+
Spannable text =
433+
getOrCreateSpannableForText(context, attributedString, reactTextViewManagerCallback);
434+
BoringLayout.Metrics boring = BoringLayout.isBoring(text, sTextPaintInstance);
435+
436+
int textBreakStrategy =
437+
TextAttributeProps.getTextBreakStrategy(
438+
paragraphAttributes.getString(PA_KEY_TEXT_BREAK_STRATEGY));
439+
boolean includeFontPadding =
440+
paragraphAttributes.contains(PA_KEY_INCLUDE_FONT_PADDING)
441+
? paragraphAttributes.getBoolean(PA_KEY_INCLUDE_FONT_PADDING)
442+
: DEFAULT_INCLUDE_FONT_PADDING;
443+
int hyphenationFrequency =
444+
TextAttributeProps.getTextBreakStrategy(
445+
paragraphAttributes.getString(PA_KEY_HYPHENATION_FREQUENCY));
446+
boolean adjustFontSizeToFit =
447+
paragraphAttributes.contains(PA_KEY_ADJUST_FONT_SIZE_TO_FIT)
448+
? paragraphAttributes.getBoolean(PA_KEY_ADJUST_FONT_SIZE_TO_FIT)
449+
: DEFAULT_ADJUST_FONT_SIZE_TO_FIT;
450+
int maximumNumberOfLines =
451+
paragraphAttributes.contains(PA_KEY_MAX_NUMBER_OF_LINES)
452+
? paragraphAttributes.getInt(PA_KEY_MAX_NUMBER_OF_LINES)
453+
: ReactConstants.UNSET;
454+
455+
Layout.Alignment alignment = getTextAlignment(attributedString, text);
456+
457+
if (adjustFontSizeToFit) {
458+
double minimumFontSize =
459+
paragraphAttributes.contains(PA_KEY_MINIMUM_FONT_SIZE)
460+
? paragraphAttributes.getDouble(PA_KEY_MINIMUM_FONT_SIZE)
461+
: Double.NaN;
462+
463+
adjustSpannableFontToFit(
464+
text,
465+
width,
466+
YogaMeasureMode.EXACTLY,
467+
height,
468+
YogaMeasureMode.UNDEFINED,
469+
minimumFontSize,
470+
maximumNumberOfLines,
471+
includeFontPadding,
472+
textBreakStrategy,
473+
hyphenationFrequency,
474+
alignment);
475+
}
476+
477+
return createLayout(
478+
text,
479+
boring,
480+
width,
481+
YogaMeasureMode.EXACTLY,
482+
includeFontPadding,
483+
textBreakStrategy,
484+
hyphenationFrequency,
485+
alignment);
486+
}
487+
425488
public static void adjustSpannableFontToFit(
426489
Spannable text,
427490
float width,
@@ -505,66 +568,25 @@ public static long measureText(
505568
@Nullable float[] attachmentsPositions) {
506569

507570
// TODO(5578671): Handle text direction (see View#getTextDirectionHeuristic)
508-
Spannable text =
509-
getOrCreateSpannableForText(context, attributedString, reactTextViewManagerCallback);
571+
Layout layout =
572+
createLayout(
573+
context,
574+
attributedString,
575+
paragraphAttributes,
576+
width,
577+
height,
578+
reactTextViewManagerCallback);
579+
Spannable text = (Spannable) layout.getText();
510580

511581
if (text == null) {
512582
return 0;
513583
}
514584

515-
int textBreakStrategy =
516-
TextAttributeProps.getTextBreakStrategy(
517-
paragraphAttributes.getString(PA_KEY_TEXT_BREAK_STRATEGY));
518-
boolean includeFontPadding =
519-
paragraphAttributes.contains(PA_KEY_INCLUDE_FONT_PADDING)
520-
? paragraphAttributes.getBoolean(PA_KEY_INCLUDE_FONT_PADDING)
521-
: DEFAULT_INCLUDE_FONT_PADDING;
522-
int hyphenationFrequency =
523-
TextAttributeProps.getHyphenationFrequency(
524-
paragraphAttributes.getString(PA_KEY_HYPHENATION_FREQUENCY));
525-
boolean adjustFontSizeToFit =
526-
paragraphAttributes.contains(PA_KEY_ADJUST_FONT_SIZE_TO_FIT)
527-
? paragraphAttributes.getBoolean(PA_KEY_ADJUST_FONT_SIZE_TO_FIT)
528-
: DEFAULT_ADJUST_FONT_SIZE_TO_FIT;
529585
int maximumNumberOfLines =
530586
paragraphAttributes.contains(PA_KEY_MAX_NUMBER_OF_LINES)
531587
? paragraphAttributes.getInt(PA_KEY_MAX_NUMBER_OF_LINES)
532588
: ReactConstants.UNSET;
533589

534-
Layout.Alignment alignment = getTextAlignment(attributedString, text);
535-
536-
if (adjustFontSizeToFit) {
537-
double minimumFontSize =
538-
paragraphAttributes.contains(PA_KEY_MINIMUM_FONT_SIZE)
539-
? paragraphAttributes.getDouble(PA_KEY_MINIMUM_FONT_SIZE)
540-
: Double.NaN;
541-
542-
adjustSpannableFontToFit(
543-
text,
544-
width,
545-
widthYogaMeasureMode,
546-
height,
547-
heightYogaMeasureMode,
548-
minimumFontSize,
549-
maximumNumberOfLines,
550-
includeFontPadding,
551-
textBreakStrategy,
552-
hyphenationFrequency,
553-
alignment);
554-
}
555-
556-
BoringLayout.Metrics boring = BoringLayout.isBoring(text, sTextPaintInstance);
557-
Layout layout =
558-
createLayout(
559-
text,
560-
boring,
561-
width,
562-
widthYogaMeasureMode,
563-
includeFontPadding,
564-
textBreakStrategy,
565-
hyphenationFrequency,
566-
alignment);
567-
568590
int calculatedLineCount =
569591
maximumNumberOfLines == ReactConstants.UNSET || maximumNumberOfLines == 0
570592
? layout.getLineCount()
@@ -718,60 +740,8 @@ public static WritableArray measureLines(
718740
float width,
719741
float height) {
720742

721-
Spannable text = getOrCreateSpannableForText(context, attributedString, null);
722-
BoringLayout.Metrics boring = BoringLayout.isBoring(text, sTextPaintInstance);
723-
724-
int textBreakStrategy =
725-
TextAttributeProps.getTextBreakStrategy(
726-
paragraphAttributes.getString(PA_KEY_TEXT_BREAK_STRATEGY));
727-
boolean includeFontPadding =
728-
paragraphAttributes.contains(PA_KEY_INCLUDE_FONT_PADDING)
729-
? paragraphAttributes.getBoolean(PA_KEY_INCLUDE_FONT_PADDING)
730-
: DEFAULT_INCLUDE_FONT_PADDING;
731-
int hyphenationFrequency =
732-
TextAttributeProps.getTextBreakStrategy(
733-
paragraphAttributes.getString(PA_KEY_HYPHENATION_FREQUENCY));
734-
boolean adjustFontSizeToFit =
735-
paragraphAttributes.contains(PA_KEY_ADJUST_FONT_SIZE_TO_FIT)
736-
? paragraphAttributes.getBoolean(PA_KEY_ADJUST_FONT_SIZE_TO_FIT)
737-
: DEFAULT_ADJUST_FONT_SIZE_TO_FIT;
738-
int maximumNumberOfLines =
739-
paragraphAttributes.contains(PA_KEY_MAX_NUMBER_OF_LINES)
740-
? paragraphAttributes.getInt(PA_KEY_MAX_NUMBER_OF_LINES)
741-
: ReactConstants.UNSET;
742-
743-
Layout.Alignment alignment = getTextAlignment(attributedString, text);
744-
745-
if (adjustFontSizeToFit) {
746-
double minimumFontSize =
747-
paragraphAttributes.contains(PA_KEY_MINIMUM_FONT_SIZE)
748-
? paragraphAttributes.getDouble(PA_KEY_MINIMUM_FONT_SIZE)
749-
: Double.NaN;
750-
751-
adjustSpannableFontToFit(
752-
text,
753-
width,
754-
YogaMeasureMode.EXACTLY,
755-
height,
756-
YogaMeasureMode.UNDEFINED,
757-
minimumFontSize,
758-
maximumNumberOfLines,
759-
includeFontPadding,
760-
textBreakStrategy,
761-
hyphenationFrequency,
762-
alignment);
763-
}
764-
765743
Layout layout =
766-
createLayout(
767-
text,
768-
boring,
769-
width,
770-
YogaMeasureMode.EXACTLY,
771-
includeFontPadding,
772-
textBreakStrategy,
773-
hyphenationFrequency,
774-
alignment);
775-
return FontMetricsUtil.getFontMetrics(text, layout, sTextPaintInstance, context);
744+
createLayout(context, attributedString, paragraphAttributes, width, height, null);
745+
return FontMetricsUtil.getFontMetrics(layout.getText(), layout, sTextPaintInstance, context);
776746
}
777747
}

0 commit comments

Comments
 (0)