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