Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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#include <variant>
10
17
18namespace impeller {
19
21 enum class Tone {
22 kDark,
23 kLight,
24 };
25
26 using ToneOrColor = std::variant<Tone, Color>;
29
30 // The tone or color of the glyph. Defaults to Tone::kDark.
31 //
32 // For alpha-channel-only glyphs, this stores a Tone (kDark or kLight). Use
33 // `ComputeTone` to determine the tone from a color.
34 //
35 // For glyphs with a built-in color, this stores the specific Color.
37 std::optional<StrokeParameters> stroke;
38
39 // Computes a Tone from a color. Use to set `tone_or_color` for an alpha-only
40 // Glyph.
41 //
42 // kLight is only used for macOS, where Apple's CoreText renders dark and
43 // light text differently, requiring different glyphs for each. On other
44 // platforms, this always returns kDark.
45 static Tone ComputeTone(const Color& c) {
46#if FML_OS_MACOSX && !FML_OS_IOS
47 // Uses BT.709 luma coefficients
48 // (https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients) to determine
49 // whether a color is light or dark.
50 Scalar luma = c.red * 0.2126f + c.green * 0.7152f + c.blue * 0.0722f;
51 return (luma > 0.5f) ? Tone::kLight : Tone::kDark;
52#else
53 return Tone::kDark;
54#endif
55 }
56
57 struct Equal {
58 inline bool operator()(const impeller::GlyphProperties& lhs,
59 const impeller::GlyphProperties& rhs) const {
60 return lhs.tone_or_color == rhs.tone_or_color && lhs.stroke == rhs.stroke;
61 }
62 };
63};
64
65//------------------------------------------------------------------------------
66/// @brief A font and a scale. Used as a key that represents a typeface
67/// within a glyph atlas.
68///
69struct ScaledFont {
72
73 template <typename H>
74 friend H AbslHashValue(H h, const ScaledFont& sf) {
75 return H::combine(std::move(h), sf.font.GetHash(), sf.scale.GetHash());
76 }
77
78 struct Equal {
79 inline bool operator()(const impeller::ScaledFont& lhs,
80 const impeller::ScaledFont& rhs) const {
81 return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
82 }
83 };
84};
85
86/// All possible positions for a subpixel alignment.
87/// The name is in the format kSubpixelXY where X and Y are numerators to 1/4
88/// fractions in their respective directions.
89enum SubpixelPosition : uint8_t {
90 // Subpixel at {0, 0}.
92 // Subpixel at {0.25, 0}.
94 // Subpixel at {0.5, 0}.
96 // Subpixel at {0.75, 0}.
98 // Subpixel at {0, 0.25}.
100 // Subpixel at {0, 0.5}.
102 // Subpixel at {0, 0.75}.
113};
114
115//------------------------------------------------------------------------------
116/// @brief A glyph and its subpixel position.
117///
122
124 SubpixelPosition p_subpixel_offset,
125 GlyphProperties p_properties)
126 : glyph(p_glyph),
127 subpixel_offset(p_subpixel_offset),
128 properties(p_properties) {}
129
130 template <typename H>
131 friend H AbslHashValue(H h, const SubpixelGlyph& sg) {
132 StrokeParameters stroke;
133 bool has_stroke = sg.properties.stroke.has_value();
134 if (has_stroke) {
135 stroke = sg.properties.stroke.value();
136 }
137 return H::combine(std::move(h), sg.glyph.index, sg.subpixel_offset,
138 sg.properties.tone_or_color, has_stroke, stroke.cap,
139 stroke.join, stroke.miter_limit, stroke.width);
140 }
141
142 struct Equal {
143 constexpr bool operator()(const impeller::SubpixelGlyph& lhs,
144 const impeller::SubpixelGlyph& rhs) const {
145 // Check simple non-optionals first.
146 if (lhs.glyph.index != rhs.glyph.index ||
147 lhs.glyph.type != rhs.glyph.type ||
148 lhs.subpixel_offset != rhs.subpixel_offset) {
149 return false;
150 }
152 }
153 };
154};
155
156//------------------------------------------------------------------------------
157/// @brief A font along with a glyph in that font rendered at a particular
158/// scale and subpixel position.
159///
166
167} // namespace impeller
168
169#endif // FLUTTER_IMPELLER_TYPOGRAPHER_FONT_GLYPH_PAIR_H_
Describes a typeface along with any modifications to its intrinsic properties.
Definition font.h:36
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:38
float Scalar
Definition scalar.h:19
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
bool operator()(const impeller::GlyphProperties &lhs, const impeller::GlyphProperties &rhs) const
static Tone ComputeTone(const Color &c)
static constexpr ToneOrColor kLightTone
static constexpr ToneOrColor kDarkTone
std::optional< StrokeParameters > stroke
std::variant< Tone, Color > ToneOrColor
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)
A structure to store all of the parameters related to stroking a path or basic geometry object.
constexpr bool operator()(const impeller::SubpixelGlyph &lhs, const impeller::SubpixelGlyph &rhs) const
A glyph and its subpixel position.
SubpixelGlyph(Glyph p_glyph, SubpixelPosition p_subpixel_offset, GlyphProperties p_properties)
friend H AbslHashValue(H h, const SubpixelGlyph &sg)
SubpixelPosition subpixel_offset