| 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 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/gfx/font.h" | 8 #include "ui/gfx/font.h" |
| 9 | 9 |
| 10 TEST(PlatformFontMacTest, DeriveFont) { | 10 TEST(PlatformFontMacTest, DeriveFont) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 EXPECT_EQ(14, italic_font.GetFontSize()); | 47 EXPECT_EQ(14, italic_font.GetFontSize()); |
| 48 EXPECT_EQ("Helvetica", italic_font.GetFontName()); | 48 EXPECT_EQ("Helvetica", italic_font.GetFontName()); |
| 49 EXPECT_EQ(gfx::Font::ITALIC, italic_font.GetStyle()); | 49 EXPECT_EQ(gfx::Font::ITALIC, italic_font.GetStyle()); |
| 50 | 50 |
| 51 gfx::Font bold_italic_font( | 51 gfx::Font bold_italic_font( |
| 52 [NSFont fontWithName:@"Helvetica-BoldOblique" size:14]); | 52 [NSFont fontWithName:@"Helvetica-BoldOblique" size:14]); |
| 53 EXPECT_EQ(14, bold_italic_font.GetFontSize()); | 53 EXPECT_EQ(14, bold_italic_font.GetFontSize()); |
| 54 EXPECT_EQ("Helvetica", bold_italic_font.GetFontName()); | 54 EXPECT_EQ("Helvetica", bold_italic_font.GetFontName()); |
| 55 EXPECT_EQ(gfx::Font::BOLD | gfx::Font::ITALIC, bold_italic_font.GetStyle()); | 55 EXPECT_EQ(gfx::Font::BOLD | gfx::Font::ITALIC, bold_italic_font.GetStyle()); |
| 56 } | 56 } |
| 57 |
| 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 |
| 66 }; |
| 67 |
| 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(); |
| 75 |
| 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]; |
| 80 |
| 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); |
| 85 |
| 86 int sum = ceil(ascender - descender + leading); |
| 87 |
| 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()); |
| 94 |
| 95 // Recreate the rounding performed for GetBaseLine(). |
| 96 EXPECT_EQ(ceil(ceil(ascender) - descender + leading), font.GetHeight()); |
| 97 } |
| 98 } |
| 99 } |
| OLD | NEW |