Skip to content

Commit

Permalink
vaev-style: Initial implementation of initial, inherit, unset, and re…
Browse files Browse the repository at this point in the history
…vert keywords.
  • Loading branch information
sleepy-monax committed Nov 27, 2024
1 parent 3b2859a commit baacae9
Show file tree
Hide file tree
Showing 15 changed files with 633 additions and 135 deletions.
4 changes: 2 additions & 2 deletions src/vaev-base/calc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ struct CalcValue {
: CalcValue(T{}) {
}

constexpr CalcValue(T value)
: type(OpType::FIXED), lhs(value) {
constexpr CalcValue(Meta::Convertible<T> auto value)
: type(OpType::FIXED), lhs(T{value}) {
}

constexpr CalcValue(Value value)
Expand Down
9 changes: 9 additions & 0 deletions src/vaev-base/flex.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ struct FlexBasis {
}
};

struct FlexItemProps {
FlexBasis flexBasis;
Number flexGrow, flexShrink;

void repr(Io::Emit &e) const {
e("({} {} {})", flexBasis, flexGrow, flexShrink);
}
};

struct FlexProps {
// FlexContainer
FlexDirection direction = FlexDirection::ROW;
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-base/insets.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum struct Position {

using Margin = Math::Insets<Width>;

using Padding = Math::Insets<PercentOr<Length>>;
using Padding = Math::Insets<CalcValue<PercentOr<Length>>>;

// https://www.w3.org/TR/CSS22/visuren.html#propdef-top
// https://www.w3.org/TR/CSS22/visuren.html#propdef-right
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-layout/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using Content = Union<
None,
Vec<Box>,
Strong<Text::Prose>,
Image::Picture>;
Karm::Image::Picture>;

struct Attrs {
usize span = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-layout/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static void _buildImage(Style::Computer &c, Markup::Element const &el, Box &pare

auto src = el.getAttribute(Html::SRC_ATTR).unwrapOr(""s);
auto url = Mime::Url::parse(src);
Image::Picture img = Image::loadOrFallback(url).unwrap();
auto img = Karm::Image::loadOrFallback(url).unwrap();
parent.add({style, font, img});
}

Expand Down
2 changes: 1 addition & 1 deletion src/vaev-layout/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Vaev::Layout {
Output _contentLayout(Tree &tree, Box &box, Input input) {
auto display = box.style->display;

if (auto image = box.content.is<Image::Picture>()) {
if (auto image = box.content.is<Karm::Image::Picture>()) {
return Output::fromSize(image->bound().size().cast<Px>());
} else if (auto run = box.content.is<Strong<Text::Prose>>()) {
return inlineLayout(tree, box, input);
Expand Down
22 changes: 5 additions & 17 deletions src/vaev-layout/paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,14 @@ static void _paintBox(Box &box, Gfx::Color currentColor, Scene::Stack &stack) {

Gfx::Borders borders;
Vec<Gfx::Fill> backgrounds;

bool hasBackgrounds = any(cssBackground);

if (hasBackgrounds) {
backgrounds.ensure(cssBackground.len());
for (auto &bg : cssBackground) {
auto color = resolve(bg.fill, currentColor);

// Skip transparent backgrounds
if (color.alpha == 0)
continue;

backgrounds.pushBack(color);
}
}
auto color = resolve(cssBackground->color, currentColor);
if (color.alpha != 0)
backgrounds.pushBack(color);

bool hasBorders = _paintBorders(box, currentColor, borders);
Math::Rectf bound = box.layout.borderBox().cast<f64>();

if (hasBackgrounds or hasBorders)
if (any(backgrounds) or hasBorders)
stack.add(makeStrong<Scene::Box>(bound, std::move(borders), std::move(backgrounds)));
}

Expand All @@ -75,7 +63,7 @@ static void _paintBox(Box &box, Scene::Stack &stack) {
box.layout.borderBox().topStart().cast<f64>(),
*prose
));
} else if (auto image = box.content.is<Image::Picture>()) {
} else if (auto image = box.content.is<Karm::Image::Picture>()) {
stack.add(makeStrong<Scene::Image>(
box.layout.borderBox().cast<f64>(),
*image
Expand Down
10 changes: 0 additions & 10 deletions src/vaev-layout/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,16 +910,6 @@ struct TableFormatingContext {
auto colSpan = cell.box->attrs.colSpan;
auto rowSpan = cell.box->attrs.rowSpan;

// https://www.w3.org/TR/CSS22/tables.html#table-layers
if (rowHelper[i].axisIdx)
cell.box->style->backgrounds.pushFront(rows[rowHelper[i].axisIdx.unwrap()].el.style->backgrounds);
if (rowHelper[i].groupIdx)
cell.box->style->backgrounds.pushFront(rowGroups[rowHelper[i].groupIdx.unwrap()].el.style->backgrounds);
if (colHelper[j].axisIdx)
cell.box->style->backgrounds.pushFront(cols[colHelper[j].axisIdx.unwrap()].el.style->backgrounds);
if (colHelper[j].groupIdx)
cell.box->style->backgrounds.pushFront(colGroups[colHelper[j].groupIdx.unwrap()].el.style->backgrounds);

// TODO: In CSS 2.2, the height of a cell box is the minimum
// height required by the content.
// The table cell's 'height' property can influence
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-style/computed.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Computed {
AlignProps aligns;
Math::Vec2<PercentOr<Length>> gaps;

Vec<BackgroundProps> backgrounds;
Cow<BackgroundProps> backgrounds;
Cow<BorderProps> borders;
Cow<Margin> margin;
Cow<Padding> padding;
Expand Down
6 changes: 3 additions & 3 deletions src/vaev-style/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ Strong<Computed> Computer::computeFor(Computed const &parent, Markup::Element co
for (auto const &styleRule : matchingRules) {
for (auto &prop : styleRule->props) {
if (prop.is<CustomProp>())
prop.apply(*computed);
prop.apply(parent, *computed);
}
}

for (auto const &styleRule : matchingRules) {
for (auto &prop : styleRule->props) {
if (not prop.is<CustomProp>()) {
if (prop.important == Important::NO)
prop.apply(*computed);
prop.apply(parent, *computed);
else
importantProps.pushBack(&prop);
}
}
}

for (auto const &prop : iterRev(importantProps))
prop->apply(*computed);
prop->apply(parent, *computed);

return computed;
}
Expand Down
29 changes: 26 additions & 3 deletions src/vaev-style/decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static inline Important _consumeImportant(Cursor<Css::Sst> &c, bool eatEverythin

template <typename P>
static inline P _deferProperty(Css::Sst const &sst) {
StyleProp prop = DeferredProp{sst.token.data, sst.content};
P prop = DeferredProp{sst.token.data, sst.content};
if constexpr (requires { P::important; }) {
Cursor<Css::Sst> content = sst.content;
prop.important = _consumeImportant(content, true);
Expand All @@ -63,6 +63,22 @@ static inline Res<P> _parseDeclaration(Css::Sst const &sst) {
return Ok(std::move(prop));
}

template <typename P>
static inline Res<P> _parseDefaulted(Css::Sst const &sst) {
Cursor<Css::Sst> content = sst.content;
Res<P> res = Error::invalidData("unknown declaration");
if (content.skip(Css::Token::ident("initial"))) {
res = Ok(DefaultedProp{sst.token.data, Default::INITIAL});
} else if (content.skip(Css::Token::ident("inherit"))) {
res = Ok(DefaultedProp{sst.token.data, Default::INHERIT});
} else if (content.skip(Css::Token::ident("unset"))) {
res = Ok(DefaultedProp{sst.token.data, Default::UNSET});
} else if (content.skip(Css::Token::ident("revert"))) {
res = Ok(DefaultedProp{sst.token.data, Default::REVERT});
}
return res;
}

template <typename P>
Res<P> parseDeclaration(Css::Sst const &sst, bool allowDeferred = true) {
if (sst != Css::Sst::DECL)
Expand Down Expand Up @@ -92,8 +108,15 @@ Res<P> parseDeclaration(Css::Sst const &sst, bool allowDeferred = true) {
}

resDecl = _parseDeclaration<P, T>(sst);
if (not resDecl and allowDeferred) {
if constexpr (Meta::Constructible<P, DeferredProp>) {

if constexpr (Meta::Constructible<P, DefaultedProp>) {
if (not resDecl) {
resDecl = _parseDefaulted<P>(sst);
}
}

if constexpr (Meta::Constructible<P, DeferredProp>) {
if (not resDecl and allowDeferred) {
resDecl = Ok(_deferProperty<P>(sst));
}
}
Expand Down
42 changes: 40 additions & 2 deletions src/vaev-style/styles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void DeferredProp::_expandContent(Cursor<Css::Sst> &c, Map<String, Css::Content>
}
}

void DeferredProp::apply(Computed &c) const {
void DeferredProp::apply(Computed const &parent, Computed &c) const {
Css::Sst decl{Css::Sst::DECL};
decl.token = Css::Token::ident(propName);
Cursor<Css::Sst> cursor = value;
Expand All @@ -76,8 +76,46 @@ void DeferredProp::apply(Computed &c) const {
if (not computed) {
logWarnIf(DEBUG_PROPS, "failed to parse declaration: {}: {}", decl, computed);
} else {
computed.unwrap().apply(c);
computed.unwrap().apply(parent, c);
}
}

// MARK: DefaultedProp ---------------------------------------------------------

void DefaultedProp::apply(Computed const &parent, Computed &c) const {
if (value == Default::INITIAL) {
StyleProp::any([&]<typename T>(Meta::Type<T>) {
if (T::name() != propName)
return false;
if constexpr (requires { T::initial(); })
StyleProp{T{T::initial()}}.apply(parent, c);
return true;
});
} else if (value == Default::INHERIT) {
StyleProp::any([&]<typename T>(Meta::Type<T>) {
if (T::name() != propName)
return false;
if constexpr (requires { T::load(parent); })
StyleProp{T{T::load(parent)}}.apply(parent, c);
return true;
});
} else if (value == Default::UNSET) {
StyleProp::any([&]<typename T>(Meta::Type<T>) {
if (T::name() != propName)
return false;
if constexpr (requires { T::inherit; T::load(parent); })
StyleProp{T{T::load(parent)}}.apply(parent, c);
else if constexpr (requires { T::initial(); })
StyleProp{T{T::initial()}}.apply(parent, c);
return true;
});
} else {
logDebug("defaulted: unsupported value '{}'", value);
}
}

void DefaultedProp::repr(Io::Emit &e) const {
e("(Defaulted {#} = {})", propName, value);
}

} // namespace Vaev::Style
Loading

0 comments on commit baacae9

Please sign in to comment.