Ocean
FontManager.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_MANAGER_H
9 #define META_OCEAN_CV_FONTS_FONT_MANAGER_H
10 
11 #include "ocean/cv/fonts/Fonts.h"
12 #include "ocean/cv/fonts/Font.h"
13 
14 #include "ocean/base/Singleton.h"
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 namespace Fonts
23 {
24 
25 /// Forward declaration for `/freetype/FTFontManage.h`
26 class FTFontManager;
27 
28 /**
29  * This class implements the manager for all fonts.
30  * Use this singleton to register or acquire fonts.<br>
31  * Before a font can be used/acquired, the font needs to be registered.<br>
32  * Font can be registered based on a supported font file, or based on the memory containing a supported font file.
33  * @ingroup cvfonts
34  */
35 class OCEAN_CV_FONTS_EXPORT FontManager : public Singleton<FontManager>
36 {
37  friend class Singleton<FontManager>;
38 
39  public:
40 
41  /**
42  * Registers all fonts available on the system.
43  * @return The number of successfully registered fonts, with range [0, infinity)
44  * @see registerFont().
45  */
47 
48  /**
49  * Registers a new font by a given font file.
50  * @param fontFile The filname of a new font to be registered, must be valid
51  * @param familyName Optional resulting name of the font family, nullptr otherwise
52  * @param styleName Optional resulting name of the font syle (if known), nullptr otherwise
53  * @return True, if succeeded
54  * @see registerSystemFonts(), registerFonts(), font().
55  */
56  bool registerFont(const std::string& fontFile, std::string* familyName = nullptr, std::string* styleName = nullptr);
57 
58  /**
59  * Registers all font files located in a specified directory.
60  * @param fontDirectory The directory in which all font files will be registered, must be valid
61  * @return The number successfully registered fonts, with range [0, infinity)
62  * @see registerSystemFonts(), registerFont(), font().
63  */
64  size_t registerFonts(const std::string& fontDirectory);
65 
66  /**
67  * Registers a new font by a given font file already stored in memory.
68  * @param fontMemory The memory holding the loaded font file, must be valid
69  * @param fontMemorySize The size of the memory in bytes, with range [1, infinity)
70  * @param familyName Optional resulting name of the font family, nullptr otherwise
71  * @param styleName Optional resulting name of the font syle (if known), nullptr otherwise
72  * @return True, if succeeded
73  * @see registerSystemFonts(), registerFonts(), font().
74  */
75  bool registerFont(const void* fontMemory, const size_t fontMemorySize, std::string* familyName = nullptr, std::string* styleName = nullptr);
76 
77  /**
78  * Registers a new font by a given font file which is already in memory.
79  * The requested font must have been registered before.
80  * @param familyName The name of the font family, empty to get any registered font
81  * @param size The size of the font, in dots, with range [1, infinity)
82  * @param styleName Optinal style name of the font, empty to get any style
83  * @param dpi The dots per inch of the font, with range [1, infinity)
84  * @return The requested font if existing, nullptr otherwise
85  * @see registerFont().
86  */
87  SharedFont font(const std::string& familyName, const unsigned int size, const std::string& styleName = std::string(), const unsigned int dpi = 72u);
88 
89  /**
90  * Returns whether a specific font is registered.
91  * @param familyName The name of the font family to check
92  * @param styleName Optional explicit font style to check
93  * @return True, if so
94  */
95  bool hasFont(const std::string& familyName, const std::string& styleName = std::string()) const;
96 
97  /**
98  * Returns the family names of all registered fonts.
99  * @return The family names of all registered fonts
100  */
101  std::vector<std::string> familyNames() const;
102 
103  /**
104  * Returns the style names of all registered fonts with specified family name.
105  * @param familyName The family name of the fonts for which all style names will be returned
106  * @return The style names of all registered fonts
107  */
108  std::vector<std::string> styleNames(const std::string& familyName) const;
109 
110  /**
111  * Returns whether this manager is valid and ready to use.
112  * @return True, if so
113  */
114  inline bool isValid() const;
115 
116  /**
117  * Returns the family name of the system's default font.
118  * The font may not be registered.
119  * @param styleName Optional resulting style name of the resulting font, nullptr if not of interest
120  * @return The system's default font
121  */
122  static std::string systemDefaultFontFamily(std::string* styleName = nullptr);
123 
124  protected:
125 
126  /**
127  * Creates a new manager for fonts.
128  */
130 
131  /**
132  * Destructs the FontManager object.
133  */
135 
136  protected:
137 
138  /// The actual implementation of the font manager.
139  std::unique_ptr<FTFontManager> ftFontManager_;
140 };
141 
142 inline bool FontManager::isValid() const
143 {
144  return ftFontManager_.get() != nullptr;
145 }
146 
147 }
148 
149 }
150 
151 }
152 
153 #endif // META_OCEAN_CV_FONTS_FONT_MANAGER_H
This class implements the manager for all fonts.
Definition: FontManager.h:36
bool isValid() const
Returns whether this manager is valid and ready to use.
Definition: FontManager.h:142
std::vector< std::string > styleNames(const std::string &familyName) const
Returns the style names of all registered fonts with specified family name.
size_t registerFonts(const std::string &fontDirectory)
Registers all font files located in a specified directory.
std::vector< std::string > familyNames() const
Returns the family names of all registered fonts.
FontManager()
Creates a new manager for fonts.
bool registerFont(const std::string &fontFile, std::string *familyName=nullptr, std::string *styleName=nullptr)
Registers a new font by a given font file.
~FontManager()
Destructs the FontManager object.
static std::string systemDefaultFontFamily(std::string *styleName=nullptr)
Returns the family name of the system's default font.
size_t registerSystemFonts()
Registers all fonts available on the system.
SharedFont font(const std::string &familyName, const unsigned int size, const std::string &styleName=std::string(), const unsigned int dpi=72u)
Registers a new font by a given font file which is already in memory.
bool hasFont(const std::string &familyName, const std::string &styleName=std::string()) const
Returns whether a specific font is registered.
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 already stored in memory.
std::unique_ptr< FTFontManager > ftFontManager_
The actual implementation of the font manager.
Definition: FontManager.h:139
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
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