Ocean
FTFont.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_FREETYPE_FT_FONT_H
9 #define META_OCEAN_CV_FONTS_FREETYPE_FT_FONT_H
10 
12 
13 #include "ocean/cv/fonts/Font.h"
14 
15 namespace Ocean
16 {
17 
18 namespace CV
19 {
20 
21 namespace Fonts
22 {
23 
24 /**
25  * This class implements a font based on FreeType.
26  * Each font object must be acquired via the `Fonts::FontManager` singleton.
27  * @ingroup cvfonts
28  */
29 class OCEAN_CV_FONTS_EXPORT FTFont : public Font
30 {
31  friend class FTFontManager;
32 
33  protected:
34 
35  /**
36  * The index of the first supported character, which is a space in ASCII code.
37  */
38  static constexpr char firstCharacter_ = 32;
39 
40  /**
41  * The index of the character after the last supported character to cover the standard ASCII code.
42  */
43  static constexpr char endCharacter_ = 127;
44 
45  public:
46 
47  /**
48  * Draws a text into a given frame.
49  * @param frame The frame in which the text will be drawn, must have `dataType() == DT_UNSIGNED_INTEGER_8`, must be valid
50  * @param text The text to be drawn, can be empty
51  * @param left The horizontal start position of the text, with range (-infinity, infinity)
52  * @param top The vertical start position of the text, with range (-infinity, infinity)
53  * @param foregroundColor The foreground color to be used, must match with the pixel format of the given frame
54  * @param backgroundColor Optional explicit background color of the text, nullptr to draw the text without background color
55  * @return True, if succeeded
56  */
57  bool drawText(Frame& frame, const std::string& text, const int left, const int top, const uint8_t* foregroundColor, const uint8_t* backgroundColor = nullptr) const override;
58 
59  /**
60  * Returns the bounding box a given text will occupy in pixel space when using this font.
61  * @param text The text for which the extent will be determined, can be empty
62  * @param width The width of the bounding box, in pixels, with range [0, infinity)
63  * @param height The height of the bounding box, in pixels, with range [0, infinity)
64  * @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
65  * @param top Optional resulting explicit vertical start location of the bounding box, nullptr otherwise
66  * @return True, if succeeded
67  */
68  bool textExtent(const std::string& text, unsigned int& width, unsigned int& height, int* left = nullptr, int* top = nullptr) const override;
69 
70  /**
71  * Returns the bounding boxes in which each individual line of a given text will fit.
72  * @param text The text for which the individual line extents will be determined, can be empty
73  * @param lineBoundingBoxes The resulting bounding boxes, one for each text line
74  * @return True, if succeeded
75  */
76  bool textExtent(const std::string& text, PixelBoundingBoxesI& lineBoundingBoxes) const override;
77 
78  /**
79  * Returns a frame containing all characters.
80  * @param frame The resulting frame containing all characters, the frame will own the memory
81  * @param characters Optional resulting layout information for each individual character
82  * @return True, if succeeded
83  */
84  bool characterFrame(Frame& frame, SharedCharacters* characters = nullptr) const override;
85 
86  protected:
87 
88  /**
89  * Creates a new FreeType font object.
90  * @param ftFace The FreeType face object describing the font, will be release when this new font is disposed.
91  * @param size The size of the font, in dots, with range [1, infinity)
92  * @param dpi The dots per inch of the font, with range [1, infinity)
93  */
94  explicit FTFont(const FT_Face& ftFace, const unsigned int size, const unsigned int dpi);
95 
96  /**
97  * Initializes the font.
98  * @param ftFace The FreeType face defining the font, must be valid
99  * @return True, if succeeded
100  */
101  bool initialize(const FT_Face& ftFace);
102 
103  /**
104  * Draws the bitmap of a FreeType character to the frame storing all characters/glyphs.
105  * @param bitmap The bitmap of the character to be drawn
106  * @param frame the frame storing all characters, must be valid
107  * @param framePositionX The horizontal start position within the frame, in pixels, with range [0, frame.width() - bitmap.width - 1]
108  * @param framePositionY The horizontal start position within the frame, in pixels, with range [0, frame.width() - bitmap.rows - 1]
109  * @return True, if succeeded
110  */
111  static bool drawCharacterBitmapToFrame(const FT_Bitmap& bitmap, Frame& frame, const unsigned int framePositionX, const unsigned int framePositionY);
112 
113  /**
114  * Renders a font character from the grayscale image storing all characters to a target image.
115  * @param ySourceFrameData The grayscale source image storing all characters, with pixel format FORMAT_Y8, must be valid
116  * @param targetFrameData The pointer to the top left corner within the target frame to which the character will be rendered, must be valid
117  * @param width The width of the area to be rendered, can be a sub-region of the actual character (e.g., at the boundary of the target image), with range [1, infinity)
118  * @param height The height of the area to be rendered, can be a sub-region of the actual character (e.g., at the boundary of the target image), with range [1, infinity)
119  * @param foregroundColor The foreground color of the character, must match with the pixel format of the target frame, must be valid
120  * @param ySourceFrameStrideElements The number of elements between two consecutive rows in the source image (including padding), in elements, with range [width, infinity)
121  * @param targetFrameStrideElements The number of elements between two consecutive rows in the target image (including padding), in elements, with range [width * tTargetChannels, infinity)
122  * @tparam T The data type of each target frame pixel element
123  * @tparam tTargetChannels The number of channels in the target frame, with range [1, infinity)
124  */
125  template <typename T, unsigned int tTargetChannels>
126  static void renderCharacterFromY8(const uint8_t* ySourceFrameData, T* targetFrameData, const unsigned int width, const unsigned int height, const T* const foregroundColor, const unsigned int ySourceFrameStrideElements, const unsigned int targetFrameStrideElements);
127 
128  /**
129  * Draws the background color into a rectangular area within the target frame.
130  * @param frameData The pointer to the top left corners of the rectangular area within the target frame, must be valid
131  * @param backgroundWidth The width of the rectangular background area, in pixels, with range [1, infinity)
132  * @param backgroundHeight The height of the rectangular background area, in pixels, with range [1, infinity)
133  * @param backgroundColor The background color to be used, must be valid
134  * @param frameStrideElements The number of elements between two consecutive frame rows (including padding), in elements, with range [backgroundWidth * tChannels, infinity)
135  * @tparam T The data type of each frame pixel element
136  * @tparam tChannels The number of frame channels, with range [1, infinity)
137  */
138  template <typename T, unsigned int tChannels>
139  static void renderBackground(T* frameData, const unsigned int backgroundWidth, const unsigned int backgroundHeight, const T* const backgroundColor, const unsigned int frameStrideElements);
140 
141  protected:
142 
143  /// The information for each individual character in `charactersFrame_`.
145 
146  /// The character frame which contains all characters of the font, with pixel format FORMAT_Y8 or FORMAT_RGBA32.
148 
149  /// The optional extra border around each glyph in the character frame to avoid that glyphs are too close together, in pixel, with range [0, infinity)
150  constexpr static unsigned int extraBorder_ = 1u;
151 };
152 
153 }
154 
155 }
156 
157 }
158 
159 #endif // META_OCEAN_CV_FONTS_FREETYPE_FT_FONT_H
This class implements a font based on FreeType.
Definition: FTFont.h:30
Frame charactersFrame_
The character frame which contains all characters of the font, with pixel format FORMAT_Y8 or FORMAT_...
Definition: FTFont.h:147
SharedCharacters characters_
The information for each individual character in charactersFrame_.
Definition: FTFont.h:144
static bool drawCharacterBitmapToFrame(const FT_Bitmap &bitmap, Frame &frame, const unsigned int framePositionX, const unsigned int framePositionY)
Draws the bitmap of a FreeType character to the frame storing all characters/glyphs.
static void renderCharacterFromY8(const uint8_t *ySourceFrameData, T *targetFrameData, const unsigned int width, const unsigned int height, const T *const foregroundColor, const unsigned int ySourceFrameStrideElements, const unsigned int targetFrameStrideElements)
Renders a font character from the grayscale image storing all characters to a target image.
static void renderBackground(T *frameData, const unsigned int backgroundWidth, const unsigned int backgroundHeight, const T *const backgroundColor, const unsigned int frameStrideElements)
Draws the background color into a rectangular area within the target frame.
bool initialize(const FT_Face &ftFace)
Initializes the font.
bool drawText(Frame &frame, const std::string &text, const int left, const int top, const uint8_t *foregroundColor, const uint8_t *backgroundColor=nullptr) const override
Draws a text into a given frame.
bool characterFrame(Frame &frame, SharedCharacters *characters=nullptr) const override
Returns a frame containing all characters.
bool textExtent(const std::string &text, unsigned int &width, unsigned int &height, int *left=nullptr, int *top=nullptr) const override
Returns the bounding box a given text will occupy in pixel space when using this font.
bool textExtent(const std::string &text, PixelBoundingBoxesI &lineBoundingBoxes) const override
Returns the bounding boxes in which each individual line of a given text will fit.
FTFont(const FT_Face &ftFace, const unsigned int size, const unsigned int dpi)
Creates a new FreeType font object.
This class implements the actual manager for all FreeType fonts.
Definition: FTFontManager.h:34
This class is the base class for all font implementations.
Definition: Font.h:43
std::shared_ptr< Characters > SharedCharacters
Definition of a shared pointers holding characters.
Definition: Font.h:170
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
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15