Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fontmanager.h
Go to the documentation of this file.
1 
18 #ifndef ION_TEXT_FONTMANAGER_H_
19 #define ION_TEXT_FONTMANAGER_H_
20 
21 #include <string>
22 #include <vector>
23 
24 #include "ion/base/referent.h"
26 #include "ion/external/gtest/gunit_prod.h" // For FRIEND_TEST().
27 #include "ion/text/font.h"
28 #include "ion/text/fontimage.h"
29 
30 namespace ion {
31 namespace text {
32 
36 class ION_API FontManager : public base::Referent {
37  public:
38  FontManager();
39 
42  void AddFont(const FontPtr& font);
43 
50  const FontPtr AddFont(const std::string& name, size_t size_in_pixels,
51  size_t sdf_padding, const void* data, size_t data_size);
52 
56  const FontPtr AddFontFromZipasset(const std::string& font_name,
57  const std::string& zipasset_name,
58  size_t size_in_pixels,
59  size_t sdf_padding);
60 
63  const FontPtr FindFont(const std::string& name, size_t size_in_pixels,
64  size_t sdf_padding) const;
65 
69  void CacheFontImage(const std::string& key, const FontImagePtr& font_image) {
70  if (font_image.Get())
71  font_image_map_[key] = font_image;
72  else
73  font_image_map_.erase(key);
74  }
75 
80  void CacheFontImage(const FontPtr& font, const FontImagePtr& font_image) {
81  CacheFontImage(BuildFontKeyFromFont(*font), font_image);
82  }
83 
85  const FontImagePtr GetCachedFontImage(const std::string& key) const {
86  const FontImageMap::const_iterator it = font_image_map_.find(key);
87  return it == font_image_map_.end() ? FontImagePtr() : it->second;
88  }
89 
91  const FontImagePtr GetCachedFontImage(const FontPtr& font) const {
92  return GetCachedFontImage(BuildFontKeyFromFont(*font));
93  }
94 
95  protected:
98  ~FontManager() override;
99 
100  private:
101  typedef base::AllocMap<std::string, FontPtr> FontMap;
102  typedef base::AllocMap<std::string, FontImagePtr> FontImageMap;
103 
105  static const std::string BuildFontKeyFromFont(const Font& font) {
106  return BuildFontKey(font.GetName(), font.GetSizeInPixels(),
107  font.GetSdfPadding());
108  }
109 
112  static const std::string BuildFontKey(
113  const std::string& name, size_t size_in_pixels, size_t sdf_padding);
114 
116  FontMap font_map_;
118  FontImageMap font_image_map_;
119 
121  FRIEND_TEST(FontManagerTest, BuildFontKey);
122 };
123 
126 
127 } // namespace text
128 } // namespace ion
129 
130 #endif // ION_TEXT_FONTMANAGER_H_
size_t GetSdfPadding() const
Returns the padding value used when generating SDF glyphs from the font.
Definition: font.h:84
void CacheFontImage(const FontPtr &font, const FontImagePtr &font_image)
This can be used to cache FontImage instances in the manager.
Definition: fontmanager.h:80
std::string text
const FontImagePtr GetCachedFontImage(const FontPtr &font) const
Returns the FontImage associated with the given Font. It may be NULL.
Definition: fontmanager.h:91
size_t GetSizeInPixels() const
Returns the size of the font in pixels.
Definition: font.h:78
Thread-safe abstract base class.
Definition: referent.h:49
The FontManager provides the main interface for fonts used to create text strings to render...
Definition: fontmanager.h:36
const FontImagePtr GetCachedFontImage(const std::string &key) const
Returns the FontImage associated with the given key. It may be NULL.
Definition: fontmanager.h:85
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
std::string name
Definition: printer.cc:324
const std::string & GetName() const
Returns the name of the font.
Definition: font.h:75
void CacheFontImage(const std::string &key, const FontImagePtr &font_image)
This can be used to cache FontImage instances in the manager.
Definition: fontmanager.h:69
base::ReferentPtr< FontImage >::Type FontImagePtr
Convenience typedef for shared pointer to a FontImage.
Definition: fontimage.h:140
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
base::ReferentPtr< FontManager >::Type FontManagerPtr
Convenience typedef for shared pointer to a FontManager.
Definition: fontmanager.h:125
Font is a base class for implementation-specific representations of fonts.
Definition: font.h:43