| 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 TEST(PlatformFontWinTest, DeriveFontWithHeight) { | |
| 21 const Font base_font; | |
| 22 PlatformFontWin* platform_font = | |
| 23 static_cast<PlatformFontWin*>(base_font.platform_font()); | |
| 24 | |
| 25 for (int i = -10; i < 10; i++) { | |
| 26 const int target_height = base_font.GetHeight() + i; | |
| 27 | |
| 28 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); | |
| 29 EXPECT_LE(derived_font.GetHeight(), target_height); | |
| 30 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height); | |
| 31 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), | |
| 32 derived_font.GetActualFontNameForTesting()); | |
| 33 EXPECT_EQ(0, derived_font.GetStyle()); | |
| 34 | |
| 35 derived_font = platform_font->DeriveFontWithHeight(target_height, | |
| 36 Font::BOLD); | |
| 37 EXPECT_LE(derived_font.GetHeight(), target_height); | |
| 38 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height); | |
| 39 EXPECT_EQ(platform_font->GetActualFontNameForTesting(), | |
| 40 derived_font.GetActualFontNameForTesting()); | |
| 41 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) { | |
| 46 gfx::Font arial_12("Arial", 12); | |
| 47 ASSERT_GT(16, arial_12.GetHeight()); | |
| 48 gfx::Font derived_1 = static_cast<PlatformFontWin*>( | |
| 49 arial_12.platform_font())->DeriveFontWithHeight(16, 0); | |
| 50 | |
| 51 gfx::Font arial_15("Arial", 15); | |
| 52 ASSERT_LT(16, arial_15.GetHeight()); | |
| 53 gfx::Font derived_2 = static_cast<PlatformFontWin*>( | |
| 54 arial_15.platform_font())->DeriveFontWithHeight(16, 0); | |
| 55 | |
| 56 EXPECT_EQ(derived_1.GetFontSize(), derived_2.GetFontSize()); | |
| 57 EXPECT_EQ(16, derived_1.GetHeight()); | |
| 58 EXPECT_EQ(16, derived_2.GetHeight()); | |
| 59 } | |
| 60 | |
| 61 // Callback function used by DeriveFontWithHeight_MinSize() below. | |
| 62 static int GetMinFontSize() { | |
| 63 return 10; | |
| 64 } | |
| 65 | |
| 66 TEST(PlatformFontWinTest, DeriveFontWithHeight_MinSize) { | |
| 67 PlatformFontWin::GetMinimumFontSizeCallback old_callback = | |
| 68 PlatformFontWin::get_minimum_font_size_callback; | |
| 69 PlatformFontWin::get_minimum_font_size_callback = &GetMinFontSize; | |
| 70 | |
| 71 const Font base_font; | |
| 72 const Font min_font(base_font.GetFontName(), GetMinFontSize()); | |
| 73 PlatformFontWin* platform_font = | |
| 74 static_cast<PlatformFontWin*>(base_font.platform_font()); | |
| 75 | |
| 76 const Font derived_font = | |
| 77 platform_font->DeriveFontWithHeight(min_font.GetHeight() - 1, 0); | |
| 78 EXPECT_EQ(min_font.GetFontSize(), derived_font.GetFontSize()); | |
| 79 EXPECT_EQ(min_font.GetHeight(), derived_font.GetHeight()); | |
| 80 | |
| 81 PlatformFontWin::get_minimum_font_size_callback = old_callback; | |
| 82 } | |
| 83 | |
| 84 TEST(PlatformFontWinTest, DeriveFontWithHeight_TooSmall) { | |
| 85 const Font base_font; | |
| 86 PlatformFontWin* platform_font = | |
| 87 static_cast<PlatformFontWin*>(base_font.platform_font()); | |
| 88 | |
| 89 const Font derived_font = platform_font->DeriveFontWithHeight(1, 0); | |
| 90 EXPECT_GT(derived_font.GetHeight(), 1); | |
| 91 } | |
| 92 | |
| 93 // Test whether font metrics retrieved by DirectWrite (skia) and GDI match as | 20 // Test whether font metrics retrieved by DirectWrite (skia) and GDI match as |
| 94 // per assumptions mentioned below:- | 21 // per assumptions mentioned below:- |
| 95 // 1. Font size is the same | 22 // 1. Font size is the same |
| 96 // 2. The difference between GDI and DirectWrite for font height, baseline, | 23 // 2. The difference between GDI and DirectWrite for font height, baseline, |
| 97 // and cap height is at most 1. For smaller font sizes under 12, GDI | 24 // and cap height is at most 1. For smaller font sizes under 12, GDI |
| 98 // font heights/baselines/cap height are equal/larger by 1 point. For larger | 25 // font heights/baselines/cap height are equal/larger by 1 point. For larger |
| 99 // font sizes DirectWrite font heights/baselines/cap height are equal/larger | 26 // font sizes DirectWrite font heights/baselines/cap height are equal/larger |
| 100 // by 1 point. | 27 // by 1 point. |
| 101 TEST(PlatformFontWinTest, Metrics_SkiaVersusGDI) { | 28 TEST(PlatformFontWinTest, Metrics_SkiaVersusGDI) { |
| 102 if (!gfx::win::IsDirectWriteEnabled()) | 29 if (!gfx::win::IsDirectWriteEnabled()) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 PlatformFontWin::GetTextMetricsForFont(screen_dc, hFont, &font_metrics); | 125 PlatformFontWin::GetTextMetricsForFont(screen_dc, hFont, &font_metrics); |
| 199 | 126 |
| 200 scoped_refptr<PlatformFontWin::HFontRef> h_font_skia( | 127 scoped_refptr<PlatformFontWin::HFontRef> h_font_skia( |
| 201 PlatformFontWin::CreateHFontRefFromSkia(hFont, font_metrics)); | 128 PlatformFontWin::CreateHFontRefFromSkia(hFont, font_metrics)); |
| 202 | 129 |
| 203 EXPECT_EQ(font.expected_font_name, h_font_skia->font_name()); | 130 EXPECT_EQ(font.expected_font_name, h_font_skia->font_name()); |
| 204 } | 131 } |
| 205 } | 132 } |
| 206 | 133 |
| 207 } // namespace gfx | 134 } // namespace gfx |
| OLD | NEW |