| 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 "impeller/geometry/color.h"
9 #include "impeller/geometry/path.h"
10 #include "impeller/geometry/point.h"
11 #include "impeller/geometry/rational.h"
12 #include "impeller/typographer/font.h"
13 #include "impeller/typographer/glyph.h"
14
15 namespace impeller {
16
17 struct GlyphProperties {
18 Color color = Color::Black();
19 Scalar stroke_width = 0.0;
20 Cap stroke_cap = Cap::kButt;
21 Join stroke_join = Join::kMiter;
22 Scalar stroke_miter = 4.0;
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 {
42 Font font;
43 Rational scale;
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}.
71 kSubpixel01 = kSubpixel10 << 2,
72 // Subpixel at {0, 0.5}.
73 kSubpixel02 = kSubpixel20 << 2,
74 // Subpixel at {0, 0.75}.
75 kSubpixel03 = kSubpixel30 << 2,
76 kSubpixel11 = kSubpixel10 | kSubpixel01,
77 kSubpixel12 = kSubpixel10 | kSubpixel02,
78 kSubpixel13 = kSubpixel10 | kSubpixel03,
79 kSubpixel21 = kSubpixel20 | kSubpixel01,
80 kSubpixel22 = kSubpixel20 | kSubpixel02,
81 kSubpixel23 = kSubpixel20 | kSubpixel03,
82 kSubpixel31 = kSubpixel30 | kSubpixel01,
83 kSubpixel32 = kSubpixel30 | kSubpixel02,
84 kSubpixel33 = kSubpixel30 | kSubpixel03,
85 };
86
87 //------------------------------------------------------------------------------
88 /// @brief A glyph and its subpixel position.
89 ///
90 struct SubpixelGlyph {
91 Glyph glyph;
92 SubpixelPosition subpixel_offset;
93 std::optional<GlyphProperties> properties;
94
95 SubpixelGlyph(Glyph p_glyph,
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 ///
138 struct FontGlyphPair {
139 FontGlyphPair(const ScaledFont& sf, const SubpixelGlyph& g)
140 : scaled_font(sf), glyph(g) {}
141 ScaledFont scaled_font;
142 SubpixelGlyph glyph;
143 };
144
145 } // namespace impeller
146
147 #endif // FLUTTER_IMPELLER_TYPOGRAPHER_FONT_GLYPH_PAIR_H_
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::Font::IsEqual
bool IsEqual(const Font &other) const override
Definition: font.cc:36
impeller::Font::GetHash
std::size_t GetHash() const override
Definition: font.cc:31
impeller::Rational
Definition: rational.h:13
impeller::Rational::GetHash
uint64_t GetHash() const
Definition: rational.cc:42
color.h
font.h
path.h
glyph.h
impeller
Definition: allocation.cc:12
impeller::Join
Join
Definition: path.h:26
impeller::Join::kMiter
@ kMiter
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Cap
Cap
Definition: path.h:20
impeller::Cap::kButt
@ kButt
impeller::SubpixelPosition
SubpixelPosition
Definition: font_glyph_pair.h:61
impeller::kSubpixel31
@ kSubpixel31
Definition: font_glyph_pair.h:82
impeller::kSubpixel23
@ kSubpixel23
Definition: font_glyph_pair.h:81
impeller::kSubpixel02
@ kSubpixel02
Definition: font_glyph_pair.h:73
impeller::kSubpixel20
@ kSubpixel20
Definition: font_glyph_pair.h:67
impeller::kSubpixel11
@ kSubpixel11
Definition: font_glyph_pair.h:76
impeller::kSubpixel13
@ kSubpixel13
Definition: font_glyph_pair.h:78
impeller::kSubpixel01
@ kSubpixel01
Definition: font_glyph_pair.h:71
impeller::kSubpixel33
@ kSubpixel33
Definition: font_glyph_pair.h:84
impeller::kSubpixel12
@ kSubpixel12
Definition: font_glyph_pair.h:77
impeller::kSubpixel03
@ kSubpixel03
Definition: font_glyph_pair.h:75
impeller::kSubpixel22
@ kSubpixel22
Definition: font_glyph_pair.h:80
impeller::kSubpixel00
@ kSubpixel00
Definition: font_glyph_pair.h:63
impeller::kSubpixel10
@ kSubpixel10
Definition: font_glyph_pair.h:65
impeller::kSubpixel21
@ kSubpixel21
Definition: font_glyph_pair.h:79
impeller::kSubpixel32
@ kSubpixel32
Definition: font_glyph_pair.h:83
impeller::kSubpixel30
@ kSubpixel30
Definition: font_glyph_pair.h:69
point.h
rational.h
impeller::Color
Definition: color.h:124
impeller::Color::ToARGB
constexpr uint32_t ToARGB() const
Convert to ARGB 32 bit color.
Definition: color.h:259
impeller::Color::Black
static constexpr Color Black()
Definition: color.h:266
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:138
impeller::FontGlyphPair::glyph
SubpixelGlyph glyph
Definition: font_glyph_pair.h:142
impeller::FontGlyphPair::FontGlyphPair
FontGlyphPair(const ScaledFont &sf, const SubpixelGlyph &g)
Definition: font_glyph_pair.h:139
impeller::FontGlyphPair::scaled_font
ScaledFont scaled_font
Definition: font_glyph_pair.h:141
impeller::Glyph
The glyph index in the typeface.
Definition: glyph.h:16
impeller::Glyph::index
uint16_t index
Definition: glyph.h:22
impeller::Glyph::type
Type type
Whether the glyph is a path or a bitmap.
Definition: glyph.h:27
impeller::GlyphProperties::Equal
Definition: font_glyph_pair.h:25
impeller::GlyphProperties::Equal::operator()
constexpr bool operator()(const impeller::GlyphProperties &lhs, const impeller::GlyphProperties &rhs) const
Definition: font_glyph_pair.h:26
impeller::GlyphProperties
Definition: font_glyph_pair.h:17
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:18
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:20
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:19
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:21
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:22
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:23
impeller::ScaledFont::Equal
Definition: font_glyph_pair.h:50
impeller::ScaledFont::Equal::operator()
constexpr bool operator()(const impeller::ScaledFont &lhs, const impeller::ScaledFont &rhs) const
Definition: font_glyph_pair.h:51
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:41
impeller::ScaledFont::scale
Rational scale
Definition: font_glyph_pair.h:43
impeller::ScaledFont::AbslHashValue
friend H AbslHashValue(H h, const ScaledFont &sf)
Definition: font_glyph_pair.h:46
impeller::ScaledFont::font
Font font
Definition: font_glyph_pair.h:42
impeller::SubpixelGlyph::Equal
Definition: font_glyph_pair.h:113
impeller::SubpixelGlyph::Equal::operator()
constexpr bool operator()(const impeller::SubpixelGlyph &lhs, const impeller::SubpixelGlyph &rhs) const
Definition: font_glyph_pair.h:114
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:90
impeller::SubpixelGlyph::properties
std::optional< GlyphProperties > properties
Definition: font_glyph_pair.h:93
impeller::SubpixelGlyph::AbslHashValue
friend H AbslHashValue(H h, const SubpixelGlyph &sg)
Definition: font_glyph_pair.h:103
impeller::SubpixelGlyph::subpixel_offset
SubpixelPosition subpixel_offset
Definition: font_glyph_pair.h:92
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:91
impeller::SubpixelGlyph::SubpixelGlyph
SubpixelGlyph(Glyph p_glyph, SubpixelPosition p_subpixel_offset, std::optional< GlyphProperties > p_properties)
Definition: font_glyph_pair.h:95