Ocean
Loading...
Searching...
No Matches
Font.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_CV_FONTS_FONT_H
9#define META_OCEAN_CV_FONTS_FONT_H
10
12
13#include "ocean/base/Frame.h"
14
16
17namespace Ocean
18{
19
20namespace CV
21{
22
23namespace Fonts
24{
25
26/// Forward declaration.
27class Font;
28
29/**
30 * Definition of a shared font pointer.
31 * @see Font.
32 * @ingroup cvfonts
33 */
34using SharedFont = std::shared_ptr<Font>;
35
36/**
37 * This class is the base class for all font implementations.
38 * Each font object must be acquired via the `FontManager` singleton.
39 * @see FontManager.
40 * @ingroup cvfonts
41 */
42class OCEAN_CV_FONTS_EXPORT Font
43{
44 public:
45
46 /**
47 * This class stores font-specific layout information about each individual character.
48 * The class does not store the actual visual information (appearance) of each character.<br>
49 * The visual information of all characters is stored in a lookup image.
50 */
52 {
53 public:
54
55 /**
56 * Creates a new invalid character object.
57 */
58 inline Character();
59
60 /**
61 * Creates the a new character object.
62 * @param characterIndex The index of the new character, with range [0, infinity)
63 * @param framePositionX The character's left pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.width() - 1]
64 * @param framePositionY The character's top pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.height() - 1]
65 * @param linePositionY The character's vertical start pixel inside the target text line, in pixels, with range [0, framePositionY]
66 * @param width The character's width, in pixels, with range [0, charactersFrame_.width()]
67 * @param height The character's hieght, in pixels, with range [0, charactersFrame_.height()]
68 * @param bearingX The character's horizontal offset, in pixels, with range (-infinity, infinity)
69 * @param bearingY The character's vertical offset, in pixels, with range (-infinity, infinity)
70 * @param advanceX The horizontal distance between this character and the next character (between the origins of both characters), in pixels, with range [0, infinity)
71 */
72 inline Character(const unsigned int characterIndex, const unsigned int framePositionX, const unsigned int framePositionY, const unsigned int linePositionY, const unsigned int width, const unsigned int height, const int bearingX, const int bearingY, const unsigned int advanceX);
73
74 /**
75 * Returns the index of this character.
76 * @return The character's index, with range [0, infinity)
77 */
78 inline unsigned int characterIndex() const;
79
80 /**
81 * Returns the horizontal start pixel of the character inside 'charactersFrame'.
82 * @return The character's left pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.width() - 1]
83 */
84 inline unsigned int framePositionX() const;
85
86 /**
87 * Returns the vertical start pixel of the character inside 'charactersFrame'.
88 * @return The character's top pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.height() - 1]
89 */
90 inline unsigned int framePositionY() const;
91
92 /**
93 * Returns the vertical start pixel of the character inside the target text line.
94 * This vertical start position may be different from 'framePositionY()' in case the 'charactersFrame_' contains some extra border around each character.
95 * @return The character's top pixel in the target text line, in pixel, with range [0, framePositionY()]
96 */
97 inline unsigned int linePositionY() const;
98
99 /**
100 * The width of the character.
101 * @return The character's width, in pixels, with rang [0, charactersFrame_.width()]
102 */
103 inline unsigned int width() const;
104
105 /**
106 * The height of the character.
107 * @return The character's hieght, in pixels, with rang [0, charactersFrame_.height()]
108 */
109 inline unsigned int height() const;
110
111 /**
112 * Returns the horizontal offset of the character in relation to the origin.
113 * @return The character's horizontal offset, in pixels, with range (-infinity, infinity)
114 */
115 inline int bearingX() const;
116
117 /**
118 * Returns the vertical offset of the character in relation to the origin.
119 * @return The character's vertical offset, in pixels, with range (-infinity, infinity)
120 */
121 inline int bearingY() const;
122
123 /**
124 * Returns the horizontal distance between this character and the next character (between the origins of both characters).
125 * @return The character's horizontal distance, in pixels, with range [0, infinity)
126 */
127 inline unsigned int advanceX() const;
128
129 /**
130 * Sets the vertical start pixel of the character inside 'charactersFrame'.
131 * @param framePositionY The vertical start position, in pixels, with range [0, charactersFrame_.height() - 1]
132 * @param linePositionY The character's top pixel in the target text line, in pixel, with range [0, framePositionY]
133 */
134 inline void setPositionY(const unsigned int framePositionY, const unsigned int linePositionY);
135
136 protected:
137
138 /// The character's index, with range [0, infinity).
139 unsigned int characterIndex_ = (unsigned int)(-1);
140
141 /// The character's left pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.width() - 1].
142 unsigned int framePositionX_ = 0u;
143
144 /// The character's top pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.height() - 1].
145 unsigned int framePositionY_ = 0u;
146
147 /// The character's top pixel in the target text line, in pixel, with range [0, font.height() - 1]
148 unsigned int linePositionY_ = 0u;
149
150 /// The character's width, in pixels, with rang [0, charactersFrame_.width()].
151 unsigned int width_ = 0u;
152
153 /// The character's hieght, in pixels, with rang [0, charactersFrame_.height()].
154 unsigned int height_ = 0u;
155
156 /// The character's horizontal offset, in pixels, with range (-infinity, infinity).
157 int bearingX_ = 0;
158
159 //// The character's vertical offset, in pixels, with range (-infinity, infinity).
160 int bearingY_ = 0;
161
162 /// The horizontal distance between this character and the next character (between the origins of both characters), in pixels, with range [0, infinity).
163 unsigned int advanceX_ = 0u;
164 };
165
166 /// Definition of a vector holding information about characters.
167 using Characters = std::vector<Character>;
168
169 /// Definition of a shared pointers holding characters.
170 using SharedCharacters = std::shared_ptr<Characters>;
171
172 public:
173
174 /**
175 * Destructs the font object.
176 */
177 virtual ~Font() = default;
178
179 /**
180 * Draws a text into a given frame.
181 * @param frame The frame in which the text will be drawn, must have `dataType() == DT_UNSIGNED_INTEGER_8`, must be valid
182 * @param text The text to be drawn, can be empty
183 * @param left The horizontal start position of the text, with range (-infinity, infinity)
184 * @param top The vertical start positoin of the text, with range (-infinity, infinity)
185 * @param foregroundColor The foreground color to be used, must match with the pixel format of the given frame
186 * @param backgroundColor Optional explicit background color of the text, nullptr to draw the text without background color
187 * @return True, if succeeded
188 */
189 virtual bool drawText(Frame& frame, const std::string& text, const int left, const int top, const uint8_t* foregroundColor, const uint8_t* backgroundColor = nullptr) const = 0;
190
191 /**
192 * Returns the bounding box in which a given text will fit.
193 * In case the text contains multiple lines, the maximal bounding box is determined.
194 * @param text The text for which the extent will be determined, can be empty
195 * @param width The width of the bounding box, in pixel, with range [0, infinity)
196 * @param height The height of the bounding box, in pixel, with range [0, infinity)
197 * @param left Optional resulting explicit horizontal start location of the bounding box (e.g., if parts of a character are drawn to the left of character's origin), nullptr otherwise
198 * @param top Optional resulting explicit vertical start location of the bounding box, nullptr otherwise
199 * @return True, if succeeded
200 */
201 virtual bool textExtent(const std::string& text, unsigned int& width, unsigned int& height, int* left = nullptr, int* top = nullptr) const = 0;
202
203 /**
204 * Returns the bounding boxes in which each individual line of a given text will fit.
205 * @param text The text for which the individual line extents will be determined, can be empty
206 * @param lineBoundingBoxes The resulting bounding boxes, one for each text line
207 * @return True, if succeeded
208 */
209 virtual bool textExtent(const std::string& text, PixelBoundingBoxesI& lineBoundingBoxes) const = 0;
210
211 /**
212 * Returns a frame containing all characters.
213 * @param frame The resulting frame containing all characters, the frame will own the memory
214 * @param characters Optional resulting layout information for each individual character
215 * @return True, if succeeded
216 */
217 virtual bool characterFrame(Frame& frame, SharedCharacters* characters = nullptr) const = 0;
218
219 /**
220 * Returns the size of the font in dots.
221 * @return The font's size in dots, with range [0, infinity)
222 */
223 inline unsigned int size() const;
224
225 /**
226 * Returns the height of the font in pixels.
227 * @return The font's height in pixel, with range [0, infinity)
228 */
229 inline unsigned int height() const;
230
231 /**
232 * Returns the dpi of the font.
233 * @return The font's dots per inch, with range [0, infinity)
234 */
235 inline unsigned int dpi() const;
236
237 /**
238 * Returns whether the font is valid and whether it can be used.
239 * @return True, if so
240 */
241 inline bool isValid() const;
242
243 protected:
244
245 /**
246 * Creates a new font.
247 */
248 inline Font();
249
250 protected:
251
252 /// The font's size in dots, with range [0, infinity).
253 unsigned int size_ = 0u;
254
255 /// The font's height in pixel, with range [0, infinity).
256 unsigned int height_ = 0u;
257
258 /// The font's dots per inch, with range [0, infinity).
259 unsigned int dpi_ = 0u;
260
261 /// True, if the font is valid.
262 bool isValid_ = false;
263};
264
266{
267 // nothing to do here
268}
269
270inline Font::Character::Character(const unsigned int characterIndex, const unsigned int framePositionX, const unsigned int framePositionY, const unsigned int linePositionY, const unsigned int width, const unsigned int height, const int bearingX, const int bearingY, const unsigned int advanceX) :
275 width_(width),
280{
281 // nothing to do here
282}
283
284inline unsigned int Font::Character::characterIndex() const
285{
286 ocean_assert(characterIndex_ != (unsigned int)(-1));
287 return characterIndex_;
288}
289
290inline unsigned int Font::Character::framePositionX() const
291{
292 return framePositionX_;
293}
294
295inline unsigned int Font::Character::framePositionY() const
296{
297 return framePositionY_;
298}
299
300inline unsigned int Font::Character::linePositionY() const
301{
302 return linePositionY_;
303}
304
305inline unsigned int Font::Character::width() const
306{
307 return width_;
308}
309
310inline unsigned int Font::Character::height() const
311{
312 return height_;
313}
314
316{
317 return bearingX_;
318}
319
321{
322 return bearingY_;
323}
324
325inline unsigned int Font::Character::advanceX() const
326{
327 return advanceX_;
328}
329
330inline void Font::Character::setPositionY(const unsigned int framePositionY, const unsigned int linePositionY)
331{
332 ocean_assert(linePositionY <= framePositionY);
333
336}
337
339{
340 // nothing to do here
341}
342
343inline unsigned int Font::size() const
344{
345 ocean_assert(isValid());
346
347 return size_;
348}
349
350inline unsigned int Font::height() const
351{
352 ocean_assert(isValid());
353
354 return height_;
355}
356
357inline unsigned int Font::dpi() const
358{
359 ocean_assert(isValid());
360
361 return dpi_;
362}
363
364inline bool Font::isValid() const
365{
366 return isValid_;
367}
368
369}
370
371}
372
373}
374
375#endif // META_OCEAN_CV_FONTS_FONT_H
unsigned int framePositionX_
The character's left pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_....
Definition Font.h:142
unsigned int framePositionY_
The character's top pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_....
Definition Font.h:145
void setPositionY(const unsigned int framePositionY, const unsigned int linePositionY)
Sets the vertical start pixel of the character inside 'charactersFrame'.
Definition Font.h:330
unsigned int height_
The character's hieght, in pixels, with rang [0, charactersFrame_.height()].
Definition Font.h:154
Character()
Creates a new invalid character object.
Definition Font.h:265
unsigned int advanceX() const
Returns the horizontal distance between this character and the next character (between the origins of...
Definition Font.h:325
unsigned int width_
The character's width, in pixels, with rang [0, charactersFrame_.width()].
Definition Font.h:151
int bearingX() const
Returns the horizontal offset of the character in relation to the origin.
Definition Font.h:315
unsigned int advanceX_
The horizontal distance between this character and the next character (between the origins of both ch...
Definition Font.h:163
unsigned int height() const
The height of the character.
Definition Font.h:310
unsigned int width() const
The width of the character.
Definition Font.h:305
int bearingX_
The character's horizontal offset, in pixels, with range (-infinity, infinity).
Definition Font.h:157
unsigned int characterIndex_
The character's index, with range [0, infinity).
Definition Font.h:139
unsigned int framePositionX() const
Returns the horizontal start pixel of the character inside 'charactersFrame'.
Definition Font.h:290
unsigned int framePositionY() const
Returns the vertical start pixel of the character inside 'charactersFrame'.
Definition Font.h:295
int bearingY() const
Returns the vertical offset of the character in relation to the origin.
Definition Font.h:320
unsigned int linePositionY() const
Returns the vertical start pixel of the character inside the target text line.
Definition Font.h:300
int bearingY_
Definition Font.h:160
unsigned int linePositionY_
The character's top pixel in the target text line, in pixel, with range [0, font.height() - 1].
Definition Font.h:148
unsigned int characterIndex() const
Returns the index of this character.
Definition Font.h:284
This class is the base class for all font implementations.
Definition Font.h:43
unsigned int dpi_
The font's dots per inch, with range [0, infinity).
Definition Font.h:259
virtual bool drawText(Frame &frame, const std::string &text, const int left, const int top, const uint8_t *foregroundColor, const uint8_t *backgroundColor=nullptr) const =0
Draws a text into a given frame.
virtual bool textExtent(const std::string &text, unsigned int &width, unsigned int &height, int *left=nullptr, int *top=nullptr) const =0
Returns the bounding box in which a given text will fit.
unsigned int size() const
Returns the size of the font in dots.
Definition Font.h:343
virtual bool characterFrame(Frame &frame, SharedCharacters *characters=nullptr) const =0
Returns a frame containing all characters.
bool isValid_
True, if the font is valid.
Definition Font.h:262
virtual bool textExtent(const std::string &text, PixelBoundingBoxesI &lineBoundingBoxes) const =0
Returns the bounding boxes in which each individual line of a given text will fit.
bool isValid() const
Returns whether the font is valid and whether it can be used.
Definition Font.h:364
virtual ~Font()=default
Destructs the font object.
std::vector< Character > Characters
Definition of a vector holding information about characters.
Definition Font.h:167
unsigned int height() const
Returns the height of the font in pixels.
Definition Font.h:350
Font()
Creates a new font.
Definition Font.h:338
unsigned int size_
The font's size in dots, with range [0, infinity).
Definition Font.h:253
unsigned int height_
The font's height in pixel, with range [0, infinity).
Definition Font.h:256
std::shared_ptr< Characters > SharedCharacters
Definition of a shared pointers holding characters.
Definition Font.h:170
unsigned int dpi() const
Returns the dpi of the font.
Definition Font.h:357
This class implements Ocean's image class.
Definition Frame.h:1969
std::vector< PixelBoundingBoxI > PixelBoundingBoxesI
Definition of a vector holding bounding box objects with positive and negative coordinate values.
Definition PixelBoundingBox.h:49
std::shared_ptr< Font > SharedFont
Definition of a shared font pointer.
Definition Font.h:34
Namespace of the CV Fonts library.
Definition Font.h:24
Namespace of the CV library.
Definition Advanced.h:17
The namespace covering the entire Ocean framework.
Definition Accessor.h:15