Open History to see the last commit
1#include "T3MarkdownTextShadowNode.h"
2#include "T3MarkdownTextRunShadowNode.h"
3#include <react/renderer/components/view/ViewShadowNode.h>
4#import <react/renderer/textlayoutmanager/RCTAttributedTextUtils.h>
5
6#include <algorithm>
7#include <cmath>
8
9namespace facebook::react {
10
11static constexpr Float ParagraphStyleEncodingOffset = 1000;
12static constexpr auto FileAttachmentNativeIdPrefix = "t3-file:";
13static constexpr auto SkillAttachmentNativeIdPrefix = "t3-skill:";
14
15static void applyParagraphStyles(
16 NSMutableAttributedString *attributedString,
17 const std::vector<T3MarkdownTextParagraphStyleRange> &styleRanges)
18{
19 for (const auto &styleRange : styleRanges) {
20 if (styleRange.length == 0 || styleRange.location >= attributedString.length) {
21 continue;
22 }
23
24 const NSRange markerRange = NSMakeRange(
25 styleRange.location,
26 MIN(styleRange.length, attributedString.length - styleRange.location));
27 const NSRange paragraphRange = [attributedString.string paragraphRangeForRange:markerRange];
28 const NSParagraphStyle *existingStyle =
29 [attributedString attribute:NSParagraphStyleAttributeName
30 atIndex:paragraphRange.location
31 effectiveRange:nil];
32 NSMutableParagraphStyle *paragraphStyle =
33 existingStyle ? [existingStyle mutableCopy] : [NSMutableParagraphStyle new];
34 paragraphStyle.firstLineHeadIndent = styleRange.firstLineHeadIndent;
35 paragraphStyle.headIndent = styleRange.headIndent;
36 paragraphStyle.paragraphSpacing = styleRange.paragraphSpacing;
37 paragraphStyle.tabStops = @[
38 [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft
39 location:styleRange.headIndent
40 options:@{}]
41 ];
42 paragraphStyle.defaultTabInterval = styleRange.headIndent;
43 [attributedString addAttribute:NSParagraphStyleAttributeName
44 value:paragraphStyle
45 range:paragraphRange];
46 }
47}
48
49static void applyAttachments(
50 NSMutableAttributedString *attributedString,
51 const std::vector<T3MarkdownTextAttachmentRange> &attachmentRanges)
52{
53 for (const auto &attachmentRange : attachmentRanges) {
54 if (attachmentRange.length == 0 || attachmentRange.location >= attributedString.length) {
55 continue;
56 }
57
58 NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
59 attachment.image = [[UIImage alloc] init];
60 const CGFloat attachmentSize = T3MarkdownTextAttachmentSize(attachmentRange);
61 attachment.bounds = CGRectMake(
62 0,
63 T3MarkdownTextAttachmentBaselineOffset(attachmentRange),
64 attachmentSize,
65 attachmentSize);
66 const NSRange range = NSMakeRange(
67 attachmentRange.location,
68 MIN(attachmentRange.length, attributedString.length - attachmentRange.location));
69 NSAttributedString *attachmentString =
70 [NSAttributedString attributedStringWithAttachment:attachment];
71 [attributedString replaceCharactersInRange:range withAttributedString:attachmentString];
72 }
73}
74
75T3MarkdownTextShadowNode::T3MarkdownTextShadowNode(
76 const ShadowNode& sourceShadowNode,
77 const ShadowNodeFragment& fragment
78) : ConcreteViewShadowNode(sourceShadowNode, fragment) {
79};
80
81Size T3MarkdownTextShadowNode::measureContent(
82 const LayoutContext& layoutContext,
83 const LayoutConstraints& layoutConstraints) const {
84 const auto &baseProps = getConcreteProps();
85
86 auto baseTextAttributes = TextAttributes::defaultTextAttributes();
87 baseTextAttributes.backgroundColor = baseProps.backgroundColor;
88 baseTextAttributes.allowFontScaling = baseProps.allowFontScaling;
89
90 Float fontSizeMultiplier = 1.0;
91 if (baseTextAttributes.allowFontScaling) {
92 fontSizeMultiplier = layoutContext.fontSizeMultiplier;
93 }
94
95 auto baseAttributedString = AttributedString{};
96 auto paragraphStyleRanges = std::vector<T3MarkdownTextParagraphStyleRange>{};
97 auto attachmentRanges = std::vector<T3MarkdownTextAttachmentRange>{};
98 size_t utf16Offset = 0;
99 const auto &children = getChildren();
100 for (size_t i = 0; i < children.size(); i++) {
101 const auto child = children[i].get();
102 if (auto textViewChild = dynamic_cast<const T3MarkdownTextRunShadowNode *>(child)) {
103 auto &props = textViewChild->getConcreteProps();
104 auto fragment = AttributedString::Fragment{};
105 auto textAttributes = TextAttributes::defaultTextAttributes();
106
107 textAttributes.allowFontScaling = baseProps.allowFontScaling;
108 textAttributes.backgroundColor = props.backgroundColor;
109 textAttributes.fontSize = props.fontSize * fontSizeMultiplier;
110 textAttributes.lineHeight = props.lineHeight * fontSizeMultiplier;
111 textAttributes.foregroundColor = props.color;
112 const bool hasParagraphStyle = props.shadowRadius >= ParagraphStyleEncodingOffset;
113 if (!hasParagraphStyle) {
114 textAttributes.textShadowColor = props.shadowColor;
115 textAttributes.textShadowOffset = props.shadowOffset;
116 textAttributes.textShadowRadius = props.shadowRadius;
117 }
118 textAttributes.letterSpacing = props.letterSpacing;
119 textAttributes.textDecorationColor = props.textDecorationColor;
120 textAttributes.fontFamily = props.fontFamily;
121
122 if (props.fontStyle == T3MarkdownTextRunFontStyle::Italic) {
123 textAttributes.fontStyle = FontStyle::Italic;
124 } else {
125 textAttributes.fontStyle = FontStyle::Normal;
126 }
127
128 if (props.fontWeight == T3MarkdownTextRunFontWeight::Bold) {
129 textAttributes.fontWeight = FontWeight::Bold;
130 } else if (props.fontWeight == T3MarkdownTextRunFontWeight::UltraLight) {
131 textAttributes.fontWeight = FontWeight::UltraLight;
132 } else if (props.fontWeight == T3MarkdownTextRunFontWeight::Light) {
133 textAttributes.fontWeight = FontWeight::Light;
134 } else if (props.fontWeight == T3MarkdownTextRunFontWeight::Medium) {
135 textAttributes.fontWeight = FontWeight::Medium;
136 } else if (props.fontWeight == T3MarkdownTextRunFontWeight::Semibold) {
137 textAttributes.fontWeight = FontWeight::Semibold;
138 } else if (props.fontWeight == T3MarkdownTextRunFontWeight::Heavy) {
139 textAttributes.fontWeight = FontWeight::Heavy;
140 } else {
141 textAttributes.fontWeight = FontWeight::Regular;
142 }
143
144 if (props.textDecorationLine == T3MarkdownTextRunTextDecorationLine::LineThrough) {
145 textAttributes.textDecorationLineType = TextDecorationLineType::Strikethrough;
146 } else if (props.textDecorationLine == T3MarkdownTextRunTextDecorationLine::Underline) {
147 textAttributes.textDecorationLineType = TextDecorationLineType::Underline;
148 } else {
149 textAttributes.textDecorationLineType = TextDecorationLineType::None;
150 }
151
152 if (props.textDecorationStyle == T3MarkdownTextRunTextDecorationStyle::Solid) {
153 textAttributes.textDecorationStyle = TextDecorationStyle::Solid;
154 } else if (props.textDecorationStyle == T3MarkdownTextRunTextDecorationStyle::Dotted) {
155 textAttributes.textDecorationStyle = TextDecorationStyle::Dotted;
156 } else if (props.textDecorationStyle == T3MarkdownTextRunTextDecorationStyle::Dashed) {
157 textAttributes.textDecorationStyle = TextDecorationStyle::Dashed;
158 } else if (props.textDecorationStyle == T3MarkdownTextRunTextDecorationStyle::Double) {
159 textAttributes.textDecorationStyle = TextDecorationStyle::Double;
160 }
161
162 if (props.textAlign == T3MarkdownTextRunTextAlign::Left) {
163 textAttributes.alignment = TextAlignment::Left;
164 } else if (props.textAlign == T3MarkdownTextRunTextAlign::Right) {
165 textAttributes.alignment = TextAlignment::Right;
166 } else if (props.textAlign == T3MarkdownTextRunTextAlign::Center) {
167 textAttributes.alignment = TextAlignment::Center;
168 } else if (props.textAlign == T3MarkdownTextRunTextAlign::Justify) {
169 textAttributes.alignment = TextAlignment::Justified;
170 } else if (props.textAlign == T3MarkdownTextRunTextAlign::Auto) {
171 textAttributes.alignment = TextAlignment::Natural;
172 }
173
174 textAttributes.backgroundColor = props.backgroundColor;
175
176 fragment.string = props.text;
177 fragment.textAttributes = textAttributes;
178
179 NSString *fragmentText = [NSString stringWithUTF8String:props.text.c_str()];
180 const size_t fragmentLength = fragmentText.length;
181 if (hasParagraphStyle) {
182 paragraphStyleRanges.push_back(T3MarkdownTextParagraphStyleRange{
183 utf16Offset,
184 fragmentLength,
185 props.shadowOffset.width,
186 props.shadowOffset.height,
187 props.shadowRadius - ParagraphStyleEncodingOffset,
188 });
189 }
190 if (props.nativeId.rfind(FileAttachmentNativeIdPrefix, 0) == 0 && fragmentLength > 0) {
191 attachmentRanges.push_back(T3MarkdownTextAttachmentRange{
192 utf16Offset,
193 1,
194 props.nativeId.substr(std::char_traits<char>::length(FileAttachmentNativeIdPrefix)),
195 });
196 } else if (
197 props.nativeId.rfind(SkillAttachmentNativeIdPrefix, 0) == 0 && fragmentLength > 0) {
198 attachmentRanges.push_back(T3MarkdownTextAttachmentRange{
199 utf16Offset,
200 1,
201 props.nativeId.substr(
202 std::char_traits<char>::length(SkillAttachmentNativeIdPrefix)),
203 });
204 }
205 utf16Offset += fragmentLength;
206 baseAttributedString.appendFragment(std::move(fragment));
207 }
208 }
209
210 _attributedString = baseAttributedString;
211 _paragraphStyleRanges = paragraphStyleRanges;
212 _attachmentRanges = attachmentRanges;
213
214 NSMutableAttributedString *convertedAttributedString =
215 [RCTNSAttributedStringFromAttributedString(baseAttributedString) mutableCopy];
216 applyParagraphStyles(convertedAttributedString, paragraphStyleRanges);
217 applyAttachments(convertedAttributedString, attachmentRanges);
218
219 const CGFloat maximumWidth = std::isfinite(layoutConstraints.maximumSize.width)
220 ? layoutConstraints.maximumSize.width
221 : CGFLOAT_MAX;
222 NSTextStorage *textStorage =
223 [[NSTextStorage alloc] initWithAttributedString:convertedAttributedString];
224 NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
225 layoutManager.usesFontLeading = NO;
226 NSTextContainer *textContainer =
227 [[NSTextContainer alloc] initWithSize:CGSizeMake(maximumWidth, CGFLOAT_MAX)];
228 textContainer.lineFragmentPadding = 0;
229 textContainer.maximumNumberOfLines = baseProps.numberOfLines;
230 if (baseProps.ellipsizeMode == T3MarkdownTextEllipsizeMode::Head) {
231 textContainer.lineBreakMode = NSLineBreakByTruncatingHead;
232 } else if (baseProps.ellipsizeMode == T3MarkdownTextEllipsizeMode::Middle) {
233 textContainer.lineBreakMode = NSLineBreakByTruncatingMiddle;
234 } else if (baseProps.ellipsizeMode == T3MarkdownTextEllipsizeMode::Tail) {
235 textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
236 } else {
237 textContainer.lineBreakMode = NSLineBreakByClipping;
238 }
239 [layoutManager addTextContainer:textContainer];
240 [textStorage addLayoutManager:layoutManager];
241 [layoutManager ensureLayoutForTextContainer:textContainer];
242 const CGRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
243
244 return {
245 std::clamp(
246 static_cast<Float>(std::ceil(usedRect.size.width)),
247 layoutConstraints.minimumSize.width,
248 layoutConstraints.maximumSize.width),
249 std::clamp(
250 static_cast<Float>(std::ceil(usedRect.size.height)),
251 layoutConstraints.minimumSize.height,
252 layoutConstraints.maximumSize.height),
253 };
254}
255
256void T3MarkdownTextShadowNode::layout(LayoutContext layoutContext) {
257 ensureUnsealed();
258 setStateData(T3MarkdownTextStateReal{
259 _attributedString,
260 _paragraphStyleRanges,
261 _attachmentRanges,
262 });
263}
264}