|
| 1 | +module; |
| 2 | + |
| 3 | +#include <karm-image/loader.h> |
| 4 | +#include <karm-text/loader.h> |
| 5 | +#include <karm-text/prose.h> |
| 6 | +#include <vaev-dom/document.h> |
| 7 | +#include <vaev-style/computer.h> |
| 8 | + |
| 9 | +export module Vaev.Layout:backgroundCanvas; |
| 10 | + |
| 11 | +import :values; |
| 12 | + |
| 13 | +namespace Vaev::Layout { |
| 14 | + |
| 15 | +static void _searchChildren(Style::Computer& c, Gc::Ref<Dom::Node> node, Box& parent, Color& canvasColor); |
| 16 | + |
| 17 | +static void extractBackground(TagName& tagname, Cow<BackgroundProps>& background, Color& canvasColor) { |
| 18 | + if (tagname == Html::HTML and background->color != Gfx::ALPHA) { |
| 19 | + canvasColor = background->color; |
| 20 | + background.cow().color = Gfx::ALPHA; |
| 21 | + } |
| 22 | + |
| 23 | + if (tagname == Html::BODY and canvasColor == Gfx::ALPHA) { |
| 24 | + if (background->color != Gfx::ALPHA) { |
| 25 | + canvasColor = background->color; |
| 26 | + background.cow().color = Gfx::ALPHA; |
| 27 | + } else { |
| 28 | + canvasColor = Gfx::WHITE; |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +static void _searchElement(Style::Computer& c, Gc::Ref<Dom::Element> el, Box& parent, Color& canvasColor) { |
| 34 | + if (el->tagName == Html::IMG) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + auto style = c.computeFor(*parent.style, el); |
| 39 | + |
| 40 | + auto display = style->display; |
| 41 | + extractBackground(el->tagName, style->backgrounds, canvasColor); |
| 42 | + |
| 43 | + if (display == Display::NONE) { |
| 44 | + // Do nothing |
| 45 | + } else if (display == Display::CONTENTS) { |
| 46 | + _searchChildren(c, el, parent, canvasColor); |
| 47 | + } else if (display == Display::TABLE) { |
| 48 | + return; |
| 49 | + } else { |
| 50 | + return; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static void _searchNode(Style::Computer& c, Gc::Ref<Dom::Node> node, Box& parent, Color& canvasColor) { |
| 55 | + if (auto el = node->is<Dom::Element>()) { |
| 56 | + _searchElement(c, *el, parent, canvasColor); |
| 57 | + } else if (auto text = node->is<Dom::Text>()) { |
| 58 | + return; |
| 59 | + } else if (auto doc = node->is<Dom::Document>()) { |
| 60 | + _searchChildren(c, *doc, parent, canvasColor); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void _searchChildren(Style::Computer& c, Gc::Ref<Dom::Node> node, Box& parent, Color& canvasColor) { |
| 65 | + for (auto child = node->firstChild(); child; child = child->nextSibling()) { |
| 66 | + _searchNode(c, *child, parent, canvasColor); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export Gfx::Color BGSearch(Style::Computer& c, Gc::Ref<Dom::Document> doc) { |
| 71 | + |
| 72 | + if (auto el = doc->documentElement()) { |
| 73 | + Color canvasColor = Gfx::ALPHA; |
| 74 | + auto style = c.computeFor(Style::Computed::initial(), *el); |
| 75 | + extractBackground(el->tagName, style->backgrounds, canvasColor); |
| 76 | + Box root = {style, Text::Fontface::fallback()}; |
| 77 | + _searchChildren(c, *el, root, canvasColor); |
| 78 | + |
| 79 | + Gfx::Color gfxCanvasCol; |
| 80 | + if (auto isRes = canvasColor.is<Gfx::Color>()) { |
| 81 | + gfxCanvasCol = *isRes; |
| 82 | + } else { |
| 83 | + logWarn("canvas color was not a Gfx color"); |
| 84 | + gfxCanvasCol = Gfx::WHITE; |
| 85 | + } |
| 86 | + return gfxCanvasCol; |
| 87 | + } |
| 88 | + // NOTE: Fallback in case of an empty document |
| 89 | + return Gfx::WHITE; |
| 90 | +} |
| 91 | +} // namespace Vaev::Layout |
0 commit comments