Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
font.cc
Go to the documentation of this file.
1 
18 #include "ion/text/font.h"
19 
20 #include <cctype>
21 #include <limits>
22 
23 #include "ion/base/invalid.h"
24 #include "ion/base/lockguards.h"
25 #include "ion/base/logging.h"
26 #include "ion/math/rangeutils.h"
27 #include "ion/math/vector.h"
28 #include "ion/text/layout.h"
29 #include "ion/text/sdfutils.h"
30 
31 namespace ion {
32 namespace text {
33 
34 Font::GlyphGrid::GlyphGrid(size_t width, size_t height)
35  : pixels(width, height), is_sdf(false) {}
36 
38  return pixels.GetWidth() * pixels.GetHeight() == 0;
39 }
40 
41 Font::Font(const std::string& name, size_t size_in_pixels, size_t sdf_padding)
42  : size_in_pixels_(size_in_pixels),
43  name_(name),
44  sdf_padding_(sdf_padding),
45  glyph_grid_map_(*this) {}
46 
48 
49 const Font::GlyphGrid& Font::GetGlyphGrid(GlyphIndex glyph_index) const {
50  Font::GlyphGrid* mutable_grid = GetMutableGlyphGrid(glyph_index);
51  return
52  mutable_grid ? *mutable_grid : base::InvalidReference<Font::GlyphGrid>();
53 }
54 
56  base::LockGuard guard(&mutex_);
57  return GetMutableGlyphGridLocked(glyph_index);
58 }
59 
61  DCHECK(mutex_.IsLocked());
62  if (!glyph_index) {
63  return NULL;
64  }
65  const auto& it = glyph_grid_map_.find(glyph_index);
66  if (it == glyph_grid_map_.end()) {
67  GlyphGrid glyph;
68  if (LoadGlyphGrid(glyph_index, &glyph)) {
69  glyph_grid_map_[glyph_index] = glyph;
70  return &glyph_grid_map_[glyph_index];
71  }
72  return NULL;
73  }
74  return &it->second;
75 }
76 
77 bool Font::LoadGlyphGrid(GlyphIndex glyph_index, GlyphGrid* glyph_data) const {
78  return false;
79 }
80 
82  const GlyphGrid& glyph) const {
83  if (base::IsInvalidReference(glyph))
84  return glyph;
85  base::LockGuard guard(&mutex_);
86  return glyph_grid_map_[glyph_index] = glyph;
87 }
88 
89 void Font::SetFontMetrics(const FontMetrics& metrics) {
91  DCHECK_EQ(font_metrics_.line_advance_height, 0.0f);
92  font_metrics_ = metrics;
93 }
94 
95 void Font::CacheSdfGrids(const GlyphSet& glyph_set) {
98  const size_t sdf_padding = GetSdfPadding();
99  for (auto it = glyph_set.cbegin(); it != glyph_set.cend(); ++it) {
100  GlyphGrid* glyph_grid = GetMutableGlyphGrid(*it);
101  DCHECK(!base::IsInvalidReference(glyph_grid));
103  if (!glyph_grid->is_sdf) {
104  CacheSdfGrid(*it, ComputeSdfGrid(glyph_grid->pixels, sdf_padding));
105  DCHECK(glyph_grid->is_sdf);
106  }
107  }
108 }
109 
111  const base::Array2<double>& sdf_pixels) {
112  GlyphGrid* grid = GetMutableGlyphGrid(glyph_index);
113  if (!grid) {
114  LOG(ERROR) << "Invalid glyph passed to SetSdfGrid";
115  } else if (grid->is_sdf) {
116  LOG(ERROR) << "Grid is already an SDF grid";
117  } else {
118  grid->pixels = sdf_pixels;
119  grid->is_sdf = true;
120  return true;
121  }
122  return false;
123 }
124 
125 void Font::FilterGlyphs(GlyphSet* glyph_set) {
126  base::LockGuard guard(&mutex_);
127  for (auto it = glyph_set->begin(); it != glyph_set->end();) {
128  GlyphGrid* glyph = GetMutableGlyphGridLocked(*it);
129  if (!glyph || glyph->IsZeroSize()) {
130  glyph_set->erase(it++);
131  } else {
132  ++it;
133  }
134  }
135 }
136 
138  ion::text::CharIndex finish,
139  ion::text::GlyphSet* glyphs) {
140  DCHECK_LE(start, finish);
141  DCHECK_GE(start, static_cast<CharIndex>(1));
142  DCHECK_LE(finish, static_cast<CharIndex>(127));
143  for (auto i = start; i <= finish; ++i) {
144  const GlyphIndex glyph_index = GetDefaultGlyphForChar(i);
145  if (glyph_index) {
146  glyphs->insert(glyph_index);
147  }
148  }
149 }
150 
151 } // namespace text
152 } // 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
bool CacheSdfGrid(GlyphIndex glyph_index, const base::Array2< double > &sdf_pixels)
Replaces the grid in a glyph with an SDF grid.
Definition: font.cc:110
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
void SetFontMetrics(const FontMetrics &metrics)
Sets FontMetrics. SetFontMetrics() should only ever be called once.
Definition: font.cc:89
std::string text
const GlyphGrid & GetGlyphGrid(GlyphIndex glyph_index) const
Returns the GlyphGrid for the indexed character.
Definition: font.cc:49
#define DCHECK(expr)
Definition: logging.h:331
float line_advance_height
Nominal font-wide line-advance height, in pixels.
Definition: font.h:71
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
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
bool IsLocked()
Returns whether the Mutex is currently locked. Does not block.
Definition: mutex.cc:83
uint32 CharIndex
Typedef for a Unicode index of a character.
Definition: font.h:35
void FilterGlyphs(GlyphSet *glyph_set)
Filter zero-size glyphs from glyph_set.
Definition: font.cc:125
A LockGuard locks a mutex when created, and unlocks it when destroyed.
Definition: lockguards.h:90
virtual GlyphIndex GetDefaultGlyphForChar(CharIndex char_index) const =0
Returns the index of a glyph corresponding to the given character in the default ("unicode", in practice) charmap of the font.
std::string name
Definition: printer.cc:324
bool IsZeroSize() const
Returns true if glyph x- or y-size is zero.
Definition: font.cc:37
base::Array2< double > pixels
Definition: font.h:53
~Font() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
Definition: font.cc:47
#define DCHECK_GE(val1, val2)
Definition: logging.h:336
const GlyphGrid & AddGlyph(GlyphIndex glyph_index, const GlyphGrid &glyph) const
Adds a GlyphGrid to the GlyphMap.
Definition: font.cc:81
void AddGlyphsForAsciiCharacterRange(ion::text::CharIndex start, ion::text::CharIndex finish, ion::text::GlyphSet *glyphs)
For each character in [start,finish] adds the default glyph from font to glyphs.
Definition: font.cc:137
Copyright 2016 Google Inc.
int width
#define DCHECK_EQ(val1, val2)
Definition: logging.h:332
#define DCHECK_LE(val1, val2)
Definition: logging.h:334
void CacheSdfGrids(const GlyphSet &glyph_set)
Makes sure that the GlyphData for each glyph in glyph_set has an SDF grid cached inside the font...
Definition: font.cc:95
GlyphGrid * GetMutableGlyphGrid(GlyphIndex glyph_index) const
Non-const version of GetGlyphGrid.
Definition: font.cc:55
const Grid ComputeSdfGrid(const Grid &image_grid, size_t padding)
Public SDF utility functions.
Definition: sdfutils.cc:428
virtual bool LoadGlyphGrid(GlyphIndex glyph_index, GlyphGrid *glyph_grid) const
Called by GetGlyphGrid() for missing glyphs.
Definition: font.cc:77
GlyphGrid * GetMutableGlyphGridLocked(GlyphIndex glyph_index) const
Prelocked version of GetMutableGlyphGrid.
Definition: font.cc:60
A grid representing a rendered glyph, with each grid pixel representing pixel coverage in the range (...
Definition: font.h:48
This struct represents the cumulative metrics for the font.
Definition: font.h:66
Font(const std::string &name, size_t size_in_pixels, size_t sdf_padding)
The constructor is protected because this is an abstract base class.
Definition: font.cc:41