File: platform_font_mac.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,662 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_PLATFORM_FONT_MAC_H_
#define UI_GFX_PLATFORM_FONT_MAC_H_

#include <CoreText/CoreText.h>

#include <optional>

#include "base/apple/scoped_cftyperef.h"
#include "ui/gfx/font_render_params.h"
#include "ui/gfx/platform_font.h"

namespace gfx {

class COMPONENT_EXPORT(GFX) PlatformFontMac : public PlatformFont {
 public:
  static constexpr int kDefaultFontSize = 0;

  // An enum indicating a type of system-specified font.
  //   - kGeneral: +[NSFont systemFontOfSize:(weight:)]
  //   - kMenu: +[NSFont menuFontOfSize:]
  //   - kToolTip: +[NSFont toolTipsFontOfSize:]
  enum class SystemFontType { kGeneral, kMenu, kToolTip };

  // Constructs a PlatformFontMac for a system-specified font of
  // |system_font_type| type. For a non-system-specified font, use any other
  // constructor.
  explicit PlatformFontMac(SystemFontType system_font_type,
                           int font_size = kDefaultFontSize);

  // Constructs a PlatformFontMac for containing the CTFontRef |ct_font|. Do
  // not call this for a system-specified font; use the |SystemFontType|
  // constructor for that. |ct_font| must not be null.
  explicit PlatformFontMac(CTFontRef ct_font);

  // Constructs a PlatformFontMac representing the font with name |font_name|
  // and the size |font_size|. Do not call this for a system-specified font; use
  // the |SystemFontType| constructor for that.
  PlatformFontMac(const std::string& font_name,
                  int font_size);

  // Constructs a PlatformFontMac representing the font specified by |typeface|
  // and the size |font_size_pixels|. Do not call this for a system-specified
  // font; use the |SystemFontType| constructor for that.
  PlatformFontMac(sk_sp<SkTypeface> typeface,
                  int font_size_pixels,
                  const std::optional<FontRenderParams>& params);

  PlatformFontMac(const PlatformFontMac&) = delete;
  PlatformFontMac& operator=(const PlatformFontMac&) = delete;

  // Overridden from PlatformFont:
  Font DeriveFont(int size_delta,
                  int style,
                  Font::Weight weight) const override;
  int GetHeight() override;
  Font::Weight GetWeight() const override;
  int GetBaseline() override;
  int GetCapHeight() override;
  int GetExpectedTextWidth(int length) override;
  int GetStyle() const override;
  const std::string& GetFontName() const override;
  std::string GetActualFontName() const override;
  int GetFontSize() const override;
  const FontRenderParams& GetFontRenderParams() override;
  CTFontRef GetCTFont() const override;
  sk_sp<SkTypeface> GetNativeSkTypeface() const override;

  std::optional<SystemFontType> GetSystemFontType() const {
    return system_font_type_;
  }

  // A utility function to get the weight of a CTFontRef. Used by the unit test.
  static Font::Weight GetFontWeightFromCTFontForTesting(CTFontRef font);

 private:
  struct FontSpec {
    std::string name;  // Corresponds to -[NSFont fontFamily].
    int size;
    int style;
    Font::Weight weight;
  };

  PlatformFontMac(CTFontRef font,
                  std::optional<SystemFontType> system_font_type);

  PlatformFontMac(CTFontRef font,
                  std::optional<SystemFontType> system_font_type,
                  FontSpec spec);

  ~PlatformFontMac() override;

  // Calculates and caches the font metrics and initializes |render_params_|.
  void CalculateMetricsAndInitRenderParams();

  // Returns a CTFontRef created with the passed-in specifications.
  static base::apple::ScopedCFTypeRef<CTFontRef> CTFontWithSpec(
      FontSpec font_spec);

  // The CTFontRef instance for this object. If this object was constructed from
  // a CTFontRef instance, this holds that instance. Otherwise this instance is
  // constructed from the name, size, and style. If there is no active font that
  // matched those criteria a default font is used.
  base::apple::ScopedCFTypeRef<CTFontRef> ct_font_;

  // If the font is a system font, and if so, what kind.
  const std::optional<SystemFontType> system_font_type_;

  // The name/size/style/weight quartet that specify the font. Initialized in
  // the constructors.
  const FontSpec font_spec_;

  // Cached metrics, generated in CalculateMetrics().
  int height_;
  int ascent_;
  int cap_height_;

  // Cached average width, generated in GetExpectedTextWidth().
  float average_width_ = 0.0;

  // Details about how the font should be rendered.
  FontRenderParams render_params_;
};

}  // namespace gfx

#endif  // UI_GFX_PLATFORM_FONT_MAC_H_