diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp new file mode 100644 index 00000000000000..a547003fe45f61 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.cpp @@ -0,0 +1,26 @@ +/* + * 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/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h similarity index 75% rename from packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h rename to packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h index ebc93341739faa..42f22ded1ca9b0 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextLayoutManager.h @@ -7,23 +7,26 @@ #pragma once -#include #include +#include #include #include #include #include +#include namespace facebook::react { class TextLayoutManager; /* - * Cross platform facade for Android-specific TextLayoutManager. + * Cross platform facade for text measurement (e.g. Android-specific + * TextLayoutManager) */ class TextLayoutManager { public: TextLayoutManager(const ContextContainer::Shared& contextContainer); + virtual ~TextLayoutManager() = default; /* * Not copyable. @@ -40,12 +43,13 @@ class TextLayoutManager { /* * Measures `attributedString` using native text rendering infrastructure. */ - TextMeasurement measure( + virtual TextMeasurement measure( const AttributedStringBox& attributedStringBox, const ParagraphAttributes& paragraphAttributes, const TextLayoutContext& layoutContext, const LayoutConstraints& layoutConstraints) const; +#ifdef ANDROID /** * Measures an AttributedString on the platform, as identified by some * opaque cache ID. @@ -54,12 +58,13 @@ class TextLayoutManager { int64_t cacheId, const ParagraphAttributes& paragraphAttributes, const LayoutConstraints& layoutConstraints) const; +#endif /* * Measures lines of `attributedString` using native text rendering * infrastructure. */ - LinesMeasurements measureLines( + virtual LinesMeasurements measureLines( const AttributedStringBox& attributedStringBox, const ParagraphAttributes& paragraphAttributes, const Size& size) const; @@ -73,8 +78,19 @@ class TextLayoutManager { const ParagraphAttributes& paragraphAttributes, const Size& size) const; - private: - ContextContainer::Shared contextContainer_; +#ifdef __APPLE__ + /* + * Returns an opaque pointer to platform-specific TextLayoutManager. + * Is used on a native views layer to delegate text rendering to the manager. + */ + std::shared_ptr getNativeTextLayoutManager() const; +#endif + + protected: + std::shared_ptr contextContainer_; +#ifdef __APPLE__ + std::shared_ptr nativeTextLayoutManager_; +#endif TextMeasureCache textMeasureCache_; LineMeasureCache lineMeasureCache_; }; diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp index 507403cd178f0a..42e808cbeb4587 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp @@ -277,18 +277,4 @@ LinesMeasurements TextLayoutManager::measureLines( return lineMeasurements; } -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/platform/cxx/TextLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.cpp index c5d867767817ba..76593cd59a97f4 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.cpp @@ -9,9 +9,10 @@ namespace facebook::react { -void* TextLayoutManager::getNativeTextLayoutManager() const { - return (void*)this; -} +TextLayoutManager::TextLayoutManager( + const ContextContainer::Shared& /*contextContainer*/) + : textMeasureCache_(kSimpleThreadSafeCacheSizeCap), + lineMeasureCache_(kSimpleThreadSafeCacheSizeCap) {} TextMeasurement TextLayoutManager::measure( const AttributedStringBox& attributedStringBox, @@ -28,12 +29,14 @@ TextMeasurement TextLayoutManager::measure( return TextMeasurement{{0, 0}, attachments}; } +#ifdef ANDROID TextMeasurement TextLayoutManager::measureCachedSpannableById( int64_t /*cacheId*/, const ParagraphAttributes& /*paragraphAttributes*/, const LayoutConstraints& /*layoutConstraints*/) const { return {}; } +#endif LinesMeasurements TextLayoutManager::measureLines( const AttributedStringBox& /*attributedStringBox*/, @@ -42,11 +45,4 @@ LinesMeasurements TextLayoutManager::measureLines( return {}; }; -Float TextLayoutManager::baseline( - const AttributedStringBox& /*attributedStringBox*/, - const ParagraphAttributes& /*paragraphAttributes*/, - const Size& /*size*/) const { - return 0; -} - } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.h deleted file mode 100644 index e2110037e48dd0..00000000000000 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.h +++ /dev/null @@ -1,76 +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. - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace facebook::react { - -class TextLayoutManager; - -/* - * Cross platform facade for Android-specific TextLayoutManager. - */ -class TextLayoutManager { - public: - TextLayoutManager(const ContextContainer::Shared& contextContainer) {} - - virtual ~TextLayoutManager() = default; - - /* - * Measures `attributedStringBox` using native text rendering infrastructure. - */ - virtual TextMeasurement measure( - const AttributedStringBox& attributedStringBox, - const ParagraphAttributes& paragraphAttributes, - const TextLayoutContext& layoutContext, - const LayoutConstraints& layoutConstraints) const; - - /** - * Measures an AttributedString on the platform, as identified by some - * opaque cache ID. - */ - virtual TextMeasurement measureCachedSpannableById( - int64_t cacheId, - const ParagraphAttributes& paragraphAttributes, - const LayoutConstraints& layoutConstraints) const; - - /* - * Measures lines of `attributedString` using native text rendering - * infrastructure. - */ - virtual LinesMeasurements measureLines( - const AttributedStringBox& attributedStringBox, - const ParagraphAttributes& paragraphAttributes, - const Size& size) const; - - /* - * Calculates baseline of `attributedString` using native text rendering - * infrastructure. - */ - virtual Float baseline( - const AttributedStringBox& attributedStringBox, - const ParagraphAttributes& paragraphAttributes, - const Size& size) const; - - /* - * Returns an opaque pointer to platform-specific TextLayoutManager. - * Is used on a native views layer to delegate text rendering to the manager. - */ - void* getNativeTextLayoutManager() const; -}; - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.h deleted file mode 100644 index 91d7e2ebb6f52d..00000000000000 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.h +++ /dev/null @@ -1,69 +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. - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include -#include - -namespace facebook::react { - -class TextLayoutManager; - -/* - * Cross platform facade for iOS-specific RCTTTextLayoutManager. - */ -class TextLayoutManager { - public: - TextLayoutManager(const ContextContainer::Shared& contextContainer); - - /* - * Measures `attributedString` using native text rendering infrastructure. - */ - TextMeasurement measure( - const AttributedStringBox& attributedStringBox, - const ParagraphAttributes& paragraphAttributes, - const TextLayoutContext& layoutContext, - const LayoutConstraints& layoutConstraints) const; - - /* - * Measures lines of `attributedString` using native text rendering - * infrastructure. - */ - LinesMeasurements measureLines( - const AttributedStringBox& attributedStringBox, - 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; - - /* - * Returns an opaque pointer to platform-specific TextLayoutManager. - * Is used on a native views layer to delegate text rendering to the manager. - */ - std::shared_ptr getNativeTextLayoutManager() const; - - private: - std::shared_ptr nativeTextLayoutManager_; - TextMeasureCache textMeasureCache_{}; - LineMeasureCache lineMeasureCache_{}; -}; - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm index db713f83b99be7..cc0edbccfbcae5 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm @@ -5,12 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -#include "TextLayoutManager.h" -#include -#include - +#import "TextLayoutManager.h" #import "RCTTextLayoutManager.h" +#import +#import + namespace facebook::react { TextLayoutManager::TextLayoutManager(const ContextContainer::Shared &contextContainer) @@ -105,18 +105,4 @@ return measurement; } -Float TextLayoutManager::baseline( - const AttributedStringBox &attributedStringBox, - const ParagraphAttributes ¶graphAttributes, - const Size &size) const -{ - auto lines = this->measureLines(attributedStringBox, paragraphAttributes, size); - - if (!lines.empty()) { - return lines[0].ascender; - } else { - return 0; - } -} - } // namespace facebook::react