Changed: Normalize style values

--HG--
branch : develop
This commit is contained in:
Nimetu 2019-05-04 10:18:24 +03:00
parent 7ca3b85c54
commit 9444e05755
3 changed files with 109 additions and 47 deletions

View file

@ -47,7 +47,7 @@ namespace NLGUI
sint32 X;
sint32 Y;
NLMISC::CRGBA Color;
};
};
public:
CStyleParams () : FontFamily(""), TextColor(255,255,255,255), TextShadow()
{
@ -110,7 +110,7 @@ namespace NLGUI
// pseudo element like ':before'
std::string PseudoElement;
// returns selector specificity
uint specificity() const;
};
@ -134,6 +134,12 @@ namespace NLGUI
void getStyleParams(const std::string &styleString, CStyleParams &style, const CStyleParams &current) const;
void getStyleParams(const TStyle &styleRules, CStyleParams &style, const CStyleParams &current) const;
// extract from styleRules into style.StyleRules (expand shorthand, normalize, calculate current font-size)
void normalize(const TStyle &styleRules, CStyleParams &style, const CStyleParams &current) const;
// apply style.StyleRyles
void apply(CStyleParams &style, const CStyleParams &current) const;
// merge src into dest by overwriting key in dest
void merge(TStyle &dst, const TStyle &src) const;
@ -171,7 +177,7 @@ namespace NLGUI
Current.MaxWidth=-1;
Current.MaxHeight=-1;
Current.BorderWidth=1;
Current.StyleRules.clear();
}

View file

@ -332,10 +332,18 @@ namespace NLGUI
return;
}
// first pass:
// - get font-size for 'em' sizes
// - split shorthand to its parts
// - get TextColor value that could be used for 'currentcolor'
normalize(styleRules, style, current);
apply(style, current);
}
// first pass
// - get font-size for 'em' sizes
// - split shorthand to its parts
// - get TextColor value that could be used for 'currentcolor'
// - normalize values
void CCssStyle::normalize(const TStyle &styleRules, CStyleParams &style, const CStyleParams &current) const
{
TStyle::const_iterator it;
for (it=styleRules.begin(); it != styleRules.end(); ++it)
{
// update local copy of applied style
@ -407,6 +415,7 @@ namespace NLGUI
}
else
{
float tmpf;
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
@ -428,9 +437,38 @@ namespace NLGUI
{
parseBackgroundShorthand(it->second, style);
}
}
else
if (it->first == "background-repeat")
{
// old ryzom specific value
if (it->second == "1")
style.StyleRules[it->first] = "repeat";
}
else
if (it->first == "background-scale")
{
// replace old ryzom specific rule with background-size
if (it->second != "1")
{
style.StyleRules["background-size"] = "auto";
}
else
{
style.StyleRules["background-size"] = "100%";
}
// second pass: use style own StyleRules as its updated from first pass
TStyle::iterator pos = style.StyleRules.find(it->first);
if (pos != style.StyleRules.end())
style.StyleRules.erase(pos);
}
}
}
// apply style rules
void CCssStyle::apply(CStyleParams &style, const CStyleParams &current) const
{
float tmpf;
TStyle::const_iterator it;
for (it=style.StyleRules.begin(); it != style.StyleRules.end(); ++it)
{
if (it->first == "border" || it->first == "border-width")
@ -458,6 +496,7 @@ namespace NLGUI
}
else
{
float tmpf;
std::string unit;
if (getCssLength(tmpf, unit, it->second.c_str()))
{
@ -767,6 +806,42 @@ namespace NLGUI
else
scanHTMLColor(it->second.c_str(), style.BackgroundColorOver);
}
else
if (it->first == "background-image")
{
// normalize
std::string image = trim(it->second);
if (toLower(image.substr(0, 4)) == "url(")
{
image = image.substr(4, image.size()-5);
}
style.StyleRules[it->first] = trimQuotes(image);
}
else
if (it->first == "background-repeat")
{
// normalize
std::string val = toLower(trim(it->second));
std::vector<std::string> parts;
NLMISC::splitString(val, " ", parts);
// check for "repeat repeat"
if (parts.size() == 2 && parts[0] == parts[1])
val = parts[0];
style.StyleRules[it->first] = val;
}
else
if (it->first == "background-size")
{
// normalize
std::string val = toLower(trim(it->second));
std::vector<std::string> parts;
NLMISC::splitString(val, " ", parts);
if (parts.size() == 2 && parts[0] == parts[1])
val = parts[0];
style.StyleRules[it->first] = val;
}
}
// if outer element has underline set, then inner element cannot remove it

View file

@ -1220,7 +1220,7 @@ namespace NLGUI
{
std::string::size_type start;
std::string token;
// not supported
// counter, open-quote, close-quote, no-open-quote, no-close-quote
if (content[pos] == '"' || content[pos] == '\'')
@ -3271,9 +3271,9 @@ namespace NLGUI
setTitle(_TitleString);
}
std::string CGroupHTML::getTitle() const {
return _TitleString.toUtf8();
return _TitleString.toUtf8();
};
// ***************************************************************************
@ -4075,7 +4075,7 @@ namespace NLGUI
{
// clear the page
beginBuild();
// clear previous page and state
removeContent();
@ -4698,7 +4698,7 @@ namespace NLGUI
nlwarning("BUG: unable to find current element iterator from parent");
return;
}
// where fragment should be moved
std::list<CHtmlElement>::iterator insertBefore;
if (_CurrentHTMLNextSibling == NULL)
@ -5010,7 +5010,7 @@ namespace NLGUI
valign = _Style.Current.VerticalAlign;
else if (elm.hasNonEmptyAttribute("valign"))
valign = toLower(elm.getAttribute("valign"));
if (valign == "top")
cellParams.VAlign = CGroupCell::Top;
else if (valign == "middle")
@ -5018,7 +5018,7 @@ namespace NLGUI
else if (valign == "bottom")
cellParams.VAlign = CGroupCell::Bottom;
}
_CellParams.push_back (cellParams);
}
@ -5030,15 +5030,9 @@ namespace NLGUI
// non-empty image
if (_Style.hasStyle("background-image"))
{
// value '1' and 'background-scale' are ryzom only
bool repeat = _Style.checkStyle("background-repeat", "1") || _Style.checkStyle("background-repeat", "repeat");
bool scale = _Style.checkStyle("background-scale", "1") || _Style.checkStyle("background-size", "100% 100%");
std::string image = trim(_Style.getStyle("background-image"));
string::size_type texExt = toLower(image).find("url(");
if (texExt != string::npos)
{
image = image.substr(texExt+4, image.size()-texExt-5);
}
bool repeat = _Style.checkStyle("background-repeat", "repeat");
bool scale = _Style.checkStyle("background-size", "100%");
std::string image = _Style.getStyle("background-image");
if (!image.empty())
{
if (root)
@ -5063,7 +5057,7 @@ namespace NLGUI
{
setBackgroundColor(bgColor);
}
// TODO: else
// TODO: else
}
}
@ -5369,7 +5363,7 @@ namespace NLGUI
{
newParagraph(LIBeginSpace);
}
renderPseudoElement(":before", elm);
}
@ -5597,7 +5591,7 @@ namespace NLGUI
// no 'type' attribute, or empty
return;
}
// Global color flag
if (elm.hasAttribute("global_color"))
_Style.Current.GlobalColor = true;
@ -5862,7 +5856,7 @@ namespace NLGUI
_ParsingLua = _TrustedDomain; // Only parse lua if TrustedDomain
_LuaScript.clear();
}
void CGroupHTML::htmlLUAend(const CHtmlElement &elm)
{
if (_ParsingLua && _TrustedDomain)
@ -6189,7 +6183,7 @@ namespace NLGUI
{
if (elm.hasNonEmptyAttribute("cellspacing"))
fromString(elm.getAttribute("cellspacing"), table->CellSpacing);
// TODO: cssLength, horiz/vert values
if (_Style.hasStyle("border-spacing"))
fromString(_Style.getStyle("border-spacing"), table->CellSpacing);
@ -6296,29 +6290,16 @@ namespace NLGUI
_Cells.back() = new CGroupCell(CViewBase::TCtorParam());
if (_Style.checkStyle("background-repeat", "1") || _Style.checkStyle("background-repeat", "repeat"))
if (_Style.checkStyle("background-repeat", "repeat"))
_Cells.back()->setTextureTile(true);
if (_Style.checkStyle("background-scale", "1") || _Style.checkStyle("background-size", "100% 100%"))
if (_Style.checkStyle("background-size", "100%"))
_Cells.back()->setTextureScale(true);
if (_Style.hasStyle("background-image"))
{
string image = _Style.getStyle("background-image");
string::size_type texExt = toLower(image).find("url(");
// Url image
if (texExt != string::npos)
{
// Remove url()
image = image.substr(4, image.size()-5);
addImageDownload(image, _Cells.back());
// Image in BNP
}
else
{
_Cells.back()->setTexture(image);
}
addImageDownload(image, _Cells.back());
}
if (elm.hasNonEmptyAttribute("colspan"))
@ -6339,7 +6320,7 @@ namespace NLGUI
getPercentage (_Cells.back()->WidthWanted, _Cells.back()->TableRatio, _Style.getStyle("width").c_str());
else if (elm.hasNonEmptyAttribute("width"))
getPercentage (_Cells.back()->WidthWanted, _Cells.back()->TableRatio, elm.getAttribute("width").c_str());
if (_Style.hasStyle("height"))
getPercentage (_Cells.back()->Height, temp, _Style.getStyle("height").c_str());
else if (elm.hasNonEmptyAttribute("height"))