Browse Source

LibHTML: Implement basic layout for inline <img alt>

LayoutReplaced objects can now participate in inline layout.

It's very hackish, but basically LayoutReplaced will just add itself to
the last line in the containing block.

This patch gets rid of the idea that only LayoutInline subclasses can
be split into lines, by moving the split_into_lines() virtual up to
LayoutNode and overriding it in LayoutReplaced.
Andreas Kling 5 years ago
parent
commit
ee567cdc3d

+ 14 - 0
Libraries/LibHTML/DOM/HTMLImageElement.cpp

@@ -1,4 +1,6 @@
+#include <LibHTML/CSS/StyleResolver.h>
 #include <LibHTML/DOM/HTMLImageElement.h>
+#include <LibHTML/Layout/LayoutImage.h>
 
 HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
     : HTMLElement(document, tag_name)
@@ -8,3 +10,15 @@ HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
 HTMLImageElement::~HTMLImageElement()
 {
 }
+
+RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_style) const
+{
+    auto style = resolver.resolve_style(*this, parent_style);
+
+    auto display_property = style->property("display");
+    String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
+
+    if (display == "none")
+        return nullptr;
+    return adopt(*new LayoutImage(*this, move(style)));
+}

+ 3 - 0
Libraries/LibHTML/DOM/HTMLImageElement.h

@@ -9,4 +9,7 @@ public:
 
     String alt() const { return attribute("alt"); }
     String src() const { return attribute("src"); }
+
+private:
+    virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_style) const override;
 };

+ 4 - 1
Libraries/LibHTML/Layout/LayoutBlock.cpp

@@ -50,7 +50,7 @@ void LayoutBlock::layout_inline_children()
     m_line_boxes.clear();
     for_each_child([&](auto& child) {
         ASSERT(child.is_inline());
-        static_cast<LayoutInline&>(child).split_into_lines(*this);
+        child.split_into_lines(*this);
     });
 
     int content_height = 0;
@@ -65,6 +65,9 @@ void LayoutBlock::layout_inline_children()
             // FIXME: Support other kinds of vertical alignment.
             fragment.rect().set_x(rect().x() + fragment.rect().x());
             fragment.rect().set_y(rect().y() + content_height + (max_height - fragment.rect().height()));
+
+            if (fragment.layout_node().is_replaced())
+                const_cast<LayoutNode&>(fragment.layout_node()).set_rect(fragment.rect());
         }
 
         content_height += max_height;

+ 19 - 0
Libraries/LibHTML/Layout/LayoutImage.cpp

@@ -1,3 +1,6 @@
+#include <LibDraw/Font.h>
+#include <LibDraw/StylePainter.h>
+#include <LibGUI/GPainter.h>
 #include <LibHTML/Layout/LayoutImage.h>
 
 LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style)
@@ -11,10 +14,26 @@ LayoutImage::~LayoutImage()
 
 void LayoutImage::layout()
 {
+    if (renders_as_alt_text()) {
+        auto& font = Font::default_font();
+        rect().set_width(font.width(node().alt()) + 16);
+        rect().set_height(font.glyph_height() + 16);
+    }
+
     LayoutReplaced::layout();
 }
 
 void LayoutImage::render(RenderingContext& context)
 {
+    if (renders_as_alt_text()) {
+        context.painter().set_font(Font::default_font());
+        StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2);
+        context.painter().draw_text(rect(), node().alt(), TextAlignment::Center, Color::White);
+    }
     LayoutReplaced::render(context);
 }
+
+bool LayoutImage::renders_as_alt_text() const
+{
+    return true;
+}

+ 4 - 0
Libraries/LibHTML/Layout/LayoutImage.h

@@ -14,5 +14,9 @@ public:
     virtual void render(RenderingContext&) override;
 
     const HTMLImageElement& node() const { return static_cast<const HTMLImageElement&>(LayoutReplaced::node()); }
+
+    bool renders_as_alt_text() const;
+
 private:
+    virtual const char* class_name() const override { return "LayoutImage"; }
 };

+ 0 - 11
Libraries/LibHTML/Layout/LayoutInline.cpp

@@ -10,14 +10,3 @@ LayoutInline::LayoutInline(const Node& node, RefPtr<StyleProperties> style_prope
 LayoutInline::~LayoutInline()
 {
 }
-
-void LayoutInline::split_into_lines(LayoutBlock& container)
-{
-    for_each_child([&](auto& child) {
-        if (child.is_inline()) {
-            static_cast<LayoutInline&>(child).split_into_lines(container);
-        } else {
-            // FIXME: Support block children of inlines.
-        }
-    });
-}

+ 0 - 2
Libraries/LibHTML/Layout/LayoutInline.h

@@ -11,7 +11,5 @@ public:
 
     virtual const char* class_name() const override { return "LayoutInline"; }
 
-    virtual void split_into_lines(LayoutBlock& container);
-
 private:
 };

+ 11 - 0
Libraries/LibHTML/Layout/LayoutNode.cpp

@@ -116,3 +116,14 @@ const Document& LayoutNode::document() const
         return parent()->document();
     return node()->document();
 }
+
+void LayoutNode::split_into_lines(LayoutBlock& container)
+{
+    for_each_child([&](auto& child) {
+        if (child.is_inline()) {
+            child.split_into_lines(container);
+        } else {
+            // FIXME: Support block children of inlines.
+        }
+    });
+}

+ 4 - 0
Libraries/LibHTML/Layout/LayoutNode.h

@@ -12,6 +12,7 @@ class Document;
 class Element;
 class LayoutBlock;
 class LayoutNode;
+class LineBoxFragment;
 class Node;
 
 struct HitTestResult {
@@ -53,6 +54,7 @@ public:
     virtual const char* class_name() const { return "LayoutNode"; }
     virtual bool is_text() const { return false; }
     virtual bool is_block() const { return false; }
+    virtual bool is_replaced() const { return false; }
 
     bool is_inline() const { return m_inline; }
     void set_inline(bool b) { m_inline = b; }
@@ -74,6 +76,8 @@ public:
     void inserted_into(LayoutNode&) {}
     void removed_from(LayoutNode&) {}
 
+    virtual void split_into_lines(LayoutBlock& container);
+
 protected:
     explicit LayoutNode(const Node*, RefPtr<StyleProperties>);
 

+ 10 - 0
Libraries/LibHTML/Layout/LayoutReplaced.cpp

@@ -1,4 +1,5 @@
 #include <LibHTML/DOM/Element.h>
+#include <LibHTML/Layout/LayoutBlock.h>
 #include <LibHTML/Layout/LayoutReplaced.h>
 
 LayoutReplaced::LayoutReplaced(const Element& element, NonnullRefPtr<StyleProperties> style)
@@ -11,3 +12,12 @@ LayoutReplaced::LayoutReplaced(const Element& element, NonnullRefPtr<StyleProper
 LayoutReplaced::~LayoutReplaced()
 {
 }
+
+void LayoutReplaced::split_into_lines(LayoutBlock& container)
+{
+    layout();
+
+    if (container.line_boxes().is_empty())
+        container.line_boxes().append(LineBox());
+    container.line_boxes().last().add_fragment(*this, 0, 0, rect().width(), rect().height());
+}

+ 6 - 1
Libraries/LibHTML/Layout/LayoutReplaced.h

@@ -4,9 +4,14 @@
 class LayoutReplaced : public LayoutNode {
 public:
     LayoutReplaced(const Element&, NonnullRefPtr<StyleProperties>);
-    virtual ~LayoutReplaced();
+    virtual ~LayoutReplaced() override;
 
     const Element& node() const { return static_cast<const Element&>(*LayoutNode::node()); }
 
+    virtual bool is_replaced() const final { return true; }
+
 private:
+    virtual const char* class_name() const override { return "LayoutReplaced"; }
+
+    virtual void split_into_lines(LayoutBlock& container) override;
 };