mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
LibHTML: Implement rendering
This commit is contained in:
parent
93003bfda1
commit
235dee8c42
Notes:
sideshowbarker
2024-07-19 11:55:46 +09:00
Author: https://github.com/bugaevc Commit: https://github.com/SerenityOS/serenity/commit/235dee8c421 Pull-request: https://github.com/SerenityOS/serenity/pull/609 Reviewed-by: https://github.com/awesomekling ✅
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
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…
Reference in New Issue
Block a user