File tree Expand file tree Collapse file tree 2 files changed +78
-0
lines changed
Expand file tree Collapse file tree 2 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ #include " length.h"
2+
3+ #include < stdexcept>
4+
5+ using namespace Gfx ;
6+
7+ Length::Length (const std::string &s)
8+ {
9+ Text::ValueUnit parser (s);
10+ auto unit = parser.getUnit ();
11+ if (unit == " %" )
12+ {
13+ relative = parser.getValue ();
14+ }
15+ else if (unit == " px" || unit.empty ())
16+ {
17+ absolute = parser.getValue ();
18+ }
19+ else throw std::logic_error (" invalid length unit: " + unit);
20+ }
21+
22+ Length::operator std::string () const
23+ {
24+ if (relative == 0.0 ) return std::to_string (absolute) + " px" ;
25+ if (absolute == 0.0 ) return std::to_string (relative) + " %" ;
26+ return std::to_string (absolute) + " px"
27+ + " +" + std::to_string (relative) + " %" ;
28+ }
Original file line number Diff line number Diff line change 1+ #ifndef GFX_LENGTH
2+ #define GFX_LENGTH
3+
4+ #include < string>
5+
6+ #include " base/text/valueunit.h"
7+
8+ namespace Gfx
9+ {
10+
11+ class Length
12+ {
13+ public:
14+ double absolute;
15+ double relative;
16+
17+ Length () : absolute(0 ), relative(0 ) {}
18+
19+ static Length Absolute (double value) { return Length (value, 0 ); }
20+ static Length Relative (double value) { return Length (0 , value); }
21+
22+ Length (double abs, double rel) :
23+ absolute (abs), relative(rel)
24+ {}
25+
26+ explicit Length (const std::string &s);
27+
28+ double get (double reference) const {
29+ return absolute + relative * reference;
30+ }
31+
32+ explicit operator std::string () const ;
33+
34+ bool operator ==(const Length &other) const {
35+ return absolute == other.absolute
36+ && relative == other.relative ;
37+ }
38+
39+ Length operator *(double scale) const {
40+ return Length (absolute * scale, relative * scale);
41+ }
42+
43+ Length operator +(const Length &other) const {
44+ return Length (absolute + other.absolute , relative + other.relative );
45+ }
46+ };
47+
48+ }
49+
50+ #endif
You can’t perform that action at this time.
0 commit comments