Debian Codegraph @

avogadro/rendering/textproperties.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_RENDERING_TEXTPROPERTIES_H
7 #define AVOGADRO_RENDERING_TEXTPROPERTIES_H
8
9 #include "avogadrorenderingexport.h"
10
11 #include <avogadro/core/vector.h>
12
13 namespace Avogadro {
14 namespace Rendering {
15
16 /**
17 * @class TextProperties textproperties.h <avogadro/rendering/textproperties.h>
18 * @brief The TextProperties class controls formatting options for text.
19 */
20 class AVOGADRORENDERING_EXPORT TextProperties
21 {
22 public:
23 /** Enum defining a minimal set of font families. */
24 enum FontFamily
25 {
26 SansSerif,
27 Serif,
28 Mono
29 };
30
31 /** Horizontal alignment options. */
32 enum HAlign
33 {
34 HLeft,
35 HCenter,
36 HRight
37 };
38
39 /** Vertical alignment options. */
40 enum VAlign
41 {
42 VTop,
43 VCenter,
44 VBottom
45 };
46
47 /** Flags for style options (bold, italic, ...) */
48 enum FontStyle
49 {
50 NoFontStyle = 0x0,
51 Bold = 0x1,
52 Italic = 0x2,
53 Underline = 0x4
54 };
55
56 /** Used for bitwise combinations of FontStyle values. */
57 typedef int FontStyles;
58
59 TextProperties();
60 TextProperties(const TextProperties& other);
61 ~TextProperties();
62
63 TextProperties& operator=(TextProperties other);
64 void swap(TextProperties& other);
65
66 bool operator==(const TextProperties& other) const;
67 bool operator!=(const TextProperties& other) const
68 {
69 return !operator==(other);
70 }
71
72 /**
73 * The height of the text in pixels.
74 */
75 void setPixelHeight(size_t height) { m_pixelHeight = height; }
76 size_t pixelHeight() const { return m_pixelHeight; }
77 /** @} */
78
79 /**
80 * Horizontal alignment of the text.
81 */
82 void setHAlign(HAlign align) { m_hAlign = align; }
83 HAlign hAlign() const { return m_hAlign; }
84 /** @} */
85
86 /**
87 * Vertical alignment of the text.
88 */
89 void setVAlign(VAlign align) { m_vAlign = align; }
90 VAlign vAlign() const { return m_vAlign; }
91 /** @} */
92
93 /**
94 * Set the horizontal and vertical alignment of the quad to the anchor point.
95 */
96 void setAlign(HAlign hAlign, VAlign vAlign);
97
98 /**
99 * Rotates the text clockwise.
100 */
101 void setRotationDegreesCW(float rot) { m_rotationDegreesCW = rot; }
102 float rotationDegreesCW() const { return m_rotationDegreesCW; }
103 /** @} */
104
105 /**
106 * The font family.
107 */
108 void setFontFamily(FontFamily family) { m_fontFamily = family; }
109 FontFamily fontFamily() const { return m_fontFamily; }
110 /** @} */
111
112 /**
113 * Font style flags.
114 */
115 void setFontStyles(FontStyles styles) { m_fontStyles = styles; }
116 FontStyles fontStyles() const { return m_fontStyles; }
117 /** @} */
118
119 /**
120 * Toggle bold text.
121 */
122 void setBold(bool b);
123 bool bold() const;
124 /** @} */
125
126 /**
127 * Toggle italic text.
128 */
129 void setItalic(bool b);
130 bool italic() const;
131 /** @} */
132
133 /**
134 * Toggle underlined text.
135 */
136 void setUnderline(bool b);
137 bool underline() const;
138 /** @} */
139
140 /**
141 * Set the color of the text. Components are in the range [0, 255]
142 * @{
143 */
144 void setColorRgba(unsigned char r, unsigned char g, unsigned char b,
145 unsigned char a);
146 void setColorRgba(const unsigned char rgba[4]);
147 void setColorRgba(const Vector4ub& rgba);
148 void colorRgba(unsigned char rgba[4]) const;
149 Vector4ub colorRgba() const;
150 void setColorRgb(unsigned char r, unsigned char g, unsigned char b);
151 void setColorRgb(const unsigned char rgb[3]);
152 void setColorRgb(const Vector3ub& rgb);
153 void colorRgb(unsigned char rgb[3]) const;
154 Vector3ub colorRgb() const;
155 void setRed(unsigned char r) { m_rgba[0] = r; }
156 unsigned char red() const { return m_rgba[0]; }
157 void setGreen(unsigned char g) { m_rgba[1] = g; }
158 unsigned char green() const { return m_rgba[1]; }
159 void setBlue(unsigned char b) { m_rgba[2] = b; }
160 unsigned char blue() const { return m_rgba[2]; }
161 void setAlpha(unsigned char a) { m_rgba[3] = a; }
162 unsigned char alpha() const { return m_rgba[3]; }
163 /** @} */
164
165 private:
166 size_t m_pixelHeight;
167 HAlign m_hAlign;
168 VAlign m_vAlign;
169 float m_rotationDegreesCW;
170 FontFamily m_fontFamily;
171 FontStyles m_fontStyles;
172 unsigned char m_rgba[4];
173 };
174
175 inline void TextProperties::setAlign(TextProperties::HAlign h,
176 TextProperties::VAlign v)
177 {
178 setHAlign(h);
179 setVAlign(v);
180 }
181
182 inline void TextProperties::setBold(bool b)
183 {
184 if (b)
185 m_fontStyles |= Bold;
186 else
187 m_fontStyles &= ~Bold;
188 }
189
190 inline bool TextProperties::bold() const
191 {
192 return (m_fontStyles & Bold) != 0;
193 }
194
195 inline void TextProperties::setItalic(bool b)
196 {
197 if (b)
198 m_fontStyles |= Italic;
199 else
200 m_fontStyles &= ~Italic;
201 }
202
203 inline bool TextProperties::italic() const
204 {
205 return (m_fontStyles & Italic) != 0;
206 }
207
208 inline void TextProperties::setUnderline(bool b)
209 {
210 if (b)
211 m_fontStyles |= Underline;
212 else
213 m_fontStyles &= ~Underline;
214 }
215
216 inline bool TextProperties::underline() const
217 {
218 return (m_fontStyles & Underline) != 0;
219 }
220
221 inline void TextProperties::setColorRgba(unsigned char r, unsigned char g,
222 unsigned char b, unsigned char a)
223 {
224 m_rgba[0] = r;
225 m_rgba[1] = g;
226 m_rgba[2] = b;
227 m_rgba[3] = a;
228 }
229
230 inline void TextProperties::setColorRgba(const unsigned char rgba[4])
231 {
232 m_rgba[0] = rgba[0];
233 m_rgba[1] = rgba[1];
234 m_rgba[2] = rgba[2];
235 m_rgba[3] = rgba[3];
236 }
237
238 inline void TextProperties::setColorRgba(const Vector4ub& rgba)
239 {
240 setColorRgba(rgba.data());
241 }
242
243 inline void TextProperties::colorRgba(unsigned char rgba[4]) const
244 {
245 rgba[0] = m_rgba[0];
246 rgba[1] = m_rgba[1];
247 rgba[2] = m_rgba[2];
248 rgba[3] = m_rgba[3];
249 }
250
251 inline Vector4ub TextProperties::colorRgba() const
252 {
253 return Vector4ub(m_rgba);
254 }
255
256 inline void TextProperties::setColorRgb(unsigned char r, unsigned char g,
257 unsigned char b)
258 {
259 m_rgba[0] = r;
260 m_rgba[1] = g;
261 m_rgba[2] = b;
262 }
263
264 inline void TextProperties::setColorRgb(const unsigned char rgb[3])
265 {
266 m_rgba[0] = rgb[0];
267 m_rgba[1] = rgb[1];
268 m_rgba[2] = rgb[2];
269 }
270
271 inline void TextProperties::setColorRgb(const Vector3ub& rgb)
272 {
273 setColorRgb(rgb.data());
274 }
275
276 inline void TextProperties::colorRgb(unsigned char rgb[3]) const
277 {
278 rgb[0] = m_rgba[0];
279 rgb[1] = m_rgba[1];
280 rgb[2] = m_rgba[2];
281 }
282
283 inline Vector3ub TextProperties::colorRgb() const
284 {
285 return Vector3ub(m_rgba);
286 }
287
288 inline void swap(TextProperties& lhs, TextProperties& rhs)
289 {
290 lhs.swap(rhs);
291 }
292
293 } // namespace Rendering
294 } // namespace Avogadro
295
296 #endif // AVOGADRO_RENDERING_TEXTPROPERTIES_H