Correct blacklist entry message
[chromium-blink-merge.git] / ui / gfx / platform_font_win.cc
bloba97da2eedcf1ad9e2ecafb5e748a1bb33675b3a6
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_hdc.h"
18 #include "base/win/scoped_select_object.h"
19 #include "base/win/win_util.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/font.h"
22 #include "ui/gfx/win/scoped_set_map_mode.h"
24 namespace {
26 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
27 // font is bold.
28 const int kTextMetricWeightBold = 700;
30 // Returns the minimum font size, using the minimum size callback, if set.
31 int GetMinimumFontSize() {
32 int min_font_size = 0;
33 if (gfx::PlatformFontWin::get_minimum_font_size_callback)
34 min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback();
35 return min_font_size;
38 // Returns either minimum font allowed for a current locale or
39 // lf_height + size_delta value.
40 int AdjustFontSize(int lf_height, int size_delta) {
41 if (lf_height < 0) {
42 lf_height -= size_delta;
43 } else {
44 lf_height += size_delta;
46 const int min_font_size = GetMinimumFontSize();
47 // Make sure lf_height is not smaller than allowed min font size for current
48 // locale.
49 if (abs(lf_height) < min_font_size) {
50 return lf_height < 0 ? -min_font_size : min_font_size;
51 } else {
52 return lf_height;
56 // Sets style properties on |font_info| based on |font_style|.
57 void SetLogFontStyle(int font_style, LOGFONT* font_info) {
58 font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0;
59 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0;
60 font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL;
63 } // namespace
65 namespace gfx {
67 // static
68 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
70 // static
71 PlatformFontWin::AdjustFontCallback
72 PlatformFontWin::adjust_font_callback = NULL;
73 PlatformFontWin::GetMinimumFontSizeCallback
74 PlatformFontWin::get_minimum_font_size_callback = NULL;
76 ////////////////////////////////////////////////////////////////////////////////
77 // PlatformFontWin, public
79 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
82 PlatformFontWin::PlatformFontWin(NativeFont native_font) {
83 InitWithCopyOfHFONT(native_font);
86 PlatformFontWin::PlatformFontWin(const std::string& font_name,
87 int font_size) {
88 InitWithFontNameAndSize(font_name, font_size);
91 Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
92 DCHECK_GE(height, 0);
93 if (GetHeight() == height && GetStyle() == style)
94 return Font(this);
96 // CreateFontIndirect() doesn't return the largest size for the given height
97 // when decreasing the height. Iterate to find it.
98 if (GetHeight() > height) {
99 const int min_font_size = GetMinimumFontSize();
100 Font font = DeriveFont(-1, style);
101 int font_height = font.GetHeight();
102 int font_size = font.GetFontSize();
103 while (font_height > height && font_size != min_font_size) {
104 font = font.DeriveFont(-1, style);
105 if (font_height == font.GetHeight() && font_size == font.GetFontSize())
106 break;
107 font_height = font.GetHeight();
108 font_size = font.GetFontSize();
110 return font;
113 LOGFONT font_info;
114 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
115 font_info.lfHeight = height;
116 SetLogFontStyle(style, &font_info);
118 HFONT hfont = CreateFontIndirect(&font_info);
119 return Font(new PlatformFontWin(CreateHFontRef(hfont)));
122 ////////////////////////////////////////////////////////////////////////////////
123 // PlatformFontWin, PlatformFont implementation:
125 Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
126 LOGFONT font_info;
127 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
128 const int requested_font_size = font_ref_->requested_font_size();
129 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta);
130 SetLogFontStyle(style, &font_info);
132 HFONT hfont = CreateFontIndirect(&font_info);
133 return Font(new PlatformFontWin(CreateHFontRef(hfont)));
136 int PlatformFontWin::GetHeight() const {
137 return font_ref_->height();
140 int PlatformFontWin::GetBaseline() const {
141 return font_ref_->baseline();
144 int PlatformFontWin::GetCapHeight() const {
145 return font_ref_->cap_height();
148 int PlatformFontWin::GetAverageCharacterWidth() const {
149 return font_ref_->ave_char_width();
152 int PlatformFontWin::GetStringWidth(const base::string16& text) const {
153 return Canvas::GetStringWidth(text,
154 Font(const_cast<PlatformFontWin*>(this)));
157 int PlatformFontWin::GetExpectedTextWidth(int length) const {
158 return length * std::min(font_ref_->GetDluBaseX(),
159 GetAverageCharacterWidth());
162 int PlatformFontWin::GetStyle() const {
163 return font_ref_->style();
166 std::string PlatformFontWin::GetFontName() const {
167 return font_ref_->font_name();
170 std::string PlatformFontWin::GetLocalizedFontName() const {
171 base::win::ScopedCreateDC memory_dc(CreateCompatibleDC(NULL));
172 if (!memory_dc.Get())
173 return GetFontName();
175 // When a font has a localized name for a language matching the system
176 // locale, GetTextFace() returns the localized name.
177 base::win::ScopedSelectObject font(memory_dc, font_ref_->hfont());
178 wchar_t localized_font_name[LF_FACESIZE];
179 int length = GetTextFace(memory_dc, arraysize(localized_font_name),
180 &localized_font_name[0]);
181 if (length <= 0)
182 return GetFontName();
183 return base::SysWideToUTF8(localized_font_name);
186 int PlatformFontWin::GetFontSize() const {
187 return font_ref_->font_size();
190 NativeFont PlatformFontWin::GetNativeFont() const {
191 return font_ref_->hfont();
194 ////////////////////////////////////////////////////////////////////////////////
195 // Font, private:
197 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) {
198 DCHECK(hfont);
199 LOGFONT font_info;
200 GetObject(hfont, sizeof(LOGFONT), &font_info);
201 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info));
204 void PlatformFontWin::InitWithFontNameAndSize(const std::string& font_name,
205 int font_size) {
206 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207 UTF8ToUTF16(font_name).c_str());
208 font_ref_ = CreateHFontRef(hf);
211 // static
212 PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() {
213 if (base_font_ref_ == NULL) {
214 NONCLIENTMETRICS metrics;
215 base::win::GetNonClientMetrics(&metrics);
217 if (adjust_font_callback)
218 adjust_font_callback(&metrics.lfMessageFont);
219 metrics.lfMessageFont.lfHeight =
220 AdjustFontSize(metrics.lfMessageFont.lfHeight, 0);
221 HFONT font = CreateFontIndirect(&metrics.lfMessageFont);
222 DLOG_ASSERT(font);
223 base_font_ref_ = PlatformFontWin::CreateHFontRef(font);
224 // base_font_ref_ is global, up the ref count so it's never deleted.
225 base_font_ref_->AddRef();
227 return base_font_ref_;
230 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
231 TEXTMETRIC font_metrics;
234 base::win::ScopedGetDC screen_dc(NULL);
235 base::win::ScopedSelectObject scoped_font(screen_dc, font);
236 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT);
237 GetTextMetrics(screen_dc, &font_metrics);
240 const int height = std::max<int>(1, font_metrics.tmHeight);
241 const int baseline = std::max<int>(1, font_metrics.tmAscent);
242 const int cap_height =
243 std::max<int>(1, font_metrics.tmAscent - font_metrics.tmInternalLeading);
244 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth);
245 const int font_size =
246 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading);
247 int style = 0;
248 if (font_metrics.tmItalic)
249 style |= Font::ITALIC;
250 if (font_metrics.tmUnderlined)
251 style |= Font::UNDERLINE;
252 if (font_metrics.tmWeight >= kTextMetricWeightBold)
253 style |= Font::BOLD;
255 return new HFontRef(font, font_size, height, baseline, cap_height,
256 ave_char_width, style);
259 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
262 ////////////////////////////////////////////////////////////////////////////////
263 // PlatformFontWin::HFontRef:
265 PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
266 int font_size,
267 int height,
268 int baseline,
269 int cap_height,
270 int ave_char_width,
271 int style)
272 : hfont_(hfont),
273 font_size_(font_size),
274 height_(height),
275 baseline_(baseline),
276 cap_height_(cap_height),
277 ave_char_width_(ave_char_width),
278 style_(style),
279 dlu_base_x_(-1),
280 requested_font_size_(font_size) {
281 DLOG_ASSERT(hfont);
283 LOGFONT font_info;
284 GetObject(hfont_, sizeof(LOGFONT), &font_info);
285 font_name_ = UTF16ToUTF8(base::string16(font_info.lfFaceName));
286 if (font_info.lfHeight < 0)
287 requested_font_size_ = -font_info.lfHeight;
290 int PlatformFontWin::HFontRef::GetDluBaseX() {
291 if (dlu_base_x_ != -1)
292 return dlu_base_x_;
294 base::win::ScopedGetDC screen_dc(NULL);
295 base::win::ScopedSelectObject font(screen_dc, hfont_);
296 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT);
298 // Yes, this is how Microsoft recommends calculating the dialog unit
299 // conversions. See: http://support.microsoft.com/kb/125681
300 SIZE ave_text_size;
301 GetTextExtentPoint32(screen_dc,
302 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
303 52, &ave_text_size);
304 dlu_base_x_ = (ave_text_size.cx / 26 + 1) / 2;
306 DCHECK_NE(dlu_base_x_, -1);
307 return dlu_base_x_;
310 PlatformFontWin::HFontRef::~HFontRef() {
311 DeleteObject(hfont_);
314 ////////////////////////////////////////////////////////////////////////////////
315 // PlatformFont, public:
317 // static
318 PlatformFont* PlatformFont::CreateDefault() {
319 return new PlatformFontWin;
322 // static
323 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
324 return new PlatformFontWin(native_font);
327 // static
328 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
329 int font_size) {
330 return new PlatformFontWin(font_name, font_size);
333 } // namespace gfx