43 inline bool parseFloatSV(std::string_view s,
float &out)
47 auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), out);
48 return ec == std::errc();
76 inline std::vector<CalcToken> tokenizeCalc(std::string_view s)
78 std::vector<CalcToken> out;
83 if (std::isspace((
unsigned char)c))
90 out.push_back({CalcToken::Kind::Plus});
96 out.push_back({CalcToken::Kind::Minus});
102 out.push_back({CalcToken::Kind::Star});
108 out.push_back({CalcToken::Kind::Slash});
114 out.push_back({CalcToken::Kind::LParen});
120 out.push_back({CalcToken::Kind::RParen});
126 out.push_back({CalcToken::Kind::Percent});
132 out.push_back({CalcToken::Kind::Comma});
137 if (std::isdigit((
unsigned char)c) || c ==
'.')
140 while (i < s.size() && (std::isdigit((
unsigned char)s[i]) || s[i] ==
'.'))
143 (void)parseFloatSV(s.substr(start, i - start), v);
145 t.kind = CalcToken::Kind::Number;
150 if (std::isalpha((
unsigned char)c) || c ==
'_')
153 while (i < s.size() && (std::isalnum((
unsigned char)s[i]) || s[i] ==
'_'))
156 t.kind = CalcToken::Kind::Ident;
157 t.ident = std::string(s.substr(start, i - start));
163 out.push_back({CalcToken::Kind::End});
168 inline bool parseOptFloatSV(std::string_view s, Opt<float> &out)
173 auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), v);
174 if (ec != std::errc())
191 explicit CalcParser(
const std::vector<CalcToken> &toks) : toks(toks) {}
193 bool parse(Value &out)
196 return !errored && pos < toks.size() && toks[pos].kind == CalcToken::Kind::End;
200 const std::vector<CalcToken> &toks;
204 const CalcToken &peek()
const {
return toks[pos]; }
207 if (pos + 1 < toks.size())
210 void error() { errored =
true; }
214 Value left = parseTerm();
215 while (!errored && (peek().kind == CalcToken::Kind::Plus ||
216 peek().kind == CalcToken::Kind::Minus))
218 auto op = peek().kind;
220 Value right = parseTerm();
221 left = (op == CalcToken::Kind::Plus) ? (left + right) : (left - right);
228 Value left = parseFactor();
229 while (!errored && (peek().kind == CalcToken::Kind::Star ||
230 peek().kind == CalcToken::Kind::Slash))
232 auto op = peek().kind;
234 Value right = parseFactor();
235 left = (op == CalcToken::Kind::Star) ? left.mulWith(right)
236 : left.divWith(right);
244 return Value::px(0.0f);
245 if (peek().kind == CalcToken::Kind::Minus)
248 return -parseFactor();
250 if (peek().kind == CalcToken::Kind::Plus)
253 return parseFactor();
255 if (peek().kind == CalcToken::Kind::LParen)
258 Value v = parseExpr();
259 if (peek().kind != CalcToken::Kind::RParen)
267 if (peek().kind == CalcToken::Kind::Ident)
269 std::string func = peek().ident;
271 if (peek().kind == CalcToken::Kind::LParen)
274 std::vector<Value> fargs;
275 if (peek().kind != CalcToken::Kind::RParen)
277 fargs.push_back(parseExpr());
278 while (!errored && peek().kind == CalcToken::Kind::Comma)
281 fargs.push_back(parseExpr());
284 if (peek().kind != CalcToken::Kind::RParen)
287 return Value::px(0.0f);
292 if (fargs.size() == 1)
295 return Value::px(0.0f);
298 ValueOp vop = ValueOp::None;
301 else if (func ==
"max")
303 else if (func ==
"clamp")
304 vop = ValueOp::Clamp;
305 else if (func ==
"round")
306 vop = ValueOp::Round;
307 else if (func ==
"floor")
308 vop = ValueOp::Floor;
309 else if (func ==
"ceil")
311 else if (func ==
"mod")
316 return Value::px(0.0f);
318 return Value::makeOp(vop, std::move(fargs));
321 return Value::px(0.0f);
323 if (peek().kind == CalcToken::Kind::Number)
325 float num = peek().num;
327 if (peek().kind == CalcToken::Kind::Percent)
330 return Value::percent(num);
332 if (peek().kind == CalcToken::Kind::Ident)
334 const std::string &u = peek().ident;
337 return Value::px(num);
339 return Value::vw(num);
341 return Value::vh(num);
343 return Value::pw(num);
345 return Value::ph(num);
346 return Value::px(num);
348 return Value::number(num);
351 return Value::px(0.0f);
358 inline FontStack parseFontStack(std::string_view raw)
362 while (i < raw.size())
364 while (i < raw.size() &&
365 (std::isspace((
unsigned char)raw[i]) || raw[i] ==
','))
371 if (raw[i] ==
'"' || raw[i] ==
'\'')
380 while (i < raw.size() && raw[i] != quote)
382 out.push_back(std::string(raw.substr(start, i - start)));
388 while (i < raw.size() &&
390 !std::isspace((
unsigned char)raw[i]))
392 out.push_back(std::string(raw.substr(start, i - start)));
409 return Value::autoSize();
411 if (s.starts_with(
"calc(") || s.starts_with(
"min(") || s.starts_with(
"max(") ||
412 s.starts_with(
"clamp(") || s.starts_with(
"round(") || s.starts_with(
"floor(") ||
413 s.starts_with(
"ceil(") || s.starts_with(
"mod("))
416 auto toks = detail::tokenizeCalc(s);
417 detail::CalcParser p(toks);
421 logWarn(
"StyleParser",
"", 0, 0,
"invalid math expression '" + std::string(s) +
"'");
428 while (n < s.size() && (std::isdigit((
unsigned char)s[n]) ||
429 s[n] ==
'.' || s[n] ==
'-' || s[n] ==
'+'))
433 if (!detail::parseFloatSV(s.substr(0, n), amount))
436 std::string_view unit = s.substr(n);
437 if (unit.empty() || unit ==
"px")
440 return Percent(amount);
459 return Colors::White;
461 return Colors::Black;
462 if (s ==
"transparent")
463 return Colors::Blank;
465 return Colors::Blank;
469 return Colors::Maroon;
471 return Colors::Green;
475 return Colors::Yellow;
479 return Colors::DarkGray;
480 if (s ==
"lightgray")
481 return Colors::LightGray;
482 if (s.size() > 4 && s[0] ==
'r' && s[1] ==
'g' && s[2] ==
'b')
484 bool hasAlpha = (s[3] ==
'a');
485 size_t paren = s.find(
'(');
486 if (paren != std::string_view::npos && s.back() ==
')')
488 std::string_view inner = s.substr(paren + 1, s.size() - paren - 2);
489 std::vector<float> parts;
497 parts.push_back(std::stof(cur));
505 else if (!std::isspace((
unsigned char)c))
514 parts.push_back(std::stof(cur));
522 const size_t expected = hasAlpha ? 4 : 3;
523 if (parts.size() != expected)
526 auto clamp255 = [](
float v) -> uint8_t
528 return (uint8_t)std::clamp((
int)std::lround(v), 0, 255);
531 uint8_t r = clamp255(parts[0]);
532 uint8_t g = clamp255(parts[1]);
533 uint8_t b = clamp255(parts[2]);
535 ? clamp255(parts[3] * 255.0f)
538 return Color{r, g, b, a};
541 if (!s.empty() && s[0] ==
'#')
543 std::string_view h = s.substr(1);
544 auto hex = [](
char c) ->
int
546 if (c >=
'0' && c <=
'9')
548 if (c >=
'a' && c <=
'f')
550 if (c >=
'A' && c <=
'F')
560 return Color{(uint8_t)(hex(h[0]) * 17), (uint8_t)(hex(h[1]) * 17), (uint8_t)(hex(h[2]) * 17), 255};
562 return Color{(uint8_t)(hex(h[0]) * 17), (uint8_t)(hex(h[1]) * 17), (uint8_t)(hex(h[2]) * 17), (uint8_t)(hex(h[3]) * 17)};
564 return Color{(uint8_t)(hex(h[0]) * 16 + hex(h[1])), (uint8_t)(hex(h[2]) * 16 + hex(h[3])), (uint8_t)(hex(h[4]) * 16 + hex(h[5])), 255};
566 return Color{(uint8_t)(hex(h[0]) * 16 + hex(h[1])), (uint8_t)(hex(h[2]) * 16 + hex(h[3])), (uint8_t)(hex(h[4]) * 16 + hex(h[5])), (uint8_t)(hex(h[6]) * 16 + hex(h[7]))};
577 std::vector<Value> vals;
581 while (i < s.size() && std::isspace((
unsigned char)s[i]))
590 else if (c ==
')' && depth > 0)
592 else if (depth == 0 && std::isspace((
unsigned char)c))
603 if (vals.size() == 1)
605 if (vals.size() == 2)
606 return Spacing(vals[0], vals[1]);
607 if (vals.size() == 3)
608 return Spacing(vals[0], vals[1], vals[2]);
609 if (vals.size() == 4)
610 return Spacing(vals[0], vals[1], vals[2], vals[3]);
614 inline std::optional<Align> parseAlignToken(std::string_view s)
629 inline std::optional<Direction> parseDirectionToken(std::string_view s)
632 return Direction::Row;
634 return Direction::Column;
636 return Direction::Auto;
640 inline std::optional<TextDirection> parseTextDirectionToken(std::string_view s)
651 inline std::optional<Justify> parseJustifyToken(std::string_view s)
659 if (s ==
"space-between")
664 inline std::optional<WhiteSpace> parseWhiteSpaceToken(std::string_view s)
667 return WhiteSpace::Normal;
669 return WhiteSpace::Nowrap;
673 inline std::optional<TextOverflow> parseTextOverflowToken(std::string_view s)
676 return TextOverflow::Visible;
678 return TextOverflow::Ellipsis;
682 inline std::optional<CursorShape> parseCursorToken(std::string_view s)
698 if (s ==
"ew-resize" || s ==
"col-resize")
700 if (s ==
"ns-resize" || s ==
"row-resize")
702 if (s ==
"nesw-resize")
704 if (s ==
"nwse-resize")
706 if (s ==
"not-allowed")
708 if (s ==
"crosshair")
710 if (s ==
"wait" || s ==
"progress")
728 (void)spec->apply(st, val, ctx);
741 const std::unordered_map<std::string, std::string> &customProps)
743 auto trimLocal = [](std::string s)
745 size_t a = 0, b = s.size();
746 while (a < b && std::isspace((
unsigned char)s[a]))
748 while (b > a && std::isspace((
unsigned char)s[b - 1]))
750 return s.substr(a, b - a);
755 while (changed && safety++ < 10)
759 while ((pos = v.find(
"var(", pos)) != std::string::npos)
761 size_t close = v.find(
')', pos);
762 if (close == std::string::npos)
764 std::string name = trimLocal(v.substr(pos + 4, close - pos - 4));
765 auto it = customProps.find(name);
766 if (it == customProps.end())
771 v.replace(pos, close - pos + 1, it->second);
782 const std::unordered_map<std::string, std::string> &unresolved,
783 const std::unordered_map<std::string, std::string> &customProps)
786 for (
auto &[k, raw] : unresolved)
Centralized standard library includes used throughout ZenitUI.
Small value types shared by the layout, rendering and input layers.
Easing curves used by transitions and CSS animations.
Lightweight, replaceable logging facility used across ZenitUI.
Executable metadata table for ZStyle properties, shorthands, and syntax constructs.
The Style / ComputedStyle pair and all style-related value types.
Length units and the compact 8-byte tagged Value expression representation.
@ Center
Center the whole set.
Definition CoreTypes.hpp:464
@ End
Pack at the trailing edge.
Definition CoreTypes.hpp:465
@ Start
Pack at the leading edge.
Definition CoreTypes.hpp:463
@ SpaceBetween
Distribute leftover space between items.
Definition CoreTypes.hpp:466
@ Auto
Defer to the parent's alignItems.
Definition CoreTypes.hpp:453
@ Center
Center within the available space.
Definition CoreTypes.hpp:455
@ End
Pack at the trailing edge.
Definition CoreTypes.hpp:456
@ Start
Pack at the leading edge.
Definition CoreTypes.hpp:454
@ Stretch
Stretch to fill the cross axis.
Definition CoreTypes.hpp:457
@ Auto
Infer paragraph direction from first strong Bidi character (fallback LTR).
Definition UIEnums.hpp:84
@ LTR
Explicit left-to-right direction.
Definition UIEnums.hpp:85
@ RTL
Explicit right-to-left direction.
Definition UIEnums.hpp:86
@ Auto
Inherit or fall back to Default.
Definition UIEnums.hpp:62
@ Crosshair
Precision crosshair (crosshair).
Definition UIEnums.hpp:74
@ Wait
Busy / loading indicator (wait, progress).
Definition UIEnums.hpp:75
@ ResizeEW
Horizontal resize (ew-resize, col-resize).
Definition UIEnums.hpp:69
@ ResizeNS
Vertical resize (ns-resize, row-resize).
Definition UIEnums.hpp:70
@ Pointer
Pointing hand (buttons, links, interactive controls).
Definition UIEnums.hpp:64
@ ResizeNESW
Diagonal NE-SW resize (nesw-resize).
Definition UIEnums.hpp:71
@ Move
Four-way move cursor.
Definition UIEnums.hpp:66
@ Grabbing
Closed hand (active drag).
Definition UIEnums.hpp:68
@ Default
Standard arrow cursor.
Definition UIEnums.hpp:63
@ ResizeNWSE
Diagonal NW-SE resize (nwse-resize).
Definition UIEnums.hpp:72
@ Text
I-beam (editable text fields).
Definition UIEnums.hpp:65
@ Grab
Open hand (draggable item).
Definition UIEnums.hpp:67
@ NotAllowed
Forbidden / disabled action (not-allowed).
Definition UIEnums.hpp:73
bool applyStyleAttr(Style &st, std::string_view key, std::string_view val)
Apply a single key: value declaration to st.
Definition StyleAttr.hpp:721
std::optional< Color > parseColorToken(std::string_view s)
Parse a color token.
Definition StyleAttr.hpp:456
Style resolveUnresolvedProps(const std::unordered_map< std::string, std::string > &unresolved, const std::unordered_map< std::string, std::string > &customProps)
Resolve every pending var(...) declaration in unresolved.
Definition StyleAttr.hpp:781
std::optional< Value > parseValueToken(std::string_view s)
Parse a single Value token.
Definition StyleAttr.hpp:404
std::optional< Spacing > parseSpacingToken(std::string_view s)
Parse a 1-4 value shorthand.
Definition StyleAttr.hpp:575
std::string substituteVarRefs(std::string v, const std::unordered_map< std::string, std::string > &customProps)
Recursively substitute var(--name) references in v.
Definition StyleAttr.hpp:739
const PropSpec * findStylePropSpec(std::string_view key)
O(1) lookup by ZStyle key; returns nullptr if unknown.
8-bit-per-channel RGBA color, non-premultiplied.
Definition CoreTypes.hpp:78
Context passed to executable property parsers.
Definition StyleSpecs.hpp:37
Executable + documentation descriptor for one ZStyle key.
Definition StyleSpecs.hpp:47
Four-sided Value tuple used for padding, margin and border-width.
Definition Style.hpp:486
Sparse style: every property is Opt<T>, unset by default.
Definition Style.hpp:932
8-byte tagged length expression:
Definition Unit.hpp:58