From 77264d3e94d99da86953162b4684e7fa952ead01 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 17 Sep 2015 00:26:19 -0700 Subject: [PATCH] Teach sky.Paragraph how to actually compute layout Previously layout was exiting early because the frame view was null. Now we actually compute some layout and paint the text. This patch makes paragraph_builder_test pass. --- sky/engine/bindings/scripts/dart_types.py | 8 ++--- sky/engine/core/core.gni | 1 + sky/engine/core/rendering/RenderView.cpp | 11 ++----- sky/engine/core/text/FontStyle.dart | 3 -- sky/engine/core/text/FontStyle.h | 2 +- sky/engine/core/text/FontWeight.h | 2 +- sky/engine/core/text/Paragraph.cpp | 26 +++++++++++------ sky/engine/core/text/Paragraph.h | 2 ++ sky/engine/core/text/TextDecoration.cpp | 33 +++++++++++++++++++++ sky/engine/core/text/TextDecoration.h | 9 ++++-- sky/engine/core/text/TextDecorationStyle.h | 2 +- sky/engine/core/text/TextStyle.cpp | 18 +++++++++--- sky/engine/core/text/TextStyle.h | 34 +++++++++++++++++----- 13 files changed, 110 insertions(+), 41 deletions(-) create mode 100644 sky/engine/core/text/TextDecoration.cpp diff --git a/sky/engine/bindings/scripts/dart_types.py b/sky/engine/bindings/scripts/dart_types.py index 5651eb419..f9aeef3b4 100644 --- a/sky/engine/bindings/scripts/dart_types.py +++ b/sky/engine/bindings/scripts/dart_types.py @@ -130,12 +130,12 @@ CPP_SPECIAL_CONVERSION_RULES = { 'FilterQuality': 'SkFilterQuality', 'PaintingStyle': 'SkPaint::Style', # TODO(abarth): Give these better C++ types. - 'FontStyle': 'int', - 'FontWeight': 'int', + 'FontStyle': 'FontStyle', + 'FontWeight': 'FontWeight', 'TextAlign': 'int', 'TextBaseline': 'int', - 'TextDecoration': 'int', - 'TextDecorationStyle': 'int', + 'TextDecoration': 'TextDecoration', + 'TextDecorationStyle': 'TextDecorationStyle', } diff --git a/sky/engine/core/core.gni b/sky/engine/core/core.gni index aec41fcf4..9e2e4e8cf 100644 --- a/sky/engine/core/core.gni +++ b/sky/engine/core/core.gni @@ -610,6 +610,7 @@ sky_core_files = [ "text/ParagraphStyle.h", "text/TextAlign.h", "text/TextBaseline.h", + "text/TextDecoration.cpp", "text/TextDecoration.h", "text/TextDecorationStyle.h", "text/TextStyle.cpp", diff --git a/sky/engine/core/rendering/RenderView.cpp b/sky/engine/core/rendering/RenderView.cpp index 04fe86032..d597a9356 100644 --- a/sky/engine/core/rendering/RenderView.cpp +++ b/sky/engine/core/rendering/RenderView.cpp @@ -106,13 +106,12 @@ bool RenderView::hitTest(const HitTestRequest& request, const HitTestLocation& l void RenderView::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit, LogicalExtentComputedValues& computedValues) const { - computedValues.m_extent = m_frameView ? LayoutUnit(viewLogicalHeight()) : logicalHeight; + computedValues.m_extent = logicalHeight; } void RenderView::updateLogicalWidth() { - if (m_frameView) - setLogicalWidth(viewLogicalWidth()); + setLogicalWidth(viewLogicalWidth()); } bool RenderView::isChildAllowed(RenderObject* child, RenderStyle*) const @@ -124,7 +123,7 @@ void RenderView::layout() { SubtreeLayoutScope layoutScope(*this); - bool relayoutChildren = (!m_frameView || width() != viewWidth() || height() != viewHeight()); + bool relayoutChildren = width() != viewWidth() || height() != viewHeight(); if (relayoutChildren) { layoutScope.setChildNeedsLayout(this); for (RenderObject* child = firstChild(); child; child = child->nextSibling()) { @@ -354,15 +353,11 @@ IntRect RenderView::documentRect() const int RenderView::viewHeight() const { - if (m_frameView) - return m_frameView->layoutSize().height(); return m_frameViewSize.height(); } int RenderView::viewWidth() const { - if (m_frameView) - return m_frameView->layoutSize().width(); return m_frameViewSize.width(); } diff --git a/sky/engine/core/text/FontStyle.dart b/sky/engine/core/text/FontStyle.dart index 679194cb4..86d3f12ed 100644 --- a/sky/engine/core/text/FontStyle.dart +++ b/sky/engine/core/text/FontStyle.dart @@ -11,7 +11,4 @@ enum FontStyle { /// Use glyphs designed for slanting italic, - - /// Use the upright glyphs but slant them during painting - oblique // TODO(abarth): Remove. We don't really support this value. } diff --git a/sky/engine/core/text/FontStyle.h b/sky/engine/core/text/FontStyle.h index eb7ee015f..d08799f95 100644 --- a/sky/engine/core/text/FontStyle.h +++ b/sky/engine/core/text/FontStyle.h @@ -12,7 +12,7 @@ namespace blink { template <> struct DartConverter - : public DartConverterEnum {}; + : public DartConverterEnum {}; } // namespace blink diff --git a/sky/engine/core/text/FontWeight.h b/sky/engine/core/text/FontWeight.h index dd7810261..00de7906a 100644 --- a/sky/engine/core/text/FontWeight.h +++ b/sky/engine/core/text/FontWeight.h @@ -12,7 +12,7 @@ namespace blink { template <> struct DartConverter - : public DartConverterEnum {}; + : public DartConverterEnum {}; } // namespace blink diff --git a/sky/engine/core/text/Paragraph.cpp b/sky/engine/core/text/Paragraph.cpp index 573f2bc71..83de6a82f 100644 --- a/sky/engine/core/text/Paragraph.cpp +++ b/sky/engine/core/text/Paragraph.cpp @@ -4,7 +4,9 @@ #include "sky/engine/core/text/ParagraphBuilder.h" +#include "sky/engine/core/rendering/PaintInfo.h" #include "sky/engine/core/rendering/style/RenderStyle.h" +#include "sky/engine/platform/graphics/GraphicsContext.h" namespace blink { @@ -19,46 +21,52 @@ Paragraph::~Paragraph() double Paragraph::width() { - return m_renderView->firstChildBox()->width(); + return firstChildBox()->width(); } double Paragraph::height() { - return m_renderView->firstChildBox()->height(); + return firstChildBox()->height(); } double Paragraph::minIntrinsicWidth() { - return 0.0; + return firstChildBox()->minPreferredLogicalWidth(); } double Paragraph::maxIntrinsicWidth() { - return 0.0; + return firstChildBox()->maxPreferredLogicalWidth(); } double Paragraph::alphabeticBaseline() { - return 0.0; + return firstChildBox()->firstLineBoxBaseline(FontBaselineOrAuto(AlphabeticBaseline)); } double Paragraph::ideographicBaseline() { - return 0.0; + return firstChildBox()->firstLineBoxBaseline(FontBaselineOrAuto(IdeographicBaseline)); } void Paragraph::layout() { LayoutUnit maxWidth = std::max(m_minWidth, m_maxWidth); LayoutUnit maxHeight = std::max(m_minHeight, m_maxHeight); - IntSize maxSize(maxWidth, maxHeight); - - m_renderView->setFrameViewSize(maxSize); + m_renderView->setFrameViewSize(IntSize(maxWidth, maxHeight)); m_renderView->layout(); } void Paragraph::paint(Canvas* canvas, const Offset& offset) { + // Very simplified painting to allow painting an arbitrary (layer-less) subtree. + GraphicsContext context(canvas->skCanvas()); + Vector layers; + PaintInfo paintInfo(&context, m_renderView->absoluteBoundingBoxRect(), m_renderView.get()); + LayoutPoint paintOffset(offset.sk_size.width(), offset.sk_size.height()); + m_renderView->RenderBox::paint(paintInfo, paintOffset, layers); + // Note we're ignoring any layers encountered. + // TODO(abarth): Remove the concept of RenderLayers. } } // namespace blink diff --git a/sky/engine/core/text/Paragraph.h b/sky/engine/core/text/Paragraph.h index cf64407e6..f7b733f82 100644 --- a/sky/engine/core/text/Paragraph.h +++ b/sky/engine/core/text/Paragraph.h @@ -48,6 +48,8 @@ public: RenderView* renderView() const { return m_renderView.get(); } private: + RenderBox* firstChildBox() const { return m_renderView->firstChildBox(); } + LayoutUnit m_minWidth; LayoutUnit m_maxWidth; LayoutUnit m_minHeight; diff --git a/sky/engine/core/text/TextDecoration.cpp b/sky/engine/core/text/TextDecoration.cpp new file mode 100644 index 000000000..fb19e5423 --- /dev/null +++ b/sky/engine/core/text/TextDecoration.cpp @@ -0,0 +1,33 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/engine/core/text/TextDecoration.h" + +namespace blink { + +static TextDecoration toTextDecoration(int index) { + switch (index) { + case 0: // none + return TextDecorationNone; + case 1: // underline + return TextDecorationUnderline; + case 2: // overline + return TextDecorationOverline; + case 3: // lineThrough + return TextDecorationLineThrough; + default: + return TextDecorationNone; + } +} + +TextDecoration DartConverter::FromArguments( + Dart_NativeArguments args, int index, Dart_Handle& exception) { + return toTextDecoration(DartConverterEnum::FromArguments(args, index, exception)); +} + +TextDecoration DartConverter::FromDart(Dart_Handle handle) { + return toTextDecoration(DartConverterEnum::FromDart(handle)); +} + +} // namespace blink diff --git a/sky/engine/core/text/TextDecoration.h b/sky/engine/core/text/TextDecoration.h index 400983955..e3cb6df24 100644 --- a/sky/engine/core/text/TextDecoration.h +++ b/sky/engine/core/text/TextDecoration.h @@ -11,8 +11,13 @@ namespace blink { template <> -struct DartConverter - : public DartConverterEnum {}; +struct DartConverter { + static TextDecoration FromArguments(Dart_NativeArguments args, + int index, + Dart_Handle& exception); + + static TextDecoration FromDart(Dart_Handle handle); +}; } // namespace blink diff --git a/sky/engine/core/text/TextDecorationStyle.h b/sky/engine/core/text/TextDecorationStyle.h index 1ae553f3d..9052daf0d 100644 --- a/sky/engine/core/text/TextDecorationStyle.h +++ b/sky/engine/core/text/TextDecorationStyle.h @@ -12,7 +12,7 @@ namespace blink { template <> struct DartConverter - : public DartConverterEnum {}; + : public DartConverterEnum {}; } // namespace blink diff --git a/sky/engine/core/text/TextStyle.cpp b/sky/engine/core/text/TextStyle.cpp index 199fc23fe..68cc7b1b2 100644 --- a/sky/engine/core/text/TextStyle.cpp +++ b/sky/engine/core/text/TextStyle.cpp @@ -10,12 +10,22 @@ TextStyle::TextStyle( SkColor color, const String& fontFamily, double fontSize, - int fontWeight, - int fontStyle, - const Vector& decoration, + FontWeight fontWeight, + FontStyle fontStyle, + const Vector& decoration, SkColor decorationColor, - int decorationStyle) + TextDecorationStyle decorationStyle) + : m_color(color) + , m_fontFamily(fontFamily) + , m_fontSize(fontSize) + , m_fontWeight(fontWeight) + , m_fontStyle(fontStyle) + , m_decoration(TextDecorationNone) + , m_decorationColor(decorationColor) + , m_decorationStyle(decorationStyle) { + for (const auto& d : decoration) + m_decoration |= d; } TextStyle::~TextStyle() diff --git a/sky/engine/core/text/TextStyle.h b/sky/engine/core/text/TextStyle.h index b114e5b9c..fb79e0eef 100644 --- a/sky/engine/core/text/TextStyle.h +++ b/sky/engine/core/text/TextStyle.h @@ -24,11 +24,11 @@ public: SkColor color = SK_ColorWHITE, const String& fontFamily = nullAtom, double fontSize = 0.0, - int fontWeight = 0, - int fontStyle = 0, - const Vector& decoration = Vector(), + FontWeight fontWeight = FontWeightNormal, + FontStyle fontStyle = FontStyleNormal, + const Vector& decoration = Vector(), SkColor decorationColor = SK_ColorWHITE, - int decorationStyle = 0 + TextDecorationStyle decorationStyle = TextDecorationStyleSolid ) { return adoptRef(new TextStyle( color, @@ -44,17 +44,35 @@ public: ~TextStyle() override; + SkColor color() const { return m_color; } + const String& fontFamily() const { return m_fontFamily; } + double fontSize() const { return m_fontSize; } + FontWeight fontWeight() const { return m_fontWeight; } + FontStyle fontStyle() const { return m_fontStyle; } + TextDecoration decoration() const { return m_decoration; } + SkColor decorationColor() const { return m_decorationColor; } + TextDecorationStyle decorationStyle() const { return m_decorationStyle; } + private: explicit TextStyle( SkColor color, const String& fontFamily, double fontSize, - int fontWeight, - int fontStyle, - const Vector& decoration, + FontWeight fontWeight, + FontStyle fontStyle, + const Vector& decoration, SkColor decorationColor, - int decorationStyle + TextDecorationStyle decorationStyle ); + + SkColor m_color; + String m_fontFamily; + double m_fontSize; + FontWeight m_fontWeight; + FontStyle m_fontStyle; + TextDecoration m_decoration; + SkColor m_decorationColor; + TextDecorationStyle m_decorationStyle; }; } // namespace blink