Skip to content

Commit

Permalink
simplify PropertyParseKeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Jan 20, 2024
1 parent 0b9a627 commit 4f981ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/ant.rmlui/src/css/EnumName.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ size_t GetCssEnumIndex(std::string_view name) {
if (name == CssEnumNameV<Style, static_cast<E>(I)>) {
return (size_t)I;
}
return GetCssEnumIndex<E, I+1>(name);
return GetCssEnumIndex<Style, E, I+1>(name);
}
return (size_t)-1;
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/ant.rmlui/src/css/PropertyParserKeyword.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <css/Property.h>
#include <css/EnumName.h>
#include <util/StaticString.h>
#include <tuple>

Expand Down Expand Up @@ -30,4 +31,14 @@ Property PropertyParseKeyword(PropertyId id, const std::string& value) {
return { id, (PropertyKeyword)v };
}

template <typename E>
requires(std::is_enum_v<E>)
Property PropertyParseKeyword(PropertyId id, const std::string& value) {
size_t v = GetCssEnumIndex<CssEnumNameStyle::Kebab, E>(value);
if (v == (size_t)-1 || v > (size_t)std::numeric_limits<PropertyKeyword>::max()) {
return {};
}
return { id, (PropertyKeyword)v };
}

}
53 changes: 28 additions & 25 deletions pkg/ant.rmlui/src/css/StyleSheetSpecification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <util/ConstexprMap.h>
#include <util/AlwaysFalse.h>
#include <core/Layout.h>
#include <core/ComputedValues.h>
#include <array>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -132,10 +133,10 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
}},

{ PropertyId::FontStyle, {
PropertyParseKeyword<"normal", "italic">,
PropertyParseKeyword<Style::FontStyle>,
}},
{ PropertyId::FontWeight, {
PropertyParseKeyword<"normal", "bold">,
PropertyParseKeyword<Style::FontWeight>,
}},
{ PropertyId::FontSize, {
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
Expand All @@ -145,14 +146,14 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
}},

{ PropertyId::TextAlign, {
PropertyParseKeyword<"left", "right", "center", "justify">,
PropertyParseKeyword<Style::TextAlign>,
}},
{ PropertyId::WordBreak, {
PropertyParseKeyword<"normal", "break-all", "break-word">,
PropertyParseKeyword<Style::WordBreak>,
}},

{ PropertyId::TextDecorationLine, {
PropertyParseKeyword<"none", "underline", "overline", "line-through">,
PropertyParseKeyword<Style::TextDecorationLine>,
}},
{ PropertyId::TextDecorationColor, {
PropertyParseKeyword<"currentColor">,
Expand All @@ -165,22 +166,22 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
PropertyParseNumber<PropertyParseNumberUnit::Length>,
}},
{ PropertyId::PerspectiveOriginX, {
PropertyParseKeyword<"left", "center", "right">,
PropertyParseKeyword<Style::OriginX>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::PerspectiveOriginY, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginY>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::Transform, {
PropertyParseTransform,
}},
{ PropertyId::TransformOriginX, {
PropertyParseKeyword<"left", "center", "right">,
PropertyParseKeyword<Style::OriginX>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::TransformOriginY, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginY>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::TransformOriginZ, {
Expand All @@ -202,10 +203,10 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
PropertyParseString,
}},
{ PropertyId::BackgroundOrigin, {
PropertyParseKeyword<"padding-box", "border-box", "content-box">,
PropertyParseKeyword<Style::BoxType>,
}},
{ PropertyId::BackgroundSize, {
PropertyParseKeyword<"unset", "auto", "cover", "contain">,
PropertyParseKeyword<Style::BackgroundSize>,
}},
{ PropertyId::BackgroundSizeX, {
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
Expand All @@ -215,47 +216,49 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
}},

{ PropertyId::BackgroundPositionX, {
PropertyParseKeyword<"left", "center", "right">,
PropertyParseKeyword<Style::OriginX>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::BackgroundPositionY, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginY>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},

{ PropertyId::BackgroundRepeat, {
PropertyParseKeyword<"repeat", "repeat-x", "repeat-y", "no-repeat">,
}},
//TODO:
//{ PropertyId::BackgroundRepeat, {
// PropertyParseKeyword<"repeat", "repeat-x", "repeat-y", "no-repeat">,
//}},

{ PropertyId::BackgroundFilter, {
PropertyParseKeyword<"none">,
PropertyParseColour,
}},

{ PropertyId::BackgroundLattice, {
PropertyParseKeyword<"auto", "cover", "contain">,
PropertyParseKeyword<Style::BackgroundLattice>,
}},
{ PropertyId::BackgroundLatticeX1, {
PropertyParseKeyword<"left", "center", "right">,
PropertyParseKeyword<Style::OriginX>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::BackgroundLatticeY1, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginY>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::BackgroundLatticeX2, {
PropertyParseKeyword<"left", "center", "right">,
PropertyParseKeyword<Style::OriginX>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::BackgroundLatticeY2, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginY>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::BackgroundLatticeU, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginX>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},
{ PropertyId::BackgroundLatticeV, {
PropertyParseKeyword<"top", "center", "bottom">,
PropertyParseKeyword<Style::OriginY>,
PropertyParseNumber<PropertyParseNumberUnit::LengthPercent>,
}},

Expand Down Expand Up @@ -284,7 +287,7 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
}},

{ PropertyId::PointerEvents, {
PropertyParseKeyword<"none", "auto">,
PropertyParseKeyword<Style::PointerEvents>,
}},
{ PropertyId::ScrollLeft, {
PropertyParseNumber<PropertyParseNumberUnit::Length>,
Expand All @@ -293,7 +296,7 @@ static constexpr auto PropertyDefinitions = MakeEnumArray<PropertyId, PropertyDe
PropertyParseNumber<PropertyParseNumberUnit::Length>,
}},
{ PropertyId::Filter, {
PropertyParseKeyword<"none", "gray">,
PropertyParseKeyword<Style::Filter>,
}},

// flex layout
Expand Down

0 comments on commit 4f981ec

Please sign in to comment.