| 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 | |
| 45 TEST(PlatformFontWinTest, DeriveFontWithHeight) { | 20 TEST(PlatformFontWinTest, DeriveFontWithHeight) { |
| 46 // TODO(ananta): Fix this test for DirectWrite. http://crbug.com/442010 | |
| 47 if (gfx::win::IsDirectWriteEnabled()) | |
| 48 return; | |
| 49 | |
| 50 const Font base_font; | 21 const Font base_font; |
| 51 PlatformFontWin* platform_font = | 22 PlatformFontWin* platform_font = |
| 52 static_cast<PlatformFontWin*>(base_font.platform_font()); | 23 static_cast<PlatformFontWin*>(base_font.platform_font()); |
| 53 | 24 |
| 54 for (int i = -10; i < 10; i++) { | 25 for (int i = -10; i < 10; i++) { |
| 55 const int target_height = base_font.GetHeight() + i; | 26 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); | |
| 58 | 27 |
| 59 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); | 28 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); |
| 60 EXPECT_EQ(expected_font.GetFontName(), derived_font.GetFontName()); | 29 EXPECT_LE(derived_font.GetHeight(), target_height); |
| 61 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); | 30 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height); |
| 62 EXPECT_LE(expected_font.GetHeight(), target_height); | 31 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), |
| 32 derived_font.GetActualFontNameForTesting()); |
| 63 EXPECT_EQ(0, derived_font.GetStyle()); | 33 EXPECT_EQ(0, derived_font.GetStyle()); |
| 64 | 34 |
| 65 derived_font = platform_font->DeriveFontWithHeight(target_height, | 35 derived_font = platform_font->DeriveFontWithHeight(target_height, |
| 66 Font::BOLD); | 36 Font::BOLD); |
| 67 EXPECT_EQ(expected_font.GetFontName(), derived_font.GetFontName()); | 37 EXPECT_LE(derived_font.GetHeight(), target_height); |
| 68 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); | 38 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height); |
| 69 EXPECT_LE(expected_font.GetHeight(), target_height); | 39 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), |
| 40 derived_font.GetActualFontNameForTesting()); |
| 70 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); | 41 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()); | |
| 79 } | 42 } |
| 80 } | 43 } |
| 81 | 44 |
| 82 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) { | 45 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) { |
| 83 // TODO(ananta): Fix this test for DirectWrite. http://crbug.com/442010 | |
| 84 if (gfx::win::IsDirectWriteEnabled()) | |
| 85 return; | |
| 86 gfx::Font arial_12("Arial", 12); | 46 gfx::Font arial_12("Arial", 12); |
| 87 ASSERT_GT(16, arial_12.GetHeight()); | 47 ASSERT_GT(16, arial_12.GetHeight()); |
| 88 gfx::Font derived_1 = static_cast<PlatformFontWin*>( | 48 gfx::Font derived_1 = static_cast<PlatformFontWin*>( |
| 89 arial_12.platform_font())->DeriveFontWithHeight(16, 0); | 49 arial_12.platform_font())->DeriveFontWithHeight(16, 0); |
| 90 | 50 |
| 91 gfx::Font arial_15("Arial", 15); | 51 gfx::Font arial_15("Arial", 15); |
| 92 ASSERT_LT(16, arial_15.GetHeight()); | 52 ASSERT_LT(16, arial_15.GetHeight()); |
| 93 gfx::Font derived_2 = static_cast<PlatformFontWin*>( | 53 gfx::Font derived_2 = static_cast<PlatformFontWin*>( |
| 94 arial_15.platform_font())->DeriveFontWithHeight(16, 0); | 54 arial_15.platform_font())->DeriveFontWithHeight(16, 0); |
| 95 | 55 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 EXPECT_EQ(h_font_gdi->font_size(), h_font_skia->font_size()); | 150 EXPECT_EQ(h_font_gdi->font_size(), h_font_skia->font_size()); |
| 191 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style()); | 151 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style()); |
| 192 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name()); | 152 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name()); |
| 193 | 153 |
| 194 EXPECT_LE(abs(h_font_gdi->cap_height() - h_font_skia->cap_height()), 1); | 154 EXPECT_LE(abs(h_font_gdi->cap_height() - h_font_skia->cap_height()), 1); |
| 195 EXPECT_LE(abs(h_font_gdi->baseline() - h_font_skia->baseline()), 1); | 155 EXPECT_LE(abs(h_font_gdi->baseline() - h_font_skia->baseline()), 1); |
| 196 EXPECT_LE(abs(h_font_gdi->height() - h_font_skia->height()), 1); | 156 EXPECT_LE(abs(h_font_gdi->height() - h_font_skia->height()), 1); |
| 197 } | 157 } |
| 198 } | 158 } |
| 199 | 159 |
| 200 | |
| 201 } // namespace gfx | 160 } // namespace gfx |
| OLD | NEW |