Skip to content

Commit

Permalink
karm-pdf+print: Fix page density and coordinate system.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 3, 2024
1 parent a6af241 commit f13f323
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/libs/karm-pdf/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ namespace Karm::Pdf {

struct Canvas : public Gfx::Canvas {
Io::Emit _e;
Math::Vec2f _mediaBox{};

Math::Vec2f _p{};

Canvas(Io::Emit e) : _e{e} {}
Canvas(Io::Emit e, Math::Vec2f mediaBox)
: _e{e}, _mediaBox{mediaBox} {}

Math::Vec2f _toPdf(Math::Vec2f p) {
return {p.x, _mediaBox.y - p.y};
}

Math::Vec2f _mapPoint(Math::Vec2f p, Math::Path::Flags flags) {
if (flags & Math::Path::RELATIVE)
return p;
return p - _p;
return _toPdf(p + _p);
return _toPdf(p);
}

Math::Vec2f _mapPointAndUpdate(Math::Vec2f p, Math::Path::Flags flags) {
p = _mapPoint(p, flags);
_p = _p + p;
return p;
if (flags & Math::Path::RELATIVE)
p = p + _p;
_p = p;
return _toPdf(p);
}

// MARK: Context Operations ------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion src/libs/karm-print/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ Async::Task<> entryPointAsync(Sys::Context &) {
Print::PdfPrinter printer{Print::A4};
auto &ctx = printer.beginPage();
ctx.fillStyle(Gfx::RED);
ctx.rect({0, 0, 200, 300});
ctx.rect({0, 0, 100, 100});
ctx.fill(Gfx::FillRule::NONZERO);

ctx.fillStyle(Gfx::BLUE);
ctx.rect({0, 100, 100, 100});
ctx.fill(Gfx::FillRule::NONZERO);

ctx.fillStyle(Gfx::GREEN);
ctx.rect({0, 200, 100, 100});
ctx.fill(Gfx::FillRule::NONZERO);

auto outFile = co_try$(Sys::File::create("file:test.pdf"_url));
Expand Down
28 changes: 28 additions & 0 deletions src/libs/karm-print/paper.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
#pragma once

#include <karm-base/distinct.h>
#include <karm-base/string.h>

namespace Karm::Print {

static constexpr f64 INCH_TO_MM = 25.4;

// Print density in Dot Per Mm (DPMM)
struct Density : public Distinct<f64, struct _DensityTag> {
using Distinct::Distinct;

static Density const DEFAULT;

static constexpr Density fromDpi(f64 dpi) {
return Density{dpi * INCH_TO_MM};
}

static constexpr Density fromDpcm(f64 dpcm) {
return Density{dpcm};
}

constexpr f64 toDpi() const {
return _value / INCH_TO_MM;
}

constexpr f64 toDpcm() const {
return _value;
}
};

inline constexpr Density Density::DEFAULT = Density::fromDpi(72.0);

struct PaperStock {
Str name;
f64 width; // in mm
Expand Down
22 changes: 16 additions & 6 deletions src/libs/karm-print/pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
namespace Karm::Print {

struct PdfPrinter : public Printer {
PaperStock _stock;
Math::Vec2f _paperSize;
Density _density;
Vec<Io::StringWriter> _pages;
Opt<Pdf::Canvas> _canvas;

PdfPrinter(PaperStock stock)
: _stock{stock} {}
PdfPrinter(PaperStock stock, Density density = Density::DEFAULT) {
_paperSize.width = (stock.width / INCH_TO_MM) * (density.toDpi());
_paperSize.height = (stock.height / INCH_TO_MM) * (density.toDpi());
_density = density;
}

Gfx::Canvas &beginPage() override {
_pages.emplaceBack();
_canvas = Pdf::Canvas{last(_pages)};
_canvas = Pdf::Canvas{last(_pages), _paperSize};
return *_canvas;
}

Pdf::File pdf() {
Pdf::Ref alloc;

Pdf::File file;
file.header = "PDF-1.7"s;
file.header = "PDF-2.0"s;

Pdf::Array pagesKids;
Pdf::Ref pagesRef = alloc.alloc();
Expand Down Expand Up @@ -65,7 +69,13 @@ struct PdfPrinter : public Printer {
pagesRef,
Pdf::Dict{
{"Type"s, Pdf::Name{"Pages"s}},
{"MediaBox"s, Pdf::Array{usize{0}, usize{0}, _stock.width, _stock.height}},
{"MediaBox"s,
Pdf::Array{
usize{0},
usize{0},
_paperSize.width,
_paperSize.height,
}},
{"Count"s, _pages.len()},
{"Kids"s, std::move(pagesKids)},
}
Expand Down

0 comments on commit f13f323

Please sign in to comment.