Skip to content

Commit

Permalink
Share TextLayoutManager.h across all platforms (#48210)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Dec 14, 2024
1 parent c2fd35a commit ae90dd3
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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/
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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*/,
Expand All @@ -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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -105,18 +105,4 @@
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

0 comments on commit ae90dd3

Please sign in to comment.