Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fontmanager.cc
Go to the documentation of this file.
1 
18 #include "ion/text/fontmanager.h"
19 
20 #include <sstream>
21 
23 #if defined(ION_PLATFORM_MAC) || defined(ION_PLATFORM_IOS)
24 #include "ion/text/coretextfont.h"
25 #else
26 #include "ion/text/freetypefont.h"
27 #endif
28 
29 namespace ion {
30 namespace text {
31 
33 
38 
39 
41  : font_map_(*this),
42  font_image_map_(*this) {}
43 
45 
46 void FontManager::AddFont(const FontPtr& font) {
47  if (font.Get())
48  font_map_[BuildFontKeyFromFont(*font)] = font;
49 }
50 
52  const std::string& name, size_t size_in_pixels,
53  size_t sdf_padding, const void* data, size_t data_size) {
54  ion::text::FontPtr font = FindFont(name, size_in_pixels, sdf_padding);
55  if (font.Get())
56  return font;
57 
58 #if defined(ION_PLATFORM_MAC) || defined(ION_PLATFORM_IOS)
60  name, size_in_pixels, sdf_padding, data, data_size));
61 #else
63  name, size_in_pixels, sdf_padding, data, data_size));
64 #endif
65  AddFont(font);
66  return font;
67 }
68 
69 const FontPtr FontManager::AddFontFromZipasset(const std::string& font_name,
70  const std::string& zipasset_name,
71  size_t size_in_pixels,
72  size_t sdf_padding) {
73  ion::text::FontPtr font = FindFont(font_name, size_in_pixels, sdf_padding);
74  if (font.Get())
75  return font;
76 
78  const std::string& data =
79  ion::base::ZipAssetManager::GetFileData(zipasset_name + ".ttf");
80  if (ion::base::IsInvalidReference(data) || data.empty()) {
81  LOG(ERROR) << "Unable to read data for font \"" << font_name << "\".";
82  return font;
83  }
84 
85  return AddFont(font_name, size_in_pixels, sdf_padding, &data[0], data.size());
86 }
87 
89  const std::string& name, size_t size_in_pixels, size_t sdf_padding) const {
90  const std::string key = BuildFontKey(name, size_in_pixels, sdf_padding);
91  FontMap::const_iterator it = font_map_.find(key);
92  return it == font_map_.end() ? FontPtr() : it->second;
93 }
94 
95 const std::string FontManager::BuildFontKey(
96  const std::string& name, size_t size_in_pixels, size_t sdf_padding) {
97  std::ostringstream s;
98  s << name << '/' << size_in_pixels << '/' << sdf_padding;
99  return s.str();
100 }
101 
102 } // namespace text
103 } // namespace ion
bool IsInvalidReference(const T &value)
IsInvalidReference() returns true if a passed const reference of type T has an address of InvalidRefe...
Definition: invalid.h:41
This derived Font class represents a FreeType2 font.
Definition: freetypefont.h:35
std::string text
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
const FontPtr AddFontFromZipasset(const std::string &font_name, const std::string &zipasset_name, size_t size_in_pixels, size_t sdf_padding)
Constructs and adds a font with name font_name from the zipasset with name zipasset_name.
Definition: fontmanager.cc:69
This represents a single CoreText font.
Definition: coretextfont.h:34
static const std::string & GetFileData(const std::string &filename)
Returns the data of the passed filename if the manager contains it.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
base::ReferentPtr< Font >::Type FontPtr
Definition: font.h:38
std::string name
Definition: printer.cc:324
void AddFont(const FontPtr &font)
Adds a Font to the manager.
Definition: fontmanager.cc:46
~FontManager() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
Definition: fontmanager.cc:44
void Reset(T *new_shared)
Changes the pointer to point to the given shared, which may be NULL.
Definition: sharedptr.h:92
const FontPtr FindFont(const std::string &name, size_t size_in_pixels, size_t sdf_padding) const
Returns the Font associated with the given name and size.
Definition: fontmanager.cc:88
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
FontManager()
FontManager functions.
Definition: fontmanager.cc:40