Share TextLayoutManager.h across all platforms (#48210)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48210

[Changelog] [Internal] - Share TextLayoutManager.h across all platforms

The goal of this change is to enable to share `TextLayoutManager.h` across all platforms, the same we do for:

https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/renderer/imagemanager/ImageManager.h

Reviewed By: philIip

Differential Revision: D67064488

fbshipit-source-id: 15ee02f3c2351ad65590b5f0dee2f3dbbde4df32
This commit is contained in:
Christoph Purrer
2024-12-13 16:12:40 -08:00
committed by Facebook GitHub Bot
parent c2fd35a442
commit ae90dd3aaa
8 changed files with 59 additions and 193 deletions
@@ -24,6 +24,7 @@ add_library(react_render_textlayoutmanager
target_include_directories(react_render_textlayoutmanager
PUBLIC
.
${REACT_COMMON_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/platform/android/
)
@@ -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
@@ -7,23 +7,26 @@
#pragma once
#include <react/renderer/attributedstring/AttributedString.h>
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/utils/ContextContainer.h>
#include <memory>
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<void> getNativeTextLayoutManager() const;
#endif
protected:
std::shared_ptr<const ContextContainer> contextContainer_;
#ifdef __APPLE__
std::shared_ptr<void> nativeTextLayoutManager_;
#endif
TextMeasureCache textMeasureCache_;
LineMeasureCache lineMeasureCache_;
};
@@ -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
@@ -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
@@ -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 <memory>
#include <react/renderer/attributedstring/AttributedString.h>
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/utils/ContextContainer.h>
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
@@ -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 <memory>
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/utils/ContextContainer.h>
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<void> getNativeTextLayoutManager() const;
private:
std::shared_ptr<void> nativeTextLayoutManager_;
TextMeasureCache textMeasureCache_{};
LineMeasureCache lineMeasureCache_{};
};
} // namespace facebook::react
@@ -5,12 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/
#include "TextLayoutManager.h"
#include <react/renderer/telemetry/TransactionTelemetry.h>
#include <react/utils/ManagedObjectWrapper.h>
#import "TextLayoutManager.h"
#import "RCTTextLayoutManager.h"
#import <react/renderer/telemetry/TransactionTelemetry.h>
#import <react/utils/ManagedObjectWrapper.h>
namespace facebook::react {
TextLayoutManager::TextLayoutManager(const ContextContainer::Shared &contextContainer)
@@ -105,18 +105,4 @@ LinesMeasurements TextLayoutManager::measureLines(
return measurement;
}
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