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 
14 
15 namespace impeller {
16 
23  bool stroke = false;
24 
25  struct Equal {
26  constexpr bool operator()(const impeller::GlyphProperties& lhs,
27  const impeller::GlyphProperties& rhs) const {
28  return lhs.color.ToARGB() == rhs.color.ToARGB() &&
29  lhs.stroke == rhs.stroke && lhs.stroke_cap == rhs.stroke_cap &&
30  lhs.stroke_join == rhs.stroke_join &&
31  lhs.stroke_miter == rhs.stroke_miter &&
32  lhs.stroke_width == rhs.stroke_width;
33  }
34  };
35 };
36 
37 //------------------------------------------------------------------------------
38 /// @brief A font and a scale. Used as a key that represents a typeface
39 /// within a glyph atlas.
40 ///
41 struct ScaledFont {
44 
45  template <typename H>
46  friend H AbslHashValue(H h, const ScaledFont& sf) {
47  return H::combine(std::move(h), sf.font.GetHash(), sf.scale.GetHash());
48  }
49 
50  struct Equal {
51  constexpr bool operator()(const impeller::ScaledFont& lhs,
52  const impeller::ScaledFont& rhs) const {
53  return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
54  }
55  };
56 };
57 
58 /// All possible positions for a subpixel alignment.
59 /// The name is in the format kSubpixelXY where X and Y are numerators to 1/4
60 /// fractions in their respective directions.
61 enum SubpixelPosition : uint8_t {
62  // Subpixel at {0, 0}.
63  kSubpixel00 = 0x0,
64  // Subpixel at {0.25, 0}.
65  kSubpixel10 = 0x1,
66  // Subpixel at {0.5, 0}.
67  kSubpixel20 = 0x2,
68  // Subpixel at {0.75, 0}.
69  kSubpixel30 = 0x3,
70  // Subpixel at {0, 0.25}.
72  // Subpixel at {0, 0.5}.
74  // Subpixel at {0, 0.75}.
85 };
86 
87 //------------------------------------------------------------------------------
88 /// @brief A glyph and its subpixel position.
89 ///
90 struct SubpixelGlyph {
93  std::optional<GlyphProperties> properties;
94 
96  SubpixelPosition p_subpixel_offset,
97  std::optional<GlyphProperties> p_properties)
98  : glyph(p_glyph),
99  subpixel_offset(p_subpixel_offset),
100  properties(p_properties) {}
101 
102  template <typename H>
103  friend H AbslHashValue(H h, const SubpixelGlyph& sg) {
104  if (!sg.properties.has_value()) {
105  return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset);
106  }
107  return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset,
108  sg.properties->color.ToARGB(), sg.properties->stroke,
109  sg.properties->stroke_cap, sg.properties->stroke_join,
110  sg.properties->stroke_miter, sg.properties->stroke_width);
111  }
112 
113  struct Equal {
114  constexpr bool operator()(const impeller::SubpixelGlyph& lhs,
115  const impeller::SubpixelGlyph& rhs) const {
116  // Check simple non-optionals first.
117  if (lhs.glyph.index != rhs.glyph.index ||
118  lhs.glyph.type != rhs.glyph.type ||
119  lhs.subpixel_offset != rhs.subpixel_offset ||
120  // Mixmatch properties.
121  lhs.properties.has_value() != rhs.properties.has_value()) {
122  return false;
123  }
124  if (lhs.properties.has_value()) {
125  // Both have properties.
126  return GlyphProperties::Equal{}(lhs.properties.value(),
127  rhs.properties.value());
128  }
129  return true;
130  }
131  };
132 };
133 
134 //------------------------------------------------------------------------------
135 /// @brief A font along with a glyph in that font rendered at a particular
136 /// scale and subpixel position.
137 ///
140  : scaled_font(sf), glyph(g) {}
143 };
144 
145 } // namespace impeller
146 
147 #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
Join
Definition: path.h:26
float Scalar
Definition: scalar.h:18
Cap
Definition: path.h:20
constexpr 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
constexpr bool operator()(const impeller::GlyphProperties &lhs, const impeller::GlyphProperties &rhs) const
constexpr 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)
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)