From b6d3ff8323c17a5c7b382fad601ccd75e244552a Mon Sep 17 00:00:00 2001 From: Nimetu Date: Tue, 12 Mar 2019 20:03:34 +0200 Subject: [PATCH] Changed: Parse css pt/em/rem size values --HG-- branch : develop --- code/nel/include/nel/gui/libwww.h | 3 + code/nel/src/gui/group_html.cpp | 96 +++++++++++++++++++++++++++---- code/nel/src/gui/libwww.cpp | 31 ++++++++++ 3 files changed, 118 insertions(+), 12 deletions(-) diff --git a/code/nel/include/nel/gui/libwww.h b/code/nel/include/nel/gui/libwww.h index 112cae7d7..84eeb01e9 100644 --- a/code/nel/include/nel/gui/libwww.h +++ b/code/nel/include/nel/gui/libwww.h @@ -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); diff --git a/code/nel/src/gui/group_html.cpp b/code/nel/src/gui/group_html.cpp index 7b81ef746..514bccf15 100644 --- a/code/nel/src/gui/group_html.cpp +++ b/code/nel/src/gui/group_html.cpp @@ -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") { diff --git a/code/nel/src/gui/libwww.cpp b/code/nel/src/gui/libwww.cpp index bb32fd5b6..ca8920b14 100644 --- a/code/nel/src/gui/libwww.cpp +++ b/code/nel/src/gui/libwww.cpp @@ -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)