LibHTML: Implement rendering

This commit is contained in:
Sergey Bugaev 2019-09-25 12:40:37 +03:00 committed by Andreas Kling
parent 93003bfda1
commit 235dee8c42
Notes: sideshowbarker 2024-07-19 11:55:46 +09:00
7 changed files with 62 additions and 0 deletions

View File

@ -1,3 +1,4 @@
#include <LibGUI/GPainter.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutBlock.h>
@ -143,3 +144,19 @@ void LayoutBlock::compute_height()
if (height_length.is_absolute())
rect().set_height(height_length.to_px());
}
void LayoutBlock::render(RenderingContext& context)
{
LayoutNode::render(context);
// FIXME: position this properly
if (style_properties().string_or_fallback("display", "block") == "list-item") {
Rect bullet_rect {
rect().x() - 8,
rect().y() + 4,
3,
3
};
context.painter().fill_rect(bullet_rect, Color::Black);
}
}

View File

@ -12,6 +12,7 @@ public:
virtual const char* class_name() const override { return "LayoutBlock"; }
virtual void layout() override;
virtual void render(RenderingContext&) override;
virtual LayoutNode& inline_wrapper() override;

View File

@ -26,3 +26,11 @@ const LayoutBlock* LayoutNode::containing_block() const
}
return nullptr;
}
void LayoutNode::render(RenderingContext& context)
{
// TODO: render our background and border
for_each_child([&](auto& child) {
child.render(context);
});
}

View File

@ -5,6 +5,7 @@
#include <LibDraw/Rect.h>
#include <LibHTML/CSS/StyleProperties.h>
#include <LibHTML/Layout/ComputedStyle.h>
#include <LibHTML/RenderingContext.h>
#include <LibHTML/TreeNode.h>
class Node;
@ -44,6 +45,7 @@ public:
virtual bool is_inline() const { return false; }
virtual void layout();
virtual void render(RenderingContext&);
const LayoutBlock* containing_block() const;

View File

@ -1,6 +1,7 @@
#include <AK/StringBuilder.h>
#include <LibCore/CDirIterator.h>
#include <LibDraw/Font.h>
#include <LibGUI/GPainter.h>
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutText.h>
#include <ctype.h>
@ -216,3 +217,19 @@ void LayoutText::layout()
rect().set_right(last_run.pos.x() + m_font->width(last_run.text));
rect().set_bottom(last_run.pos.y() + m_font->glyph_height());
}
void LayoutText::render(RenderingContext& context)
{
auto& painter = context.painter();
painter.set_font(*m_font);
for (auto& run : m_runs) {
Rect rect {
run.pos.x(),
run.pos.y(),
m_font->width(run.text),
m_font->glyph_height()
};
painter.draw_text(rect, run.text);
}
}

View File

@ -17,6 +17,7 @@ public:
virtual const char* class_name() const override { return "LayoutText"; }
virtual bool is_text() const final { return true; }
virtual void layout() override;
virtual void render(RenderingContext&) override;
struct Run {
Point pos;

View File

@ -0,0 +1,16 @@
#pragma once
class GPainter;
class RenderingContext {
public:
explicit RenderingContext(GPainter& painter)
: m_painter(painter)
{
}
GPainter& painter() const { return m_painter; }
private:
GPainter& m_painter;
};