1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2021 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "nel/gui/css_length.h"
22 using namespace NLMISC
;
31 bool CSSLength::parseValue(const std::string
&value
, bool allowPercent
, bool allowNegative
)
33 static const std::set
<std::string
> knownUnits
= {
34 "%", "rem", "em", "px", "pt", "vw", "vh", "vi", "vb", "vmin", "vmax"
37 std::string::size_type pos
= 0;
38 std::string::size_type len
= value
.size();
42 if (len
== 1 && value
[0] == '0')
52 else if (allowNegative
&& value
[0] == '-')
57 bool isNumeric
= (value
[pos
] >= '0' && value
[pos
] <= '9')
58 || (pos
== 0 && value
[pos
] == '.')
59 || (pos
> 0 && value
[pos
] == '.' && value
[pos
-1] >= '0' && value
[pos
-1] <= '9');
67 std::string unit
= toLowerAscii(value
.substr(pos
));
68 if (knownUnits
.count(unit
))
70 std::string tmpstr
= value
.substr(0, pos
);
71 return fromString(tmpstr
, m_Value
);
77 float CSSLength::getValue() const
79 if (m_Unit
== CSS_UNIT_PERCENT
)
80 return m_Value
/ 100.f
;
84 void CSSLength::setFloatValue(float f
, const std::string
&unit
)
90 void CSSLength::setUnit(const std::string
&unit
)
94 m_Unit
= CSS_UNIT_NONE
;
97 else if (unit
== "px")
101 } else if (unit
== "pt")
103 m_Unit
= CSS_UNIT_PT
;
105 } else if (unit
== "%")
107 m_Unit
= CSS_UNIT_PERCENT
;
109 } else if (unit
== "em")
111 m_Unit
= CSS_UNIT_EM
;
113 } else if (unit
== "rem")
115 m_Unit
= CSS_UNIT_REM
;
117 } else if (unit
== "vw")
119 m_Unit
= CSS_UNIT_VW
;
121 } else if (unit
== "vh")
123 m_Unit
= CSS_UNIT_VH
;
125 } else if (unit
== "vi")
127 m_Unit
= CSS_UNIT_VI
;
129 } else if (unit
== "vb")
131 m_Unit
= CSS_UNIT_VB
;
133 } else if (unit
== "vmin")
135 m_Unit
= CSS_UNIT_VMIN
;
137 } else if (unit
== "vmax")
139 m_Unit
= CSS_UNIT_VMAX
;
141 } else if (unit
== "auto")
143 m_Unit
= CSS_UNIT_NONE
;
148 m_Unit
= CSS_UNIT_NONE
;
153 float CSSLength::calculate(uint32 relValue
, uint32 emSize
, uint32 remSize
, uint32 vwSize
, uint32 vhSize
= 0) const
158 float value
= getValue();
162 return emSize
* value
;
164 return remSize
* value
;
165 case CSS_UNIT_PERCENT
:
166 return relValue
* value
;
172 // Vi for horizontal writing mode only
173 return (float)vwSize
*0.01f
;
176 // Vb for horizontal writing mode only
177 return (float)vhSize
*0.01f
;
179 return (float)std::min(vwSize
, vhSize
)*0.01f
;
181 return (float)std::max(vwSize
, vhSize
)*0.01f
;
184 nldebug("Unknown CSS unit '%s'", toString().c_str());
188 std::string
CSSLength::toString() const
194 ret
+= NLMISC::toString("%f", m_Value
);
196 size_t pos
= ret
.find(".");
197 for( ; pos
< ret
.size(); ++pos
)
202 if (pos
== ret
.size())
203 ret
= ret
.substr(0, ret
.find("."));
207 case CSS_UNIT_NONE
: break;
208 case CSS_UNIT_EM
: ret
+= "em"; break;
209 case CSS_UNIT_REM
: ret
+= "rem"; break;
210 case CSS_UNIT_PERCENT
: ret
+= "%"; break;
211 case CSS_UNIT_PX
: ret
+= "px"; break;
212 case CSS_UNIT_PT
: ret
+= "pt"; break;
213 case CSS_UNIT_VW
: ret
+= "vw"; break;
214 case CSS_UNIT_VH
: ret
+= "vh"; break;
215 case CSS_UNIT_VI
: ret
+= "vi"; break;
216 case CSS_UNIT_VB
: ret
+= "vb"; break;
217 case CSS_UNIT_VMIN
: ret
+= "vmin"; break;
218 case CSS_UNIT_VMAX
: ret
+= "vmax"; break;