Flutter Impeller
font_glyph_pair.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_TYPOGRAPHER_FONT_GLYPH_PAIR_H_
6 #define FLUTTER_IMPELLER_TYPOGRAPHER_FONT_GLYPH_PAIR_H_
7 
8 #include <optional>
9 
16 
17 namespace impeller {
18 
21  std::optional<StrokeParameters> stroke;
22 
23  struct Equal {
24  inline bool operator()(const impeller::GlyphProperties& lhs,
25  const impeller::GlyphProperties& rhs) const {
26  return lhs.color.ToARGB() == rhs.color.ToARGB() &&
27  lhs.stroke == rhs.stroke;
28  }
29  };
30 };
31 
32 //------------------------------------------------------------------------------
33 /// @brief A font and a scale. Used as a key that represents a typeface
34 /// within a glyph atlas.
35 ///
36 struct ScaledFont {
39 
40  template <typename H>
41  friend H AbslHashValue(H h, const ScaledFont& sf) {
42  return H::combine(std::move(h), sf.font.GetHash(), sf.scale.GetHash());
43  }
44 
45  struct Equal {
46  inline bool operator()(const impeller::ScaledFont& lhs,
47  const impeller::ScaledFont& rhs) const {
48  return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
49  }
50  };
51 };
52 
53 /// All possible positions for a subpixel alignment.
54 /// The name is in the format kSubpixelXY where X and Y are numerators to 1/4
55 /// fractions in their respective directions.
56 enum SubpixelPosition : uint8_t {
57  // Subpixel at {0, 0}.
58  kSubpixel00 = 0x0,
59  // Subpixel at {0.25, 0}.
60  kSubpixel10 = 0x1,
61  // Subpixel at {0.5, 0}.
62  kSubpixel20 = 0x2,
63  // Subpixel at {0.75, 0}.
64  kSubpixel30 = 0x3,
65  // Subpixel at {0, 0.25}.
67  // Subpixel at {0, 0.5}.
69  // Subpixel at {0, 0.75}.
80 };
81 
82 //------------------------------------------------------------------------------
83 /// @brief A glyph and its subpixel position.
84 ///
85 struct SubpixelGlyph {
88  std::optional<GlyphProperties> properties;
89 
91  SubpixelPosition p_subpixel_offset,
92  std::optional<GlyphProperties> p_properties)
93  : glyph(p_glyph),
94  subpixel_offset(p_subpixel_offset),
95  properties(p_properties) {}
96 
97  template <typename H>
98  friend H AbslHashValue(H h, const SubpixelGlyph& sg) {
99  if (!sg.properties.has_value()) {
100  return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset);
101  }
102  StrokeParameters stroke;
103  bool has_stroke = sg.properties->stroke.has_value();
104  if (has_stroke) {
105  stroke = sg.properties->stroke.value();
106  }
107  return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset,
108  sg.properties->color.ToARGB(), has_stroke, stroke.cap,
109  stroke.join, stroke.miter_limit, stroke.width);
110  }
111 
112  struct Equal {
113  constexpr bool operator()(const impeller::SubpixelGlyph& lhs,
114  const impeller::SubpixelGlyph& rhs) const {
115  // Check simple non-optionals first.
116  if (lhs.glyph.index != rhs.glyph.index ||
117  lhs.glyph.type != rhs.glyph.type ||
118  lhs.subpixel_offset != rhs.subpixel_offset ||
119  // Mixmatch properties.
120  lhs.properties.has_value() != rhs.properties.has_value()) {
121  return false;
122  }
123  if (lhs.properties.has_value()) {
124  // Both have properties.
125  return GlyphProperties::Equal{}(lhs.properties.value(),
126  rhs.properties.value());
127  }
128  return true;
129  }
130  };
131 };
132 
133 //------------------------------------------------------------------------------
134 /// @brief A font along with a glyph in that font rendered at a particular
135 /// scale and subpixel position.
136 ///
139  : scaled_font(sf), glyph(g) {}
142 };
143 
144 } // namespace impeller
145 
146 #endif // FLUTTER_IMPELLER_TYPOGRAPHER_FONT_GLYPH_PAIR_H_
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
bool IsEqual(const Font &other) const override
Definition: font.cc:36
std::size_t GetHash() const override
Definition: font.cc:31
uint64_t GetHash() const
Definition: rational.cc:42
uint32_t ToARGB() const
Convert to ARGB 32 bit color.
Definition: color.h:259
static constexpr Color Black()
Definition: color.h:266
A font along with a glyph in that font rendered at a particular scale and subpixel position.
FontGlyphPair(const ScaledFont &sf, const SubpixelGlyph &g)
The glyph index in the typeface.
Definition: glyph.h:16
uint16_t index
Definition: glyph.h:22
Type type
Whether the glyph is a path or a bitmap.
Definition: glyph.h:27
bool operator()(const impeller::GlyphProperties &lhs, const impeller::GlyphProperties &rhs) const
std::optional< StrokeParameters > stroke
bool operator()(const impeller::ScaledFont &lhs, const impeller::ScaledFont &rhs) const
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
friend H AbslHashValue(H h, const ScaledFont &sf)
A structure to store all of the parameters related to stroking a path or basic geometry object.
constexpr bool operator()(const impeller::SubpixelGlyph &lhs, const impeller::SubpixelGlyph &rhs) const
A glyph and its subpixel position.
std::optional< GlyphProperties > properties
friend H AbslHashValue(H h, const SubpixelGlyph &sg)
SubpixelPosition subpixel_offset
SubpixelGlyph(Glyph p_glyph, SubpixelPosition p_subpixel_offset, std::optional< GlyphProperties > p_properties)