Skip to content

Commit

Permalink
tools: Allow dumping stylesheets, dom, layout tree, and paint tree th…
Browse files Browse the repository at this point in the history
…rough the CLI.
  • Loading branch information
sleepy-monax committed Sep 17, 2024
1 parent 38d2562 commit c1b15c6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/vaev-driver/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
logDebugIf(DEBUG_RENDER, "layout tree paint time: {}", elapsed);

return {
std::move(stylebook),
makeStrong<Layout::Frag>(std::move(tree.root)),
paintRoot,
};
Expand Down Expand Up @@ -156,6 +157,7 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
paintRoot->prepare();

return {
std::move(stylebook),
makeStrong<Layout::Frag>(std::move(tree.root)),
paintRoot,
};
Expand Down
1 change: 1 addition & 0 deletions src/vaev-driver/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Vaev::Driver {

struct RenderResult {
Style::StyleBook style;
Strong<Layout::Frag> layout;
Strong<Paint::Node> paint;
};
Expand Down
60 changes: 45 additions & 15 deletions src/vaev-tools/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,40 @@ Vaev::Style::Media constructMedia(Print::PaperStock paper) {
};
}

Res<> html2pdf(Mime::Url const &input, Mime::Url const &output) {
auto dom = try$(Vaev::Driver::fetchDocument(input));
struct Html2PdfOption {
bool dumpStyle = false;
bool dumpDom = false;
bool dumpLayout = false;
bool dumpPaint = false;
};

Res<> html2pdf(Mime::Url const &input, Io::Writer &output, Html2PdfOption options = {}) {
auto start = Sys::now();

auto dom = try$(Vaev::Driver::fetchDocument(input));
auto paper = Print::A4;
auto media = constructMedia(paper);

auto start = Sys::now();
auto [layout, paint] = Vaev::Driver::render(*dom, media, paper);
auto [style, layout, paint] = Vaev::Driver::render(*dom, media, paper);
auto elapsed = Sys::now() - start;

logInfo("render time: {}", elapsed);
logDebug("layout tree: {}", layout);
logDebug("paint tree: {}", paint);

if (options.dumpDom)
Sys::println("--- START OF DOM ---\n{}\n--- END OF DOM ---\n", dom);

if (options.dumpStyle)
Sys::println("--- START OF STYLE ---\n{}\n--- END OF STYLE ---\n", style);

if (options.dumpLayout)
Sys::println("--- START OF LAYOUT ---\n{}\n--- END OF LAYOUT ---\n", layout);

if (options.dumpPaint)
Sys::println("--- START OF PAINT ---\n{}\n--- END OF PAINT ---\n", paint);

Print::PdfPrinter printer{Print::A4, Print::Density::DEFAULT};
paint->print(printer);

auto file = try$(Sys::File::create(output));
Io::TextEncoder<> encoder{file};
Io::TextEncoder<> encoder{output};
Io::Emit e{encoder};
printer.write(e);
try$(e.flush());
Expand Down Expand Up @@ -256,23 +271,38 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
}
);

Cli::Flag dumpStyleArg = Cli::flag('s', "dump-style"s, "Dump the stylesheet"s);
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);

cmd.subCommand(
"html2pdf"s,
'r',
"Convert HTML to PDF"s,
{inputArg, outputArg
},
[inputArg, outputArg](Sys::Context &) -> Async::Task<> {
{inputArg, outputArg, dumpStyleArg, dumpDomArg, dumpLayoutArg, dumpPaintArg},
[inputArg, outputArg, dumpStyleArg, dumpDomArg, dumpLayoutArg, dumpPaintArg](Sys::Context &) -> Async::Task<> {
Vaev::Tools::Html2PdfOption options{
.dumpStyle = dumpStyleArg,
.dumpDom = dumpDomArg,
.dumpLayout = dumpLayoutArg,
.dumpPaint = dumpPaintArg,
};

auto input = co_try$(Mime::parseUrlOrPath(inputArg));
auto output = co_try$(Mime::parseUrlOrPath(outputArg));
co_return Vaev::Tools::html2pdf(input, output);
if (outputArg.unwrap() == "-"s)
co_return Vaev::Tools::html2pdf(input, Sys::out(), options);

auto outputUrl = co_try$(Mime::parseUrlOrPath(outputArg));
auto outputFile = co_try$(Sys::File::create(outputUrl));
co_return Vaev::Tools::html2pdf(input, outputFile, options);
}
);

auto &inspectorCmd = cmd.subCommand(
"inspector"s,
'i',
"Inspect a document"s,
"View a document in the inspector for debugging"s,
{inputArg}
);

Expand Down
4 changes: 2 additions & 2 deletions src/vaev-view/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct View : public Ui::View<View> {
g.origin(bound().xy.cast<f64>());
g.clip(viewport);

auto [layout, paint] = *_renderResult;
auto [_, layout, paint] = *_renderResult;
g.clear(rect, Gfx::WHITE);

paint->paint(g);
Expand All @@ -83,7 +83,7 @@ struct View : public Ui::View<View> {
Math::Vec2i size(Math::Vec2i size, Ui::Hint) override {
// FIXME: This is wasteful, we should cache the result
auto media = _constructMedia(size);
auto [layout, _] = Driver::render(*_dom, media, size.cast<Px>());
auto [_, layout, _] = Driver::render(*_dom, media, size.cast<Px>());

return {
layout->layout.borderBox().width.cast<isize>(),
Expand Down

0 comments on commit c1b15c6

Please sign in to comment.