Ocean
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 
11 #include "ocean/cv/fonts/Fonts.h"
12 
13 #include "ocean/base/Frame.h"
14 
16 
17 namespace Ocean
18 {
19 
20 namespace CV
21 {
22 
23 namespace Fonts
24 {
25 
26 /// Forward declaration.
27 class Font;
28 
29 /**
30  * Definition of a shared font pointer.
31  * @see Font.
32  * @ingroup cvfonts
33  */
34 typedef std::shared_ptr<Font> SharedFont;
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  */
42 class 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  */
51  class Character
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_;
140 
141  /// The character's left pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.width() - 1].
142  unsigned int framePositionX_;
143 
144  /// The character's top pixel in 'charactersFrame', in pixels, with range [0, charactersFrame_.height() - 1].
145  unsigned int framePositionY_;
146 
147  /// The character's top pixel in the target text line, in pixel, with range [0, font.height() - 1]
148  unsigned int linePositionY_;
149 
150  /// The character's width, in pixels, with rang [0, charactersFrame_.width()].
151  unsigned int width_;
152 
153  /// The character's hieght, in pixels, with rang [0, charactersFrame_.height()].
154  unsigned int height_;
155 
156  /// The character's horizontal offset, in pixels, with range (-infinity, infinity).
158 
159  //// The character's vertical offset, in pixels, with range (-infinity, infinity).
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_;
164  };
165 
166  /// Definition of a vector holding information about characters.
167  typedef std::vector<Character> Characters;
168 
169  /// Definition of a shared pointers holding characters.
170  typedef std::shared_ptr<Characters> SharedCharacters;
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_;
254 
255  /// The font's height in pixel, with range [0, infinity).
256  unsigned int height_;
257 
258  /// The font's dots per inch, with range [0, infinity).
259  unsigned int dpi_;
260 
261  /// True, if the font is valid.
262  bool isValid_;
263 };
264 
266  characterIndex_((unsigned int)(-1)),
267  framePositionX_(0u),
268  framePositionY_(0u),
269  linePositionY_(0u),
270  width_(0u),
271  height_(0u),
272  bearingX_(0),
273  bearingY_(0),
274  advanceX_(0u)
275 {
276  // nothing to do here
277 }
278 
279 inline 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) :
280  characterIndex_(characterIndex),
281  framePositionX_(framePositionX),
282  framePositionY_(framePositionY),
283  linePositionY_(linePositionY),
284  width_(width),
285  height_(height),
286  bearingX_(bearingX),
287  bearingY_(bearingY),
288  advanceX_(advanceX)
289 {
290  // nothing to do here
291 }
292 
293 inline unsigned int Font::Character::characterIndex() const
294 {
295  ocean_assert(characterIndex_ != (unsigned int)(-1));
296  return characterIndex_;
297 }
298 
299 inline unsigned int Font::Character::framePositionX() const
300 {
301  return framePositionX_;
302 }
303 
304 inline unsigned int Font::Character::framePositionY() const
305 {
306  return framePositionY_;
307 }
308 
309 inline unsigned int Font::Character::linePositionY() const
310 {
311  return linePositionY_;
312 }
313 
314 inline unsigned int Font::Character::width() const
315 {
316  return width_;
317 }
318 
319 inline unsigned int Font::Character::height() const
320 {
321  return height_;
322 }
323 
324 inline int Font::Character::bearingX() const
325 {
326  return bearingX_;
327 }
328 
329 inline int Font::Character::bearingY() const
330 {
331  return bearingY_;
332 }
333 
334 inline unsigned int Font::Character::advanceX() const
335 {
336  return advanceX_;
337 }
338 
339 inline void Font::Character::setPositionY(const unsigned int framePositionY, const unsigned int linePositionY)
340 {
341  ocean_assert(linePositionY <= framePositionY);
342 
343  framePositionY_ = framePositionY;
344  linePositionY_ = linePositionY;
345 }
346 
347 inline Font::Font() :
348  size_(0u),
349  height_(0u),
350  dpi_(0u),
351  isValid_(false)
352 {
353  // nothing to do here
354 }
355 
356 inline unsigned int Font::size() const
357 {
358  ocean_assert(isValid());
359 
360  return size_;
361 }
362 
363 inline unsigned int Font::height() const
364 {
365  ocean_assert(isValid());
366 
367  return height_;
368 }
369 
370 inline unsigned int Font::dpi() const
371 {
372  ocean_assert(isValid());
373 
374  return dpi_;
375 }
376 
377 inline bool Font::isValid() const
378 {
379  return isValid_;
380 }
381 
382 }
383 
384 }
385 
386 }
387 
388 #endif // META_OCEAN_CV_FONTS_FONT_H
This class stores font-specific layout information about each individual character.
Definition: Font.h:52
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:339
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:334
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:324
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:319
unsigned int width() const
The width of the character.
Definition: Font.h:314
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:299
unsigned int framePositionY() const
Returns the vertical start pixel of the character inside 'charactersFrame'.
Definition: Font.h:304
int bearingY() const
Returns the vertical offset of the character in relation to the origin.
Definition: Font.h:329
unsigned int linePositionY() const
Returns the vertical start pixel of the character inside the target text line.
Definition: Font.h:309
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:293
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:356
virtual bool characterFrame(Frame &frame, SharedCharacters *characters=nullptr) const =0
Returns a frame containing all characters.
std::shared_ptr< Characters > SharedCharacters
Definition of a shared pointers holding characters.
Definition: Font.h:170
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:377
virtual ~Font()=default
Destructs the font object.
unsigned int height() const
Returns the height of the font in pixels.
Definition: Font.h:363
Font()
Creates a new font.
Definition: Font.h:347
std::vector< Character > Characters
Definition of a vector holding information about characters.
Definition: Font.h:167
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
unsigned int dpi() const
Returns the dpi of the font.
Definition: Font.h:370
This class implements Ocean's image class.
Definition: Frame.h:1760
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:27
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15