From 01cc16bbfe323b1e53d3126fbf6496b15f087429 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 24 Apr 2025 20:10:57 -0700 Subject: [PATCH] Extract TextLayoutManager::baseline() (#50888) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50888 This is shared between platforms using a very strange pattern. Let's just extract this into its own function. Not considering breaking, since TextLayoutManager is internal interface. Changelog: [internal] Reviewed By: rshest Differential Revision: D73555465 fbshipit-source-id: ea99fbebd9db44efd1dc56c2cad68b5b56e77ad1 --- .../components/text/ParagraphShadowNode.cpp | 5 ++-- .../textinput/BaseTextInputShadowNode.h | 4 +-- .../AndroidTextInputShadowNode.cpp | 4 +-- .../textlayoutmanager/TextLayoutManager.cpp | 26 ------------------- .../textlayoutmanager/TextLayoutManager.h | 9 ------- .../textlayoutmanager/TextMeasureCache.h | 7 +++++ 6 files changed, 14 insertions(+), 41 deletions(-) delete mode 100644 packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp index cc1b1a447ba..9724456948c 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp @@ -214,8 +214,9 @@ Float ParagraphShadowNode::baseline( } AttributedStringBox attributedStringBox{attributedString}; - return textLayoutManager_->baseline( - attributedStringBox, getConcreteProps().paragraphAttributes, size); + + return LineMeasurement::baseline(textLayoutManager_->measureLines( + attributedStringBox, getConcreteProps().paragraphAttributes, size)); } void ParagraphShadowNode::layout(LayoutContext layoutContext) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h index ca450b3e0c6..85ec361b7da 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h @@ -106,8 +106,8 @@ class BaseTextInputShadowNode : public ConcreteViewShadowNode< &(YogaLayoutableShadowNode::yogaNode_), YGEdgeTop); AttributedStringBox attributedStringBox{attributedString}; - return textLayoutManager_->baseline( - attributedStringBox, props.paragraphAttributes, size) + + return LineMeasurement::baseline(textLayoutManager_->measureLines( + attributedStringBox, props.paragraphAttributes, size)) + top; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp index 22f2f6e4df0..ef8a581a28e 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp @@ -92,10 +92,10 @@ Float AndroidTextInputShadowNode::baseline( YGNodeLayoutGetPadding(&yogaNode_, YGEdgeTop); AttributedStringBox attributedStringBox{attributedString}; - return textLayoutManager_->baseline( + return LineMeasurement::baseline(textLayoutManager_->measureLines( attributedStringBox, getConcreteProps().paragraphAttributes, - size) + + size)) + top; } diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp deleted file mode 100644 index a547003fe45..00000000000 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "TextLayoutManager.h" - -namespace facebook::react { - -Float TextLayoutManager::baseline( - const AttributedStringBox& attributedStringBox, - const ParagraphAttributes& paragraphAttributes, - const Size& size) const { - auto lines = - this->measureLines(attributedStringBox, paragraphAttributes, size); - - if (!lines.empty()) { - return lines[0].ascender; - } else { - return 0; - } -} - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h index 42f22ded1ca..14a6527aac2 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h @@ -69,15 +69,6 @@ class TextLayoutManager { const ParagraphAttributes& paragraphAttributes, const Size& size) const; - /* - * Calculates baseline of `attributedString` using native text rendering - * infrastructure. - */ - Float baseline( - const AttributedStringBox& attributedStringBox, - const ParagraphAttributes& paragraphAttributes, - const Size& size) const; - #ifdef __APPLE__ /* * Returns an opaque pointer to platform-specific TextLayoutManager. diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h index 0a9ff8c78d5..ecd6798f47f 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h @@ -35,6 +35,13 @@ struct LineMeasurement { LineMeasurement(const folly::dynamic& data); bool operator==(const LineMeasurement& rhs) const; + + static inline Float baseline(const std::vector& lines) { + if (!lines.empty()) { + return lines[0].ascender; + } + return 0; + } }; using LinesMeasurements = std::vector;