File: platform_font_mac_unittest.mm

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 (269 lines) | stat: -rw-r--r-- 10,709 bytes parent folder | download | duplicates (9)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// 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.

#include "ui/gfx/platform_font_mac.h"

#include <Cocoa/Cocoa.h>
#include <CoreText/CoreText.h>
#include <stddef.h>

#include "base/apple/bridging.h"
#import "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font.h"

namespace gfx {

using Weight = Font::Weight;

TEST(PlatformFontMacTest, DeriveFont) {
  // macOS 13.0 bug: For non-system fonts with 0-valued traits,
  // `kCFBooleanFalse` is used instead of a `CFNumberRef` of 0. See
  // https://crbug.com/1372420. Filed as FB11673021, fixed in macOS 13.1.
  auto GetValueFromDictionaryAndWorkAroundMacOS13Bug = [](CFDictionaryRef dict,
                                                          CFStringRef key) {
    NSOperatingSystemVersion version =
        NSProcessInfo.processInfo.operatingSystemVersion;

    if (version.majorVersion == 13 && version.minorVersion == 0) {
      CFTypeRef value = CFDictionaryGetValue(dict, key);
      if (value == kCFBooleanFalse) {
        CGFloat zero = 0;
        return (CFNumberRef)CFAutorelease(
            CFNumberCreate(nullptr, kCFNumberCGFloatType, &zero));
      }
    }

    return base::apple::GetValueFromDictionary<CFNumberRef>(dict, key);
  };

  // |weight_tri| is either -1, 0, or 1 meaning "light", "normal", or "bold".
  auto CheckExpected = [GetValueFromDictionaryAndWorkAroundMacOS13Bug](
                           const Font& font, int weight_tri, bool isItalic) {
    base::apple::ScopedCFTypeRef<CFDictionaryRef> traits(
        CTFontCopyTraits(font.GetCTFont()));
    DCHECK(traits);

    CFNumberRef cf_slant = GetValueFromDictionaryAndWorkAroundMacOS13Bug(
        traits.get(), kCTFontSlantTrait);
    CGFloat slant;
    CFNumberGetValue(cf_slant, kCFNumberCGFloatType, &slant);
    if (isItalic)
      EXPECT_GT(slant, 0);
    else
      EXPECT_EQ(slant, 0);

    CFNumberRef cf_weight = GetValueFromDictionaryAndWorkAroundMacOS13Bug(
        traits.get(), kCTFontWeightTrait);
    CGFloat weight;
    CFNumberGetValue(cf_weight, kCFNumberCGFloatType, &weight);
    if (weight_tri < 0)
      EXPECT_LT(weight, 0);
    else if (weight_tri == 0)
      EXPECT_EQ(weight, 0);
    else
      EXPECT_GT(weight, 0);
  };

  // Use a base font that support all traits.
  Font base_font("Helvetica", 13);
  {
    SCOPED_TRACE("plain font");
    CheckExpected(base_font, 0, false);
  }

  // Italic
  Font italic_font(base_font.Derive(0, Font::ITALIC, Weight::NORMAL));
  {
    SCOPED_TRACE("italic font");
    CheckExpected(italic_font, 0, true);
  }

  // Bold
  Font bold_font(base_font.Derive(0, Font::NORMAL, Weight::BOLD));
  {
    SCOPED_TRACE("bold font");
    CheckExpected(bold_font, 1, false);
  }

  // Bold italic
  Font bold_italic_font(base_font.Derive(0, Font::ITALIC, Weight::BOLD));
  {
    SCOPED_TRACE("bold italic font");
    CheckExpected(bold_italic_font, 1, true);
  }

  // Non-existent thin will return the closest weight, light
  Font thin_font(base_font.Derive(0, Font::NORMAL, Weight::THIN));
  {
    SCOPED_TRACE("thin font");
    CheckExpected(thin_font, -1, false);
  }

  // Non-existent black will return the closest weight, bold
  Font black_font(base_font.Derive(0, Font::NORMAL, Weight::BLACK));
  {
    SCOPED_TRACE("black font");
    CheckExpected(black_font, 1, false);
  }
}

TEST(PlatformFontMacTest, DeriveFontUnderline) {
  // Create a default font.
  Font base_font;

  // Make the font underlined.
  Font derived_font(base_font.Derive(0, base_font.GetStyle() | Font::UNDERLINE,
                                     base_font.GetWeight()));

  // Validate the derived font properties against its native font instance.
  NSFontTraitMask traits = [NSFontManager.sharedFontManager
      traitsOfFont:base::apple::CFToNSPtrCast(derived_font.GetCTFont())];
  Weight actual_weight =
      (traits & NSFontBoldTrait) ? Weight::BOLD : Weight::NORMAL;

  int actual_style = Font::UNDERLINE;
  if (traits & NSFontItalicTrait)
    actual_style |= Font::ITALIC;

  EXPECT_TRUE(derived_font.GetStyle() & Font::UNDERLINE);
  EXPECT_EQ(derived_font.GetStyle(), actual_style);
  EXPECT_EQ(derived_font.GetWeight(), actual_weight);
}

// Tests internal methods for extracting Font properties from the
// underlying CTFont representation.
TEST(PlatformFontMacTest, ConstructFromNativeFont) {
  NSFont* ns_light_font = [NSFont fontWithName:@"Helvetica-Light" size:12];
  Font light_font(base::apple::NSToCFPtrCast(ns_light_font));
  EXPECT_EQ(12, light_font.GetFontSize());
  EXPECT_EQ("Helvetica", light_font.GetFontName());
  EXPECT_EQ(Font::NORMAL, light_font.GetStyle());
  EXPECT_EQ(Weight::LIGHT, light_font.GetWeight());

  NSFont* ns_light_italic_font = [NSFont fontWithName:@"Helvetica-LightOblique"
                                                 size:14];
  Font light_italic_font(base::apple::NSToCFPtrCast(ns_light_italic_font));
  EXPECT_EQ(14, light_italic_font.GetFontSize());
  EXPECT_EQ("Helvetica", light_italic_font.GetFontName());
  EXPECT_EQ(Font::ITALIC, light_italic_font.GetStyle());
  EXPECT_EQ(Weight::LIGHT, light_italic_font.GetWeight());

  NSFont* ns_normal_font = [NSFont fontWithName:@"Helvetica" size:12];
  Font normal_font(base::apple::NSToCFPtrCast(ns_normal_font));
  EXPECT_EQ(12, normal_font.GetFontSize());
  EXPECT_EQ("Helvetica", normal_font.GetFontName());
  EXPECT_EQ(Font::NORMAL, normal_font.GetStyle());
  EXPECT_EQ(Weight::NORMAL, normal_font.GetWeight());

  NSFont* ns_italic_font = [NSFont fontWithName:@"Helvetica-Oblique" size:14];
  Font italic_font(base::apple::NSToCFPtrCast(ns_italic_font));
  EXPECT_EQ(14, italic_font.GetFontSize());
  EXPECT_EQ("Helvetica", italic_font.GetFontName());
  EXPECT_EQ(Font::ITALIC, italic_font.GetStyle());
  EXPECT_EQ(Weight::NORMAL, italic_font.GetWeight());

  NSFont* ns_bold_font = [NSFont fontWithName:@"Helvetica-Bold" size:12];
  Font bold_font(base::apple::NSToCFPtrCast(ns_bold_font));
  EXPECT_EQ(12, bold_font.GetFontSize());
  EXPECT_EQ("Helvetica", bold_font.GetFontName());
  EXPECT_EQ(Font::NORMAL, bold_font.GetStyle());
  EXPECT_EQ(Weight::BOLD, bold_font.GetWeight());

  NSFont* ns_bold_italic_font = [NSFont fontWithName:@"Helvetica-BoldOblique"
                                                size:14];
  Font bold_italic_font(base::apple::NSToCFPtrCast(ns_bold_italic_font));
  EXPECT_EQ(14, bold_italic_font.GetFontSize());
  EXPECT_EQ("Helvetica", bold_italic_font.GetFontName());
  EXPECT_EQ(Font::ITALIC, bold_italic_font.GetStyle());
  EXPECT_EQ(Weight::BOLD, bold_italic_font.GetWeight());
}

// Test font derivation for fine-grained font weights.
TEST(PlatformFontMacTest, DerivedFineGrainedFonts) {
  // The resulting, actual font weight after deriving |weight| from |base|.
  auto DerivedIntWeight = [](Weight weight) {
    Font base;  // The default system font.
    Font derived(base.Derive(0, 0, weight));
    // PlatformFont should always pass the requested weight, not what the OS
    // could provide. This just checks a constructor argument, so not very
    // interesting.
    EXPECT_EQ(static_cast<int>(weight), static_cast<int>(derived.GetWeight()));

    return static_cast<int>(PlatformFontMac::GetFontWeightFromCTFontForTesting(
        derived.GetCTFont()));
  };

  EXPECT_EQ(static_cast<int>(Weight::THIN), DerivedIntWeight(Weight::THIN));
  EXPECT_EQ(static_cast<int>(Weight::EXTRA_LIGHT),
            DerivedIntWeight(Weight::EXTRA_LIGHT));
  EXPECT_EQ(static_cast<int>(Weight::LIGHT), DerivedIntWeight(Weight::LIGHT));
  EXPECT_EQ(static_cast<int>(Weight::NORMAL), DerivedIntWeight(Weight::NORMAL));
  EXPECT_EQ(static_cast<int>(Weight::MEDIUM), DerivedIntWeight(Weight::MEDIUM));
  EXPECT_EQ(static_cast<int>(Weight::SEMIBOLD),
            DerivedIntWeight(Weight::SEMIBOLD));
  EXPECT_EQ(static_cast<int>(Weight::BOLD), DerivedIntWeight(Weight::BOLD));
  EXPECT_EQ(static_cast<int>(Weight::EXTRA_BOLD),
            DerivedIntWeight(Weight::EXTRA_BOLD));
  EXPECT_EQ(static_cast<int>(Weight::BLACK), DerivedIntWeight(Weight::BLACK));
}

// Ensures that the Font's reported height is consistent with the native font's
// ascender and descender metrics.
TEST(PlatformFontMacTest, ValidateFontHeight) {
  // Use the default ResourceBundle system font (i.e. San Francisco).
  Font default_font;
  Font::FontStyle styles[] = {Font::NORMAL, Font::ITALIC, Font::UNDERLINE};

  for (auto& style : styles) {
    SCOPED_TRACE(testing::Message() << "Font::FontStyle: " << style);
    // Include the range of sizes used by ResourceBundle::FontStyle (-1 to +8).
    for (int delta = -1; delta <= 8; ++delta) {
      Font font = default_font.Derive(delta, style, Weight::NORMAL);
      SCOPED_TRACE(testing::Message() << "FontSize(): " << font.GetFontSize());
      NSFont* ns_font = base::apple::CFToNSPtrCast(font.GetCTFont());

      // Font height (an integer) should be the sum of these.
      CGFloat ascender = ns_font.ascender;
      CGFloat descender = ns_font.descender;
      CGFloat leading = ns_font.leading;

      // NSFont always gives a negative value for descender. Others positive.
      EXPECT_GE(0, descender);
      EXPECT_LE(0, ascender);
      EXPECT_LE(0, leading);

      int sum = ceil(ascender - descender + leading);

      // Text layout is performed using an integral baseline offset derived from
      // the ascender. The height needs to be enough to fit the full descender
      // (plus baseline). So the height depends on the rounding of the ascender,
      // and can be as much as 1 greater than the simple sum of floats.
      EXPECT_LE(sum, font.GetHeight());
      EXPECT_GE(sum + 1, font.GetHeight());

      // Recreate the rounding performed for GetBaseLine().
      EXPECT_EQ(ceil(ceil(ascender) - descender + leading), font.GetHeight());
    }
  }
}

// Test to ensure we cater for the AppKit quirk that can make the font italic
// when asking for a fine-grained weight. See http://crbug.com/742261. Note that
// AppKit's bug was detected on macOS 10.10 which uses Helvetica Neue as the
// system font.
TEST(PlatformFontMacTest, DerivedSemiboldFontIsNotItalic) {
  Font base_font;
  NSFontTraitMask base_traits = [NSFontManager.sharedFontManager
      traitsOfFont:base::apple::CFToNSPtrCast(base_font.GetCTFont())];
  ASSERT_FALSE(base_traits & NSItalicFontMask);

  Font semibold_font = base_font.Derive(0, Font::NORMAL, Weight::SEMIBOLD);
  NSFontTraitMask semibold_traits = [NSFontManager.sharedFontManager
      traitsOfFont:base::apple::CFToNSPtrCast(semibold_font.GetCTFont())];
  EXPECT_FALSE(semibold_traits & NSItalicFontMask);
}

}  // namespace gfx