-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathprint.cpp
More file actions
293 lines (246 loc) Β· 11.2 KB
/
Copy pathprint.cpp
File metadata and controls
293 lines (246 loc) Β· 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
export module Vaev.Engine:driver.print;
import Karm.Core;
import Karm.Gc;
import Karm.Print;
import Karm.Scene;
import Karm.Font;
import Karm.Sys;
import Karm.Gfx;
import Karm.Math;
import Karm.Logger;
import :style;
import :layout;
import :values;
import :dom.document;
import :css;
using namespace Karm;
namespace Vaev::Driver {
struct Page {
Rc<Style::PageSpecifiedValues> style;
usize index = 0;
RectAu borderBox = {};
RectAu contentBox = {};
Layout::Frag fragment = {};
Opt<Rc<Scene::Node>> canvas = {};
Layout::Breakpoint breakpoint = {};
};
export struct PaginationContext {
Layout::Tree _tree;
Style::Media _media;
Print::Settings _settings;
Style::Computer _computer;
Style::SpecifiedValues _style;
Layout::Runnings _runnings = {};
Vec<Page> _pages = {};
static PaginationContext create(Gc::Ref<Dom::Document> dom, Print::Settings const& settings) {
auto media = Style::Media::forPrint(settings);
Font::Database database;
if (not database.loadSystemFonts())
logWarn("not all fonts were properly loaded into database");
Style::Computer computer{
media, *dom->styleSheets, std::move(database)
};
computer.build();
computer.styleDocument(*dom);
Style::SpecifiedValues initialStyle = Style::SpecifiedValues::initial();
initialStyle.color = Gfx::BLACK;
initialStyle.setCustomProp("-vaev-url", {Css::Token::string(Io::format("\"{}\"", dom->url()))});
initialStyle.setCustomProp("-vaev-title", {Css::Token::string(Io::format("\"{}\"", dom->title()))});
initialStyle.setCustomProp("-vaev-datetime", {Css::Token::string(Io::format("\"{}\"", Sys::now()))});
return {
._tree = {Layout::build(dom)},
._media = media,
._settings = settings,
._computer = std::move(computer),
._style = initialStyle,
};
}
// MARK: Page Creation ---------------------------------------------------------
InsetsAu _resolvePageMargins(Page& page) {
InsetsAu margins;
if (_settings.margins == Print::Margins::DEFAULT) {
Layout::Resolver resolver{};
margins = {
resolver.resolve(page.style->style->margin->top, page.borderBox.height),
resolver.resolve(page.style->style->margin->end, page.borderBox.width),
resolver.resolve(page.style->style->margin->bottom, page.borderBox.height),
resolver.resolve(page.style->style->margin->start, page.borderBox.width),
};
} else if (_settings.margins == Print::Margins::CUSTOM) {
margins = _settings.margins.custom.cast<Au>();
} else if (_settings.margins == Print::Margins::MINIMUM) {
margins = {};
}
return margins;
}
Page _createPage(usize index) {
Page page = {
_computer.computeFor(
_style,
{
.name = ""s,
.number = index,
.blank = false,
}
)
};
page.borderBox = {
_media.width / Au{_media.resolution.toDppx()},
_media.height / Au{_media.resolution.toDppx()}
};
auto size = page.borderBox.size().cast<f64>();
auto margins = _resolvePageMargins(page);
page.contentBox = page.borderBox.shrink(margins);
return page;
}
// MARK: Page Breaking ---------------------------------------------------------
Pair<bool, Layout::Breakpoint> _pageBreak(Page& page, Cursor<Layout::Breakpoint> previousBreakpoint) {
Layout::Viewport vp{.small = page.contentBox.size()};
_tree.viewport = vp;
_tree.fc = {page.contentBox.size()};
_tree.fc.enterDiscovery();
auto output = Layout::layout(
_tree,
{
.knownSize = {page.contentBox.width, NONE},
.position = page.contentBox.topStart(),
.availableSpace = page.contentBox.size(),
.containingBlock = page.contentBox.size(),
.runningPosition = &_runnings,
.pageIndex = page.index,
.breakpointTraverser = {
previousBreakpoint
},
}
);
_tree.fc.leaveDiscovery();
return {output.completelyLaidOut, output.breakpoint.unwrap()};
}
void _splitPages() {
usize pageNumber = 0;
while (true) {
auto page = _createPage(pageNumber++);
Cursor previousBreakpoint =
_pages ? &last(_pages).breakpoint
: &Layout::Breakpoint::START_OF_DOCUMENT;
auto [done, breakpoint] = _pageBreak(page, previousBreakpoint);
page.breakpoint = std::move(breakpoint);
_pages.pushBack(page);
if (done)
break;
}
}
// MARK: Page Painting ---------------------------------------------------------
void _paintCornerMargin(Style::PageSpecifiedValues& pageStyle, Scene::Stack& stack, RectAu const& rect, Style::PageArea area, usize currentPage, Layout::Runnings& runningPosition) {
Layout::Tree tree{
.root = Layout::buildForPseudoElement(pageStyle.area(area), currentPage, runningPosition),
.viewport = Layout::Viewport{.small = rect.size()}
};
auto [_, frag] = Layout::layoutCreateFragment(
tree,
{
.knownSize = rect.size().cast<Opt<Au>>(),
.position = rect.topStart(),
.availableSpace = rect.size(),
.containingBlock = rect.size(),
}
);
Layout::paint(frag, stack);
}
void _paintMainMargin(Style::PageSpecifiedValues& pageStyle, Scene::Stack& stack, RectAu const& rect, Style::PageArea mainArea, Array<Style::PageArea, 3> subAreas, usize currentPage, Layout::Runnings& runningPosition) {
auto box = Layout::buildForPseudoElement(pageStyle.area(mainArea), currentPage, runningPosition);
for (auto subArea : subAreas) {
box.add(Layout::buildForPseudoElement(pageStyle.area(subArea), currentPage, runningPosition));
}
Layout::Tree tree{
.root = std::move(box),
.viewport = Layout::Viewport{.small = rect.size()}
};
auto [_, frag] = Layout::layoutCreateFragment(
tree,
{
.knownSize = rect.size().cast<Opt<Au>>(),
.position = rect.topStart(),
.availableSpace = rect.size(),
.containingBlock = rect.size(),
}
);
Layout::paint(frag, stack);
}
void _paintMargins(Page& page, Scene::Stack& stack, Layout::Runnings& runningPosition) {
// Compute all corner rects
auto topLeftMarginCornerRect = RectAu::fromTwoPoint(page.borderBox.topStart(), page.contentBox.topStart());
auto topRightMarginCornerRect = RectAu::fromTwoPoint(page.borderBox.topEnd(), page.contentBox.topEnd());
auto bottomLeftMarginCornerRect = RectAu::fromTwoPoint(page.borderBox.bottomStart(), page.contentBox.bottomStart());
auto bottomRightMarginCornerRect = RectAu::fromTwoPoint(page.borderBox.bottomEnd(), page.contentBox.bottomEnd());
// Paint corners
_paintCornerMargin(*page.style, stack, topLeftMarginCornerRect, Style::PageArea::TOP_LEFT_CORNER, page.index, runningPosition);
_paintCornerMargin(*page.style, stack, topRightMarginCornerRect, Style::PageArea::TOP_RIGHT_CORNER, page.index, runningPosition);
_paintCornerMargin(*page.style, stack, bottomLeftMarginCornerRect, Style::PageArea::BOTTOM_LEFT_CORNER, page.index, runningPosition);
_paintCornerMargin(*page.style, stack, bottomRightMarginCornerRect, Style::PageArea::BOTTOM_RIGHT_CORNER, page.index, runningPosition);
// Compute main area rects
auto topRect = RectAu::fromTwoPoint(topLeftMarginCornerRect.topEnd(), topRightMarginCornerRect.bottomStart());
auto bottomRect = RectAu::fromTwoPoint(bottomLeftMarginCornerRect.topEnd(), bottomRightMarginCornerRect.bottomStart());
auto leftRect = RectAu::fromTwoPoint(topLeftMarginCornerRect.bottomEnd(), bottomLeftMarginCornerRect.topStart());
auto rightRect = RectAu::fromTwoPoint(topRightMarginCornerRect.bottomEnd(), bottomRightMarginCornerRect.topStart());
// Paint main areas
_paintMainMargin(*page.style, stack, topRect, Style::PageArea::TOP, {Style::PageArea::TOP_LEFT, Style::PageArea::TOP_CENTER, Style::PageArea::TOP_RIGHT}, page.index, runningPosition);
_paintMainMargin(*page.style, stack, bottomRect, Style::PageArea::BOTTOM, {Style::PageArea::BOTTOM_LEFT, Style::PageArea::BOTTOM_CENTER, Style::PageArea::BOTTOM_RIGHT}, page.index, runningPosition);
_paintMainMargin(*page.style, stack, leftRect, Style::PageArea::LEFT, {Style::PageArea::LEFT_TOP, Style::PageArea::LEFT_MIDDLE, Style::PageArea::LEFT_BOTTOM}, page.index, runningPosition);
_paintMainMargin(*page.style, stack, rightRect, Style::PageArea::RIGHT, {Style::PageArea::RIGHT_TOP, Style::PageArea::RIGHT_MIDDLE, Style::PageArea::RIGHT_BOTTOM}, page.index, runningPosition);
}
// https://drafts.csswg.org/css-page/#painting
Rc<Scene::Node> _paintPage(Page& page, Layout::Frag& fragment) {
auto pageCanvas = makeRc<Scene::Stack>();
// When drawing a page of content, the page layers are painted in
// the following painting order (bottommost first):
// 1. Page background
// TODO
// 2. document canvas
// TODO
// 3. page borders
// TODO
// 4. document contents
Layout::paint(fragment, *pageCanvas);
// 5. page-margin boxes
if (_settings.headerFooter and _settings.margins != Print::Margins::NONE)
_paintMargins(page, *pageCanvas, _runnings);
pageCanvas->prepare();
return makeRc<Scene::Transform>(
pageCanvas,
Math::Trans2f::scale(_media.resolution.toDppx())
);
}
// MARK: Page Rendering ----------------------------------------------------
Generator<Print::Page> _renderPages() {
for (auto& page : _pages) {
auto [_, fragment] = Layout::layoutCreateFragment(
_tree,
{
.knownSize = {page.contentBox.width, NONE},
.position = page.contentBox.topStart(),
.availableSpace = page.contentBox.size(),
.containingBlock = page.contentBox.size(),
.pageIndex = page.index,
.breakpointTraverser = {
page.index
? &_pages[page.index - 1].breakpoint
: &Layout::Breakpoint::START_OF_DOCUMENT,
&page.breakpoint,
},
}
);
page.fragment = std::move(fragment);
co_yield Print::Page(
_settings.paper,
_paintPage(page, fragment)
);
}
}
[[clang::coro_wrapper]]
Generator<Print::Page> iterPages() {
_splitPages();
return _renderPages();
}
};
} // namespace Vaev::Driver