| 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/typographer/font.h"
12 #include "impeller/typographer/glyph.h"
13
14 namespace impeller {
15
16 struct GlyphProperties {
17 Color color = Color::Black();
18 Scalar stroke_width = 0.0;
19 Cap stroke_cap = Cap::kButt;
20 Join stroke_join = Join::kMiter;
21 Scalar stroke_miter = 4.0;
22 bool stroke = false;
23
24 struct Equal {
25 constexpr bool operator()(const impeller::GlyphProperties& lhs,
26 const impeller::GlyphProperties& rhs) const {
27 return lhs.color.ToARGB() == rhs.color.ToARGB() &&
28 lhs.stroke == rhs.stroke && lhs.stroke_cap == rhs.stroke_cap &&
29 lhs.stroke_join == rhs.stroke_join &&
30 lhs.stroke_miter == rhs.stroke_miter &&
31 lhs.stroke_width == rhs.stroke_width;
32 }
33 };
34 };
35
36 //------------------------------------------------------------------------------
37 /// @brief A font and a scale. Used as a key that represents a typeface
38 /// within a glyph atlas.
39 ///
40 struct ScaledFont {
41 Font font;
42 Scalar scale;
43
44 template <typename H>
45 friend H AbslHashValue(H h, const ScaledFont& sf) {
46 return H::combine(std::move(h), sf.font.GetHash(), sf.scale);
47 }
48
49 struct Equal {
50 constexpr bool operator()(const impeller::ScaledFont& lhs,
51 const impeller::ScaledFont& rhs) const {
52 return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
53 }
54 };
55 };
56
57 //------------------------------------------------------------------------------
58 /// @brief A glyph and its subpixel position.
59 ///
60 struct SubpixelGlyph {
61 Glyph glyph;
62 Point subpixel_offset;
63 std::optional<GlyphProperties> properties;
64
65 SubpixelGlyph(Glyph p_glyph,
66 Point p_subpixel_offset,
67 std::optional<GlyphProperties> p_properties)
68 : glyph(p_glyph),
69 subpixel_offset(p_subpixel_offset),
70 properties(p_properties) {}
71
72 template <typename H>
73 friend H AbslHashValue(H h, const SubpixelGlyph& sg) {
74 if (!sg.properties.has_value()) {
75 return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset.x,
76 sg.subpixel_offset.y);
77 }
78 return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset.x,
79 sg.subpixel_offset.y, sg.properties->color.ToARGB(),
80 sg.properties->stroke, sg.properties->stroke_cap,
81 sg.properties->stroke_join, sg.properties->stroke_miter,
82 sg.properties->stroke_width);
83 }
84
85 struct Equal {
86 constexpr bool operator()(const impeller::SubpixelGlyph& lhs,
87 const impeller::SubpixelGlyph& rhs) const {
88 // Check simple non-optionals first.
89 if (lhs.glyph.index != rhs.glyph.index ||
90 lhs.glyph.type != rhs.glyph.type ||
91 lhs.subpixel_offset != rhs.subpixel_offset ||
92 // Mixmatch properties.
93 lhs.properties.has_value() != rhs.properties.has_value()) {
94 return false;
95 }
96 if (lhs.properties.has_value()) {
97 // Both have properties.
98 return GlyphProperties::Equal{}(lhs.properties.value(),
99 rhs.properties.value());
100 }
101 return true;
102 }
103 };
104 };
105
106 //------------------------------------------------------------------------------
107 /// @brief A font along with a glyph in that font rendered at a particular
108 /// scale and subpixel position.
109 ///
110 struct FontGlyphPair {
111 FontGlyphPair(const ScaledFont& sf, const SubpixelGlyph& g)
112 : scaled_font(sf), glyph(g) {}
113 ScaledFont scaled_font;
114 SubpixelGlyph glyph;
115 };
116
117 } // namespace impeller
118
119 #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::GlyphProperties
Definition: font_glyph_pair.h:16
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:50
impeller::SubpixelGlyph::subpixel_offset
Point subpixel_offset
Definition: font_glyph_pair.h:62
impeller::SubpixelGlyph::Equal::operator()
constexpr bool operator()(const impeller::SubpixelGlyph &lhs, const impeller::SubpixelGlyph &rhs) const
Definition: font_glyph_pair.h:86
impeller::GlyphProperties::Equal
Definition: font_glyph_pair.h:24
impeller::ScaledFont::font
Font font
Definition: font_glyph_pair.h:41
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:60
impeller::Color::ToARGB
constexpr uint32_t ToARGB() const
Convert to ARGB 32 bit color.
Definition: color.h:258
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:21
impeller::Cap::kButt
@ kButt
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:17
impeller::SubpixelGlyph::properties
std::optional< GlyphProperties > properties
Definition: font_glyph_pair.h:63
impeller::Join::kMiter
@ kMiter
impeller::Glyph
The glyph index in the typeface.
Definition: glyph.h:16
impeller::FontGlyphPair::scaled_font
ScaledFont scaled_font
Definition: font_glyph_pair.h:113
impeller::ScaledFont::AbslHashValue
friend H AbslHashValue(H h, const ScaledFont &sf)
Definition: font_glyph_pair.h:45
glyph.h
font.h
impeller::ScaledFont::Equal
Definition: font_glyph_pair.h:49
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:61
impeller::GlyphProperties::Equal::operator()
constexpr bool operator()(const impeller::GlyphProperties &lhs, const impeller::GlyphProperties &rhs) const
Definition: font_glyph_pair.h:25
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:20
impeller::Glyph::index
uint16_t index
Definition: glyph.h:22
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:22
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:110
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:18
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:42
impeller::FontGlyphPair::glyph
SubpixelGlyph glyph
Definition: font_glyph_pair.h:114
impeller::SubpixelGlyph::SubpixelGlyph
SubpixelGlyph(Glyph p_glyph, Point p_subpixel_offset, std::optional< GlyphProperties > p_properties)
Definition: font_glyph_pair.h:65
impeller::FontGlyphPair::FontGlyphPair
FontGlyphPair(const ScaledFont &sf, const SubpixelGlyph &g)
Definition: font_glyph_pair.h:111
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:19
impeller::Join
Join
Definition: path.h:25
impeller::SubpixelGlyph::AbslHashValue
friend H AbslHashValue(H h, const SubpixelGlyph &sg)
Definition: font_glyph_pair.h:73
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:85
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:40
impeller::Cap
Cap
Definition: path.h:19