Update getCssLength for new types, enable reading negative values.

develop
Nimetu 3 years ago
parent b2ec03d1a1
commit 5d4a04169b

@ -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);

@ -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<std::string> 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);

Loading…
Cancel
Save