Skip to content

Commit 0c72bb0

Browse files
committed
vaev: Update from upstream.
1 parent 4f627f4 commit 0c72bb0

File tree

19 files changed

+328
-125
lines changed

19 files changed

+328
-125
lines changed

src/apps/hideo-spreadsheet/app.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct Sheet {
8181
usize freezedRows = 0;
8282
usize freezedCols = 0;
8383
Vec<Row> rows = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}};
84-
Vec<Col> cols = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}};
84+
Vec<Col> cols = {{}, {}, {}, {}, {}, {}, {}, {}};
8585
Map<Pos, Cell> cells = {};
8686

8787
void recompute() {

src/libs/karm-print/paper.h

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <karm-base/distinct.h>
4+
#include <karm-base/res.h>
45
#include <karm-base/string.h>
56

67
namespace Karm::Print {
@@ -115,4 +116,14 @@ static constexpr Array PAPER_STOCKS = {
115116
ENVELOPE_SERIES,
116117
};
117118

119+
static inline Res<PaperStock> findPaperStock(Str name) {
120+
for (auto const &series : PAPER_STOCKS) {
121+
for (auto const &stock : series) {
122+
if (eqCi(stock.name, name))
123+
return Ok(stock);
124+
}
125+
}
126+
return Error::invalidData("unknown paper stock");
127+
}
128+
118129
} // namespace Karm::Print

src/libs/karm-print/pdf.h

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ struct PdfPrinter : public Printer {
1919
_density = density;
2020
}
2121

22+
PdfPrinter(Math::Vec2i size, Density density = Density::DEFAULT) {
23+
_paperSize.width = size.x;
24+
_paperSize.height = size.y;
25+
_density = density;
26+
}
27+
2228
Gfx::Canvas &beginPage() override {
2329
_pages.emplaceBack();
2430
_canvas = Pdf::Canvas{last(_pages), _paperSize};

src/web/vaev-css/lexer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct Token {
5353
using enum Type;
5454

5555
Type type;
56-
Str data;
56+
String data;
5757

5858
#define ITER(ID, NAME) \
5959
static Token NAME(Str data = "") { return {ID, data}; }
@@ -62,7 +62,7 @@ struct Token {
6262

6363
Token() : type(NIL) {}
6464

65-
Token(Type type, Str data = "")
65+
Token(Type type, String data = ""s)
6666
: type(type), data(data) {}
6767

6868
explicit operator bool() const {

src/web/vaev-driver/render.cpp

+14-27
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void _collectStyle(Markup::Node const &node, Style::StyleBook &sb) {
5151
}
5252
}
5353

54-
RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2Px viewport) {
54+
RenderResult render(Markup::Document const &dom, Style::Media const &media, Layout::Viewport viewport) {
5555
Style::StyleBook stylebook;
5656
stylebook.add(
5757
fetchStylesheet("bundle://vaev-driver/user-agent.css"_url)
@@ -68,9 +68,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
6868
Style::Computer computer{media, stylebook};
6969
Layout::Tree tree = {
7070
Layout::build(computer, dom),
71-
{
72-
.small = viewport,
73-
}
71+
viewport,
7472
};
7573

7674
elapsed = Sys::now() - start;
@@ -79,25 +77,18 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
7977

8078
start = Sys::now();
8179

82-
Layout::Viewport vp{.small = viewport};
83-
84-
elapsed = Sys::now() - start;
85-
86-
logDebugIf(DEBUG_RENDER, "layout tree measure time: {}", elapsed);
87-
88-
start = Sys::now();
89-
9080
Layout::layout(
9181
tree,
9282
tree.root,
9383
{
9484
.commit = Layout::Commit::YES,
95-
.knownSize = {vp.small.width, NONE},
96-
.availableSpace = {vp.small.width, 0_px},
97-
.containingBlock = {vp.small.width, vp.small.height},
85+
.knownSize = {viewport.small.width, NONE},
86+
.availableSpace = {viewport.small.width, 0_px},
87+
.containingBlock = {viewport.small.width, viewport.small.height},
9888
}
9989
);
100-
Layout::layoutPositioned(tree, tree.root, {vp.small.width, vp.small.height});
90+
91+
Layout::layoutPositioned(tree, tree.root, {viewport.small.width, viewport.small.height});
10192

10293
auto sceneRoot = makeStrong<Scene::Stack>();
10394

@@ -119,7 +110,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
119110
};
120111
}
121112

122-
RenderResult render(Markup::Document &dom, Style::Media const &media, Print::PaperStock paper) {
113+
Vec<Scene::Page> print(Markup::Document &dom, Style::Media const &media) {
123114
Style::StyleBook stylebook;
124115
stylebook.add(
125116
fetchStylesheet("bundle://vaev-driver/user-agent.css"_url)
@@ -135,8 +126,8 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
135126

136127
Layout::Viewport vp{
137128
.small = {
138-
Px{paper.width},
139-
Px{paper.height},
129+
Px{media.width},
130+
Px{media.height},
140131
},
141132
};
142133

@@ -156,15 +147,11 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
156147
}
157148
);
158149

159-
auto sceneRoot = makeStrong<Scene::Page>();
160-
Layout::paint(tree.root, *sceneRoot);
161-
sceneRoot->prepare();
150+
Vec<Scene::Page> pages;
151+
Layout::paint(tree.root, pages.emplaceBack());
152+
last(pages).prepare();
162153

163-
return {
164-
std::move(stylebook),
165-
makeStrong<Layout::Box>(std::move(tree.root)),
166-
sceneRoot,
167-
};
154+
return pages;
168155
}
169156

170157
} // namespace Vaev::Driver

src/web/vaev-driver/render.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <karm-scene/base.h>
4+
#include <karm-scene/page.h>
45
#include <vaev-base/length.h>
56
#include <vaev-layout/box.h>
67
#include <vaev-markup/dom.h>
@@ -14,8 +15,8 @@ struct RenderResult {
1415
Strong<Scene::Node> scene;
1516
};
1617

17-
RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2Px viewport);
18+
RenderResult render(Markup::Document const &dom, Style::Media const &media, Layout::Viewport viewport);
1819

19-
RenderResult render(Markup::Document &dom, Style::Media const &media, Print::PaperStock paper);
20+
Vec<Scene::Page> print(Markup::Document &dom, Style::Media const &media);
2021

2122
} // namespace Vaev::Driver

src/web/vaev-layout/base.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <vaev-base/length.h>
4+
#include <vaev-base/resolution.h>
45
#include <vaev-base/writing.h>
56

67
namespace Vaev::Layout {
@@ -13,7 +14,7 @@ enum struct IntrinsicSize {
1314
};
1415

1516
struct Viewport {
16-
Px dpi = 96_px;
17+
Resolution dpi = Resolution::fromDpi(96);
1718
// https://drafts.csswg.org/css-values/#small-viewport-size
1819
RectPx small;
1920
// https://drafts.csswg.org/css-values/#large-viewport-size

src/web/vaev-layout/values.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,22 @@ Px Resolver::resolve(Length value) {
195195
// Absolute
196196
// https://drafts.csswg.org/css-values/#absolute-lengths
197197
case Length::CM:
198-
return Px::fromFloatNearest(value.val() * viewport.dpi.cast<f64>() / 2.54);
198+
return Px::fromFloatNearest(value.val() * viewport.dpi.toDpi() / 2.54);
199199

200200
case Length::MM:
201-
return Px::fromFloatNearest(value.val() * viewport.dpi.cast<f64>() / 25.4);
201+
return Px::fromFloatNearest(value.val() * viewport.dpi.toDpi() / 25.4);
202202

203203
case Length::Q:
204-
return Px::fromFloatNearest(value.val() * viewport.dpi.cast<f64>() / 101.6);
204+
return Px::fromFloatNearest(value.val() * viewport.dpi.toDpi() / 101.6);
205205

206206
case Length::IN:
207-
return Px::fromFloatNearest(value.val() * viewport.dpi.cast<f64>());
207+
return Px::fromFloatNearest(value.val() * viewport.dpi.toDpi());
208208

209209
case Length::PT:
210-
return Px::fromFloatNearest(value.val() * viewport.dpi.cast<f64>() / 72.0);
210+
return Px::fromFloatNearest(value.val() * viewport.dpi.toDpi() / 72.0);
211211

212212
case Length::PC:
213-
return Px::fromFloatNearest(value.val() * viewport.dpi.cast<f64>() / 6.0);
213+
return Px::fromFloatNearest(value.val() * viewport.dpi.toDpi() / 6.0);
214214

215215
case Length::PX:
216216
return Px::fromFloatNearest(value.val());

src/web/vaev-style/computed.h

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <vaev-base/text.h>
2020
#include <vaev-base/visibility.h>
2121
#include <vaev-base/z-index.h>
22+
#include <vaev-css/parser.h>
2223

2324
namespace Vaev::Style {
2425

@@ -64,6 +65,8 @@ struct Computed {
6465
Cow<FlexProps> flex;
6566
Cow<BreakProps> break_;
6667

68+
Cow<Map<String, Css::Content>> variables;
69+
6770
Float float_ = Float::NONE;
6871
Clear clear = Clear::NONE;
6972

@@ -74,6 +77,7 @@ struct Computed {
7477
color = parent.color;
7578
font = parent.font;
7679
text = parent.text;
80+
variables = parent.variables;
7781
}
7882

7983
void repr(Io::Emit &e) const {
@@ -103,6 +107,7 @@ struct Computed {
103107
e(" float: {}", float_);
104108
e(" clear: {}", clear);
105109
e(" zIndex: {}", zIndex);
110+
e(" variables: {}", variables);
106111
e(")");
107112
}
108113
};

src/web/vaev-style/computer.cpp

+67-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Computed const &Computed::initial() {
77
static Computed computed = [] {
88
Computed res{};
99
StyleProp::any([&]<typename T>(Meta::Type<T>) {
10-
T{}.apply(res);
10+
if constexpr (requires { T::initial(); })
11+
T{}.apply(res);
1112
});
1213
return res;
1314
}();
@@ -31,6 +32,61 @@ void Computer::_evalRule(Rule const &rule, Markup::Element const &el, MatchingRu
3132
});
3233
}
3334

35+
static Css::Content expandVariables(Cursor<Css::Sst> &cursor, Map<String, Css::Content> computedVars) {
36+
// replacing the var with its value in the cascade
37+
Css::Content computedDecl = {};
38+
while (not cursor.ended()) {
39+
if (cursor.peek() == Css::Sst::FUNC and cursor.peek().prefix == Css::Token::function("var(")) {
40+
41+
auto const varName = cursor->content[0].token.data;
42+
// if the variable is defined in the cascade
43+
if (computedVars.has(varName)) {
44+
computedDecl.pushBack(computedVars.get(varName));
45+
} else {
46+
if (cursor->content.len() > 2) {
47+
// using the default value
48+
Cursor<Css::Sst> cur = cursor->content;
49+
cur.next(2);
50+
eatWhitespace(cur);
51+
auto defaultValue = expandVariables(cur, computedVars);
52+
computedDecl.pushBack(defaultValue);
53+
} else {
54+
// invalid property
55+
logWarn("variable not found: {} {}", varName, cursor->content);
56+
}
57+
}
58+
cursor.next();
59+
} else if (cursor.peek() == Css::Sst::FUNC) {
60+
Cursor<Css::Sst> cur = cursor->content;
61+
computedDecl.pushBack(cursor.peek());
62+
computedDecl[computedDecl.len() - 1].content = expandVariables(cur, computedVars);
63+
cursor.next();
64+
} else {
65+
computedDecl.pushBack(cursor.peek());
66+
cursor.next();
67+
}
68+
}
69+
return computedDecl;
70+
}
71+
72+
static Res<StyleProp> resolve(DeferredProp const &prop, Map<String, Css::Content> computedVars) {
73+
Cursor<Css::Sst> cursor = prop.value;
74+
75+
auto computedDecl = expandVariables(cursor, computedVars);
76+
77+
// building the declaration
78+
Css::Sst decl{Css::Sst::DECL};
79+
decl.token = Css::Token::ident(prop.propName);
80+
decl.content = computedDecl;
81+
82+
// parsing the declaration
83+
Res<StyleProp> computed = parseDeclaration<StyleProp>(decl, false);
84+
if (not computed) {
85+
logWarn("failed to parse declaration: {}", computed.none());
86+
}
87+
return computed;
88+
}
89+
3490
Strong<Computed> Computer::computeFor(Computed const &parent, Markup::Element const &el) {
3591
MatchingRules matchingRules;
3692

@@ -51,6 +107,7 @@ Strong<Computed> Computer::computeFor(Computed const &parent, Markup::Element co
51107

52108
// Get the style attribute if any
53109
auto styleAttr = el.getAttribute(Html::STYLE_ATTR);
110+
54111
StyleRule styleRule{
55112
.selector = UNIVERSAL,
56113
.props = parseDeclarations<StyleProp>(styleAttr ? *styleAttr : ""),
@@ -62,7 +119,15 @@ Strong<Computed> Computer::computeFor(Computed const &parent, Markup::Element co
62119
computed->inherit(parent);
63120

64121
for (auto const &styleRule : matchingRules) {
65-
for (auto const &prop : styleRule->props) {
122+
for (auto &prop : styleRule->props) {
123+
if (auto deferred = prop.is<DeferredProp>()) {
124+
auto resolved = resolve(prop.unwrap<DeferredProp>(), computed->variables.cow());
125+
126+
if (not resolved) {
127+
continue;
128+
}
129+
resolved.unwrap().apply(*computed);
130+
}
66131
if (prop.important == Important::NO)
67132
prop.apply(*computed);
68133
}

0 commit comments

Comments
 (0)