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 <unordered_map>
9 #include <unordered_set>
10 
11 #include "fml/hash_combine.h"
13 #include "impeller/geometry/path.h"
17 
18 namespace impeller {
19 
26  bool stroke = false;
27 };
28 
29 //------------------------------------------------------------------------------
30 /// @brief A font and a scale. Used as a key that represents a typeface
31 /// within a glyph atlas.
32 ///
33 struct ScaledFont {
36 
37  struct Hash {
38  constexpr std::size_t operator()(const impeller::ScaledFont& sf) const {
39  return fml::HashCombine(sf.font.GetHash(), sf.scale);
40  }
41  };
42 
43  struct Equal {
44  constexpr bool operator()(const impeller::ScaledFont& lhs,
45  const impeller::ScaledFont& rhs) const {
46  return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
47  }
48  };
49 };
50 
51 //------------------------------------------------------------------------------
52 /// @brief A glyph and its subpixel position.
53 ///
54 struct SubpixelGlyph {
57  std::optional<GlyphProperties> properties;
58 
60  Point p_subpixel_offset,
61  std::optional<GlyphProperties> p_properties)
62  : glyph(p_glyph),
63  subpixel_offset(p_subpixel_offset),
64  properties(p_properties) {}
65 
66  struct Hash {
67  constexpr std::size_t operator()(const impeller::SubpixelGlyph& sg) const {
68  if (!sg.properties.has_value()) {
69  return fml::HashCombine(sg.glyph.index, sg.subpixel_offset.x,
70  sg.subpixel_offset.y);
71  }
72  return fml::HashCombine(
74  sg.properties->color.ToARGB(), sg.properties->stroke,
75  sg.properties->stroke_cap, sg.properties->stroke_join,
76  sg.properties->stroke_miter, sg.properties->stroke_width);
77  }
78  };
79 
80  struct Equal {
81  constexpr bool operator()(const impeller::SubpixelGlyph& lhs,
82  const impeller::SubpixelGlyph& rhs) const {
83  if (!lhs.properties.has_value() && !rhs.properties.has_value()) {
84  return lhs.glyph.index == rhs.glyph.index &&
85  lhs.glyph.type == rhs.glyph.type &&
87  }
88  return lhs.glyph.index == rhs.glyph.index &&
89  lhs.glyph.type == rhs.glyph.type &&
90  lhs.subpixel_offset == rhs.subpixel_offset &&
91  lhs.properties.has_value() && rhs.properties.has_value() &&
92  lhs.properties->color.ToARGB() == rhs.properties->color.ToARGB() &&
93  lhs.properties->stroke == rhs.properties->stroke &&
94  lhs.properties->stroke_cap == rhs.properties->stroke_cap &&
95  lhs.properties->stroke_join == rhs.properties->stroke_join &&
96  lhs.properties->stroke_miter == rhs.properties->stroke_miter &&
97  lhs.properties->stroke_width == rhs.properties->stroke_width;
98  }
99  };
100 };
101 
102 using FontGlyphMap =
103  std::unordered_map<ScaledFont,
104  std::unordered_set<SubpixelGlyph,
105  SubpixelGlyph::Hash,
106  SubpixelGlyph::Equal>,
107  ScaledFont::Hash,
109 
110 //------------------------------------------------------------------------------
111 /// @brief A font along with a glyph in that font rendered at a particular
112 /// scale and subpixel position.
113 ///
116  : scaled_font(sf), glyph(g) {}
119 };
120 
121 } // namespace impeller
122 
123 #endif // FLUTTER_IMPELLER_TYPOGRAPHER_FONT_GLYPH_PAIR_H_
impeller::Font::GetHash
std::size_t GetHash() const override
Definition: font.cc:31
point.h
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::FontGlyphMap
std::unordered_map< ScaledFont, std::unordered_set< SubpixelGlyph, SubpixelGlyph::Hash, SubpixelGlyph::Equal >, ScaledFont::Hash, ScaledFont::Equal > FontGlyphMap
Definition: font_glyph_pair.h:108
impeller::GlyphProperties
Definition: font_glyph_pair.h:20
impeller::ScaledFont::Hash
Definition: font_glyph_pair.h:37
impeller::Color
Definition: color.h:123
impeller::ScaledFont::Equal::operator()
constexpr bool operator()(const impeller::ScaledFont &lhs, const impeller::ScaledFont &rhs) const
Definition: font_glyph_pair.h:44
impeller::SubpixelGlyph::subpixel_offset
Point subpixel_offset
Definition: font_glyph_pair.h:56
impeller::SubpixelGlyph::Equal::operator()
constexpr bool operator()(const impeller::SubpixelGlyph &lhs, const impeller::SubpixelGlyph &rhs) const
Definition: font_glyph_pair.h:81
impeller::ScaledFont::font
Font font
Definition: font_glyph_pair.h:34
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:54
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:25
impeller::Cap::kButt
@ kButt
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:21
impeller::SubpixelGlyph::properties
std::optional< GlyphProperties > properties
Definition: font_glyph_pair.h:57
impeller::SubpixelGlyph::Hash
Definition: font_glyph_pair.h:66
impeller::Join::kMiter
@ kMiter
impeller::Glyph
The glyph index in the typeface.
Definition: glyph.h:16
impeller::FontGlyphPair::glyph
const SubpixelGlyph & glyph
Definition: font_glyph_pair.h:118
glyph.h
impeller::FontGlyphPair::scaled_font
const ScaledFont & scaled_font
Definition: font_glyph_pair.h:117
font.h
impeller::ScaledFont::Equal
Definition: font_glyph_pair.h:43
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:55
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:24
impeller::Glyph::index
uint16_t index
Definition: glyph.h:22
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:26
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:114
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:22
impeller::Glyph::type
Type type
Whether the glyph is a path or a bitmap.
Definition: glyph.h:27
impeller::ScaledFont::scale
Scalar scale
Definition: font_glyph_pair.h:35
impeller::ScaledFont::Hash::operator()
constexpr std::size_t operator()(const impeller::ScaledFont &sf) const
Definition: font_glyph_pair.h:38
impeller::SubpixelGlyph::Hash::operator()
constexpr std::size_t operator()(const impeller::SubpixelGlyph &sg) const
Definition: font_glyph_pair.h:67
impeller::SubpixelGlyph::SubpixelGlyph
SubpixelGlyph(Glyph p_glyph, Point p_subpixel_offset, std::optional< GlyphProperties > p_properties)
Definition: font_glyph_pair.h:59
impeller::FontGlyphPair::FontGlyphPair
FontGlyphPair(const ScaledFont &sf, const SubpixelGlyph &g)
Definition: font_glyph_pair.h:115
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:23
impeller::Join
Join
Definition: path.h:24
impeller::TPoint< Scalar >
impeller::Font::IsEqual
bool IsEqual(const Font &other) const override
Definition: font.cc:36
impeller::Color::Black
static constexpr Color Black()
Definition: color.h:265
color.h
path.h
impeller::SubpixelGlyph::Equal
Definition: font_glyph_pair.h:80
impeller
Definition: allocation.cc:12
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:33
impeller::Cap
Cap
Definition: path.h:18