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
3 // found in the LICENSE file.
5 #include "ui/gfx/platform_font_win.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/win/scoped_hdc.h"
12 #include "base/win/scoped_select_object.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/font.h"
15 #include "ui/gfx/win/direct_write.h"
16 #include "ui/gfx/win/scoped_set_map_mode.h"
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
,
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);
34 } else if (expected_font
.GetHeight() > target_height
) {
35 // Decrease size until height is <= |target_height|.
37 expected_font
= expected_font
.Derive(-1, 0);
38 } while (expected_font
.GetHeight() > target_height
);
45 TEST(PlatformFontWinTest
, DeriveFontWithHeight
) {
46 // TODO(ananta): Fix this test for DirectWrite. http://crbug.com/442010
47 if (gfx::win::IsDirectWriteEnabled())
51 PlatformFontWin
* platform_font
=
52 static_cast<PlatformFontWin
*>(base_font
.platform_font());
54 for (int i
= -10; i
< 10; 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
);
59 Font derived_font
= platform_font
->DeriveFontWithHeight(target_height
, 0);
60 EXPECT_EQ(expected_font
.GetFontName(), derived_font
.GetFontName());
61 EXPECT_EQ(expected_font
.GetFontSize(), derived_font
.GetFontSize());
62 EXPECT_LE(expected_font
.GetHeight(), target_height
);
63 EXPECT_EQ(0, derived_font
.GetStyle());
65 derived_font
= platform_font
->DeriveFontWithHeight(target_height
,
67 EXPECT_EQ(expected_font
.GetFontName(), derived_font
.GetFontName());
68 EXPECT_EQ(expected_font
.GetFontSize(), derived_font
.GetFontSize());
69 EXPECT_LE(expected_font
.GetHeight(), target_height
);
70 EXPECT_EQ(Font::BOLD
, derived_font
.GetStyle());
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());
82 TEST(PlatformFontWinTest
, DeriveFontWithHeight_Consistency
) {
83 // TODO(ananta): Fix this test for DirectWrite. http://crbug.com/442010
84 if (gfx::win::IsDirectWriteEnabled())
86 gfx::Font
arial_12("Arial", 12);
87 ASSERT_GT(16, arial_12
.GetHeight());
88 gfx::Font derived_1
= static_cast<PlatformFontWin
*>(
89 arial_12
.platform_font())->DeriveFontWithHeight(16, 0);
91 gfx::Font
arial_15("Arial", 15);
92 ASSERT_LT(16, arial_15
.GetHeight());
93 gfx::Font derived_2
= static_cast<PlatformFontWin
*>(
94 arial_15
.platform_font())->DeriveFontWithHeight(16, 0);
96 EXPECT_EQ(derived_1
.GetFontSize(), derived_2
.GetFontSize());
97 EXPECT_EQ(16, derived_1
.GetHeight());
98 EXPECT_EQ(16, derived_2
.GetHeight());
101 // Callback function used by DeriveFontWithHeight_MinSize() below.
102 static int GetMinFontSize() {
106 TEST(PlatformFontWinTest
, DeriveFontWithHeight_MinSize
) {
107 PlatformFontWin::GetMinimumFontSizeCallback old_callback
=
108 PlatformFontWin::get_minimum_font_size_callback
;
109 PlatformFontWin::get_minimum_font_size_callback
= &GetMinFontSize
;
111 const Font base_font
;
112 const Font
min_font(base_font
.GetFontName(), GetMinFontSize());
113 PlatformFontWin
* platform_font
=
114 static_cast<PlatformFontWin
*>(base_font
.platform_font());
116 const Font derived_font
=
117 platform_font
->DeriveFontWithHeight(min_font
.GetHeight() - 1, 0);
118 EXPECT_EQ(min_font
.GetFontSize(), derived_font
.GetFontSize());
119 EXPECT_EQ(min_font
.GetHeight(), derived_font
.GetHeight());
121 PlatformFontWin::get_minimum_font_size_callback
= old_callback
;
124 TEST(PlatformFontWinTest
, DeriveFontWithHeight_TooSmall
) {
125 const Font base_font
;
126 PlatformFontWin
* platform_font
=
127 static_cast<PlatformFontWin
*>(base_font
.platform_font());
129 const Font derived_font
= platform_font
->DeriveFontWithHeight(1, 0);
130 EXPECT_GT(derived_font
.GetHeight(), 1);
133 // Test whether font metrics retrieved by DirectWrite (skia) and GDI match as
134 // per assumptions mentioned below:-
135 // 1. Font size is the same
136 // 2. The difference between GDI and DirectWrite for font height, baseline,
137 // and cap height is at most 1. For smaller font sizes under 12, GDI
138 // font heights/baselines/cap height are equal/larger by 1 point. For larger
139 // font sizes DirectWrite font heights/baselines/cap height are equal/larger
141 TEST(PlatformFontWinTest
, Metrics_SkiaVersusGDI
) {
142 if (!gfx::win::IsDirectWriteEnabled())
145 // Describes the font being tested.
147 base::string16 font_name
;
152 {base::ASCIIToUTF16("Arial"), 6},
153 {base::ASCIIToUTF16("Arial"), 8},
154 {base::ASCIIToUTF16("Arial"), 10},
155 {base::ASCIIToUTF16("Arial"), 12},
156 {base::ASCIIToUTF16("Arial"), 16},
157 {base::ASCIIToUTF16("Symbol"), 6},
158 {base::ASCIIToUTF16("Symbol"), 10},
159 {base::ASCIIToUTF16("Symbol"), 12},
160 {base::ASCIIToUTF16("Tahoma"), 10},
161 {base::ASCIIToUTF16("Tahoma"), 16},
162 {base::ASCIIToUTF16("Segoe UI"), 6},
163 {base::ASCIIToUTF16("Segoe UI"), 8},
164 {base::ASCIIToUTF16("Segoe UI"), 20},
167 base::win::ScopedGetDC
screen_dc(NULL
);
168 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
170 for (int i
= 0; i
< arraysize(fonts
); ++i
) {
171 LOGFONT font_info
= {0};
173 font_info
.lfHeight
= -fonts
[i
].font_size
;
174 font_info
.lfWeight
= FW_NORMAL
;
175 wcscpy_s(font_info
.lfFaceName
,
176 fonts
[i
].font_name
.length() + 1,
177 fonts
[i
].font_name
.c_str());
179 HFONT font
= CreateFontIndirect(&font_info
);
181 TEXTMETRIC font_metrics
;
182 PlatformFontWin::GetTextMetricsForFont(screen_dc
, font
, &font_metrics
);
184 scoped_refptr
<PlatformFontWin::HFontRef
> h_font_gdi(
185 PlatformFontWin::CreateHFontRefFromGDI(font
, font_metrics
));
187 scoped_refptr
<PlatformFontWin::HFontRef
> h_font_skia(
188 PlatformFontWin::CreateHFontRefFromSkia(font
, font_metrics
));
190 EXPECT_EQ(h_font_gdi
->font_size(), h_font_skia
->font_size());
191 EXPECT_EQ(h_font_gdi
->style(), h_font_skia
->style());
192 EXPECT_EQ(h_font_gdi
->font_name(), h_font_skia
->font_name());
194 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);
196 EXPECT_LE(abs(h_font_gdi
->height() - h_font_skia
->height()), 1);