Skip to content

Commit

Permalink
karm-pdf: Initial support for text rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Jan 7, 2025
1 parent cac036f commit 4afceaf
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 221 deletions.
25 changes: 21 additions & 4 deletions src/libs/karm-gfx/canvas.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <karm-text/font.h>
#include <karm-text/run.h>
#include <karm-text/prose.h>

#include "canvas.h"

Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-gfx/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------------

Expand Down
14 changes: 4 additions & 10 deletions src/libs/karm-pdf/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------
Expand Down Expand Up @@ -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) {
Expand All @@ -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 &) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions src/libs/karm-pdf/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-scene/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Text : public Node {

g.push();
g.origin(_origin);
_prose->paint(g);
g.fill(*_prose);
g.pop();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-text/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Karm::Text {

struct Run;
struct Prose;

struct Font;

Expand Down
26 changes: 0 additions & 26 deletions src/libs/karm-text/prose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 0 additions & 3 deletions src/libs/karm-text/prose.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <karm-gfx/canvas.h>
#include <karm-logger/logger.h>

#include "font.h"
Expand Down Expand Up @@ -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);
Expand Down
68 changes: 0 additions & 68 deletions src/libs/karm-text/run.cpp

This file was deleted.

95 changes: 0 additions & 95 deletions src/libs/karm-text/run.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/libs/karm-ui/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ struct Input : public View<Input> {
auto &text = _ensureText();

text.paintCaret(g, _model->_cur.head, _style.color.unwrapOr(Ui::GRAY100));
text.paint(g);
g.fill(text);

g.pop();
}
Expand Down Expand Up @@ -386,7 +386,7 @@ struct SimpleInput : public View<SimpleInput> {

if (_focus)
text.paintCaret(g, _ensureModel()._cur.head, _style.color.unwrapOr(Ui::GRAY100));
text.paint(g);
g.fill(text);

g.pop();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-ui/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ struct Text : public View<Text> {
void paint(Gfx::Canvas &g, Math::Recti) override {
g.push();
g.origin(bound().xy.cast<f64>());
_prose->paint(g);
g.fill(*_prose);
g.pop();
}

Expand Down
4 changes: 2 additions & 2 deletions src/web/vaev-layout/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Px>::MAX, Limits<Px>::MAX}) : _size(currSize) {}
Fragmentainer(Vec2Px currSize = Vec2Px::MAX) : _size(currSize) {}

Vec2Px size() const { return _size; }

Expand All @@ -43,7 +43,7 @@ struct Fragmentainer {
}

bool hasInfiniteDimensions() {
return _size == Vec2Px{Limits<Px>::MAX, Limits<Px>::MAX};
return _size == Vec2Px::MAX;
}

bool allowBreak() {
Expand Down
7 changes: 4 additions & 3 deletions src/web/vaev-layout/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4afceaf

Please sign in to comment.