srctree

Robin Linden parent dfccc21f e8753c87
render: Render layouts using the correct background color

inlinesplit
browser/gui/app.cpp added: 56, removed: 5, total 51
@@ -313,7 +313,7 @@ void App::clear_render_surface() {
 
void App::render_layout() {
if (layout_) {
render::debug::render_layout_depth(painter_, *layout_);
render::render_layout(painter_, *layout_);
}
}
 
 
render/BUILD added: 56, removed: 5, total 51
@@ -12,5 +12,6 @@ cc_library(
deps = [
"//gfx",
"//layout",
"@spdlog",
],
)
 
render/render.cpp added: 56, removed: 5, total 51
@@ -4,6 +4,8 @@
 
#include "render/render.h"
 
#include "style/style.h"
 
// MSVC gl.h doesn't include everything it uses.
#ifdef _MSC_VER
#ifndef WIN32_LEAN_AND_MEAN
@@ -13,8 +15,45 @@
#endif // _MSC_VER
 
#include <GL/gl.h>
#include <spdlog/spdlog.h>
 
#include <charconv>
#include <cstdint>
 
namespace render {
namespace {
 
bool looks_like_hex(std::string_view str) {
return str.starts_with('#') && str.length() == 7;
}
 
gfx::Color from_hex_chars(std::string_view hex_chars) {
hex_chars.remove_prefix(1);
std::int32_t hex{};
std::from_chars(hex_chars.data(), hex_chars.data() + hex_chars.size(), hex, /*base*/ 16);
return gfx::Color::from_rgb(hex);
}
 
gfx::Color parse_color(std::string_view str) {
if (looks_like_hex(str)) {
return from_hex_chars(str);
}
 
spdlog::warn("Unrecognized color format: {}", str);
return gfx::Color{0xFF, 0, 0};
}
 
void do_render(gfx::IPainter &painter, layout::LayoutBox const &layout) {
if (auto maybe_color = style::get_property(*layout.node, "background-color")) {
painter.fill_rect(layout.dimensions.content, parse_color(*maybe_color));
}
}
 
bool should_render(layout::LayoutBox const &layout) {
return layout.type == layout::LayoutType::Block || layout.type == layout::LayoutType::Inline;
}
 
} // namespace
 
void render_setup(int width, int height) {
glMatrixMode(GL_PROJECTION);
@@ -25,6 +64,16 @@ void render_setup(int width, int height) {
glLoadIdentity();
}
 
void render_layout(gfx::IPainter &painter, layout::LayoutBox const &layout) {
if (should_render(layout)) {
do_render(painter, layout);
}
 
for (auto const &child : layout.children) {
render_layout(painter, child);
}
}
 
namespace debug {
namespace {
void render_layout_depth_impl(gfx::IPainter &painter, layout::LayoutBox const &layout, int depth) {
 
render/render.h added: 56, removed: 5, total 51
@@ -11,6 +11,7 @@
namespace render {
 
void render_setup(int width, int height);
void render_layout(gfx::IPainter &, layout::LayoutBox const &);
 
namespace debug {
void render_layout_depth(gfx::IPainter &, layout::LayoutBox const &);