| 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
10 #include "impeller/geometry/color.h"
11 #include "impeller/geometry/rational.h"
12 #include "impeller/geometry/scalar.h"
13 #include "impeller/geometry/stroke_parameters.h"
14 #include "impeller/typographer/font.h"
15 #include "impeller/typographer/glyph.h"
16
17 namespace impeller {
18
19 struct GlyphProperties {
20 Color color = Color::Black();
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 {
37 Font font;
38 Rational scale;
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}.
66 kSubpixel01 = kSubpixel10 << 2,
67 // Subpixel at {0, 0.5}.
68 kSubpixel02 = kSubpixel20 << 2,
69 // Subpixel at {0, 0.75}.
70 kSubpixel03 = kSubpixel30 << 2,
71 kSubpixel11 = kSubpixel10 | kSubpixel01,
72 kSubpixel12 = kSubpixel10 | kSubpixel02,
73 kSubpixel13 = kSubpixel10 | kSubpixel03,
74 kSubpixel21 = kSubpixel20 | kSubpixel01,
75 kSubpixel22 = kSubpixel20 | kSubpixel02,
76 kSubpixel23 = kSubpixel20 | kSubpixel03,
77 kSubpixel31 = kSubpixel30 | kSubpixel01,
78 kSubpixel32 = kSubpixel30 | kSubpixel02,
79 kSubpixel33 = kSubpixel30 | kSubpixel03,
80 };
81
82 //------------------------------------------------------------------------------
83 /// @brief A glyph and its subpixel position.
84 ///
85 struct SubpixelGlyph {
86 Glyph glyph;
87 SubpixelPosition subpixel_offset;
88 std::optional<GlyphProperties> properties;
89
90 SubpixelGlyph(Glyph p_glyph,
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 ///
137 struct FontGlyphPair {
138 FontGlyphPair(const ScaledFont& sf, const SubpixelGlyph& g)
139 : scaled_font(sf), glyph(g) {}
140 ScaledFont scaled_font;
141 SubpixelGlyph glyph;
142 };
143
144 } // namespace impeller
145
146 #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:38
color.h
font.h
glyph.h
impeller
Definition: allocation.cc:12
impeller::SubpixelPosition
SubpixelPosition
Definition: font_glyph_pair.h:56
impeller::kSubpixel31
@ kSubpixel31
Definition: font_glyph_pair.h:77
impeller::kSubpixel23
@ kSubpixel23
Definition: font_glyph_pair.h:76
impeller::kSubpixel02
@ kSubpixel02
Definition: font_glyph_pair.h:68
impeller::kSubpixel20
@ kSubpixel20
Definition: font_glyph_pair.h:62
impeller::kSubpixel11
@ kSubpixel11
Definition: font_glyph_pair.h:71
impeller::kSubpixel13
@ kSubpixel13
Definition: font_glyph_pair.h:73
impeller::kSubpixel01
@ kSubpixel01
Definition: font_glyph_pair.h:66
impeller::kSubpixel33
@ kSubpixel33
Definition: font_glyph_pair.h:79
impeller::kSubpixel12
@ kSubpixel12
Definition: font_glyph_pair.h:72
impeller::kSubpixel03
@ kSubpixel03
Definition: font_glyph_pair.h:70
impeller::kSubpixel22
@ kSubpixel22
Definition: font_glyph_pair.h:75
impeller::kSubpixel00
@ kSubpixel00
Definition: font_glyph_pair.h:58
impeller::kSubpixel10
@ kSubpixel10
Definition: font_glyph_pair.h:60
impeller::kSubpixel21
@ kSubpixel21
Definition: font_glyph_pair.h:74
impeller::kSubpixel32
@ kSubpixel32
Definition: font_glyph_pair.h:78
impeller::kSubpixel30
@ kSubpixel30
Definition: font_glyph_pair.h:64
rational.h
scalar.h
stroke_parameters.h
impeller::Color
Definition: color.h:124
impeller::Color::ToARGB
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:137
impeller::FontGlyphPair::glyph
SubpixelGlyph glyph
Definition: font_glyph_pair.h:141
impeller::FontGlyphPair::FontGlyphPair
FontGlyphPair(const ScaledFont &sf, const SubpixelGlyph &g)
Definition: font_glyph_pair.h:138
impeller::FontGlyphPair::scaled_font
ScaledFont scaled_font
Definition: font_glyph_pair.h:140
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:23
impeller::GlyphProperties::Equal::operator()
bool operator()(const impeller::GlyphProperties &lhs, const impeller::GlyphProperties &rhs) const
Definition: font_glyph_pair.h:24
impeller::GlyphProperties
Definition: font_glyph_pair.h:19
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:20
impeller::GlyphProperties::stroke
std::optional< StrokeParameters > stroke
Definition: font_glyph_pair.h:21
impeller::ScaledFont::Equal
Definition: font_glyph_pair.h:45
impeller::ScaledFont::Equal::operator()
bool operator()(const impeller::ScaledFont &lhs, const impeller::ScaledFont &rhs) const
Definition: font_glyph_pair.h:46
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:36
impeller::ScaledFont::scale
Rational scale
Definition: font_glyph_pair.h:38
impeller::ScaledFont::AbslHashValue
friend H AbslHashValue(H h, const ScaledFont &sf)
Definition: font_glyph_pair.h:41
impeller::ScaledFont::font
Font font
Definition: font_glyph_pair.h:37
impeller::StrokeParameters
A structure to store all of the parameters related to stroking a path or basic geometry object.
Definition: stroke_parameters.h:28
impeller::StrokeParameters::cap
Cap cap
Definition: stroke_parameters.h:30
impeller::StrokeParameters::join
Join join
Definition: stroke_parameters.h:31
impeller::StrokeParameters::miter_limit
Scalar miter_limit
Definition: stroke_parameters.h:32
impeller::StrokeParameters::width
Scalar width
Definition: stroke_parameters.h:29
impeller::SubpixelGlyph::Equal
Definition: font_glyph_pair.h:112
impeller::SubpixelGlyph::Equal::operator()
constexpr bool operator()(const impeller::SubpixelGlyph &lhs, const impeller::SubpixelGlyph &rhs) const
Definition: font_glyph_pair.h:113
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:85
impeller::SubpixelGlyph::properties
std::optional< GlyphProperties > properties
Definition: font_glyph_pair.h:88
impeller::SubpixelGlyph::AbslHashValue
friend H AbslHashValue(H h, const SubpixelGlyph &sg)
Definition: font_glyph_pair.h:98
impeller::SubpixelGlyph::subpixel_offset
SubpixelPosition subpixel_offset
Definition: font_glyph_pair.h:87
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:86
impeller::SubpixelGlyph::SubpixelGlyph
SubpixelGlyph(Glyph p_glyph, SubpixelPosition p_subpixel_offset, std::optional< GlyphProperties > p_properties)
Definition: font_glyph_pair.h:90