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