Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fontimage.h
Go to the documentation of this file.
1 
18 #ifndef ION_TEXT_FONTIMAGE_H_
19 #define ION_TEXT_FONTIMAGE_H_
20 
21 #include <algorithm>
22 #include <memory>
23 #include <vector>
24 
25 #include "ion/base/allocator.h"
26 #include "ion/base/invalid.h"
27 #include "ion/base/readwritelock.h"
28 #include "ion/base/referent.h"
32 #include "ion/gfx/image.h"
33 #include "ion/gfx/texture.h"
34 #include "ion/math/range.h"
35 #include "ion/text/font.h"
36 
37 namespace ion {
38 namespace text {
39 
40 class BinPacker;
41 
43 
61 
62 
63 class FontImage : public base::Referent {
64  public:
65  enum Type {
68  };
69 
71  struct ImageData {
72  explicit ImageData(const base::AllocatorPtr& allocator);
82  };
83 
85  Type GetType() const { return type_; }
86 
88  const FontPtr& GetFont() { return font_; }
89 
91  size_t GetMaxImageSize() { return max_image_size_; }
92 
97  virtual const ImageData& FindImageData(const GlyphSet& glyph_set) = 0;
98 
101  static bool HasAllGlyphs(const ImageData& image_data,
102  const GlyphSet& glyph_set) {
103  const auto& iglyphs = image_data.glyph_set;
104  return !base::IsInvalidReference(image_data) &&
105  std::includes(iglyphs.cbegin(), iglyphs.cend(), glyph_set.cbegin(),
106  glyph_set.cend());
107  }
108 
111  static bool HasGlyph(const ImageData& image_data, GlyphIndex glyph_index) {
112  return !base::IsInvalidReference(image_data) &&
113  image_data.glyph_set.count(glyph_index);
114  }
115 
119  static bool GetTextureCoords(const ImageData& image_data,
120  GlyphIndex glyph_index,
121  math::Range2f* rectangle);
122 
123  protected:
127  FontImage(Type type, const FontPtr& font, size_t max_image_size);
128 
131  ~FontImage() override;
132 
133  private:
134  const Type type_;
135  FontPtr font_;
136  const size_t max_image_size_;
137 };
138 
141 
143 
150 
151 
152 class StaticFontImage : public FontImage {
153  public:
158  StaticFontImage(const FontPtr& font, size_t max_image_size,
159  const GlyphSet& glyph_set);
160 
162  const ImageData& GetImageData() const { return image_data_; }
163 
168  const ImageData& FindImageData(const GlyphSet& glyph_sets) override;
169 
170  protected:
173  StaticFontImage(const FontPtr& font, size_t max_image_size,
174  const ImageData& image_data);
175  ~StaticFontImage() override;
176 
177  private:
180  const ImageData InitImageData(const std::string& texture_name,
181  const GlyphSet& glyph_set);
182 
183  const ImageData image_data_;
184 };
185 
188 
190 
206 
207 
208 class DynamicFontImage : public FontImage {
209  public:
213  DynamicFontImage(const FontPtr& font, size_t image_size);
214 
216  size_t GetImageDataCount() const;
217 
220  const ImageData& GetImageData(size_t index) const;
221 
224  float GetImageDataUsedAreaFraction(size_t index) const;
225 
227  void EnableDeferredUpdates(bool enable) { updates_deferred_ = enable; }
228 
230  bool AreUpdatesDeferred() const { return updates_deferred_; }
231 
235  void ProcessDeferredUpdates();
236 
243  const ImageData& FindImageData(const GlyphSet& glyph_set) override;
244 
247  size_t FindImageDataIndex(const GlyphSet& glyph_set);
248 
252  size_t FindContainingImageDataIndex(const GlyphSet& glyph_set);
253 
254  protected:
255  ~DynamicFontImage() override;
256 
257  private:
259  class Helper;
260 
265  size_t FindContainingImageDataIndexPrefiltered(const GlyphSet& glyph_set);
266 
269  size_t FindImageDataThatFits(const GlyphSet& glyph_set);
270 
273  size_t AddImageData(const GlyphSet& glyph_set);
274 
275  std::unique_ptr<Helper> helper_;
276 
278  bool updates_deferred_;
279 
281  base::ReadWriteLock update_lock_;
282 };
283 
286 
287 } // namespace text
288 } // namespace ion
289 
290 #endif // ION_TEXT_FONTIMAGE_H_
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
base::ReferentPtr< StaticFontImage >::Type StaticFontImagePtr
Convenience typedef for shared pointer to a StaticFontImage.
Definition: fontimage.h:187
gfx::TexturePtr texture
Font glyph texture.
Definition: fontimage.h:74
Type GetType() const
Returns the type of an instance.
Definition: fontimage.h:85
std::string type
Definition: printer.cc:353
~FontImage() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
Definition: fontimage.cc:450
std::string text
const ImageData & FindImageData(const GlyphSet &glyph_set) override
Implements this function to find an existing ImageData that already contains all of the glyphs (prese...
Definition: fontimage.cc:618
base::ReferentPtr< DynamicFontImage >::Type DynamicFontImagePtr
Convenience typedef for shared pointer to a DynamicFontImage.
Definition: fontimage.h:285
const ImageData & GetImageData() const
Returns the single ImageData instance.
Definition: fontimage.h:162
size_t FindContainingImageDataIndex(const GlyphSet &glyph_set)
Returns the index of an ImageData instance that contains all of the glyphs (present in the Font) in g...
Definition: fontimage.cc:662
const ImageData & GetImageData(size_t index) const
Returns the indexed ImageData instance, or an invalid reference if the index is out of range...
Definition: fontimage.cc:591
Range< 2, float > Range2f
Definition: range.h:373
StaticFontImage is a derived FontImage that contains a single ImageData instance that is created by t...
Definition: fontimage.h:152
virtual const ImageData & FindImageData(const GlyphSet &glyph_set)=0
Returns a reference to an ImageData instance that best contains the requested glyphs.
GlyphSet glyph_set
Set of glyphs in the image.
Definition: fontimage.h:76
Thread-safe abstract base class.
Definition: referent.h:49
static bool HasGlyph(const ImageData &image_data, GlyphIndex glyph_index)
Convenience function that returns true if an ImageData instance contains a glyph with the given index...
Definition: fontimage.h:111
A FontImage contains image and texture coordinate information used to render font glyphs...
Definition: fontimage.h:63
void ProcessDeferredUpdates()
Updates internal texture data with any deferred updates.
Definition: fontimage.cc:604
ImageData(const base::AllocatorPtr &allocator)
FontImage::ImageData functions.
Definition: fontimage.cc:434
size_t FindImageDataIndex(const GlyphSet &glyph_set)
This is the same as FindImageData(), but instead returns the index of the ImageData, or kInvalidIndex if unsuccessful.
Definition: fontimage.cc:623
static bool GetTextureCoords(const ImageData &image_data, GlyphIndex glyph_index, math::Range2f *rectangle)
Convenience function that sets rectangle to the texture coordinate rectangle to use for the indexed g...
Definition: fontimage.cc:452
size_t GetMaxImageSize()
Returns the maximum image size passed to the constructor.
Definition: fontimage.h:91
float GetImageDataUsedAreaFraction(size_t index) const
Returns the area covered by glyphs in the indexed ImageData, or 0 if the index is out of range...
Definition: fontimage.cc:598
The ReadWriteLock class defines a non-promotable lock that is very fast when only readers try to obta...
Definition: readwritelock.h:51
bool AreUpdatesDeferred() const
Returns whether updates are deferred.
Definition: fontimage.h:230
FontImage(Type type, const FontPtr &font, size_t max_image_size)
The constructor is passed the type of derived class, the Font to use, and the maximum image size (in ...
Definition: fontimage.cc:447
base::AllocVector< gfx::Texture::SubImage > SubImageVec
Vector of SubImages to set on textures.
Definition: fontimage.h:81
base::AllocMap< GlyphIndex, math::Range2f > TexRectMap
Maps glyph index to a texture coordinate rectangle.
Definition: fontimage.h:78
base::ReferentPtr< FontImage >::Type FontImagePtr
Convenience typedef for shared pointer to a FontImage.
Definition: fontimage.h:140
const FontPtr & GetFont()
Returns the Font passed to the constructor.
Definition: fontimage.h:88
static bool HasAllGlyphs(const ImageData &image_data, const GlyphSet &glyph_set)
Convenience function that returns true if an ImageData instance contains all glyphs in glyph_set...
Definition: fontimage.h:101
void EnableDeferredUpdates(bool enable)
Sets whether deferred updates are enabled.
Definition: fontimage.h:227
StaticFontImage(const FontPtr &font, size_t max_image_size, const GlyphSet &glyph_set)
The constructor sets up the single ImageData instance to contain glyphs for all the requested glyphs...
Definition: fontimage.cc:473
const ImageData & FindImageData(const GlyphSet &glyph_sets) override
Implements this function to return the single ImageData instance, whether or not it contains all the ...
Definition: fontimage.cc:490
FontImage::ImageData image_data
The wrapped ImageData instance.
Definition: fontimage.cc:84
DynamicFontImage(const FontPtr &font, size_t image_size)
The constructor sets up the DynamicFontImage to use the Font.
Definition: fontimage.cc:580
uint64 GlyphIndex
Definition: layout.h:46
Data for each image in the FontImage.
Definition: fontimage.h:71
size_t GetImageDataCount() const
Returns the current count of ImageData instances.
Definition: fontimage.cc:587
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...
Definition: allocvector.h:50
DynamicFontImage is a derived FontImage that may contain any number of ImageData instances.
Definition: fontimage.h:208