-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathParagraphComponentView.cpp
More file actions
321 lines (270 loc) · 11.4 KB
/
ParagraphComponentView.cpp
File metadata and controls
321 lines (270 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include "ParagraphComponentView.h"
#include <react/renderer/textlayoutmanager/WindowsTextLayoutManager.h>
#include <AutoDraw.h>
#include <Utils/ValueUtils.h>
#include <react/renderer/components/text/ParagraphShadowNode.h>
#include <react/renderer/components/text/ParagraphState.h>
#include <unicode.h>
#include <winrt/Microsoft.ReactNative.Composition.h>
#include "CompositionDynamicAutomationProvider.h"
#include "CompositionHelpers.h"
#include "TextDrawing.h"
namespace winrt::Microsoft::ReactNative::Composition::implementation {
ParagraphComponentView::ParagraphComponentView(
const winrt::Microsoft::ReactNative::Composition::Experimental::ICompositionContext &compContext,
facebook::react::Tag tag,
winrt::Microsoft::ReactNative::ReactContext const &reactContext)
: Super(
ParagraphComponentView::defaultProps(),
compContext,
tag,
reactContext,
ComponentViewFeatures::Default & ~ComponentViewFeatures::Background) {}
void ParagraphComponentView::MountChildComponentView(
const winrt::Microsoft::ReactNative::ComponentView &childComponentView,
uint32_t index) noexcept {
base_type::MountChildComponentView(childComponentView, index);
}
void ParagraphComponentView::UnmountChildComponentView(
const winrt::Microsoft::ReactNative::ComponentView &childComponentView,
uint32_t index) noexcept {
base_type::UnmountChildComponentView(childComponentView, index);
}
void ParagraphComponentView::updateProps(
facebook::react::Props::Shared const &props,
facebook::react::Props::Shared const &oldProps) noexcept {
const auto &oldViewProps =
*std::static_pointer_cast<const facebook::react::ParagraphProps>(oldProps ? oldProps : viewProps());
const auto &newViewProps = *std::static_pointer_cast<const facebook::react::ParagraphProps>(props);
ensureVisual();
if (oldViewProps.textAttributes.foregroundColor != newViewProps.textAttributes.foregroundColor) {
m_requireRedraw = true;
}
if (oldViewProps.textAttributes.opacity != newViewProps.textAttributes.opacity) {
m_requireRedraw = true;
}
if (oldViewProps.textAttributes.alignment != newViewProps.textAttributes.alignment) {
updateTextAlignment(newViewProps.textAttributes.alignment);
}
if (oldViewProps.textAttributes.baseWritingDirection != newViewProps.textAttributes.baseWritingDirection) {
m_textLayout = nullptr;
}
if (oldViewProps.paragraphAttributes.ellipsizeMode != newViewProps.paragraphAttributes.ellipsizeMode) {
m_textLayout = nullptr;
}
if (oldViewProps.paragraphAttributes.adjustsFontSizeToFit != newViewProps.paragraphAttributes.adjustsFontSizeToFit) {
m_requireRedraw = true;
}
Super::updateProps(props, oldProps);
}
void ParagraphComponentView::updateState(
facebook::react::State::Shared const &state,
facebook::react::State::Shared const &oldState) noexcept {
const auto &newState = *std::static_pointer_cast<facebook::react::ParagraphShadowNode::ConcreteState const>(state);
m_attributedStringBox = facebook::react::AttributedStringBox(newState.getData().attributedString);
m_paragraphAttributes = facebook::react::ParagraphAttributes(newState.getData().paragraphAttributes);
m_textLayout = nullptr;
}
void ParagraphComponentView::updateLayoutMetrics(
facebook::react::LayoutMetrics const &layoutMetrics,
facebook::react::LayoutMetrics const &oldLayoutMetrics) noexcept {
Super::updateLayoutMetrics(layoutMetrics, oldLayoutMetrics);
if (layoutMetrics.pointScaleFactor != oldLayoutMetrics.pointScaleFactor) {
m_textLayout = nullptr;
}
}
void ParagraphComponentView::FinalizeUpdates(
winrt::Microsoft::ReactNative::ComponentViewUpdateMask updateMask) noexcept {
ensureVisual();
updateVisualBrush();
Super::FinalizeUpdates(updateMask);
}
facebook::react::SharedViewEventEmitter ParagraphComponentView::eventEmitterAtPoint(
facebook::react::Point pt) noexcept {
if (m_attributedStringBox.getValue().getFragments().size() && m_textLayout) {
BOOL isTrailingHit = false;
BOOL isInside = false;
DWRITE_HIT_TEST_METRICS metrics;
winrt::check_hresult(m_textLayout->HitTestPoint(pt.x, pt.y, &isTrailingHit, &isInside, &metrics));
if (isInside) {
uint32_t textPosition = metrics.textPosition;
for (auto fragment : m_attributedStringBox.getValue().getFragments()) {
if (textPosition < fragment.string.length()) {
return std::static_pointer_cast<const facebook::react::ViewEventEmitter>(
fragment.parentShadowView.eventEmitter);
}
textPosition -= static_cast<uint32_t>(fragment.string.length());
}
}
}
return m_eventEmitter;
}
void ParagraphComponentView::updateTextAlignment(
const std::optional<facebook::react::TextAlignment> &fbAlignment) noexcept {
// Reset text layout to force recreation with new alignment
m_textLayout = nullptr;
}
void ParagraphComponentView::OnRenderingDeviceLost() noexcept {
DrawText();
}
void ParagraphComponentView::updateVisualBrush() noexcept {
bool requireNewBrush{false};
// TODO
// updateTextAlignment(paragraphProps.textAttributes.alignment);
if (!m_textLayout) {
facebook::react::LayoutConstraints constraints;
constraints.maximumSize.width =
m_layoutMetrics.frame.size.width - m_layoutMetrics.contentInsets.left - m_layoutMetrics.contentInsets.right;
constraints.maximumSize.height =
m_layoutMetrics.frame.size.height - m_layoutMetrics.contentInsets.top - m_layoutMetrics.contentInsets.bottom;
facebook::react::WindowsTextLayoutManager::GetTextLayout(
m_attributedStringBox, m_paragraphAttributes, constraints, m_textLayout);
// Apply text alignment after creating the text layout
if (m_textLayout) {
const auto &props = paragraphProps();
DWRITE_TEXT_ALIGNMENT alignment = DWRITE_TEXT_ALIGNMENT_LEADING;
if (props.textAttributes.alignment) {
switch (*props.textAttributes.alignment) {
case facebook::react::TextAlignment::Center:
alignment = DWRITE_TEXT_ALIGNMENT_CENTER;
break;
case facebook::react::TextAlignment::Justified:
alignment = DWRITE_TEXT_ALIGNMENT_JUSTIFIED;
break;
case facebook::react::TextAlignment::Left:
alignment = DWRITE_TEXT_ALIGNMENT_LEADING;
break;
case facebook::react::TextAlignment::Right:
alignment = DWRITE_TEXT_ALIGNMENT_TRAILING;
break;
case facebook::react::TextAlignment::Natural:
alignment = DWRITE_TEXT_ALIGNMENT_LEADING;
break;
default:
alignment = DWRITE_TEXT_ALIGNMENT_LEADING;
break;
}
}
winrt::check_hresult(m_textLayout->SetTextAlignment(alignment));
}
requireNewBrush = true;
}
if (requireNewBrush || !m_drawingSurface) {
if (!m_textLayout) { // Empty Text element
m_drawingSurface = nullptr;
} else {
// Create the surface just big enough to hold the formatted text block.
DWRITE_TEXT_METRICS metrics;
winrt::check_hresult(m_textLayout->GetMetrics(&metrics));
if (metrics.width == 0 || metrics.height == 0) {
m_drawingSurface = nullptr;
return;
}
winrt::Windows::Foundation::Size surfaceSize = {
m_layoutMetrics.frame.size.width * m_layoutMetrics.pointScaleFactor,
m_layoutMetrics.frame.size.height * m_layoutMetrics.pointScaleFactor};
m_drawingSurface = m_compContext.CreateDrawingSurfaceBrush(
surfaceSize,
winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
winrt::Windows::Graphics::DirectX::DirectXAlphaMode::Premultiplied);
}
DrawText();
// The surfaceBrush's size is based on the size the text takes up, which maybe smaller than the total visual
// So we need to align the brush within the visual to match the text alignment.
float horizAlignment{0.f};
/*
const auto &props = paragraphProps()
if (props.textAttributes.alignment) {
switch (*props.textAttributes.alignment) {
case facebook::react::TextAlignment::Center:
horizAlignment = 0.5f;
break;
case facebook::react::TextAlignment::Justified:
horizAlignment = 0.5f;
break;
case facebook::react::TextAlignment::Left:
horizAlignment = 0.f;
break;
case facebook::react::TextAlignment::Right:
horizAlignment = 1.f;
break;
// TODO use LTR values
case facebook::react::TextAlignment::Natural:
horizAlignment = 0.f;
break;
default:
assert(false);
}
}
*/
// TODO Using brush alignment to align the text makes it blurry...
if (m_drawingSurface) {
m_drawingSurface.HorizontalAlignmentRatio(horizAlignment);
m_drawingSurface.Stretch(winrt::Microsoft::ReactNative::Composition::Experimental::CompositionStretch::None);
}
Visual().as<Experimental::ISpriteVisual>().Brush(m_drawingSurface);
}
if (m_requireRedraw) {
DrawText();
}
}
facebook::react::SharedViewProps ParagraphComponentView::defaultProps() noexcept {
static auto const defaultProps = std::make_shared<facebook::react::ParagraphProps const>();
return defaultProps;
}
const facebook::react::ParagraphProps &ParagraphComponentView::paragraphProps() const noexcept {
return *std::static_pointer_cast<const facebook::react::ParagraphProps>(viewProps());
}
void ParagraphComponentView::onThemeChanged() noexcept {
DrawText();
Super::onThemeChanged();
}
// Renders the text into our composition surface
void ParagraphComponentView::DrawText() noexcept {
if (!m_drawingSurface || theme()->IsEmpty())
return;
if (m_layoutMetrics.frame.size.width == 0 || m_layoutMetrics.frame.size.height == 0) {
m_requireRedraw = false;
return;
}
POINT offset;
{
::Microsoft::ReactNative::Composition::AutoDrawDrawingSurface autoDraw(
m_drawingSurface, m_layoutMetrics.pointScaleFactor, &offset);
if (auto d2dDeviceContext = autoDraw.GetRenderTarget()) {
d2dDeviceContext->Clear(
viewProps()->backgroundColor ? theme()->D2DColor(*viewProps()->backgroundColor)
: D2D1::ColorF(D2D1::ColorF::Black, 0.0f));
const auto &props = paragraphProps();
RenderText(
*d2dDeviceContext,
*m_textLayout,
m_attributedStringBox.getValue(),
props.textAttributes,
{static_cast<float>(offset.x) + m_layoutMetrics.contentInsets.left,
static_cast<float>(offset.y) + m_layoutMetrics.contentInsets.top},
m_layoutMetrics.pointScaleFactor,
*theme());
if (!isnan(props.opacity)) {
Visual().Opacity(props.opacity);
}
}
m_requireRedraw = false;
}
}
std::string ParagraphComponentView::DefaultControlType() const noexcept {
return "text";
}
std::string ParagraphComponentView::DefaultAccessibleName() const noexcept {
return m_attributedStringBox.getValue().getString();
}
winrt::Microsoft::ReactNative::ComponentView ParagraphComponentView::Create(
const winrt::Microsoft::ReactNative::Composition::Experimental::ICompositionContext &compContext,
facebook::react::Tag tag,
winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept {
return winrt::make<ParagraphComponentView>(compContext, tag, reactContext);
}
} // namespace winrt::Microsoft::ReactNative::Composition::implementation