Ocean
FTFontManager.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_MANAGER_H
9 #define META_OCEAN_CV_FONTS_FREETYPE_FT_FONT_MANAGER_H
10 
12 
13 #include "ocean/cv/fonts/Font.h"
14 
15 #include <unordered_map>
16 
17 namespace Ocean
18 {
19 
20 namespace CV
21 {
22 
23 namespace Fonts
24 {
25 
26 /**
27  * This class implements the actual manager for all FreeType fonts.
28  * Do not call functions of this class, but use the `FontManager` singleton instead.
29  * All fonts which are registered at the FontManager do not consume aditional resources until the font is actually aqcuired.
30  * @see FontManager.
31  * @ingroup cvfonts
32  */
33 class OCEAN_CV_FONTS_EXPORT FTFontManager
34 {
35  friend class FontManager;
36  friend class Face;
37 
38  protected:
39 
40  /**
41  * This class wrapps a FreeType face.
42  * Each face represent a font with individual size.
43  */
44  class OCEAN_CV_FONTS_EXPORT Face
45  {
46  protected:
47 
48  /**
49  * Definition of a map mapping size and dpi to font objects.
50  */
51  typedef std::unordered_map<uint64_t, SharedFont> FontMap;
52 
53  public:
54 
55  /**
56  * Move constructor.
57  * @param face The face object to be moved
58  */
59  inline Face(Face&& face);
60 
61  /**
62  * Creates a new face object for a given FreeType face and keeps ownership of this face object.
63  * @param fontManager The owner of this new object
64  * @param ftFace The FreeType face defining the font, will be release after usage, can be nullptr if a valid 'filename' is provided
65  * @param filename The filename from which the FreeType face can be accessed when actually needed, can be empty if 'ftFace' is valid
66  */
67  inline Face(FTFontManager& fontManager, const FT_Face& ftFace, const std::string& filename);
68 
69  /**
70  * Destructs a Face object.
71  */
72  inline ~Face();
73 
74  /**
75  * Returns the font associated with this Face object for a specified size.
76  * If the font does not exist yet, the font will be create and initialized.
77  * @param size The size of the font, in dots, with range [1, infinity)
78  * @param dpi The dots per inch of the font, with range [1, infinity)
79  * @return The requested font, nullptr if invalid
80  */
81  SharedFont font(const unsigned int size, const unsigned int dpi);
82 
83  /**
84  * Returns the filename of this face object.
85  * @return The face object's filename, if known
86  */
87  const std::string& filename() const;
88 
89  /**
90  * Move operator.
91  * @param face The object to be moved
92  * @return The reference to this object
93  */
94  Face& operator=(Face&& face);
95 
96  protected:
97 
98  /**
99  * Disabled copy constructor.
100  * @param face The object which would be copied
101  */
102  Face(const Face& face) = delete;
103 
104  /**
105  * Disabled copy operator.
106  * @param face The object which would be copied
107  * @return The reference to this object
108  */
109  Face& operator=(const Face& face) = delete;
110 
111  protected:
112 
113  /// The owner of the face object.
115 
116  /// The name of the file from which the FreeType face can be acquired if requested.
117  std::string filename_;
118 
119  /// The FreeType face (which represents a font) associated with this object, nullptr if the face object needs to be acquired from the file first.
120  FT_Face ftFace_ = nullptr;
121 
122  /// The map of font objects, one object for each individual combination of size and dpi.
124  };
125 
126  /**
127  * Definition of a map mapping style names to Face objects.
128  */
129  typedef std::unordered_map<std::string, Face> FaceMap;
130 
131  /**
132  * Definition of a map mapping family names to face maps.
133  */
134  typedef std::unordered_map<std::string, FaceMap> FamilyMap;
135 
136  public:
137 
138  /**
139  * Destructs the FontManager object.
140  */
142 
143  protected:
144 
145  /**
146  * Creates a new manager for fonts.
147  */
149 
150  /**
151  * Registers all fonts available on the system.
152  * @return The number of successfully registered fonts, with range [0, infinity)
153  * @see registerFont().
154  */
156 
157  /**
158  * Registers a new font by a given font file.
159  * @param fontFile The filname of a new font to be registered, must be valid
160  * @param familyName Optional resulting name of the font family, nullptr otherwise
161  * @param styleName Optional resulting name of the font syle (if known), nullptr otherwise
162  * @return True, if succeeded
163  * @see registerSystemFonts(), registerFonts(), font().
164  */
165  bool registerFont(const std::string& fontFile, std::string* familyName = nullptr, std::string* styleName = nullptr);
166 
167  /**
168  * Registers all font files located in a specified directory.
169  * @param fontDirectory The directory in which all font files will be registered, must be valid
170  * @return The number successfully registered fonts, with range [0, infinity)
171  * @see registerSystemFonts(), registerFont(), font().
172  */
173  size_t registerFonts(const std::string& fontDirectory);
174 
175  /**
176  * Registers a new font by a given font file which is already in memory.
177  * @param fontMemory The memory holding the loaded font file, must be valid
178  * @param fontMemorySize The size of the memory in bytes, with range [1, infinity)
179  * @param familyName Optional resulting name of the font family, nullptr otherwise
180  * @param styleName Optional resulting name of the font syle (if known), nullptr otherwise
181  * @return True, if succeeded
182  * @see registerFonts(), font().
183  */
184  bool registerFont(const void* fontMemory, const size_t fontMemorySize, std::string* familyName = nullptr, std::string* styleName = nullptr);
185 
186  /**
187  * Registers a new font by a FreeType face.
188  * @param ftFace The FreeType face representing the font, the FreeType face will be released afterwards
189  * @param filename The filename from which the FreeType face can be re-created so that a placeholder is registered, empty if unknown or to avoid registering a placeholder
190  * @param familyName Optional resulting name of the font family, nullptr otherwise
191  * @param styleName Optional resulting name of the font syle (if known), nullptr otherwise
192  * @return True, if succeeded
193  * @see registerFonts(), font().
194  */
195  bool registerFont(const FT_Face& ftFace, const std::string& filename, std::string* familyName = nullptr, std::string* styleName = nullptr);
196 
197  /**
198  * Returns a font with specific family name and size.
199  * The requested font must have been registered before.
200  * @param familyName The name of the font family, empty to get any registered font
201  * @param size The size of the font, in dots, with range [1, infinity)
202  * @param styleName Optinal style name of the font, empty to get any style
203  * @param dpi The dots per inch of the font, with range [1, infinity)
204  * @return The requested font if existing, nullptr otherwise
205  * @see registerFont().
206  */
207  SharedFont font(const std::string& familyName, const unsigned int size, const std::string& styleName = std::string(), const unsigned int dpi = 72u);
208 
209  /**
210  * Returns whether a specific font is registered.
211  * @param familyName The name of the font family to check
212  * @param styleName Optional explicit font style to check
213  * @return True, if so
214  */
215  bool hasFont(const std::string& familyName, const std::string& styleName = std::string()) const;
216 
217  /**
218  * Returns the family names of all registered fonts.
219  * @return The family names of all registered fonts
220  */
221  std::vector<std::string> familyNames() const;
222 
223  /**
224  * Returns the style names of all registered fonts with specified family name.
225  * @param familyName The family name of the fonts for which all style names will be returned
226  * @return The style names of all registered font
227  */
228  std::vector<std::string> styleNames(const std::string& familyName) const;
229 
230  /**
231  * Returns whether this manager is valid and ready to use.
232  * @return True, if so
233  */
234  inline bool isValid() const;
235 
236  protected:
237 
238  /// The map mapping font family names to face maps.
240 
241  /// The FreeType library object.
242  FT_Library library_;
243 
244  /// The manager's lock.
245  mutable Lock lock_;
246 };
247 
249  fontManager_(face.fontManager_)
250 {
251  *this = std::move(face);
252 }
253 
254 inline FTFontManager::Face::Face(FTFontManager& fontManager, const FT_Face& ftFace, const std::string& filename) :
255  fontManager_(fontManager),
256  filename_(filename),
257  ftFace_(ftFace)
258 {
259  ocean_assert(ftFace_ != nullptr || !filename_.empty());
260 }
261 
263 {
264  if (ftFace_ != nullptr)
265  {
266  FT_Done_Face(ftFace_);
267  }
268 }
269 
270 inline const std::string& FTFontManager::Face::filename() const
271 {
272  return filename_;
273 }
274 
275 inline bool FTFontManager::isValid() const
276 {
277  const ScopedLock scopedLock(lock_);
278 
279  return library_ != nullptr;
280 }
281 
282 }
283 
284 }
285 
286 }
287 
288 #endif // META_OCEAN_CV_FONTS_FREETYPE_FT_FONT_MANAGER_H
This class wrapps a FreeType face.
Definition: FTFontManager.h:45
Face(const Face &face)=delete
Disabled copy constructor.
~Face()
Destructs a Face object.
Definition: FTFontManager.h:262
Face & operator=(const Face &face)=delete
Disabled copy operator.
std::string filename_
The name of the file from which the FreeType face can be acquired if requested.
Definition: FTFontManager.h:117
FontMap fontMap_
The map of font objects, one object for each individual combination of size and dpi.
Definition: FTFontManager.h:123
Face(Face &&face)
Move constructor.
Definition: FTFontManager.h:248
const std::string & filename() const
Returns the filename of this face object.
Definition: FTFontManager.h:270
FTFontManager & fontManager_
The owner of the face object.
Definition: FTFontManager.h:114
std::unordered_map< uint64_t, SharedFont > FontMap
Definition of a map mapping size and dpi to font objects.
Definition: FTFontManager.h:51
Face & operator=(Face &&face)
Move operator.
SharedFont font(const unsigned int size, const unsigned int dpi)
Returns the font associated with this Face object for a specified size.
FT_Face ftFace_
The FreeType face (which represents a font) associated with this object, nullptr if the face object n...
Definition: FTFontManager.h:120
This class implements the actual manager for all FreeType fonts.
Definition: FTFontManager.h:34
FTFontManager()
Creates a new manager for fonts.
~FTFontManager()
Destructs the FontManager object.
bool isValid() const
Returns whether this manager is valid and ready to use.
Definition: FTFontManager.h:275
bool registerFont(const void *fontMemory, const size_t fontMemorySize, std::string *familyName=nullptr, std::string *styleName=nullptr)
Registers a new font by a given font file which is already in memory.
std::vector< std::string > styleNames(const std::string &familyName) const
Returns the style names of all registered fonts with specified family name.
size_t registerSystemFonts()
Registers all fonts available on the system.
FamilyMap familyMap_
The map mapping font family names to face maps.
Definition: FTFontManager.h:239
std::unordered_map< std::string, Face > FaceMap
Definition of a map mapping style names to Face objects.
Definition: FTFontManager.h:129
Lock lock_
The manager's lock.
Definition: FTFontManager.h:245
size_t registerFonts(const std::string &fontDirectory)
Registers all font files located in a specified directory.
SharedFont font(const std::string &familyName, const unsigned int size, const std::string &styleName=std::string(), const unsigned int dpi=72u)
Returns a font with specific family name and size.
bool registerFont(const FT_Face &ftFace, const std::string &filename, std::string *familyName=nullptr, std::string *styleName=nullptr)
Registers a new font by a FreeType face.
bool hasFont(const std::string &familyName, const std::string &styleName=std::string()) const
Returns whether a specific font is registered.
FT_Library library_
The FreeType library object.
Definition: FTFontManager.h:242
bool registerFont(const std::string &fontFile, std::string *familyName=nullptr, std::string *styleName=nullptr)
Registers a new font by a given font file.
std::vector< std::string > familyNames() const
Returns the family names of all registered fonts.
std::unordered_map< std::string, FaceMap > FamilyMap
Definition of a map mapping family names to face maps.
Definition: FTFontManager.h:134
This class implements the manager for all fonts.
Definition: FontManager.h:36
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
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