Skip to content

Commit

Permalink
vaev-driver: Added density option and fixup transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 23, 2024
1 parent 2743e7f commit e79e0cd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/libs/karm-print/image-printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ struct ImagePrinter : public FilePrinter {

Vec<Strong<Gfx::Surface>> _pages;
Opt<Gfx::CpuCanvas> _canvas;
f64 _density;
Image::Saver _saver;

ImagePrinter(Image::Saver saver = {})
: _saver(saver) {}
ImagePrinter(f64 density = 1, Image::Saver saver = {})
: _density(density),
_saver(saver) {}

Gfx::Canvas &beginPage(PaperStock paper) override {
_pages.emplaceBack(Gfx::Surface::alloc(paper.size().cast<isize>(), Gfx::RGBA8888));
_pages.emplaceBack(Gfx::Surface::alloc(paper.size().cast<isize>() * _density, Gfx::RGBA8888));

if (_canvas)
_canvas->end();
_canvas = Gfx::CpuCanvas{};
_canvas->begin(*last(_pages));
_canvas->scale(_density);
_canvas->clear(Gfx::WHITE);

return *_canvas;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-scene/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct Transform : public Node {

Math::Rectf bound() override {
return _transform
.inverse()
.apply(_content->bound())
.bound();
}
Expand All @@ -28,6 +27,7 @@ struct Transform : public Node {

g.push();
g.transform(_transform);
r = _transform.inverse().apply(r).bound();
_content->paint(g, r, o);
g.pop();
}
Expand Down
43 changes: 27 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ struct PrintOption {
bool printToBMP = false;

Resolution scale = Resolution::fromDppx(1);
Resolution density = Resolution::fromDppx(1);
Opt<Length> width = NONE;
Opt<Length> height = NONE;
Print::PaperStock paper = Print::A4;
Expand Down Expand Up @@ -153,7 +154,7 @@ Res<> print(Mime::Url const &, Strong<Markup::Document> dom, Io::Writer &output,

Strong<Print::FilePrinter> printer =
options.printToBMP
? Strong<Print::FilePrinter>{makeStrong<Print::ImagePrinter>()}
? Strong<Print::FilePrinter>{makeStrong<Print::ImagePrinter>(options.density.toDppx())}
: makeStrong<Print::PdfPrinter>();

for (auto &page : pages) {
Expand All @@ -169,7 +170,8 @@ Res<> print(Mime::Url const &, Strong<Markup::Document> dom, Io::Writer &output,
}

Res<> print(Mime::Url const &url, Io::Reader &input, Io::Writer &output, PrintOption options = {}) {
auto dom = try$(Vaev::Driver::loadDocument(url, "application/xhtml+xml"_mime, input));
auto mime = Mime::sniffSuffix(url.path.suffix()).unwrapOr("application/xhtml+xml"_mime);
auto dom = try$(Vaev::Driver::loadDocument(url, mime, input));
return print(url, dom, output, options);
}

Expand Down Expand Up @@ -219,6 +221,7 @@ struct RenderOption {
bool dumpPaint = false;

Resolution scale = Resolution::fromDpi(96);
Resolution density = Resolution::fromDpi(96);
Length width = 800_px;
Length height = 600_px;
};
Expand Down Expand Up @@ -247,10 +250,11 @@ Res<> render(Mime::Url const &, Strong<Markup::Document> dom, Io::Writer &output
if (options.dumpPaint)
Sys::println("--- START OF PAINT ---\n{}\n--- END OF PAINT ---\n", paint);

auto image = Gfx::Surface::alloc(imageSize.cast<isize>(), Gfx::RGBA8888);
auto image = Gfx::Surface::alloc(imageSize.cast<isize>() * options.density.toDppx(), Gfx::RGBA8888);
Gfx::CpuCanvas g;
g.begin(*image);
g.clear(Gfx::WHITE);
g.scale(options.density.toDppx());
paint->paint(g);
g.end();

Expand All @@ -260,7 +264,8 @@ Res<> render(Mime::Url const &, Strong<Markup::Document> dom, Io::Writer &output
}

Res<> render(Mime::Url const &input, Io::Reader &reader, Io::Writer &output, RenderOption options = {}) {
auto dom = try$(Vaev::Driver::loadDocument(input, "application/xhtml+xml"_mime, reader));
auto mime = Mime::sniffSuffix(input.path.suffix()).unwrapOr("application/xhtml+xml"_mime);
auto dom = try$(Vaev::Driver::loadDocument(input, mime, reader));
return render(input, dom, output, options);
}

Expand Down Expand Up @@ -368,7 +373,8 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
Cli::Flag dumpDomArg = Cli::flag('d', "dump-dom"s, "Dump the DOM tree"s);
Cli::Flag dumpLayoutArg = Cli::flag('l', "dump-layout"s, "Dump the layout tree"s);
Cli::Flag dumpPaintArg = Cli::flag('p', "dump-paint"s, "Dump the paint tree"s);
Cli::Option scaleArg = Cli::option<Str>(NONE, "scale"s, "Scale of the output document in css units (e.g. 96dpi)"s, "1x"s);
Cli::Option scaleArg = Cli::option<Str>(NONE, "scale"s, "Scale of the output document in css units (e.g. 1x)"s, "1x"s);
Cli::Option densityArg = Cli::option<Str>(NONE, "density"s, "Density of the output document in css units (e.g. 96dpi)"s, "1x"s);
Cli::Option widthArg = Cli::option<Str>('w', "width"s, "Width of the output document in css units (e.g. 800px)"s, ""s);
Cli::Option heightArg = Cli::option<Str>('h', "height"s, "Height of the output document in css units (e.g. 600px)"s, ""s);
Cli::Option paperArg = Cli::option<Str>(NONE, "paper"s, "Paper size for printing (default: A4)"s, "A4"s);
Expand All @@ -378,17 +384,19 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
"print"s,
'p',
"Render document for printing"s,
{inputArg,
outputArg,
dumpStyleArg,
dumpDomArg,
dumpLayoutArg,
dumpPaintArg,
scaleArg,
widthArg,
heightArg,
paperArg,
orientationArg
{
inputArg,
outputArg,
dumpStyleArg,
dumpDomArg,
dumpLayoutArg,
dumpPaintArg,
scaleArg,
densityArg,
widthArg,
heightArg,
paperArg,
orientationArg,
},
[=](Sys::Context &) -> Async::Task<> {
Vaev::Tools::PrintOption options{
Expand All @@ -399,6 +407,7 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
};

options.scale = co_try$(Vaev::Style::parseValue<Vaev::Resolution>(scaleArg.unwrap()));
options.density = co_try$(Vaev::Style::parseValue<Vaev::Resolution>(densityArg.unwrap()));

if (widthArg.unwrap())
options.width = co_try$(Vaev::Style::parseValue<Vaev::Length>(widthArg.unwrap()));
Expand Down Expand Up @@ -448,6 +457,7 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
dumpLayoutArg,
dumpPaintArg,
scaleArg,
densityArg,
widthArg,
heightArg,
},
Expand All @@ -460,6 +470,7 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
};

options.scale = co_try$(Vaev::Style::parseValue<Vaev::Resolution>(scaleArg.unwrap()));
options.density = co_try$(Vaev::Style::parseValue<Vaev::Resolution>(densityArg.unwrap()));

if (widthArg.unwrap())
options.width = co_try$(Vaev::Style::parseValue<Vaev::Length>(widthArg.unwrap()));
Expand Down
1 change: 1 addition & 0 deletions src/web/vaev-driver/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"requires": [
"vaev-layout",
"vaev-markup",
"karm-print",
"karm-mime",
"karm-sys"
]
Expand Down

0 comments on commit e79e0cd

Please sign in to comment.