Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3MarkdownTextShadowNode.mm

275 lines12.3 KB

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