Flutter Impeller
font.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_H_
6 #define FLUTTER_IMPELLER_TYPOGRAPHER_FONT_H_
7 
8 #include <memory>
9 
10 #include "fml/hash_combine.h"
14 
15 namespace impeller {
16 
17 //------------------------------------------------------------------------------
18 /// @brief Determines the axis along which there is subpixel positioning.
19 ///
20 enum class AxisAlignment : uint8_t {
21  // No subpixel positioning.
22  kNone,
23  // Subpixel positioning in the X axis only.
24  kX,
25  // Subpixel positioning in the Y axis only.
26  kY,
27  // No specific axis, subpixel positioning in each direction.
28  kAll,
29 };
30 
31 //------------------------------------------------------------------------------
32 /// @brief Describes a typeface along with any modifications to its
33 /// intrinsic properties.
34 ///
35 class Font : public Comparable<Font> {
36  public:
37  //----------------------------------------------------------------------------
38  /// @brief Describes the modifications made to the intrinsic properties
39  /// of a typeface.
40  ///
41  /// The coordinate system of a font has its origin at (0, 0) on
42  /// the baseline with an upper-left-origin coordinate system.
43  ///
44  struct Metrics {
45  //--------------------------------------------------------------------------
46  /// The point size of the font.
47  ///
48  Scalar point_size = 12.0f;
49  bool embolden = false;
50  Scalar skewX = 0.0f;
51  Scalar scaleX = 1.0f;
52 
53  constexpr bool operator==(const Metrics& o) const {
54  return point_size == o.point_size && embolden == o.embolden &&
55  skewX == o.skewX && scaleX == o.scaleX;
56  }
57  };
58 
59  Font(std::shared_ptr<Typeface> typeface,
60  Metrics metrics,
61  AxisAlignment axis_alignment);
62 
63  ~Font();
64 
65  bool IsValid() const;
66 
67  //----------------------------------------------------------------------------
68  /// @brief The typeface whose intrinsic properties this font modifies.
69  ///
70  /// @return The typeface.
71  ///
72  const std::shared_ptr<Typeface>& GetTypeface() const;
73 
74  const Metrics& GetMetrics() const;
75 
76  // |Comparable<Font>|
77  std::size_t GetHash() const override;
78 
79  // |Comparable<Font>|
80  bool IsEqual(const Font& other) const override;
81 
83 
84  private:
85  std::shared_ptr<Typeface> typeface_;
86  Metrics metrics_ = {};
87  AxisAlignment axis_alignment_;
88  bool is_valid_ = false;
89 };
90 
91 } // namespace impeller
92 
93 template <>
94 struct std::hash<impeller::Font::Metrics> {
95  constexpr std::size_t operator()(const impeller::Font::Metrics& m) const {
96  return fml::HashCombine(m.point_size, m.skewX, m.scaleX);
97  }
98 };
99 
100 #endif // FLUTTER_IMPELLER_TYPOGRAPHER_FONT_H_
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
bool IsEqual(const Font &other) const override
Definition: font.cc:36
const std::shared_ptr< Typeface > & GetTypeface() const
The typeface whose intrinsic properties this font modifies.
Definition: font.cc:27
AxisAlignment GetAxisAlignment() const
Definition: font.cc:41
std::size_t GetHash() const override
Definition: font.cc:31
const Metrics & GetMetrics() const
Definition: font.cc:45
Font(std::shared_ptr< Typeface > typeface, Metrics metrics, AxisAlignment axis_alignment)
Definition: font.cc:9
bool IsValid() const
Definition: font.cc:23
float Scalar
Definition: scalar.h:19
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
Describes the modifications made to the intrinsic properties of a typeface.
Definition: font.h:44
constexpr bool operator==(const Metrics &o) const
Definition: font.h:53
constexpr std::size_t operator()(const impeller::Font::Metrics &m) const
Definition: font.h:95