mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +00:00
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.
This commit is contained in:
parent
f150134de9
commit
ee567cdc3d
11 changed files with 75 additions and 15 deletions
|
@ -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)));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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"; }
|
||||
};
|
||||
|
|
|
@ -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.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -11,7 +11,5 @@ public:
|
|||
|
||||
virtual const char* class_name() const override { return "LayoutInline"; }
|
||||
|
||||
virtual void split_into_lines(LayoutBlock& container);
|
||||
|
||||
private:
|
||||
};
|
||||
|
|
|
@ -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.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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>);
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue