Changed: Parse css pt/em/rem size values

--HG--
branch : develop
This commit is contained in:
Nimetu 2019-03-12 20:03:34 +02:00
parent d62c139ee0
commit b6d3ff8323
3 changed files with 118 additions and 12 deletions

View file

@ -280,6 +280,9 @@ namespace NLGUI
#undef HTML_ATTR
// ***************************************************************************
// 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);
// 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);

View file

@ -1851,10 +1851,11 @@ namespace NLGUI
// Get the string name
if (present[MY_HTML_IMG_SRC] && value[MY_HTML_IMG_SRC])
{
CStyleParams style;
float tmpf;
std::string id;
CStyleParams style;
style.FontSize = _Style.FontSize;
if (present[MY_HTML_IMG_ID] && value[MY_HTML_IMG_ID])
id = value[MY_HTML_IMG_ID];
@ -2190,6 +2191,7 @@ namespace NLGUI
if (!(_Forms.empty()))
{
CStyleParams style;
style.FontSize = _Style.FontSize;
// A select box
string name;
@ -2699,6 +2701,7 @@ namespace NLGUI
if (sep)
{
CStyleParams style;
style.FontSize = _Style.FontSize;
style.TextColor = CRGBA(120, 120, 120, 255);
style.Height = 0;
style.Width = 0;
@ -6291,22 +6294,39 @@ namespace NLGUI
float tmpf;
TStyle styles = parseStyle(styleString);
TStyle::iterator it;
// first pass: get font-size for 'em' sizes
for (it=styles.begin(); it != styles.end(); ++it)
{
if (it->first == "font-size")
{
if (it->second == "inherit")
{
style.FontSize = current.FontSize;
}
else
{
float tmp;
sint size = 0;
getPercentage (size, tmp, it->second.c_str());
if (size > 0)
style.FontSize = size;
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
if (unit == "rem")
style.FontSize = _StyleDefault.FontSize * tmpf;
else if (unit == "em")
style.FontSize = current.FontSize * tmpf;
else if (unit == "pt")
style.FontSize = tmpf / 0.75f;
else if (unit == "%")
style.FontSize = current.FontSize * tmpf / 100.f;
else
style.FontSize = tmpf;
}
}
}
else
}
// second pass: rest of style
for (it=styles.begin(); it != styles.end(); ++it)
{
if (it->first == "font-style")
{
if (it->second == "inherit")
@ -6485,16 +6505,68 @@ namespace NLGUI
}
else
if (it->first == "width")
getPercentage(style.Width, tmpf, it->second.c_str());
{
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
if (unit == "rem")
style.Width = tmpf * _StyleDefault.FontSize;
else if (unit == "em")
style.Width = tmpf * style.FontSize;
else if (unit == "pt")
style.FontSize = tmpf / 0.75f;
else
style.Width = tmpf;
}
}
else
if (it->first == "height")
getPercentage(style.Height, tmpf, it->second.c_str());
{
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
if (unit == "rem")
style.Height = tmpf * _StyleDefault.FontSize;
else if (unit == "em")
style.Height = tmpf * style.FontSize;
else if (unit == "pt")
style.FontSize = tmpf / 0.75f;
else
style.Height = tmpf;
}
}
else
if (it->first == "max-width")
getPercentage(style.MaxWidth, tmpf, it->second.c_str());
{
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
if (unit == "rem")
style.MaxWidth = tmpf * _StyleDefault.FontSize;
else if (unit == "em")
style.MaxWidth = tmpf * style.FontSize;
else if (unit == "pt")
style.FontSize = tmpf / 0.75f;
else
style.MaxWidth = tmpf;
}
}
else
if (it->first == "max-height")
getPercentage(style.MaxHeight, tmpf, it->second.c_str());
{
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
if (unit == "rem")
style.MaxHeight = tmpf * _StyleDefault.FontSize;
else if (unit == "em")
style.MaxHeight = tmpf * style.FontSize;
else if (unit == "pt")
style.FontSize = tmpf / 0.75f;
else
style.MaxHeight = tmpf;
}
}
else
if (it->first == "-ryzom-modulate-color")
{

View file

@ -293,6 +293,37 @@ namespace NLGUI
};
// ***************************************************************************
bool getCssLength (float &value, std::string &unit, const std::string &str)
{
std::string::size_type pos = 0;
std::string::size_type len = str.size();
if (len == 1 && str[0] == '.')
{
return false;
}
while(pos < len)
{
bool isNumeric = (str[pos] >= '0' && str[pos] <= '9')
|| (pos == 0 && str[pos] == '.')
|| (pos > 0 && str[pos] == '.' && str[pos-1] >= '0' && str[pos-1] <= '9');
if (!isNumeric)
{
break;
}
pos++;
}
unit = toLower(str.substr(pos));
if (unit == "%" || unit == "rem" || unit == "em" || unit == "px" || unit == "pt")
{
std::string tmpstr = str.substr(0, pos);
return fromString(tmpstr, value);
}
return 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)