Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
font.h
Go to the documentation of this file.
1 
18 #ifndef ION_TEXT_FONT_H_
19 #define ION_TEXT_FONT_H_
20 
21 #include <string>
22 
23 #include "ion/base/allocator.h"
24 #include "ion/base/array2.h"
25 #include "ion/base/invalid.h"
26 #include "ion/base/referent.h"
28 #include "ion/math/vector.h"
29 #include "ion/text/layout.h"
30 
31 namespace ion {
32 namespace text {
33 
35 typedef uint32 CharIndex;
36 
38 class Font;
40 
43 class ION_API Font : public base::Referent {
44  public:
48  struct GlyphGrid {
49  GlyphGrid() : pixels(), is_sdf(false) {}
50  GlyphGrid(size_t width, size_t height);
51 
54 
56  bool IsZeroSize() const;
57 
61  bool is_sdf;
62  };
63 
66  struct FontMetrics {
68  FontMetrics() : line_advance_height(0.f) {}
69 
72  };
73 
75  const std::string& GetName() const { return name_; }
76 
78  size_t GetSizeInPixels() const { return size_in_pixels_; }
79 
84  size_t GetSdfPadding() const { return sdf_padding_; }
85 
87  const FontMetrics& GetFontMetrics() const { return font_metrics_; }
88 
91  const GlyphGrid& GetGlyphGrid(GlyphIndex glyph_index) const;
92 
94  void FilterGlyphs(GlyphSet* glyph_set);
95 
105  virtual GlyphIndex GetDefaultGlyphForChar(CharIndex char_index) const = 0;
106 
111  void AddGlyphsForAsciiCharacterRange(ion::text::CharIndex start,
112  ion::text::CharIndex finish,
113  ion::text::GlyphSet* glyphs);
114 
117  virtual const Layout BuildLayout(const std::string& text,
118  const LayoutOptions& options) const = 0;
119 
124  void CacheSdfGrids(const GlyphSet& glyph_set);
125 
130  virtual void AddFallbackFont(const FontPtr& fallback) = 0;
131 
132  protected:
135 
137  Font(const std::string& name, size_t size_in_pixels, size_t sdf_padding);
138 
141  ~Font() override;
142 
144  GlyphGrid* GetMutableGlyphGrid(GlyphIndex glyph_index) const;
145 
147  GlyphGrid* GetMutableGlyphGridLocked(GlyphIndex glyph_index) const;
148 
152  virtual bool LoadGlyphGrid(GlyphIndex glyph_index,
153  GlyphGrid* glyph_grid) const;
154 
156  const GlyphGrid& AddGlyph(GlyphIndex glyph_index,
157  const GlyphGrid& glyph) const;
158 
160  void SetFontMetrics(const FontMetrics& metrics);
161 
165  bool CacheSdfGrid(GlyphIndex glyph_index,
166  const base::Array2<double>& sdf_pixels);
167 
169  const size_t size_in_pixels_;
170 
171  private:
173  const std::string name_;
175  const size_t sdf_padding_;
177  FontMetrics font_metrics_;
180  mutable GlyphMap glyph_grid_map_;
182  mutable port::Mutex mutex_;
183 
184  DISALLOW_IMPLICIT_CONSTRUCTORS(Font);
185 };
186 
187 } // namespace text
188 } // namespace ion
189 
190 #endif // ION_TEXT_FONT_H_
size_t GetSdfPadding() const
Returns the padding value used when generating SDF glyphs from the font.
Definition: font.h:84
const size_t size_in_pixels_
Size in pixels.
Definition: font.h:169
base::AllocMap< GlyphIndex, GlyphGrid > GlyphMap
Convenience typedef for the map storing GlyphGrid instances.
Definition: font.h:134
std::string text
float line_advance_height
Nominal font-wide line-advance height, in pixels.
Definition: font.h:71
bool is_sdf
When a Font is set up for rendering, the pixels are replaced with a signed-distance field (SDF)...
Definition: font.h:61
uint32 CharIndex
Typedef for a Unicode index of a character.
Definition: font.h:35
size_t GetSizeInPixels() const
Returns the size of the font in pixels.
Definition: font.h:78
This struct defines parameters affecting layout of a single text string when passed to BuildLayout()...
Definition: layout.h:101
Thread-safe abstract base class.
Definition: referent.h:49
FontMetrics()
The default constructor initializes everything to 0.
Definition: font.h:68
base::ReferentPtr< Font >::Type FontPtr
Definition: font.h:38
std::string name
Definition: printer.cc:324
const FontMetrics & GetFontMetrics() const
Returns the FontMetrics for the font.
Definition: font.h:87
base::Array2< double > pixels
Definition: font.h:53
A Layout instance specifies how glyphs are arranged to form text.
Definition: layout.h:127
int width
const std::string & GetName() const
Returns the name of the font.
Definition: font.h:75
port::Mutex mutex_
Protects shared access to the Allocator and FT_Library.
uint64 GlyphIndex
Definition: layout.h:46
A grid representing a rendered glyph, with each grid pixel representing pixel coverage in the range (...
Definition: font.h:48
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
This struct represents the cumulative metrics for the font.
Definition: font.h:66
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
Font is a base class for implementation-specific representations of fonts.
Definition: font.h:43