Chromium Code Reviews| 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 | |
| 59 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); | 27 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); |
| 60 EXPECT_EQ(expected_font.GetFontName(), derived_font.GetFontName()); | 28 // Ideally a derived font should have its height less or equal to the |
|
msw
2015/01/10 01:50:15
I'm confused about how either of these behaviors a
ananta
2015/01/10 02:05:49
Yes. They both retrieve the DW metrics. However wi
msw
2015/01/10 02:28:32
Okay, this must only occur when PlatformFontWin::D
ananta
2015/01/12 20:10:31
I changed the logic in the DeriveFontWithHeight fu
| |
| 61 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); | 29 // target height. However for font heights like 17 with DirectWrite we |
| 62 EXPECT_LE(expected_font.GetHeight(), target_height); | 30 // end up getting a font with height 18. We just check that the derived |
| 31 // font height is at most 1 px off. | |
| 32 EXPECT_LE(abs(derived_font.GetHeight() - target_height), 1); | |
| 33 // A derived Font one size larger should ideally exceed the target height. | |
| 34 // However with DirectWrite with bigger heights like 24+, the derived font | |
| 35 // ends up being the same height as the target height. | |
| 36 EXPECT_GE(derived_font.Derive(1, 0).GetHeight(), target_height); | |
| 37 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), | |
| 38 derived_font.GetActualFontNameForTesting()); | |
| 63 EXPECT_EQ(0, derived_font.GetStyle()); | 39 EXPECT_EQ(0, derived_font.GetStyle()); |
| 64 | |
| 65 derived_font = platform_font->DeriveFontWithHeight(target_height, | 40 derived_font = platform_font->DeriveFontWithHeight(target_height, |
| 66 Font::BOLD); | 41 Font::BOLD); |
| 67 EXPECT_EQ(expected_font.GetFontName(), derived_font.GetFontName()); | 42 // Please refer above as to why we validate that the difference between |
| 68 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); | 43 // the derived font height and desired font height is at most 1. |
| 69 EXPECT_LE(expected_font.GetHeight(), target_height); | 44 EXPECT_LE(abs(derived_font.GetHeight() - target_height), 1); |
| 45 // Please refer above as to why we use EXPECT_GE to check the derived font | |
| 46 // height. | |
| 47 EXPECT_GE(derived_font.Derive(1, 0).GetHeight(), target_height); | |
| 48 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), | |
| 49 derived_font.GetActualFontNameForTesting()); | |
| 70 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); | 50 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 } | 51 } |
| 80 } | 52 } |
| 81 | 53 |
| 82 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) { | 54 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); | 55 gfx::Font arial_12("Arial", 12); |
| 87 ASSERT_GT(16, arial_12.GetHeight()); | 56 ASSERT_GT(16, arial_12.GetHeight()); |
| 88 gfx::Font derived_1 = static_cast<PlatformFontWin*>( | 57 gfx::Font derived_1 = static_cast<PlatformFontWin*>( |
| 89 arial_12.platform_font())->DeriveFontWithHeight(16, 0); | 58 arial_12.platform_font())->DeriveFontWithHeight(16, 0); |
| 90 | 59 |
| 91 gfx::Font arial_15("Arial", 15); | 60 gfx::Font arial_15("Arial", 15); |
| 92 ASSERT_LT(16, arial_15.GetHeight()); | 61 ASSERT_LT(16, arial_15.GetHeight()); |
| 93 gfx::Font derived_2 = static_cast<PlatformFontWin*>( | 62 gfx::Font derived_2 = static_cast<PlatformFontWin*>( |
| 94 arial_15.platform_font())->DeriveFontWithHeight(16, 0); | 63 arial_15.platform_font())->DeriveFontWithHeight(16, 0); |
| 95 | 64 |
| (...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()); | 159 EXPECT_EQ(h_font_gdi->font_size(), h_font_skia->font_size()); |
| 191 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style()); | 160 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style()); |
| 192 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name()); | 161 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name()); |
| 193 | 162 |
| 194 EXPECT_LE(abs(h_font_gdi->cap_height() - h_font_skia->cap_height()), 1); | 163 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); | 164 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); | 165 EXPECT_LE(abs(h_font_gdi->height() - h_font_skia->height()), 1); |
| 197 } | 166 } |
| 198 } | 167 } |
| 199 | 168 |
| 200 | |
| 201 } // namespace gfx | 169 } // namespace gfx |
| OLD | NEW |