#include "T3SourceTextShadowNode.h" #include "T3SourceTextRunShadowNode.h" #include #import #include #include namespace facebook::react { T3SourceTextShadowNode::T3SourceTextShadowNode( const ShadowNode &sourceShadowNode, const ShadowNodeFragment &fragment) : ConcreteViewShadowNode(sourceShadowNode, fragment){}; Size T3SourceTextShadowNode::measureContent( const LayoutContext &layoutContext, const LayoutConstraints &layoutConstraints) const { const auto &baseProps = getConcreteProps(); auto baseTextAttributes = TextAttributes::defaultTextAttributes(); baseTextAttributes.backgroundColor = baseProps.backgroundColor; baseTextAttributes.allowFontScaling = baseProps.allowFontScaling; Float fontSizeMultiplier = 1.0; if (baseTextAttributes.allowFontScaling) { fontSizeMultiplier = layoutContext.fontSizeMultiplier; } auto baseAttributedString = AttributedString{}; const auto &children = getChildren(); for (size_t i = 0; i < children.size(); i++) { const auto child = children[i].get(); if (auto textViewChild = dynamic_cast(child)) { auto &props = textViewChild->getConcreteProps(); auto fragment = AttributedString::Fragment{}; auto textAttributes = TextAttributes::defaultTextAttributes(); textAttributes.allowFontScaling = baseProps.allowFontScaling; textAttributes.backgroundColor = props.backgroundColor; textAttributes.fontSize = props.fontSize * fontSizeMultiplier; textAttributes.lineHeight = props.lineHeight * fontSizeMultiplier; textAttributes.foregroundColor = props.color; textAttributes.textShadowColor = props.shadowColor; textAttributes.textShadowOffset = props.shadowOffset; textAttributes.textShadowRadius = props.shadowRadius; textAttributes.letterSpacing = props.letterSpacing; textAttributes.textDecorationColor = props.textDecorationColor; textAttributes.fontFamily = props.fontFamily; if (props.fontStyle == T3SourceTextRunFontStyle::Italic) { textAttributes.fontStyle = FontStyle::Italic; } else { textAttributes.fontStyle = FontStyle::Normal; } if (props.fontWeight == T3SourceTextRunFontWeight::Bold) { textAttributes.fontWeight = FontWeight::Bold; } else if (props.fontWeight == T3SourceTextRunFontWeight::UltraLight) { textAttributes.fontWeight = FontWeight::UltraLight; } else if (props.fontWeight == T3SourceTextRunFontWeight::Light) { textAttributes.fontWeight = FontWeight::Light; } else if (props.fontWeight == T3SourceTextRunFontWeight::Medium) { textAttributes.fontWeight = FontWeight::Medium; } else if (props.fontWeight == T3SourceTextRunFontWeight::Semibold) { textAttributes.fontWeight = FontWeight::Semibold; } else if (props.fontWeight == T3SourceTextRunFontWeight::Heavy) { textAttributes.fontWeight = FontWeight::Heavy; } else { textAttributes.fontWeight = FontWeight::Regular; } if (props.textDecorationLine == T3SourceTextRunTextDecorationLine::LineThrough) { textAttributes.textDecorationLineType = TextDecorationLineType::Strikethrough; } else if (props.textDecorationLine == T3SourceTextRunTextDecorationLine::Underline) { textAttributes.textDecorationLineType = TextDecorationLineType::Underline; } else { textAttributes.textDecorationLineType = TextDecorationLineType::None; } if (props.textDecorationStyle == T3SourceTextRunTextDecorationStyle::Solid) { textAttributes.textDecorationStyle = TextDecorationStyle::Solid; } else if (props.textDecorationStyle == T3SourceTextRunTextDecorationStyle::Dotted) { textAttributes.textDecorationStyle = TextDecorationStyle::Dotted; } else if (props.textDecorationStyle == T3SourceTextRunTextDecorationStyle::Dashed) { textAttributes.textDecorationStyle = TextDecorationStyle::Dashed; } else if (props.textDecorationStyle == T3SourceTextRunTextDecorationStyle::Double) { textAttributes.textDecorationStyle = TextDecorationStyle::Double; } if (props.textAlign == T3SourceTextRunTextAlign::Left) { textAttributes.alignment = TextAlignment::Left; } else if (props.textAlign == T3SourceTextRunTextAlign::Right) { textAttributes.alignment = TextAlignment::Right; } else if (props.textAlign == T3SourceTextRunTextAlign::Center) { textAttributes.alignment = TextAlignment::Center; } else if (props.textAlign == T3SourceTextRunTextAlign::Justify) { textAttributes.alignment = TextAlignment::Justified; } else if (props.textAlign == T3SourceTextRunTextAlign::Auto) { textAttributes.alignment = TextAlignment::Natural; } textAttributes.backgroundColor = props.backgroundColor; fragment.string = props.text; fragment.textAttributes = textAttributes; baseAttributedString.appendFragment(std::move(fragment)); } } _attributedString = baseAttributedString; NSMutableAttributedString *convertedAttributedString = [RCTNSAttributedStringFromAttributedString(baseAttributedString) mutableCopy]; // Match React Native's baseline offset in both measurement and rendering. RCTApplyBaselineOffset(convertedAttributedString); const CGFloat maximumWidth = std::isfinite(layoutConstraints.maximumSize.width) ? layoutConstraints.maximumSize.width : CGFLOAT_MAX; NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:convertedAttributedString]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; layoutManager.usesFontLeading = NO; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(maximumWidth, CGFLOAT_MAX)]; textContainer.lineFragmentPadding = 0; textContainer.maximumNumberOfLines = baseProps.numberOfLines; if (baseProps.ellipsizeMode == T3SourceTextEllipsizeMode::Head) { textContainer.lineBreakMode = NSLineBreakByTruncatingHead; } else if (baseProps.ellipsizeMode == T3SourceTextEllipsizeMode::Middle) { textContainer.lineBreakMode = NSLineBreakByTruncatingMiddle; } else if (baseProps.ellipsizeMode == T3SourceTextEllipsizeMode::Tail) { textContainer.lineBreakMode = NSLineBreakByTruncatingTail; } else { textContainer.lineBreakMode = NSLineBreakByClipping; } [layoutManager addTextContainer:textContainer]; [textStorage addLayoutManager:layoutManager]; [layoutManager ensureLayoutForTextContainer:textContainer]; const CGRect usedRect = [layoutManager usedRectForTextContainer:textContainer]; return { std::clamp(static_cast(std::ceil(usedRect.size.width)), layoutConstraints.minimumSize.width, layoutConstraints.maximumSize.width), std::clamp(static_cast(std::ceil(usedRect.size.height)), layoutConstraints.minimumSize.height, layoutConstraints.maximumSize.height), }; } void T3SourceTextShadowNode::layout(LayoutContext layoutContext) { ensureUnsealed(); setStateData(T3SourceTextStateReal{ _attributedString, }); } } // namespace facebook::react