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/win/scoped_set_map_mode.h"
27 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
29 const int kTextMetricWeightBold
= 700;
31 // Returns the minimum font size, using the minimum size callback, if set.
32 int GetMinimumFontSize() {
33 int min_font_size
= 0;
34 if (gfx::PlatformFontWin::get_minimum_font_size_callback
)
35 min_font_size
= gfx::PlatformFontWin::get_minimum_font_size_callback();
39 // Returns either minimum font allowed for a current locale or
40 // lf_height + size_delta value.
41 int AdjustFontSize(int lf_height
, int size_delta
) {
43 lf_height
-= size_delta
;
45 lf_height
+= size_delta
;
47 const int min_font_size
= GetMinimumFontSize();
48 // Make sure lf_height is not smaller than allowed min font size for current
50 if (abs(lf_height
) < min_font_size
) {
51 return lf_height
< 0 ? -min_font_size
: min_font_size
;
57 // Sets style properties on |font_info| based on |font_style|.
58 void SetLogFontStyle(int font_style
, LOGFONT
* font_info
) {
59 font_info
->lfUnderline
= (font_style
& gfx::Font::UNDERLINE
) != 0;
60 font_info
->lfItalic
= (font_style
& gfx::Font::ITALIC
) != 0;
61 font_info
->lfWeight
= (font_style
& gfx::Font::BOLD
) ? FW_BOLD
: FW_NORMAL
;
64 void GetTextMetricsForFont(HDC hdc
, HFONT font
, TEXTMETRIC
* text_metrics
) {
65 base::win::ScopedSelectObject
scoped_font(hdc
, font
);
66 GetTextMetrics(hdc
, text_metrics
);
74 PlatformFontWin::HFontRef
* PlatformFontWin::base_font_ref_
;
77 PlatformFontWin::AdjustFontCallback
78 PlatformFontWin::adjust_font_callback
= NULL
;
79 PlatformFontWin::GetMinimumFontSizeCallback
80 PlatformFontWin::get_minimum_font_size_callback
= NULL
;
82 ////////////////////////////////////////////////////////////////////////////////
83 // PlatformFontWin, public
85 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
88 PlatformFontWin::PlatformFontWin(NativeFont native_font
) {
89 InitWithCopyOfHFONT(native_font
);
92 PlatformFontWin::PlatformFontWin(const std::string
& font_name
,
94 InitWithFontNameAndSize(font_name
, font_size
);
97 Font
PlatformFontWin::DeriveFontWithHeight(int height
, int style
) {
99 if (GetHeight() == height
&& GetStyle() == style
)
102 // CreateFontIndirect() doesn't return the largest size for the given height
103 // when decreasing the height. Iterate to find it.
104 if (GetHeight() > height
) {
105 const int min_font_size
= GetMinimumFontSize();
106 Font font
= DeriveFont(-1, style
);
107 int font_height
= font
.GetHeight();
108 int font_size
= font
.GetFontSize();
109 while (font_height
> height
&& font_size
!= min_font_size
) {
110 font
= font
.Derive(-1, style
);
111 if (font_height
== font
.GetHeight() && font_size
== font
.GetFontSize())
113 font_height
= font
.GetHeight();
114 font_size
= font
.GetFontSize();
120 GetObject(GetNativeFont(), sizeof(LOGFONT
), &font_info
);
121 font_info
.lfHeight
= height
;
122 SetLogFontStyle(style
, &font_info
);
124 HFONT hfont
= CreateFontIndirect(&font_info
);
125 return DeriveWithCorrectedSize(hfont
);
128 ////////////////////////////////////////////////////////////////////////////////
129 // PlatformFontWin, PlatformFont implementation:
131 Font
PlatformFontWin::DeriveFont(int size_delta
, int style
) const {
133 GetObject(GetNativeFont(), sizeof(LOGFONT
), &font_info
);
134 const int requested_font_size
= font_ref_
->requested_font_size();
135 font_info
.lfHeight
= AdjustFontSize(-requested_font_size
, size_delta
);
136 SetLogFontStyle(style
, &font_info
);
138 HFONT hfont
= CreateFontIndirect(&font_info
);
139 return Font(new PlatformFontWin(CreateHFontRef(hfont
)));
142 int PlatformFontWin::GetHeight() const {
143 return font_ref_
->height();
146 int PlatformFontWin::GetBaseline() const {
147 return font_ref_
->baseline();
150 int PlatformFontWin::GetCapHeight() const {
151 return font_ref_
->cap_height();
154 int PlatformFontWin::GetExpectedTextWidth(int length
) const {
155 return length
* std::min(font_ref_
->GetDluBaseX(),
156 font_ref_
->ave_char_width());
159 int PlatformFontWin::GetStyle() const {
160 return font_ref_
->style();
163 std::string
PlatformFontWin::GetFontName() const {
164 return font_ref_
->font_name();
167 std::string
PlatformFontWin::GetActualFontNameForTesting() const {
168 // With the current implementation on Windows, HFontRef::font_name() returns
169 // the font name taken from the HFONT handle, but it's not the name that comes
170 // from the font's metadata. See http://crbug.com/327287
171 return font_ref_
->font_name();
174 std::string
PlatformFontWin::GetLocalizedFontName() const {
175 base::win::ScopedCreateDC
memory_dc(CreateCompatibleDC(NULL
));
176 if (!memory_dc
.Get())
177 return GetFontName();
179 // When a font has a localized name for a language matching the system
180 // locale, GetTextFace() returns the localized name.
181 base::win::ScopedSelectObject
font(memory_dc
, font_ref_
->hfont());
182 wchar_t localized_font_name
[LF_FACESIZE
];
183 int length
= GetTextFace(memory_dc
, arraysize(localized_font_name
),
184 &localized_font_name
[0]);
186 return GetFontName();
187 return base::SysWideToUTF8(localized_font_name
);
190 int PlatformFontWin::GetFontSize() const {
191 return font_ref_
->font_size();
194 NativeFont
PlatformFontWin::GetNativeFont() const {
195 return font_ref_
->hfont();
198 ////////////////////////////////////////////////////////////////////////////////
201 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont
) {
204 GetObject(hfont
, sizeof(LOGFONT
), &font_info
);
205 font_ref_
= CreateHFontRef(CreateFontIndirect(&font_info
));
208 void PlatformFontWin::InitWithFontNameAndSize(const std::string
& font_name
,
210 HFONT hf
= ::CreateFont(-font_size
, 0, 0, 0, FW_DONTCARE
, FALSE
, FALSE
, FALSE
,
215 DEFAULT_PITCH
| FF_DONTCARE
,
216 base::UTF8ToUTF16(font_name
).c_str());
217 font_ref_
= CreateHFontRef(hf
);
221 PlatformFontWin::HFontRef
* PlatformFontWin::GetBaseFontRef() {
222 if (base_font_ref_
== NULL
) {
223 NONCLIENTMETRICS metrics
;
224 base::win::GetNonClientMetrics(&metrics
);
226 if (adjust_font_callback
)
227 adjust_font_callback(&metrics
.lfMessageFont
);
228 metrics
.lfMessageFont
.lfHeight
=
229 AdjustFontSize(metrics
.lfMessageFont
.lfHeight
, 0);
230 HFONT font
= CreateFontIndirect(&metrics
.lfMessageFont
);
232 base_font_ref_
= PlatformFontWin::CreateHFontRef(font
);
233 // base_font_ref_ is global, up the ref count so it's never deleted.
234 base_font_ref_
->AddRef();
236 return base_font_ref_
;
239 PlatformFontWin::HFontRef
* PlatformFontWin::CreateHFontRef(HFONT font
) {
240 TEXTMETRIC font_metrics
;
243 base::win::ScopedGetDC
screen_dc(NULL
);
244 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
245 GetTextMetricsForFont(screen_dc
, font
, &font_metrics
);
248 return CreateHFontRef(font
, font_metrics
);
251 PlatformFontWin::HFontRef
* PlatformFontWin::CreateHFontRef(
253 const TEXTMETRIC
& font_metrics
) {
254 const int height
= std::max
<int>(1, font_metrics
.tmHeight
);
255 const int baseline
= std::max
<int>(1, font_metrics
.tmAscent
);
256 const int cap_height
=
257 std::max
<int>(1, font_metrics
.tmAscent
- font_metrics
.tmInternalLeading
);
258 const int ave_char_width
= std::max
<int>(1, font_metrics
.tmAveCharWidth
);
259 const int font_size
=
260 std::max
<int>(1, font_metrics
.tmHeight
- font_metrics
.tmInternalLeading
);
262 if (font_metrics
.tmItalic
)
263 style
|= Font::ITALIC
;
264 if (font_metrics
.tmUnderlined
)
265 style
|= Font::UNDERLINE
;
266 if (font_metrics
.tmWeight
>= kTextMetricWeightBold
)
269 return new HFontRef(font
, font_size
, height
, baseline
, cap_height
,
270 ave_char_width
, style
);
273 Font
PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font
) {
274 base::win::ScopedGetDC
screen_dc(NULL
);
275 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
277 base::win::ScopedGDIObject
<HFONT
> best_font(base_font
);
278 TEXTMETRIC best_font_metrics
;
279 GetTextMetricsForFont(screen_dc
, best_font
, &best_font_metrics
);
282 GetObject(base_font
, sizeof(LOGFONT
), &font_info
);
284 // Set |lfHeight| to negative value to indicate it's the size, not the height.
286 -(best_font_metrics
.tmHeight
- best_font_metrics
.tmInternalLeading
);
289 // Increment font size. Prefer font with greater size if its height isn't
290 // greater than height of base font.
291 font_info
.lfHeight
= AdjustFontSize(font_info
.lfHeight
, 1);
292 base::win::ScopedGDIObject
<HFONT
> font(CreateFontIndirect(&font_info
));
293 TEXTMETRIC font_metrics
;
294 GetTextMetricsForFont(screen_dc
, font
, &font_metrics
);
295 if (font_metrics
.tmHeight
> best_font_metrics
.tmHeight
)
297 best_font
.Set(font
.release());
298 best_font_metrics
= font_metrics
;
301 return Font(new PlatformFontWin(CreateHFontRef(best_font
.release())));
304 PlatformFontWin::PlatformFontWin(HFontRef
* hfont_ref
) : font_ref_(hfont_ref
) {
307 ////////////////////////////////////////////////////////////////////////////////
308 // PlatformFontWin::HFontRef:
310 PlatformFontWin::HFontRef::HFontRef(HFONT hfont
,
318 font_size_(font_size
),
321 cap_height_(cap_height
),
322 ave_char_width_(ave_char_width
),
325 requested_font_size_(font_size
) {
329 GetObject(hfont_
, sizeof(LOGFONT
), &font_info
);
330 font_name_
= base::UTF16ToUTF8(base::string16(font_info
.lfFaceName
));
331 if (font_info
.lfHeight
< 0)
332 requested_font_size_
= -font_info
.lfHeight
;
335 int PlatformFontWin::HFontRef::GetDluBaseX() {
336 if (dlu_base_x_
!= -1)
339 base::win::ScopedGetDC
screen_dc(NULL
);
340 base::win::ScopedSelectObject
font(screen_dc
, hfont_
);
341 gfx::ScopedSetMapMode
mode(screen_dc
, MM_TEXT
);
343 // Yes, this is how Microsoft recommends calculating the dialog unit
344 // conversions. See: http://support.microsoft.com/kb/125681
346 GetTextExtentPoint32(screen_dc
,
347 L
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
349 dlu_base_x_
= (ave_text_size
.cx
/ 26 + 1) / 2;
351 DCHECK_NE(dlu_base_x_
, -1);
355 PlatformFontWin::HFontRef::~HFontRef() {
356 DeleteObject(hfont_
);
359 ////////////////////////////////////////////////////////////////////////////////
360 // PlatformFont, public:
363 PlatformFont
* PlatformFont::CreateDefault() {
364 return new PlatformFontWin
;
368 PlatformFont
* PlatformFont::CreateFromNativeFont(NativeFont native_font
) {
369 return new PlatformFontWin(native_font
);
373 PlatformFont
* PlatformFont::CreateFromNameAndSize(const std::string
& font_name
,
375 return new PlatformFontWin(font_name
, font_size
);