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.
5 #include <Cocoa/Cocoa.h>
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/font.h"
10 TEST(PlatformFontMacTest, DeriveFont) {
11 // Use a base font that support all traits.
12 gfx::Font base_font("Helvetica", 13);
15 gfx::Font bold_font(base_font.Derive(0, gfx::Font::BOLD));
16 NSFontTraitMask traits = [[NSFontManager sharedFontManager]
17 traitsOfFont:bold_font.GetNativeFont()];
18 EXPECT_EQ(NSBoldFontMask, traits);
21 gfx::Font italic_font(base_font.Derive(0, gfx::Font::ITALIC));
22 traits = [[NSFontManager sharedFontManager]
23 traitsOfFont:italic_font.GetNativeFont()];
24 EXPECT_EQ(NSItalicFontMask, traits);
27 gfx::Font bold_italic_font(base_font.Derive(
28 0, gfx::Font::BOLD | gfx::Font::ITALIC));
29 traits = [[NSFontManager sharedFontManager]
30 traitsOfFont:bold_italic_font.GetNativeFont()];
31 EXPECT_EQ(static_cast<NSFontTraitMask>(NSBoldFontMask | NSItalicFontMask),
35 TEST(PlatformFontMacTest, ConstructFromNativeFont) {
36 gfx::Font normal_font([NSFont fontWithName:@"Helvetica" size:12]);
37 EXPECT_EQ(12, normal_font.GetFontSize());
38 EXPECT_EQ("Helvetica", normal_font.GetFontName());
39 EXPECT_EQ(gfx::Font::NORMAL, normal_font.GetStyle());
41 gfx::Font bold_font([NSFont fontWithName:@"Helvetica-Bold" size:14]);
42 EXPECT_EQ(14, bold_font.GetFontSize());
43 EXPECT_EQ("Helvetica", bold_font.GetFontName());
44 EXPECT_EQ(gfx::Font::BOLD, bold_font.GetStyle());
46 gfx::Font italic_font([NSFont fontWithName:@"Helvetica-Oblique" size:14]);
47 EXPECT_EQ(14, italic_font.GetFontSize());
48 EXPECT_EQ("Helvetica", italic_font.GetFontName());
49 EXPECT_EQ(gfx::Font::ITALIC, italic_font.GetStyle());
51 gfx::Font bold_italic_font(
52 [NSFont fontWithName:@"Helvetica-BoldOblique" size:14]);
53 EXPECT_EQ(14, bold_italic_font.GetFontSize());
54 EXPECT_EQ("Helvetica", bold_italic_font.GetFontName());
55 EXPECT_EQ(gfx::Font::BOLD | gfx::Font::ITALIC, bold_italic_font.GetStyle());
58 // Ensures that the Font's reported height is consistent with the native font's
59 // ascender and descender metrics.
60 TEST(PlatformFontMacTest, ValidateFontHeight) {
61 // Use the default ResourceBundle system font. E.g. Helvetica Neue in 10.10,
62 // Lucida Grande before that, and San Francisco after.
63 gfx::Font default_font;
64 gfx::Font::FontStyle styles[] = {
65 gfx::Font::NORMAL, gfx::Font::BOLD, gfx::Font::ITALIC, gfx::Font::UNDERLINE
68 for (size_t i = 0; i < arraysize(styles); ++i) {
69 SCOPED_TRACE(testing::Message() << "Font::FontStyle: " << styles[i]);
70 // Include the range of sizes used by ResourceBundle::FontStyle (-1 to +8).
71 for (int delta = -1; delta <= 8; ++delta) {
72 gfx::Font font = default_font.Derive(delta, styles[i]);
73 SCOPED_TRACE(testing::Message() << "FontSize(): " << font.GetFontSize());
74 NSFont* native_font = font.GetNativeFont();
76 // Font height (an integer) should be the sum of these.
77 CGFloat ascender = [native_font ascender];
78 CGFloat descender = [native_font descender];
79 CGFloat leading = [native_font leading];
81 // NSFont always gives a negative value for descender. Others positive.
82 EXPECT_GE(0, descender);
83 EXPECT_LE(0, ascender);
84 EXPECT_LE(0, leading);
86 int sum = ceil(ascender - descender + leading);
88 // Text layout is performed using an integral baseline offset derived from
89 // the ascender. The height needs to be enough to fit the full descender
90 // (plus baseline). So the height depends on the rounding of the ascender,
91 // and can be as much as 1 greater than the simple sum of floats.
92 EXPECT_LE(sum, font.GetHeight());
93 EXPECT_GE(sum + 1, font.GetHeight());
95 // Recreate the rounding performed for GetBaseLine().
96 EXPECT_EQ(ceil(ceil(ascender) - descender + leading), font.GetHeight());