diff --git a/lib/ui/text.dart b/lib/ui/text.dart index 66b6b8dcf33f437ccb0ad7e0d5f6544a68c5ebeb..19270c7ec74883d7d958a29f8090815baca26164 100644 --- a/lib/ui/text.dart +++ b/lib/ui/text.dart @@ -716,8 +716,8 @@ abstract class Paragraph extends NativeFieldWrapperClass2 { class ParagraphBuilder extends NativeFieldWrapperClass2 { /// Creates a [ParagraphBuilder] object, which is used to create a /// [Paragraph]. - ParagraphBuilder() { _constructor(); } - void _constructor() native "ParagraphBuilder_constructor"; + ParagraphBuilder(ParagraphStyle style) { _constructor(style._encoded, style._fontFamily, style._fontSize, style._lineHeight, style._ellipsis); } + void _constructor(Int32List encoded, String fontFamily, double fontSize, double lineHeight, String ellipsis) native "ParagraphBuilder_constructor"; /// Applies the given style to the added text until [pop] is called. /// @@ -742,6 +742,5 @@ class ParagraphBuilder extends NativeFieldWrapperClass2 { /// /// After calling this function, the paragraph builder object is invalid and /// cannot be used further. - Paragraph build(ParagraphStyle style) => _build(style._encoded, style._fontFamily, style._fontSize, style._lineHeight, style._ellipsis); - Paragraph _build(Int32List encoded, String fontFamily, double fontSize, double lineHeight, String ellipsis) native "ParagraphBuilder_build"; + Paragraph build() native "ParagraphBuilder_build"; } diff --git a/lib/ui/text/paragraph_builder.cc b/lib/ui/text/paragraph_builder.cc index 1de865d1ee6bc1117da66c356cc2808164241fcd..fde8d59a5b99e8915fd257fb8c0fbe427b5eb147 100644 --- a/lib/ui/text/paragraph_builder.cc +++ b/lib/ui/text/paragraph_builder.cc @@ -20,48 +20,6 @@ namespace blink { namespace { -RenderParagraph* createRenderParagraph(RenderStyle* parentStyle) { - RefPtr style = RenderStyle::create(); - style->inheritFrom(parentStyle); - style->setDisplay(PARAGRAPH); - - RenderParagraph* renderParagraph = new RenderParagraph(); - renderParagraph->setStyle(style.release()); - return renderParagraph; -} - -float getComputedSizeFromSpecifiedSize(float specifiedSize) { - if (specifiedSize < std::numeric_limits::epsilon()) - return 0.0f; - return specifiedSize; -} - -void createFontForDocument(RenderStyle* style) { - FontDescription fontDescription = FontDescription(); - fontDescription.setScript( - localeToScriptCodeForFontSelection(style->locale())); - - // Using 14px default to match Material Design English Body1: - // http://www.google.com/design/spec/style/typography.html#typography-typeface - const float defaultFontSize = 14.0; - - fontDescription.setSpecifiedSize(defaultFontSize); - fontDescription.setComputedSize(defaultFontSize); - - FontOrientation fontOrientation = Horizontal; - NonCJKGlyphOrientation glyphOrientation = NonCJKGlyphOrientationVerticalRight; - - fontDescription.setOrientation(fontOrientation); - fontDescription.setNonCJKGlyphOrientation(glyphOrientation); - style->setFontDescription(fontDescription); - style->font().update(UIDartState::Current()->font_selector()); -} - -Color getColorFromARGB(int argb) { - return Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, - (argb & 0x000000FF) >> 0, (argb & 0xFF000000) >> 24); -} - // TextStyle const int tsColorIndex = 1; @@ -109,6 +67,96 @@ const int psFontSizeMask = 1 << psFontSizeIndex; const int psLineHeightMask = 1 << psLineHeightIndex; const int psEllipsisMask = 1 << psEllipsisIndex; +float getComputedSizeFromSpecifiedSize(float specifiedSize) { + if (specifiedSize < std::numeric_limits::epsilon()) + return 0.0f; + return specifiedSize; +} + +void createFontForDocument(RenderStyle* style) { + FontDescription fontDescription = FontDescription(); + fontDescription.setScript( + localeToScriptCodeForFontSelection(style->locale())); + + // Using 14px default to match Material Design English Body1: + // http://www.google.com/design/spec/style/typography.html#typography-typeface + const float defaultFontSize = 14.0; + + fontDescription.setSpecifiedSize(defaultFontSize); + fontDescription.setComputedSize(defaultFontSize); + + FontOrientation fontOrientation = Horizontal; + NonCJKGlyphOrientation glyphOrientation = NonCJKGlyphOrientationVerticalRight; + + fontDescription.setOrientation(fontOrientation); + fontDescription.setNonCJKGlyphOrientation(glyphOrientation); + style->setFontDescription(fontDescription); + style->font().update(UIDartState::Current()->font_selector()); +} + +PassRefPtr decodeParagraphStyle( + RenderStyle* parentStyle, + tonic::Int32List& encoded, + const std::string& fontFamily, + double fontSize, + double lineHeight, + const std::string& ellipsis) { + FTL_DCHECK(encoded.num_elements() == 5); + + RefPtr style = RenderStyle::create(); + style->inheritFrom(parentStyle); + style->setDisplay(PARAGRAPH); + + int32_t mask = encoded[0]; + + if (mask & psTextAlignMask) + style->setTextAlign(static_cast(encoded[psTextAlignIndex])); + + if (mask & (psFontWeightMask | psFontStyleMask | psFontFamilyMask | + psFontSizeMask)) { + FontDescription fontDescription = style->fontDescription(); + + if (mask & psFontWeightMask) + fontDescription.setWeight( + static_cast(encoded[psFontWeightIndex])); + + if (mask & psFontStyleMask) + fontDescription.setStyle( + static_cast(encoded[psFontStyleIndex])); + + if (mask & psFontFamilyMask) { + FontFamily family; + family.setFamily(String::fromUTF8(fontFamily)); + fontDescription.setFamily(family); + } + + if (mask & psFontSizeMask) { + fontDescription.setSpecifiedSize(fontSize); + fontDescription.setIsAbsoluteSize(true); + fontDescription.setComputedSize( + getComputedSizeFromSpecifiedSize(fontSize)); + } + + style->setFontDescription(fontDescription); + style->font().update(UIDartState::Current()->font_selector()); + } + + if (mask & psLineHeightMask) + style->setLineHeight(Length(lineHeight * 100.0, Percent)); + + if (mask & psEllipsisMask) { + style->setEllipsis(AtomicString::fromUTF8(ellipsis.c_str())); + style->setWordBreak(BreakAllWordBreak); + } + + return style.release(); +} + +Color getColorFromARGB(int argb) { + return Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, + (argb & 0x000000FF) >> 0, (argb & 0xFF000000) >> 24); +} + } // namespace static void ParagraphBuilder_constructor(Dart_NativeArguments args) { @@ -127,13 +175,34 @@ FOR_EACH_BINDING(DART_NATIVE_CALLBACK) void ParagraphBuilder::RegisterNatives(tonic::DartLibraryNatives* natives) { natives->Register( - {{"ParagraphBuilder_constructor", ParagraphBuilder_constructor, 1, true}, + {{"ParagraphBuilder_constructor", ParagraphBuilder_constructor, 6, true}, FOR_EACH_BINDING(DART_REGISTER_NATIVE)}); } -ParagraphBuilder::ParagraphBuilder() { +ftl::RefPtr ParagraphBuilder::create( + tonic::Int32List& encoded, + const std::string& fontFamily, + double fontSize, + double lineHeight, + const std::string& ellipsis) { + return ftl::MakeRefCounted( + encoded, fontFamily, fontSize, lineHeight, ellipsis); +} + +ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded, + const std::string& fontFamily, + double fontSize, + double lineHeight, + const std::string& ellipsis) { createRenderView(); - m_renderParagraph = createRenderParagraph(m_renderView->style()); + + RefPtr paragraphStyle = decodeParagraphStyle( + m_renderView->style(), encoded, fontFamily, fontSize, lineHeight, ellipsis); + encoded.Release(); + + m_renderParagraph = new RenderParagraph(); + m_renderParagraph->setStyle(paragraphStyle.release()); + m_currentRenderObject = m_renderParagraph; m_renderView->addChild(m_currentRenderObject); } @@ -240,60 +309,7 @@ void ParagraphBuilder::addText(const std::string& text) { m_currentRenderObject->addChild(renderText); } -ftl::RefPtr ParagraphBuilder::build(tonic::Int32List& encoded, - const std::string& fontFamily, - double fontSize, - double lineHeight, - const std::string& ellipsis) { - FTL_DCHECK(encoded.num_elements() == 5); - int32_t mask = encoded[0]; - - if (mask) { - RefPtr style = RenderStyle::clone(m_renderParagraph->style()); - - if (mask & psTextAlignMask) - style->setTextAlign(static_cast(encoded[psTextAlignIndex])); - - if (mask & (psFontWeightMask | psFontStyleMask | psFontFamilyMask | - psFontSizeMask)) { - FontDescription fontDescription = style->fontDescription(); - - if (mask & psFontWeightMask) - fontDescription.setWeight( - static_cast(encoded[psFontWeightIndex])); - - if (mask & psFontStyleMask) - fontDescription.setStyle( - static_cast(encoded[psFontStyleIndex])); - - if (mask & psFontFamilyMask) { - FontFamily family; - family.setFamily(String::fromUTF8(fontFamily)); - fontDescription.setFamily(family); - } - - if (mask & psFontSizeMask) { - fontDescription.setSpecifiedSize(fontSize); - fontDescription.setIsAbsoluteSize(true); - fontDescription.setComputedSize( - getComputedSizeFromSpecifiedSize(fontSize)); - } - - style->setFontDescription(fontDescription); - style->font().update(UIDartState::Current()->font_selector()); - } - - if (mask & psLineHeightMask) - style->setLineHeight(Length(lineHeight * 100.0, Percent)); - - if (mask & psEllipsisMask) - style->setEllipsis(AtomicString::fromUTF8(ellipsis.c_str())); - - m_renderParagraph->setStyle(style.release()); - } - - encoded.Release(); - +ftl::RefPtr ParagraphBuilder::build() { m_currentRenderObject = nullptr; return Paragraph::create(m_renderView.release()); } diff --git a/lib/ui/text/paragraph_builder.h b/lib/ui/text/paragraph_builder.h index 089dcbc4fcfbc7cde24efc728e38eb3dcc23ee1c..7a156f23c2d6d82161742ff790a0d9aed2aa42e2 100644 --- a/lib/ui/text/paragraph_builder.h +++ b/lib/ui/text/paragraph_builder.h @@ -21,9 +21,11 @@ class ParagraphBuilder : public ftl::RefCountedThreadSafe, FRIEND_MAKE_REF_COUNTED(ParagraphBuilder); public: - static ftl::RefPtr create() { - return ftl::MakeRefCounted(); - } + static ftl::RefPtr create(tonic::Int32List& encoded, + const std::string& fontFamily, + double fontSize, + double lineHeight, + const std::string& ellipsis); ~ParagraphBuilder() override; @@ -37,16 +39,16 @@ class ParagraphBuilder : public ftl::RefCountedThreadSafe, void addText(const std::string& text); - ftl::RefPtr build(tonic::Int32List& encoded, - const std::string& fontFamily, - double fontSize, - double lineHeight, - const std::string& ellipsis); + ftl::RefPtr build(); static void RegisterNatives(tonic::DartLibraryNatives* natives); private: - explicit ParagraphBuilder(); + explicit ParagraphBuilder(tonic::Int32List& encoded, + const std::string& fontFamily, + double fontSize, + double lineHeight, + const std::string& ellipsis); void createRenderView(); diff --git a/sky/engine/core/rendering/line/BreakingContextInlineHeaders.h b/sky/engine/core/rendering/line/BreakingContextInlineHeaders.h index c1fa515e907c7a13f59398d552d6ac7c8bce8e7f..d378d2b861baf140d8d1ebf7e5147631b6a5ced9 100644 --- a/sky/engine/core/rendering/line/BreakingContextInlineHeaders.h +++ b/sky/engine/core/rendering/line/BreakingContextInlineHeaders.h @@ -471,8 +471,8 @@ inline bool BreakingContext::handleText(WordMeasurements& wordMeasurements, bool float ellipsisWidth = 0; unsigned ellipsisBreakOffset = 0; if (ellipsizeMode) { + ASSERT(breakAll); ellipsisWidth = measureEllipsisWidth(renderText, font, m_blockStyle->ellipsis().string()); - breakAll = true; } if (m_renderTextInfo.m_text != renderText) {