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 "ui/gfx/platform_font_win.h" | 5 #include "ui/gfx/platform_font_win.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/win/scoped_hdc.h" | 11 #include "base/win/scoped_hdc.h" |
12 #include "base/win/scoped_select_object.h" | 12 #include "base/win/scoped_select_object.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
15 #include "ui/gfx/win/direct_write.h" | 15 #include "ui/gfx/win/direct_write.h" |
16 #include "ui/gfx/win/scoped_set_map_mode.h" | 16 #include "ui/gfx/win/scoped_set_map_mode.h" |
17 | 17 |
18 namespace gfx { | 18 namespace gfx { |
19 | 19 |
| 20 namespace { |
| 21 |
| 22 // Returns a font based on |base_font| with height at most |target_height| and |
| 23 // font size maximized. Returns |base_font| if height is already equal. |
| 24 gfx::Font AdjustFontSizeForHeight(const gfx::Font& base_font, |
| 25 int target_height) { |
| 26 Font expected_font = base_font; |
| 27 if (base_font.GetHeight() < target_height) { |
| 28 // Increase size while height is <= |target_height|. |
| 29 Font larger_font = base_font.Derive(1, 0); |
| 30 while (larger_font.GetHeight() <= target_height) { |
| 31 expected_font = larger_font; |
| 32 larger_font = larger_font.Derive(1, 0); |
| 33 } |
| 34 } else if (expected_font.GetHeight() > target_height) { |
| 35 // Decrease size until height is <= |target_height|. |
| 36 do { |
| 37 expected_font = expected_font.Derive(-1, 0); |
| 38 } while (expected_font.GetHeight() > target_height); |
| 39 } |
| 40 return expected_font; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
20 TEST(PlatformFontWinTest, DeriveFontWithHeight) { | 45 TEST(PlatformFontWinTest, DeriveFontWithHeight) { |
| 46 // TODO(ananta): Fix this test for DirectWrite. http://crbug.com/442010 |
| 47 if (gfx::win::IsDirectWriteEnabled()) |
| 48 return; |
| 49 |
21 const Font base_font; | 50 const Font base_font; |
22 PlatformFontWin* platform_font = | 51 PlatformFontWin* platform_font = |
23 static_cast<PlatformFontWin*>(base_font.platform_font()); | 52 static_cast<PlatformFontWin*>(base_font.platform_font()); |
24 | 53 |
25 for (int i = -10; i < 10; i++) { | 54 for (int i = -10; i < 10; i++) { |
26 const int target_height = base_font.GetHeight() + i; | 55 const int target_height = base_font.GetHeight() + i; |
| 56 Font expected_font = AdjustFontSizeForHeight(base_font, target_height); |
| 57 ASSERT_LE(expected_font.GetHeight(), target_height); |
27 | 58 |
28 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); | 59 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); |
29 EXPECT_LE(derived_font.GetHeight(), target_height); | 60 EXPECT_EQ(expected_font.GetFontName(), derived_font.GetFontName()); |
30 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height); | 61 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); |
31 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), | 62 EXPECT_LE(expected_font.GetHeight(), target_height); |
32 derived_font.GetActualFontNameForTesting()); | |
33 EXPECT_EQ(0, derived_font.GetStyle()); | 63 EXPECT_EQ(0, derived_font.GetStyle()); |
34 | 64 |
35 derived_font = platform_font->DeriveFontWithHeight(target_height, | 65 derived_font = platform_font->DeriveFontWithHeight(target_height, |
36 Font::BOLD); | 66 Font::BOLD); |
37 EXPECT_LE(derived_font.GetHeight(), target_height); | 67 EXPECT_EQ(expected_font.GetFontName(), derived_font.GetFontName()); |
38 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height); | 68 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); |
39 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), | 69 EXPECT_LE(expected_font.GetHeight(), target_height); |
40 derived_font.GetActualFontNameForTesting()); | |
41 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); | 70 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); |
| 71 |
| 72 // Test that deriving from the new font has the expected result. |
| 73 Font rederived_font = derived_font.Derive(1, 0); |
| 74 expected_font = Font(derived_font.GetFontName(), |
| 75 derived_font.GetFontSize() + 1); |
| 76 EXPECT_EQ(expected_font.GetFontName(), rederived_font.GetFontName()); |
| 77 EXPECT_EQ(expected_font.GetFontSize(), rederived_font.GetFontSize()); |
| 78 EXPECT_EQ(expected_font.GetHeight(), rederived_font.GetHeight()); |
42 } | 79 } |
43 } | 80 } |
44 | 81 |
45 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) { | 82 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) { |
| 83 // TODO(ananta): Fix this test for DirectWrite. http://crbug.com/442010 |
| 84 if (gfx::win::IsDirectWriteEnabled()) |
| 85 return; |
46 gfx::Font arial_12("Arial", 12); | 86 gfx::Font arial_12("Arial", 12); |
47 ASSERT_GT(16, arial_12.GetHeight()); | 87 ASSERT_GT(16, arial_12.GetHeight()); |
48 gfx::Font derived_1 = static_cast<PlatformFontWin*>( | 88 gfx::Font derived_1 = static_cast<PlatformFontWin*>( |
49 arial_12.platform_font())->DeriveFontWithHeight(16, 0); | 89 arial_12.platform_font())->DeriveFontWithHeight(16, 0); |
50 | 90 |
51 gfx::Font arial_15("Arial", 15); | 91 gfx::Font arial_15("Arial", 15); |
52 ASSERT_LT(16, arial_15.GetHeight()); | 92 ASSERT_LT(16, arial_15.GetHeight()); |
53 gfx::Font derived_2 = static_cast<PlatformFontWin*>( | 93 gfx::Font derived_2 = static_cast<PlatformFontWin*>( |
54 arial_15.platform_font())->DeriveFontWithHeight(16, 0); | 94 arial_15.platform_font())->DeriveFontWithHeight(16, 0); |
55 | 95 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 EXPECT_EQ(h_font_gdi->font_size(), h_font_skia->font_size()); | 190 EXPECT_EQ(h_font_gdi->font_size(), h_font_skia->font_size()); |
151 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style()); | 191 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style()); |
152 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name()); | 192 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name()); |
153 | 193 |
154 EXPECT_LE(abs(h_font_gdi->cap_height() - h_font_skia->cap_height()), 1); | 194 EXPECT_LE(abs(h_font_gdi->cap_height() - h_font_skia->cap_height()), 1); |
155 EXPECT_LE(abs(h_font_gdi->baseline() - h_font_skia->baseline()), 1); | 195 EXPECT_LE(abs(h_font_gdi->baseline() - h_font_skia->baseline()), 1); |
156 EXPECT_LE(abs(h_font_gdi->height() - h_font_skia->height()), 1); | 196 EXPECT_LE(abs(h_font_gdi->height() - h_font_skia->height()), 1); |
157 } | 197 } |
158 } | 198 } |
159 | 199 |
| 200 |
160 } // namespace gfx | 201 } // namespace gfx |
OLD | NEW |