mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
LibHTML: Implement rendering
This commit is contained in:
parent
93003bfda1
commit
235dee8c42
7 changed files with 62 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#include <LibGUI/GPainter.h>
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
|
|
||||||
|
@ -143,3 +144,19 @@ void LayoutBlock::compute_height()
|
||||||
if (height_length.is_absolute())
|
if (height_length.is_absolute())
|
||||||
rect().set_height(height_length.to_px());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ public:
|
||||||
virtual const char* class_name() const override { return "LayoutBlock"; }
|
virtual const char* class_name() const override { return "LayoutBlock"; }
|
||||||
|
|
||||||
virtual void layout() override;
|
virtual void layout() override;
|
||||||
|
virtual void render(RenderingContext&) override;
|
||||||
|
|
||||||
virtual LayoutNode& inline_wrapper() override;
|
virtual LayoutNode& inline_wrapper() override;
|
||||||
|
|
||||||
|
|
|
@ -26,3 +26,11 @@ const LayoutBlock* LayoutNode::containing_block() const
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LayoutNode::render(RenderingContext& context)
|
||||||
|
{
|
||||||
|
// TODO: render our background and border
|
||||||
|
for_each_child([&](auto& child) {
|
||||||
|
child.render(context);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <LibDraw/Rect.h>
|
#include <LibDraw/Rect.h>
|
||||||
#include <LibHTML/CSS/StyleProperties.h>
|
#include <LibHTML/CSS/StyleProperties.h>
|
||||||
#include <LibHTML/Layout/ComputedStyle.h>
|
#include <LibHTML/Layout/ComputedStyle.h>
|
||||||
|
#include <LibHTML/RenderingContext.h>
|
||||||
#include <LibHTML/TreeNode.h>
|
#include <LibHTML/TreeNode.h>
|
||||||
|
|
||||||
class Node;
|
class Node;
|
||||||
|
@ -44,6 +45,7 @@ public:
|
||||||
virtual bool is_inline() const { return false; }
|
virtual bool is_inline() const { return false; }
|
||||||
|
|
||||||
virtual void layout();
|
virtual void layout();
|
||||||
|
virtual void render(RenderingContext&);
|
||||||
|
|
||||||
const LayoutBlock* containing_block() const;
|
const LayoutBlock* containing_block() const;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/CDirIterator.h>
|
#include <LibCore/CDirIterator.h>
|
||||||
#include <LibDraw/Font.h>
|
#include <LibDraw/Font.h>
|
||||||
|
#include <LibGUI/GPainter.h>
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
#include <LibHTML/Layout/LayoutText.h>
|
#include <LibHTML/Layout/LayoutText.h>
|
||||||
#include <ctype.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_right(last_run.pos.x() + m_font->width(last_run.text));
|
||||||
rect().set_bottom(last_run.pos.y() + m_font->glyph_height());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ public:
|
||||||
virtual const char* class_name() const override { return "LayoutText"; }
|
virtual const char* class_name() const override { return "LayoutText"; }
|
||||||
virtual bool is_text() const final { return true; }
|
virtual bool is_text() const final { return true; }
|
||||||
virtual void layout() override;
|
virtual void layout() override;
|
||||||
|
virtual void render(RenderingContext&) override;
|
||||||
|
|
||||||
struct Run {
|
struct Run {
|
||||||
Point pos;
|
Point pos;
|
||||||
|
|
16
Libraries/LibHTML/RenderingContext.h
Normal file
16
Libraries/LibHTML/RenderingContext.h
Normal 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;
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue