From 565932c18d9312ea92f53b4f14809e97709d174e Mon Sep 17 00:00:00 2001 From: Nimetu Date: Tue, 1 Jun 2021 12:46:13 +0300 Subject: [PATCH] Expand css margin shorthand --- nel/include/nel/gui/css_style.h | 5 ++++ nel/src/gui/css_style.cpp | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/nel/include/nel/gui/css_style.h b/nel/include/nel/gui/css_style.h index 7a1d11ee5..604fac431 100644 --- a/nel/include/nel/gui/css_style.h +++ b/nel/include/nel/gui/css_style.h @@ -72,6 +72,7 @@ namespace NLGUI // background BackgroundColor=NLMISC::CRGBA::Black; BackgroundColorOver=NLMISC::CRGBA::Black; + MarginTop = MarginRight = MarginBottom = MarginLeft = 0; PaddingTop = PaddingRight = PaddingBottom = PaddingLeft = 0; } @@ -106,6 +107,7 @@ namespace NLGUI NLMISC::CRGBA BorderTopColor, BorderRightColor, BorderBottomColor, BorderLeftColor; NLMISC::CRGBA BackgroundColor; NLMISC::CRGBA BackgroundColorOver; + uint32 MarginTop, MarginRight, MarginBottom, MarginLeft; uint32 PaddingTop, PaddingRight, PaddingBottom, PaddingLeft; std::string WhiteSpace; @@ -177,6 +179,7 @@ namespace NLGUI // parse 'padding' into 'padding-top', 'padding-left', etc void expandPaddingShorthand(const std::string &value, TStyle &style) const; + void expandMarginShorthand(const std::string &value, TStyle &style) const; // expand shorthand rule, ie "border", into longhand names, ie "border-top-width" // if shorthand is present in style, then its removed @@ -187,6 +190,7 @@ namespace NLGUI void applyBorderColor(const std::string &value, NLMISC::CRGBA *dest, const NLMISC::CRGBA ¤tColor, const NLMISC::CRGBA &textColor) const; void applyLineStyle(const std::string &value, CSSLineStyle *dest, const CSSLineStyle ¤tStyle) const; void applyPaddingWidth(const std::string &value, uint32 *dest, const uint32 currentPadding, uint32 fontSize) const; + void applyMarginWidth(const std::string &value, uint32 *dest, const uint32 current, uint32 fontSize) const; // parse and replace var(--name, fallback) function // return false if property should be ignored @@ -224,6 +228,7 @@ namespace NLGUI Current.BorderTopWidth = Current.BorderRightWidth = Current.BorderBottomWidth = Current.BorderLeftWidth = CSS_LINE_WIDTH_MEDIUM; Current.BorderTopStyle = Current.BorderRightStyle = Current.BorderBottomStyle = Current.BorderLeftStyle = CSS_LINE_STYLE_NONE; Current.BorderTopColor = Current.BorderRightColor = Current.BorderBottomColor = Current.BorderLeftColor = Current.TextColor; + Current.MarginTop = Current.MarginRight = Current.MarginBottom = Current.MarginLeft = 0; Current.PaddingTop = Current.PaddingRight = Current.PaddingBottom = Current.PaddingLeft = 0; Current.StyleRules.clear(); diff --git a/nel/src/gui/css_style.cpp b/nel/src/gui/css_style.cpp index e4a9751fa..c2b4a6ff1 100644 --- a/nel/src/gui/css_style.cpp +++ b/nel/src/gui/css_style.cpp @@ -606,6 +606,39 @@ namespace NLGUI } } + void CCssStyle::applyMarginWidth(const std::string &value, uint32 *dest, const uint32 current, uint32 fontSize) const + { + if (!dest) return; + + if (value == "inherit") + { + *dest = current; + return; + } + else if (value == "auto") + { + // TODO: requires content width; + *dest = 0; + return; + } + + float tmpf; + std::string unit; + if (getCssLength(tmpf, unit, value.c_str())) + { + if (unit == "rem") + *dest = fontSize * tmpf; + else if (unit == "em") + *dest = fontSize * tmpf; + else if (unit == "pt") + *dest = tmpf / 0.75f; + else if (unit == "%") + *dest = 0; // TODO: requires content width, must remember 'unit' type + else + *dest = tmpf; + } + } + // apply style rules void CCssStyle::apply(CStyleParams &style, const CStyleParams ¤t) const { @@ -625,6 +658,10 @@ namespace NLGUI else if (it->first == "border-left-width") applyBorderWidth(it->second, &style.BorderLeftWidth, current.BorderLeftWidth, current.FontSize); else if (it->first == "border-left-color") applyBorderColor(it->second, &style.BorderLeftColor, current.BorderLeftColor, current.TextColor); else if (it->first == "border-left-style") applyLineStyle(it->second, &style.BorderLeftStyle, current.BorderLeftStyle); + else if (it->first == "margin-top") applyMarginWidth(it->second, &style.MarginTop, current.MarginTop, current.FontSize); + else if (it->first == "margin-right") applyMarginWidth(it->second, &style.MarginRight, current.MarginRight, current.FontSize); + else if (it->first == "margin-bottom") applyMarginWidth(it->second, &style.MarginBottom, current.MarginBottom, current.FontSize); + else if (it->first == "margin-left") applyMarginWidth(it->second, &style.MarginLeft, current.MarginLeft, current.FontSize); else if (it->first == "padding-top") applyPaddingWidth(it->second, &style.PaddingTop, current.PaddingTop, current.FontSize); else if (it->first == "padding-right") applyPaddingWidth(it->second, &style.PaddingRight, current.PaddingRight, current.FontSize); else if (it->first == "padding-bottom") applyPaddingWidth(it->second, &style.PaddingBottom, current.PaddingBottom, current.FontSize); @@ -1556,6 +1593,22 @@ namespace NLGUI style["padding-left"] = parts[l]; } + // *************************************************************************** + void CCssStyle::expandMarginShorthand(const std::string &value, TStyle &style) const + { + std::vector parts; + splitParams(toLowerAscii(value), ' ', parts); + + uint8 t, r, b, l; + if (!getShorthandIndices(parts.size(), t, r, b, l)) + return; + + style["margin-top"] = parts[t]; + style["margin-right"] = parts[r]; + style["margin-bottom"] = parts[b]; + style["margin-left"] = parts[l]; + } + // *************************************************************************** void CCssStyle::expandShorthand(const std::string &prop, const std::string &value, TStyle &style) const {