Files
react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.cpp
T
Samuel Susla d41e95fb1a Cache NSTextStorage
Summary:
changelog: [internal]

This diff introduces a mechanism to cache NSTextStorage on Paragraph's state. The old renderer already has caching for NSTextStorage: https://github.com/facebook/react-native/blob/main/Libraries/Text/Text/RCTTextShadowView.m#L21

Fabric will store `NSTextStorage` inside `ParagraphLayoutManager` which is owned by state.

There is one notable change which is not gated: Paragraph's state strongly owns `TextLayoutManager`, not `ParagraphShadowNode` like previously. This shouldn't change anything

Reviewed By: mdvacca

Differential Revision: D43692171

fbshipit-source-id: efe6077222a30ab6d89abd9916534b9a96e745d4
2023-03-08 08:36:08 -08:00

60 lines
1.8 KiB
C++

/*
* 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 "ParagraphLayoutManager.h"
#include <folly/Hash.h>
#include <react/renderer/core/CoreFeatures.h>
namespace facebook::react {
TextMeasurement ParagraphLayoutManager::measure(
AttributedString const &attributedString,
ParagraphAttributes const &paragraphAttributes,
LayoutConstraints layoutConstraints) const {
if (CoreFeatures::cacheNSTextStorage) {
size_t newHash = folly::hash::hash_combine(
0,
textAttributedStringHashLayoutWise(attributedString),
paragraphAttributes);
if (!hostTextStorage_ || newHash != hash_) {
hostTextStorage_ = textLayoutManager_->getHostTextStorage(
attributedString, paragraphAttributes, layoutConstraints);
hash_ = newHash;
}
}
return textLayoutManager_->measure(
AttributedStringBox(attributedString),
paragraphAttributes,
layoutConstraints,
hostTextStorage_);
}
LinesMeasurements ParagraphLayoutManager::measureLines(
AttributedString const &attributedString,
ParagraphAttributes const &paragraphAttributes,
Size size) const {
return textLayoutManager_->measureLines(
attributedString, paragraphAttributes, size);
}
void ParagraphLayoutManager::setTextLayoutManager(
std::shared_ptr<TextLayoutManager const> textLayoutManager) const {
textLayoutManager_ = std::move(textLayoutManager);
}
std::shared_ptr<TextLayoutManager const>
ParagraphLayoutManager::getTextLayoutManager() const {
return textLayoutManager_;
}
std::shared_ptr<void> ParagraphLayoutManager::getHostTextStorage() const {
return hostTextStorage_;
}
} // namespace facebook::react