Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vaev-driver: Handle correctly HTML/BODY backgrounds. #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Async::Task<> renderAsync(
};

auto media = constructMediaForRender(options.scale, imageSize);
auto [style, layout, paint, frags] = Vaev::Driver::render(*dom, media, {.small = imageSize});
auto [style, layout, paint, frags, canvasColor] = Vaev::Driver::render(*dom, media, {.small = imageSize});

auto image = Gfx::Surface::alloc(
imageSize.cast<isize>() * options.density.toDppx(),
Expand All @@ -166,7 +166,7 @@ Async::Task<> renderAsync(

Gfx::CpuCanvas g;
g.begin(*image);
g.clear(Gfx::WHITE);
g.clear(canvasColor);
g.scale(options.density.toDppx());
paint->paint(g);
if (options.wireframe)
Expand Down
8 changes: 6 additions & 2 deletions src/web/vaev-driver/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export struct RenderResult {
Rc<Layout::Box> layout;
Rc<Scene::Node> scenes;
Rc<Layout::Frag> frag;
Gfx::Color canvasColor;
};

export RenderResult render(Gc::Ref<Dom::Document> dom, Style::Media const& media, Layout::Viewport viewport) {
Expand All @@ -43,9 +44,11 @@ export RenderResult render(Gc::Ref<Dom::Document> dom, Style::Media const& media
Style::Computer computer{media, stylebook, fontBook};
computer.loadFontFaces();

auto canvasColor = Layout::BGSearch(computer, dom);

Layout::Tree tree = {
Layout::build(computer, dom),
viewport,
viewport
};

elapsed = Sys::now() - start;
Expand Down Expand Up @@ -79,7 +82,8 @@ export RenderResult render(Gc::Ref<Dom::Document> dom, Style::Media const& media
std::move(stylebook),
makeRc<Layout::Box>(std::move(tree.root)),
sceneRoot,
makeRc<Layout::Frag>(std::move(root))
makeRc<Layout::Frag>(std::move(root)),
canvasColor
};
}

Expand Down
55 changes: 55 additions & 0 deletions src/web/vaev-layout/backgroundCanvas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module;

#include <karm-image/loader.h>
#include <karm-text/loader.h>
#include <karm-text/prose.h>
#include <vaev-dom/document.h>
#include <vaev-style/computer.h>

export module Vaev.Layout:backgroundCanvas;

import :values;

namespace Vaev::Layout {

Gfx::Color ColorToGfx(Color color) {
if (auto isRes = color.is<Gfx::Color>()) {
return *isRes;
} else {
logWarn("color was not a Gfx color");
return Gfx::WHITE;
}
}

export Gfx::Color BGSearch(Style::Computer& c, Gc::Ref<Dom::Document> doc) {
auto el = doc->documentElement();
if (!el) {
return Gfx::WHITE;
}

auto style = c.computeFor(Style::Computed::initial(), *el);
if (style->backgrounds->color != Gfx::ALPHA) {
style->backgrounds.cow().color = Gfx::PINK;
return ColorToGfx(style->backgrounds->color);
}

for (auto child = el->firstChild(); child; child = child->nextSibling()) {
if (auto isRes = child->is<Dom::Element>()) {
if (isRes->tagName != Html::BODY) {
continue;
}

auto childStyle = c.computeFor(*style, *isRes);
if (childStyle->backgrounds->color != Gfx::ALPHA) {
childStyle->backgrounds.cow().color = Gfx::PINK;
return ColorToGfx(childStyle->backgrounds->color);
} else {
return Gfx::WHITE;
}
}
}

return Gfx::WHITE;
}

} // namespace Vaev::Layout
4 changes: 2 additions & 2 deletions src/web/vaev-layout/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ static void _buildElement(Style::Computer& c, Gc::Ref<Dom::Element> el, Box& par
}

auto style = c.computeFor(*parent.style, el);

auto font = _lookupFontface(c.fontBook, *style);

auto display = style->display;
Expand All @@ -295,12 +296,11 @@ static void _buildNode(Style::Computer& c, Gc::Ref<Dom::Node> node, Box& parent)
_buildElement(c, *el, parent);
} else if (auto text = node->is<Dom::Text>()) {
_buildRun(c, *text, parent);
} else if (auto doc = node->is<Dom::Document>()) {
_buildChildren(c, *doc, parent);
}
}

export Box build(Style::Computer& c, Gc::Ref<Dom::Document> doc) {

if (auto el = doc->documentElement()) {
auto style = c.computeFor(Style::Computed::initial(), *el);
auto font = _lookupFontface(c.fontBook, *style);
Expand Down
1 change: 1 addition & 0 deletions src/web/vaev-layout/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export import :replaced;
export import :table;
export import :values;
export import :writing;
export import :backgroundCanvas;
10 changes: 6 additions & 4 deletions src/web/vaev-view/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct View : public Ui::View<View> {
}

void paint(Gfx::Canvas& g, Math::Recti rect) override {
// Painting browser's viewport.
auto viewport = bound().size();
if (not _renderResult) {
auto media = _constructMedia(viewport);
Expand All @@ -82,10 +83,11 @@ struct View : public Ui::View<View> {
g.origin(bound().xy.cast<f64>());
g.clip(viewport);

auto [_, layout, paint, frag] = *_renderResult;
g.clear(rect, Gfx::WHITE);
auto [_, layout, paint, frag, canvasColor] = *_renderResult;
auto paintRect = rect.offset(-bound().xy);
g.clear(paintRect, canvasColor);

paint->paint(g, rect.offset(-bound().xy).cast<f64>());
paint->paint(g, paintRect.cast<f64>());
if (_props.wireframe)
Layout::wireframe(*frag, g);

Expand All @@ -100,7 +102,7 @@ struct View : public Ui::View<View> {
Math::Vec2i size(Math::Vec2i size, Ui::Hint) override {
// FIXME: This is wasteful, we should cache the result
auto media = _constructMedia(size);
auto [_, layout, _, frag] = Driver::render(*_dom, media, {.small = size.cast<Au>()});
auto [_, layout, _, frag, _] = Driver::render(*_dom, media, {.small = size.cast<Au>()});

return {
frag->metrics.borderBox().width.cast<isize>(),
Expand Down
65 changes: 65 additions & 0 deletions tests/css/colors/stupid-backgrounds.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<test name="html-body-bg">
<container>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<style>
body {
height: 100svh;
width: 100vw;
margin: 0;
}

html {
height: 100svh;
width: 100vw;
}

#ref {
height: 100svh;
width: 100vw;
background-color: #b98af1;
}
</style>
</head>

<body>
<slot />
</body>

</html>
</container>

<rendering>
<div id="ref" style="height: 100svh;width: 100vw;background-color: #b98af1;"></div>
</rendering>

<rendering>
<style>
html {
background-color: #b98af1;
}
</style>
</rendering>

<rendering>
<style>
body {
background-color: #b98af1;
}
</style>
</rendering>

<rendering>
<style>
html {
background-color: #b98af1;
}

body {
background-color: #ff2b8d;
display: none;
}
</style>
</rendering>
</test>