Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ struct PrintOption {

auto paper = self.paper;

if (self.orientation == Print::Orientation::LANDSCAPE)
paper = paper.landscape();

if (self.width) {
paper.name = "custom";
paper.width = resolver.resolve(*self.width).template cast<f64>();
Expand All @@ -68,6 +65,7 @@ struct PrintOption {

return {
.paper = paper,
.orientation = self.orientation,
.scale = self.scale.toDppx(),
};
}
Expand Down
105 changes: 75 additions & 30 deletions src/vaev-engine/driver/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,65 @@ void _paintMargins(Style::PageSpecifiedValues& pageStyle, RectAu pageRect, RectA
_paintMainMargin(pageStyle, stack, rightRect, Style::PageArea::RIGHT, {Style::PageArea::RIGHT_TOP, Style::PageArea::RIGHT_MIDDLE, Style::PageArea::RIGHT_BOTTOM});
}

Pair<RectAu> _computePageBounds(Print::Settings const& settings, Style::Media const& media, Style::PageSpecifiedValues const& pageStyle) {
Vec2Au pageRectSize = resolve(
*pageStyle.style->pageSize,
Vec2Au{
media.width / Au{media.resolution.toDppx()},
media.height / Au{media.resolution.toDppx()}
}
);

InsetsAu pageMargin;
if (settings.margins == Print::Margins::DEFAULT) {
Layout::Resolver resolver{};
pageMargin = {
resolver.resolve(pageStyle.style->margin->top, pageRectSize.height),
resolver.resolve(pageStyle.style->margin->end, pageRectSize.width),
resolver.resolve(pageStyle.style->margin->bottom, pageRectSize.height),
resolver.resolve(pageStyle.style->margin->start, pageRectSize.width),
};
} else if (settings.margins == Print::Margins::CUSTOM) {
pageMargin = settings.margins.custom.cast<Au>();
} else if (settings.margins == Print::Margins::MINIMUM) {
pageMargin = {};
}

return {
pageRectSize,
RectAu{pageRectSize}.shrink(pageMargin)
};
}

// https://www.w3.org/TR/css-page-3/#issue-59a903e8
Style::Media _constructMediaForBasePageSize(Gc::Ref<Dom::Document> dom, Print::Settings const& settings) {
auto const media = Style::Media::forPrint(settings);

// ----------------- Computing UA page size
InsetsAu uAPageMargin;
if (settings.margins == Print::Margins::DEFAULT) {
uAPageMargin = {resolveAbsoluteLength(Length{0.5, Length::Unit::IN})};
} else if (settings.margins == Print::Margins::CUSTOM) {
uAPageMargin = settings.margins.custom.cast<Au>();
} else if (settings.margins == Print::Margins::MINIMUM) {
uAPageMargin = {};
}

RectAu uAPageRect{
media.width / Au{media.resolution.toDppx()},
media.height / Au{media.resolution.toDppx()}
};

RectAu uAPageContent = uAPageRect.shrink(uAPageMargin);

// ----------------- Computing Page Base Size
auto pageStyleFromUAPage = computePageBaseSize(*dom->styleSheets, media);
auto [basePageRect, _] = _computePageBounds(settings, media, *pageStyleFromUAPage);
return media.withPixelsDimensions(basePageRect.size());
}

export Generator<Print::Page> print(Gc::Ref<Dom::Document> dom, Print::Settings const& settings) {
auto media = Style::Media::forPrint(settings);
auto const media = _constructMediaForBasePageSize(dom, settings);

Font::Database db;
if (not db.loadSystemFonts())
Expand All @@ -98,6 +155,7 @@ export Generator<Print::Page> print(Gc::Ref<Dom::Document> dom, Print::Settings
Style::Computer computer{
media, *dom->styleSheets, db
};

computer.build();
computer.styleDocument(*dom);

Expand Down Expand Up @@ -130,36 +188,10 @@ export Generator<Print::Page> print(Gc::Ref<Dom::Document> dom, Print::Settings
};

auto pageStyle = computer.computeFor(initialStyle, page);
RectAu pageRect{
media.width / Au{media.resolution.toDppx()},
media.height / Au{media.resolution.toDppx()}
};
auto pageStack = makeRc<Scene::Stack>();

InsetsAu pageMargin;

if (settings.margins == Print::Margins::DEFAULT) {
Layout::Resolver resolver{};
pageMargin = {
resolver.resolve(pageStyle->style->margin->top, pageRect.height),
resolver.resolve(pageStyle->style->margin->end, pageRect.width),
resolver.resolve(pageStyle->style->margin->bottom, pageRect.height),
resolver.resolve(pageStyle->style->margin->start, pageRect.width),
};
} else if (settings.margins == Print::Margins::CUSTOM) {
pageMargin = settings.margins.custom.cast<Au>();
} else if (settings.margins == Print::Margins::MINIMUM) {
pageMargin = {};
}

RectAu pageContent = pageRect.shrink(pageMargin);
auto [pageRect, pageContent] = _computePageBounds(settings, media, *pageStyle);

Layout::Viewport vp{
.small = pageContent.size(),
};

contentTree.viewport = vp;
contentTree.fc = {pageContent.size()};
auto pageStack = makeRc<Scene::Stack>();

if (settings.headerFooter and settings.margins != Print::Margins::NONE)
_paintMargins(*pageStyle, pageRect, pageContent, *pageStack);
Expand All @@ -171,6 +203,13 @@ export Generator<Print::Page> print(Gc::Ref<Dom::Document> dom, Print::Settings
.containingBlock = pageContent.size(),
};

Layout::Viewport vp{
.small = pageContent.size(),
};

contentTree.viewport = vp;
contentTree.fc = {pageContent.size()};

contentTree.fc.enterDiscovery();
auto outDiscovery = Layout::layout(
contentTree,
Expand All @@ -192,8 +231,14 @@ export Generator<Print::Page> print(Gc::Ref<Dom::Document> dom, Print::Settings
Layout::paint(fragment, *pageStack);
pageStack->prepare();

Print::PaperStock pagePaperStock{
""s,
(f64)pageRect.size().width * media.resolution.toDppx(),
(f64)pageRect.size().height * media.resolution.toDppx()
};

co_yield Print::Page(
settings.paper,
pagePaperStock,
makeRc<Scene::Clear>(
makeRc<Scene::Transform>(
pageStack,
Expand Down
15 changes: 15 additions & 0 deletions src/vaev-engine/driver/tests/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "vaev-engine.driver.tests",
"type": "lib",
"props": {
"cpp-excluded": true
},
"requires": [
"karm-test",
"vaev-engine"
],
"injects": [
"__tests__"
]
}
Loading
Loading