1 // Copyright (c) 2012 The Chromium 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 UI_GFX_PLATFORM_FONT_MAC_H_
6 #define UI_GFX_PLATFORM_FONT_MAC_H_
7 
8 #include "base/compiler_specific.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/macros.h"
11 #include "base/optional.h"
12 #include "ui/gfx/font_render_params.h"
13 #include "ui/gfx/platform_font.h"
14 
15 namespace gfx {
16 
17 class GFX_EXPORT PlatformFontMac : public PlatformFont {
18  public:
19   // An enum indicating a type of system-specified font.
20   //   - kGeneral: +[NSFont systemFontOfSize:(weight:)]
21   //   - kMenu: +[NSFont menuFontOfSize:]
22   //   - kToolTip: +[NSFont toolTipsFontOfSize:]
23   enum class SystemFontType { kGeneral, kMenu, kToolTip };
24 
25   // Constructs a PlatformFontMac for a system-specified font of
26   // |system_font_type| type. For a non-system-specified font, use any other
27   // constructor.
28   explicit PlatformFontMac(SystemFontType system_font_type);
29 
30   // Constructs a PlatformFontMac for containing the NSFont* |native_font|. Do
31   // not call this for a system-specified font; use the |SystemFontType|
32   // constructor for that. |native_font| must not be null.
33   explicit PlatformFontMac(NativeFont native_font);
34 
35   // Constructs a PlatformFontMac representing the font with name |font_name|
36   // and the size |font_size|. Do not call this for a system-specified font; use
37   // the |SystemFontType| constructor for that.
38   PlatformFontMac(const std::string& font_name,
39                   int font_size);
40 
41   // Constructs a PlatformFontMac representing the font specified by |typeface|
42   // and the size |font_size_pixels|. Do not call this for a system-specified
43   // font; use the |SystemFontType| constructor for that.
44   PlatformFontMac(sk_sp<SkTypeface> typeface,
45                   int font_size_pixels,
46                   const base::Optional<FontRenderParams>& params);
47 
48   // Overridden from PlatformFont:
49   Font DeriveFont(int size_delta,
50                   int style,
51                   Font::Weight weight) const override;
52   int GetHeight() override;
53   Font::Weight GetWeight() const override;
54   int GetBaseline() override;
55   int GetCapHeight() override;
56   int GetExpectedTextWidth(int length) override;
57   int GetStyle() const override;
58   const std::string& GetFontName() const override;
59   std::string GetActualFontName() const override;
60   int GetFontSize() const override;
61   const FontRenderParams& GetFontRenderParams() override;
62   NativeFont GetNativeFont() const override;
63   sk_sp<SkTypeface> GetNativeSkTypeface() const override;
64 
65   // A utility function to get the weight of an NSFont. Used by the unit test.
66   static Font::Weight GetFontWeightFromNSFontForTesting(NSFont* font);
67 
68  private:
69   struct FontSpec {
70     std::string name;  // Corresponds to -[NSFont fontFamily].
71     int size;
72     int style;
73     Font::Weight weight;
74   };
75 
76   PlatformFontMac(NativeFont font,
77                   base::Optional<SystemFontType> system_font_type);
78 
79   PlatformFontMac(NativeFont font,
80                   base::Optional<SystemFontType> system_font_type,
81                   FontSpec spec);
82 
83   ~PlatformFontMac() override;
84 
85   // Calculates and caches the font metrics and initializes |render_params_|.
86   void CalculateMetricsAndInitRenderParams();
87 
88   // Returns an autoreleased NSFont created with the passed-in specifications.
89   NSFont* NSFontWithSpec(FontSpec font_spec) const;
90 
91   // The NSFont instance for this object. If this object was constructed from an
92   // NSFont instance, this holds that NSFont instance. Otherwise this NSFont
93   // instance is constructed from the name, size, and style. If there is no
94   // active font that matched those criteria a default font is used.
95   base::scoped_nsobject<NSFont> native_font_;
96 
97   // If the font is a system font, and if so, what kind.
98   const base::Optional<SystemFontType> system_font_type_;
99 
100   // The name/size/style/weight quartet that specify the font. Initialized in
101   // the constructors.
102   const FontSpec font_spec_;
103 
104   // Cached metrics, generated in CalculateMetrics().
105   int height_;
106   int ascent_;
107   int cap_height_;
108 
109   // Cached average width, generated in GetExpectedTextWidth().
110   float average_width_ = 0.0;
111 
112   // Details about how the font should be rendered.
113   FontRenderParams render_params_;
114 
115   DISALLOW_COPY_AND_ASSIGN(PlatformFontMac);
116 };
117 
118 }  // namespace gfx
119 
120 #endif  // UI_GFX_PLATFORM_FONT_MAC_H_
121