Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / ui / gfx / platform_font_win.cc
blobf01399406fcc1c2731af345d0f1e36eb2e83d8f5
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 <windows.h>
8 #include <math.h>
10 #include <algorithm>
11 #include <string>
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"
25 namespace {
27 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
28 // font is bold.
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();
36 return min_font_size;
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) {
42 if (lf_height < 0) {
43 lf_height -= size_delta;
44 } else {
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
49 // locale.
50 if (abs(lf_height) < min_font_size) {
51 return lf_height < 0 ? -min_font_size : min_font_size;
52 } else {
53 return lf_height;
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);
69 } // namespace
71 namespace gfx {
73 // static
74 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
76 // static
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,
93 int font_size) {
94 InitWithFontNameAndSize(font_name, font_size);
97 Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
98 DCHECK_GE(height, 0);
99 if (GetHeight() == height && GetStyle() == style)
100 return Font(this);
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())
112 break;
113 font_height = font.GetHeight();
114 font_size = font.GetFontSize();
116 return font;
119 LOGFONT font_info;
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 {
132 LOGFONT font_info;
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]);
185 if (length <= 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 ////////////////////////////////////////////////////////////////////////////////
199 // Font, private:
201 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) {
202 DCHECK(hfont);
203 LOGFONT font_info;
204 GetObject(hfont, sizeof(LOGFONT), &font_info);
205 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info));
208 void PlatformFontWin::InitWithFontNameAndSize(const std::string& font_name,
209 int font_size) {
210 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
211 DEFAULT_CHARSET,
212 OUT_DEFAULT_PRECIS,
213 CLIP_DEFAULT_PRECIS,
214 DEFAULT_QUALITY,
215 DEFAULT_PITCH | FF_DONTCARE,
216 base::UTF8ToUTF16(font_name).c_str());
217 font_ref_ = CreateHFontRef(hf);
220 // static
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);
231 DLOG_ASSERT(font);
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(
252 HFONT font,
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);
261 int style = 0;
262 if (font_metrics.tmItalic)
263 style |= Font::ITALIC;
264 if (font_metrics.tmUnderlined)
265 style |= Font::UNDERLINE;
266 if (font_metrics.tmWeight >= kTextMetricWeightBold)
267 style |= Font::BOLD;
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);
281 LOGFONT font_info;
282 GetObject(base_font, sizeof(LOGFONT), &font_info);
284 // Set |lfHeight| to negative value to indicate it's the size, not the height.
285 font_info.lfHeight =
286 -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading);
288 do {
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)
296 break;
297 best_font.Set(font.release());
298 best_font_metrics = font_metrics;
299 } while (true);
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,
311 int font_size,
312 int height,
313 int baseline,
314 int cap_height,
315 int ave_char_width,
316 int style)
317 : hfont_(hfont),
318 font_size_(font_size),
319 height_(height),
320 baseline_(baseline),
321 cap_height_(cap_height),
322 ave_char_width_(ave_char_width),
323 style_(style),
324 dlu_base_x_(-1),
325 requested_font_size_(font_size) {
326 DLOG_ASSERT(hfont);
328 LOGFONT font_info;
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)
337 return dlu_base_x_;
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
345 SIZE ave_text_size;
346 GetTextExtentPoint32(screen_dc,
347 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
348 52, &ave_text_size);
349 dlu_base_x_ = (ave_text_size.cx / 26 + 1) / 2;
351 DCHECK_NE(dlu_base_x_, -1);
352 return dlu_base_x_;
355 PlatformFontWin::HFontRef::~HFontRef() {
356 DeleteObject(hfont_);
359 ////////////////////////////////////////////////////////////////////////////////
360 // PlatformFont, public:
362 // static
363 PlatformFont* PlatformFont::CreateDefault() {
364 return new PlatformFontWin;
367 // static
368 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
369 return new PlatformFontWin(native_font);
372 // static
373 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
374 int font_size) {
375 return new PlatformFontWin(font_name, font_size);
378 } // namespace gfx