From 5d4a04169bdab9978e9a1bb0060d13a81bff66d7 Mon Sep 17 00:00:00 2001 From: Nimetu Date: Sun, 4 Jul 2021 16:56:54 +0300 Subject: [PATCH] Update getCssLength for new types, enable reading negative values. --- nel/include/nel/gui/libwww.h | 2 +- nel/src/gui/libwww.cpp | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/nel/include/nel/gui/libwww.h b/nel/include/nel/gui/libwww.h index df9c20cd8..a3d4c25fd 100644 --- a/nel/include/nel/gui/libwww.h +++ b/nel/include/nel/gui/libwww.h @@ -184,7 +184,7 @@ namespace NLGUI // *************************************************************************** // Read a CSS length value, return true if one of supported units '%, rem, em, px, pt' // On failure: 'value' and 'unit' values are undefined - bool getCssLength (float &value, std::string &unit, const std::string &str); + bool getCssLength (float &value, std::string &unit, const std::string &str, bool neg = false); // Read a width HTML parameter. "100" or "100%". Returns true if percent (0 ~ 1) else false bool getPercentage (sint32 &width, float &percent, const char *str); diff --git a/nel/src/gui/libwww.cpp b/nel/src/gui/libwww.cpp index 200e7baf1..02217a541 100644 --- a/nel/src/gui/libwww.cpp +++ b/nel/src/gui/libwww.cpp @@ -206,11 +206,15 @@ namespace NLGUI // *************************************************************************** // *************************************************************************** - bool getCssLength (float &value, std::string &unit, const std::string &str) + bool getCssLength (float &value, std::string &unit, const std::string &str, bool neg) { + static const std::set knownUnits = { + "%", "rem", "em", "px", "pt", "vw", "vh", "vi", "vb", "vmin", "vmax" + }; + std::string::size_type pos = 0; std::string::size_type len = str.size(); - if (len == 1 && str[0] == '.') + if (len == 0) { return false; } @@ -222,6 +226,12 @@ namespace NLGUI return true; } + // +100px; -100px + if (str[0] == '+') + pos++; + else if (neg && str[0] == '-') + pos++; + while(pos < len) { bool isNumeric = (str[pos] >= '0' && str[pos] <= '9') @@ -236,7 +246,7 @@ namespace NLGUI } unit = toLowerAscii(str.substr(pos)); - if (unit == "%" || unit == "rem" || unit == "em" || unit == "px" || unit == "pt") + if (knownUnits.count(unit)) { std::string tmpstr = str.substr(0, pos); return fromString(tmpstr, value);