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"
13 #include "base/logging.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/win/scoped_gdi_object.h"
18 #include "base/win/scoped_hdc.h"
19 #include "base/win/scoped_select_object.h"
20 #include "base/win/win_util.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/font.h"
23 #include "ui/gfx/font_render_params.h"
24 #include "ui/gfx/win/scoped_set_map_mode.h"
28 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
30 const int kTextMetricWeightBold
= 700;
32 // Returns the minimum font size, using the minimum size callback, if set.
33 int GetMinimumFontSize() {
34 int min_font_size
= 0;
35 if (gfx::PlatformFontWin::get_minimum_font_size_callback
)
36 min_font_size
= gfx::PlatformFontWin::get_minimum_font_size_callback();
40 // Returns either minimum font allowed for a current locale or
41 // lf_height + size_delta value.
42 int AdjustFontSize(int lf_height
, int size_delta
) {
44 lf_height
-= size_delta
;
46 lf_height
+= size_delta
;
48 const int min_font_size
= GetMinimumFontSize();
49 // Make sure lf_height is not smaller than allowed min font size for current
51 if (abs(lf_height
) < min_font_size
) {
52 return lf_height
< 0 ? -min_font_size
: min_font_size
;
58 // Sets style properties on |font_info| based on |font_style|.
59 void SetLogFontStyle(int font_style
, LOGFONT
* font_info
) {
60 font_info
->lfUnderline
= (font_style
& gfx::Font::UNDERLINE
) != 0;
61 font_info
->lfItalic
= (font_style
& gfx::Font::ITALIC
) != 0;
62 font_info
->lfWeight
= (font_style
& gfx::Font::BOLD
) ? FW_BOLD
: FW_NORMAL
;
65 void GetTextMetricsForFont(HDC hdc
, HFONT font
, TEXTMETRIC
* text_metrics
) {
66 base::win::ScopedSelectObject
scoped_font(hdc
, font
);
67 GetTextMetrics(hdc
, text_metrics
);
75 PlatformFontWin::HFontRef
* PlatformFontWin::base_font_ref_
;
78 PlatformFontWin::AdjustFontCallback
79 PlatformFontWin::adjust_font_callback
= NULL
;
80 PlatformFontWin::GetMinimumFontSizeCallback
81 PlatformFontWin::get_minimum_font_size_callback
= NULL
;
83 ////////////////////////////////////////////////////////////////////////////////
84 // PlatformFontWin, public
86 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
89 PlatformFontWin::PlatformFontWin(NativeFont native_font
) {
90 InitWithCopyOfHFONT(native_font
);
93 PlatformFontWin::PlatformFontWin(const std::string
& font_name
,
95 InitWithFontNameAndSize(font_name
, font_size
);
98 Font
PlatformFontWin::DeriveFontWithHeight(int height
, int style
) {
100 if (GetHeight() == height
&& GetStyle() == style
)
103 // CreateFontIndirect() doesn't return the largest size for the given height
104 // when decreasing the height. Iterate to find it.
105 if (GetHeight() > height
) {
106 const int min_font_size
= GetMinimumFontSize();
107 Font font
= DeriveFont(-1, style
);
108 int font_height
= font
.GetHeight();
109 int font_size
= font
.GetFontSize();
110 while (font_height
> height
&& font_size
!= min_font_size
) {
111 font
= font
.Derive(-1, style
);
112 if (font_height
== font
.GetHeight() && font_size
== font
.GetFontSize())
114 font_height
= font
.GetHeight();
115 font_size
= font
.GetFontSize();
121 GetObject(GetNativeFont(), sizeof(LOGFONT
), &font_info
);
122 font_info
.lfHeight
= height
;
123 SetLogFontStyle(style
, &font_info
);
125 HFONT hfont
= CreateFontIndirect(&font_info
);
126 return DeriveWithCorrectedSize(hfont
);
129 ////////////////////////////////////////////////////////////////////////////////
130 // PlatformFontWin, PlatformFont implementation:
132 Font
PlatformFontWin::DeriveFont(int size_delta
, int style
) const {
134 GetObject(GetNativeFont(), sizeof(LOGFONT
), &font_info
);
135 const int requested_font_size
= font_ref_
->requested_font_size();
136 font_info
.lfHeight
= AdjustFontSize(-requested_font_size
, size_delta
);
137 SetLogFontStyle(style
, &font_info
);
139 HFONT hfont
= CreateFontIndirect(&font_info
);
140 return Font(new PlatformFontWin(CreateHFontRef(hfont
)));
143 int PlatformFontWin::GetHeight() const {
144 return font_ref_
->height();
147 int PlatformFontWin::GetBaseline() const {
148 return font_ref_
->baseline();
151 int PlatformFontWin::GetCapHeight() const {
152 return font_ref_
->cap_height();
155 int PlatformFontWin::GetExpectedTextWidth(int length
) const {
156 return length
* std::min(font_ref_
->GetDluBaseX(),
157 font_ref_
->ave_char_width());
160 int PlatformFontWin::GetStyle() const {
161 return font_ref_
->style();
164 std::string
PlatformFontWin::GetFontName() const {
165 return font_ref_
->font_name();
168 std::string
PlatformFontWin::GetActualFontNameForTesting() const {
169 // With the current implementation on Windows, HFontRef::font_name() returns
170 // the font name taken from the HFONT handle, but it's not the name that comes
171 // from the font's metadata. See http://crbug.com/327287
172 return font_ref_
->font_name();
175 std::string
PlatformFontWin::GetLocalizedFontName() const {
176 base::win::ScopedCreateDC
memory_dc(CreateCompatibleDC(NULL
));
177 if (!memory_dc
.Get())
178 return GetFontName();
180 // When a font has a localized name for a language matching the system
181 // locale, GetTextFace() returns the localized name.
182 base::win::ScopedSelectObject
font(memory_dc
, font_ref_
->hfont());
183 wchar_t localized_font_name
[LF_FACESIZE
];
184 int length
= GetTextFace(memory_dc
, arraysize(localized_font_name
),
185 &localized_font_name
[0]);
187 return GetFontName();
188 return base::SysWideToUTF8(localized_font_name
);
191 int PlatformFontWin::GetFontSize() const {
192 return font_ref_
->font_size();
195 const FontRenderParams
& PlatformFontWin::GetFontRenderParams() const {
196 return GetDefaultFontRenderParams();
199 NativeFont
PlatformFontWin::GetNativeFont() const {
200 return font_ref_
->hfont();
203 ////////////////////////////////////////////////////////////////////////////////
206 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont
) {
209 GetObject(hfont
, sizeof(LOGFONT
), &font_info
);
210 font_ref_
= CreateHFontRef(CreateFontIndirect(&font_info
));
213 void PlatformFontWin::InitWithFontNameAndSize(const std::string
& font_name
,
215 HFONT hf
= ::CreateFont(-font_size
, 0, 0, 0, FW_DONTCARE
, FALSE
, FALSE
, FALSE
,
220 DEFAULT_PITCH
| FF_DONTCARE
,
221 base::UTF8ToUTF16(font_name
).c_str());
222 font_ref_
= CreateHFontRef(hf
);
226 PlatformFontWin::HFontRef
* PlatformFontWin::GetBaseFontRef() {
227 if (base_font_ref_
== NULL
) {
228 NONCLIENTMETRICS metrics
;
229 base::win::GetNonClientMetrics(&metrics
);
231 if (adjust_font_callback
)
232 adjust_font_callback(&metrics
.lfMessageFont
);
233 metrics
.lfMessageFont
.lfHeight
=
234 AdjustFontSize(metrics
.lfMessageFont
.lfHeight
, 0);
235 HFONT font
= CreateFontIndirect(&metrics
.lfMessageFont
);
237 base_font_ref_
= PlatformFontWin::CreateHFontRef(font
);
238 // base_font_ref_ is global, up the ref count so it's never deleted.
239 base_font_ref_
->AddRef();
241 return base_font_ref_
;
244 PlatformFontWin::HFontRef
* PlatformFontWin::CreateHFontRef(HFONT font
) {
245 TEXTMETRIC font_metrics
;
248 base::win::ScopedGetDC
screen_dc(NULL
);
249 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
250 GetTextMetricsForFont(screen_dc
, font
, &font_metrics
);
253 return CreateHFontRef(font
, font_metrics
);
256 PlatformFontWin::HFontRef
* PlatformFontWin::CreateHFontRef(
258 const TEXTMETRIC
& font_metrics
) {
259 const int height
= std::max
<int>(1, font_metrics
.tmHeight
);
260 const int baseline
= std::max
<int>(1, font_metrics
.tmAscent
);
261 const int cap_height
=
262 std::max
<int>(1, font_metrics
.tmAscent
- font_metrics
.tmInternalLeading
);
263 const int ave_char_width
= std::max
<int>(1, font_metrics
.tmAveCharWidth
);
264 const int font_size
=
265 std::max
<int>(1, font_metrics
.tmHeight
- font_metrics
.tmInternalLeading
);
267 if (font_metrics
.tmItalic
)
268 style
|= Font::ITALIC
;
269 if (font_metrics
.tmUnderlined
)
270 style
|= Font::UNDERLINE
;
271 if (font_metrics
.tmWeight
>= kTextMetricWeightBold
)
274 return new HFontRef(font
, font_size
, height
, baseline
, cap_height
,
275 ave_char_width
, style
);
278 Font
PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font
) {
279 base::win::ScopedGetDC
screen_dc(NULL
);
280 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
282 base::win::ScopedGDIObject
<HFONT
> best_font(base_font
);
283 TEXTMETRIC best_font_metrics
;
284 GetTextMetricsForFont(screen_dc
, best_font
, &best_font_metrics
);
287 GetObject(base_font
, sizeof(LOGFONT
), &font_info
);
289 // Set |lfHeight| to negative value to indicate it's the size, not the height.
291 -(best_font_metrics
.tmHeight
- best_font_metrics
.tmInternalLeading
);
294 // Increment font size. Prefer font with greater size if its height isn't
295 // greater than height of base font.
296 font_info
.lfHeight
= AdjustFontSize(font_info
.lfHeight
, 1);
297 base::win::ScopedGDIObject
<HFONT
> font(CreateFontIndirect(&font_info
));
298 TEXTMETRIC font_metrics
;
299 GetTextMetricsForFont(screen_dc
, font
, &font_metrics
);
300 if (font_metrics
.tmHeight
> best_font_metrics
.tmHeight
)
302 best_font
.Set(font
.release());
303 best_font_metrics
= font_metrics
;
306 return Font(new PlatformFontWin(CreateHFontRef(best_font
.release())));
309 PlatformFontWin::PlatformFontWin(HFontRef
* hfont_ref
) : font_ref_(hfont_ref
) {
312 ////////////////////////////////////////////////////////////////////////////////
313 // PlatformFontWin::HFontRef:
315 PlatformFontWin::HFontRef::HFontRef(HFONT hfont
,
323 font_size_(font_size
),
326 cap_height_(cap_height
),
327 ave_char_width_(ave_char_width
),
330 requested_font_size_(font_size
) {
334 GetObject(hfont_
, sizeof(LOGFONT
), &font_info
);
335 font_name_
= base::UTF16ToUTF8(base::string16(font_info
.lfFaceName
));
336 if (font_info
.lfHeight
< 0)
337 requested_font_size_
= -font_info
.lfHeight
;
340 int PlatformFontWin::HFontRef::GetDluBaseX() {
341 if (dlu_base_x_
!= -1)
344 base::win::ScopedGetDC
screen_dc(NULL
);
345 base::win::ScopedSelectObject
font(screen_dc
, hfont_
);
346 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
348 // Yes, this is how Microsoft recommends calculating the dialog unit
349 // conversions. See: http://support.microsoft.com/kb/125681
351 GetTextExtentPoint32(screen_dc
,
352 L
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
354 dlu_base_x_
= (ave_text_size
.cx
/ 26 + 1) / 2;
356 DCHECK_NE(dlu_base_x_
, -1);
360 PlatformFontWin::HFontRef::~HFontRef() {
361 DeleteObject(hfont_
);
364 ////////////////////////////////////////////////////////////////////////////////
365 // PlatformFont, public:
368 PlatformFont
* PlatformFont::CreateDefault() {
369 return new PlatformFontWin
;
373 PlatformFont
* PlatformFont::CreateFromNativeFont(NativeFont native_font
) {
374 return new PlatformFontWin(native_font
);
378 PlatformFont
* PlatformFont::CreateFromNameAndSize(const std::string
& font_name
,
380 return new PlatformFontWin(font_name
, font_size
);