diff --git a/src/libs/karm-gfx/canvas.cpp b/src/libs/karm-gfx/canvas.cpp index 6dc6e9f..aeb8773 100644 --- a/src/libs/karm-gfx/canvas.cpp +++ b/src/libs/karm-gfx/canvas.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include "canvas.h" @@ -116,10 +116,27 @@ void Canvas::fill(Text::Font &font, Text::Glyph glyph, Math::Vec2f baseline) { pop(); } -void Canvas::fill(Text::Font &font, Text::Run const &run, Math::Vec2f baseline) { +void Canvas::fill(Text::Prose &prose) { push(); - for (auto &cell : run._cells) - fill(font, cell.glyph, baseline + Math::Vec2f{cell.xpos, 0}); + + if (prose._style.color) + fillStyle(*prose._style.color); + + for (auto const &line : prose._lines) { + for (auto &block : line.blocks()) { + for (auto &cell : block.cells()) { + if (cell.span and cell.span->color) { + push(); + fillStyle(*cell.span->color); + fill(prose._style.font, cell.glyph, {block.pos + cell.pos, line.baseline}); + pop(); + } else { + fill(prose._style.font, cell.glyph, {block.pos + cell.pos, line.baseline}); + } + } + } + } + pop(); } diff --git a/src/libs/karm-gfx/canvas.h b/src/libs/karm-gfx/canvas.h index a4aa4f3..1a13398 100644 --- a/src/libs/karm-gfx/canvas.h +++ b/src/libs/karm-gfx/canvas.h @@ -163,7 +163,7 @@ struct Canvas : Meta::NoCopy { virtual void fill(Text::Font &font, Text::Glyph glyph, Math::Vec2f baseline); // Fill a run of text - virtual void fill(Text::Font &font, Text::Run const &run, Math::Vec2f baseline); + virtual void fill(Text::Prose &prose); // MARK: Clear Operations -------------------------------------------------- diff --git a/src/libs/karm-pdf/canvas.cpp b/src/libs/karm-pdf/canvas.cpp index 3458454..0d624b6 100644 --- a/src/libs/karm-pdf/canvas.cpp +++ b/src/libs/karm-pdf/canvas.cpp @@ -23,8 +23,8 @@ void Canvas::strokeStyle(Gfx::Stroke) { logDebug("pdf: strokeStyle() operation not implemented"); } -void Canvas::transform(Math::Trans2f) { - logDebug("pdf: transform() operation not implemented"); +void Canvas::transform(Math::Trans2f trans) { + _e.ln("{} {} {} {} {} {} cm", trans.xx, trans.xy, trans.yx, trans.yy, trans.ox, trans.oy); } // MARK: Path Operations --------------------------------------------------- @@ -70,7 +70,7 @@ void Canvas::quadTo(Math::Vec2f cp, Math::Vec2f p, Math::Path::Flags flags) { } void Canvas::arcTo(Math::Vec2f, f64, Math::Vec2f, Math::Path::Flags) { - notImplemented(); + logDebug("pdf: arcTo() operation not implemented"); } void Canvas::line(Math::Edgef line) { @@ -92,7 +92,7 @@ void Canvas::rect(Math::Rectf rect, Math::Radiif) { } void Canvas::ellipse(Math::Ellipsef) { - notImplemented(); + logDebug("pdf: ellipse() operation not implemented"); } void Canvas::path(Math::Path const &) { @@ -146,12 +146,6 @@ void Canvas::apply(Gfx::Filter) { logDebug("pdf: apply() operation not implemented"); }; -// MARK: Shape Operations -------------------------------------------------- - -void Canvas::fill(Text::Font &, Text::Glyph, Math::Vec2f) { - // logDebug("pdf: fill() operation not implemented"); -} - // MARK: Clear Operations -------------------------------------------------- void Canvas::clear(Gfx::Color) { diff --git a/src/libs/karm-pdf/canvas.h b/src/libs/karm-pdf/canvas.h index 2a94dd9..fe17e1d 100644 --- a/src/libs/karm-pdf/canvas.h +++ b/src/libs/karm-pdf/canvas.h @@ -86,10 +86,6 @@ struct Canvas : public Gfx::Canvas { void apply(Gfx::Filter filter) override; - // MARK: Shape Operations -------------------------------------------------- - - void fill(Text::Font &font, Text::Glyph glyph, Math::Vec2f baseline) override; - // MARK: Clear Operations -------------------------------------------------- void clear(Gfx::Color color) override; diff --git a/src/libs/karm-scene/text.h b/src/libs/karm-scene/text.h index b8822a7..894007d 100644 --- a/src/libs/karm-scene/text.h +++ b/src/libs/karm-scene/text.h @@ -23,7 +23,7 @@ struct Text : public Node { g.push(); g.origin(_origin); - _prose->paint(g); + g.fill(*_prose); g.pop(); } diff --git a/src/libs/karm-text/base.h b/src/libs/karm-text/base.h index 349f982..45cfb57 100644 --- a/src/libs/karm-text/base.h +++ b/src/libs/karm-text/base.h @@ -7,7 +7,7 @@ namespace Karm::Text { -struct Run; +struct Prose; struct Font; diff --git a/src/libs/karm-text/prose.cpp b/src/libs/karm-text/prose.cpp index 439c5f5..2a971dd 100644 --- a/src/libs/karm-text/prose.cpp +++ b/src/libs/karm-text/prose.cpp @@ -188,30 +188,4 @@ Math::Vec2f Prose::layout(f64 width) { return {textWidth, textHeight}; } -// MARK: Paint ------------------------------------------------------------- - -void Prose::paint(Gfx::Canvas &g) { - g.push(); - - if (_style.color) - g.fillStyle(*_style.color); - - for (auto const &line : _lines) { - for (auto &block : line.blocks()) { - for (auto &cell : block.cells()) { - if (cell.span and cell.span->color) { - g.push(); - g.fillStyle(*cell.span->color); - g.fill(_style.font, cell.glyph, {block.pos + cell.pos, line.baseline}); - g.pop(); - } else { - g.fill(_style.font, cell.glyph, {block.pos + cell.pos, line.baseline}); - } - } - } - } - - g.pop(); -} - } // namespace Karm::Text diff --git a/src/libs/karm-text/prose.h b/src/libs/karm-text/prose.h index 8bcd7bf..95c67cd 100644 --- a/src/libs/karm-text/prose.h +++ b/src/libs/karm-text/prose.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include "font.h" @@ -218,8 +217,6 @@ struct Prose : public Meta::Pinned { // MARK: Paint ------------------------------------------------------------- - void paint(Gfx::Canvas &g); - void paintCaret(Gfx::Canvas &g, usize runeIndex, Gfx::Color color) const { auto m = _style.font.metrics(); auto baseline = queryPosition(runeIndex); diff --git a/src/libs/karm-text/run.cpp b/src/libs/karm-text/run.cpp deleted file mode 100644 index 25f7048..0000000 --- a/src/libs/karm-text/run.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "run.h" - -namespace Karm::Text { - -Run Run::from(Slice runes) { - Run run; - run.append(runes); - return run; -} - -void Run::clear() { - _runes.clear(); - _cells.clear(); - _shaped = false; - _size = {}; -} - -void Run::append(Rune rune) { - _runes.pushBack(rune); -} - -void Run::append(Slice runes) { - _runes.ensure(_runes.len() + runes.len()); - for (auto rune : runes) { - append(rune); - } -} - -void Run::_layout(Font &font) { - auto xpos = 0.0f; - bool first = true; - Glyph prev = Glyph::TOFU; - for (auto &cell : _cells) { - if (not first) - xpos += font.kern(prev, cell.glyph); - else - first = false; - - cell.xpos = xpos; - cell.adv = font.advance(cell.glyph); - xpos += cell.adv; - prev = cell.glyph; - } - - _size = { - xpos, - font.metrics().lineheight(), - }; -} - -void Run::shape(Font &font) { - _cells.clear(); - - _cells.ensure(_runes.len()); - for (auto rune : _runes) { - auto glyph = font.glyph(rune == '\n' ? ' ' : rune); - _cells.pushBack({ - .runeRange = {_runes.len(), 1}, - .glyph = glyph, - }); - } - - _layout(font); - - _shaped = true; -} - -} // namespace Karm::Text diff --git a/src/libs/karm-text/run.h b/src/libs/karm-text/run.h deleted file mode 100644 index 7733aa3..0000000 --- a/src/libs/karm-text/run.h +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include "base.h" -#include "font.h" - -namespace Karm::Text { - -struct Run { - struct Cell { - urange runeRange; - Glyph glyph; - f64 xpos = 0; //< Position of the glyph within the block - f64 adv = 0; //< Advance of the glyph - - MutSlice runes(Run &t) { - return mutSub(t._runes, runeRange); - } - - Slice runes(Run const &t) const { - return sub(t._runes, runeRange); - } - - bool newline(Run const &t) const { - auto r = runes(t); - if (not r) - return false; - return last(r) == '\n'; - } - - bool space(Run const &t) const { - auto r = runes(t); - if (not r) - return false; - return last(r) == '\n' or isAsciiSpace(last(r)); - } - }; - - Vec _runes{}; - Vec _cells{}; - Math::Vec2f _size{}; - bool _shaped = false; - - // MARK: Access ------------------------------------------------------------ - - Slice runes() const { - return _runes; - } - - Slice cells() const { - if (not _shaped) - panic("run not shaped"); - return _cells; - } - - Math::Vec2f size() const { - if (not _shaped) - panic("run not shaped"); - return _size; - } - - bool shaped() const { - return _shaped; - } - - // MARK: Builder ----------------------------------------------------------- - - template - static Run from(_Str str) { - Run run; - run.append(str); - return run; - } - - static Run from(Slice runes); - - void clear(); - - void append(Rune rune); - - template - void append(_Str str) { - for (auto rune : iterRunes(str)) - append(rune); - } - - void append(Slice runes); - - // MARK: Layout ------------------------------------------------------------ - - void _layout(Font &font); - - void shape(Font &font); -}; - -} // namespace Karm::Text diff --git a/src/libs/karm-ui/input.cpp b/src/libs/karm-ui/input.cpp index 20f231a..4104512 100644 --- a/src/libs/karm-ui/input.cpp +++ b/src/libs/karm-ui/input.cpp @@ -307,7 +307,7 @@ struct Input : public View { auto &text = _ensureText(); text.paintCaret(g, _model->_cur.head, _style.color.unwrapOr(Ui::GRAY100)); - text.paint(g); + g.fill(text); g.pop(); } @@ -386,7 +386,7 @@ struct SimpleInput : public View { if (_focus) text.paintCaret(g, _ensureModel()._cur.head, _style.color.unwrapOr(Ui::GRAY100)); - text.paint(g); + g.fill(text); g.pop(); } diff --git a/src/libs/karm-ui/view.cpp b/src/libs/karm-ui/view.cpp index da7d496..f3ce034 100644 --- a/src/libs/karm-ui/view.cpp +++ b/src/libs/karm-ui/view.cpp @@ -236,7 +236,7 @@ struct Text : public View { void paint(Gfx::Canvas &g, Math::Recti) override { g.push(); g.origin(bound().xy.cast()); - _prose->paint(g); + g.fill(*_prose); g.pop(); } diff --git a/src/web/vaev-layout/base.h b/src/web/vaev-layout/base.h index cc45b82..8ad36a3 100644 --- a/src/web/vaev-layout/base.h +++ b/src/web/vaev-layout/base.h @@ -18,7 +18,7 @@ struct Fragmentainer { bool _isDiscoveryMode = false; //< Are we looking for suitable breakpoints? usize _monolithicCount = 0; //< How deep we are in a monolithic box - Fragmentainer(Vec2Px currSize = {Limits::MAX, Limits::MAX}) : _size(currSize) {} + Fragmentainer(Vec2Px currSize = Vec2Px::MAX) : _size(currSize) {} Vec2Px size() const { return _size; } @@ -43,7 +43,7 @@ struct Fragmentainer { } bool hasInfiniteDimensions() { - return _size == Vec2Px{Limits::MAX, Limits::MAX}; + return _size == Vec2Px::MAX; } bool allowBreak() { diff --git a/src/web/vaev-layout/block.cpp b/src/web/vaev-layout/block.cpp index 4b847cc..a9cc84e 100644 --- a/src/web/vaev-layout/block.cpp +++ b/src/web/vaev-layout/block.cpp @@ -115,8 +115,7 @@ Output fragmentEmptyBox(Tree &tree, Input input) { // https://www.w3.org/TR/CSS22/visuren.html#normal-flow struct BlockFormatingContext { - - Px computeCAPMIN(Tree &tree, Box &box, Input input, Px inlineSize) { + Px _computeCapmin(Tree &tree, Box &box, Input input, Px inlineSize) { Px capmin{}; for (auto &c : box.children()) { if (c.style->display != Display::TABLE_BOX) { @@ -200,8 +199,10 @@ struct BlockFormatingContext { childInput.position = input.position + Vec2Px{margin.start, blockSize}; + // HACK: Table Box mostly behaves like a block box, let's compute its capmin + // and avoid duplicating the layout code if (c.style->display == Display::Internal::TABLE_BOX) { - childInput.capmin = computeCAPMIN(tree, box, input, inlineSize); + childInput.capmin = _computeCapmin(tree, box, input, inlineSize); } auto output = layout(