Photon 1.0.0
Loading...
Searching...
No Matches
css_length.h
Go to the documentation of this file.
1#ifndef LH_CSS_LENGTH_H
2#define LH_CSS_LENGTH_H
3
4#include "types.h"
5#include "css_tokenizer.h"
6
7namespace litehtml
8{
9 // from_token options (flags)
10 enum {
11 f_length = 1, // <length> (includes unitless zero)
12 f_percentage = 2, // <percentage>
13 f_length_percentage = f_length | f_percentage, // <length-percentage>
14 f_number = 4, // <number>
15 f_integer = 8, // <integer>
16 f_positive = 16 // ⩾0 (non-negative)
17 };
18
19 // <length> | <percentage> | <number> | auto | none | normal
20 // https://developer.mozilla.org/en-US/docs/Web/CSS/length
22 {
23 union
24 {
25 float m_value;
27 };
30 public:
31 css_length();
33 css_length& operator=(float val);
34
35 bool is_predefined() const;
36 void predef(int val);
37 int predef() const;
38 static css_length predef_value(int val = 0);
39 void set_value(float val, css_units units);
40 float val() const;
41 css_units units() const;
42 int calc_percent(int width) const;
43 bool from_token(const css_token& token, int options, const string& predefined_keywords = "");
44 string to_string() const;
45 };
46
47 using length_vector = vector<css_length>;
48
49 // css_length inlines
50
52 {
53 m_value = 0;
54 m_predef = 0;
56 m_is_predefined = false;
57 }
58
59 inline css_length::css_length(float val, css_units units)
60 {
61 m_value = val;
62 m_units = units;
63 m_is_predefined = false;
64 }
65
67 {
68 m_value = val;
70 m_is_predefined = false;
71 return *this;
72 }
73
74 inline bool css_length::is_predefined() const
75 {
76 return m_is_predefined;
77 }
78
79 inline void css_length::predef(int val)
80 {
81 m_predef = val;
82 m_is_predefined = true;
83 }
84
85 inline int css_length::predef() const
86 {
88 {
89 return m_predef;
90 }
91 return 0;
92 }
93
94 inline void css_length::set_value(float val, css_units units)
95 {
96 m_value = val;
97 m_is_predefined = false;
98 m_units = units;
99 }
100
101 inline float css_length::val() const
102 {
103 if(!m_is_predefined)
104 {
105 return m_value;
106 }
107 return 0;
108 }
109
111 {
112 return m_units;
113 }
114
115 inline int css_length::calc_percent(int width) const
116 {
117 if(!is_predefined())
118 {
120 {
121 return (int) ((double) width * (double) m_value / 100.0);
122 } else
123 {
124 return (int) val();
125 }
126 }
127 return 0;
128 }
129}
130
131#endif // LH_CSS_LENGTH_H
Definition css_length.h:22
bool m_is_predefined
Definition css_length.h:29
static css_length predef_value(int val=0)
Definition css_length.cpp:71
void set_value(float val, css_units units)
Definition css_length.h:94
float m_value
Definition css_length.h:25
int predef() const
Definition css_length.h:85
css_length()
Definition css_length.h:51
css_units units() const
Definition css_length.h:110
bool is_predefined() const
Definition css_length.h:74
float val() const
Definition css_length.h:101
bool from_token(const css_token &token, int options, const string &predefined_keywords="")
Definition css_length.cpp:7
string to_string() const
Definition css_length.cpp:62
int m_predef
Definition css_length.h:26
css_units m_units
Definition css_length.h:28
int calc_percent(int width) const
Definition css_length.h:115
css_length & operator=(float val)
Definition css_length.h:66
Definition background.h:12
vector< css_length > length_vector
Definition css_length.h:47
@ f_integer
Definition css_length.h:15
@ f_length_percentage
Definition css_length.h:13
@ f_number
Definition css_length.h:14
@ f_length
Definition css_length.h:11
@ f_positive
Definition css_length.h:16
@ f_percentage
Definition css_length.h:12
css_units
Definition types.h:500
@ css_units_px
Definition types.h:510
@ css_units_percentage
Definition types.h:502
@ css_units_none
Definition types.h:501
Definition css_tokenizer.h:69