[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / platform_font_win.cc
blob9f61db39bc7caff325da65a8f41d1d19c6e60eea
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/font_render_params.h"
24 #include "ui/gfx/win/scoped_set_map_mode.h"
26 namespace {
28 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
29 // font is bold.
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();
37 return min_font_size;
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) {
43 if (lf_height < 0) {
44 lf_height -= size_delta;
45 } else {
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
50 // locale.
51 if (abs(lf_height) < min_font_size) {
52 return lf_height < 0 ? -min_font_size : min_font_size;
53 } else {
54 return lf_height;
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);
70 } // namespace
72 namespace gfx {
74 // static
75 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
77 // static
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,
94 int font_size) {
95 InitWithFontNameAndSize(font_name, font_size);
98 Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
99 DCHECK_GE(height, 0);
100 if (GetHeight() == height && GetStyle() == style)
101 return Font(this);
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())
113 break;
114 font_height = font.GetHeight();
115 font_size = font.GetFontSize();
117 return font;
120 LOGFONT font_info;
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 {
133 LOGFONT font_info;
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]);
186 if (length <= 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 ////////////////////////////////////////////////////////////////////////////////
204 // Font, private:
206 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) {
207 DCHECK(hfont);
208 LOGFONT font_info;
209 GetObject(hfont, sizeof(LOGFONT), &font_info);
210 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info));
213 void PlatformFontWin::InitWithFontNameAndSize(const std::string& font_name,
214 int font_size) {
215 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
216 DEFAULT_CHARSET,
217 OUT_DEFAULT_PRECIS,
218 CLIP_DEFAULT_PRECIS,
219 DEFAULT_QUALITY,
220 DEFAULT_PITCH | FF_DONTCARE,
221 base::UTF8ToUTF16(font_name).c_str());
222 font_ref_ = CreateHFontRef(hf);
225 // static
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);
236 DLOG_ASSERT(font);
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(
257 HFONT font,
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);
266 int style = 0;
267 if (font_metrics.tmItalic)
268 style |= Font::ITALIC;
269 if (font_metrics.tmUnderlined)
270 style |= Font::UNDERLINE;
271 if (font_metrics.tmWeight >= kTextMetricWeightBold)
272 style |= Font::BOLD;
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);
286 LOGFONT font_info;
287 GetObject(base_font, sizeof(LOGFONT), &font_info);
289 // Set |lfHeight| to negative value to indicate it's the size, not the height.
290 font_info.lfHeight =
291 -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading);
293 do {
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)
301 break;
302 best_font.Set(font.release());
303 best_font_metrics = font_metrics;
304 } while (true);
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,
316 int font_size,
317 int height,
318 int baseline,
319 int cap_height,
320 int ave_char_width,
321 int style)
322 : hfont_(hfont),
323 font_size_(font_size),
324 height_(height),
325 baseline_(baseline),
326 cap_height_(cap_height),
327 ave_char_width_(ave_char_width),
328 style_(style),
329 dlu_base_x_(-1),
330 requested_font_size_(font_size) {
331 DLOG_ASSERT(hfont);
333 LOGFONT font_info;
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)
342 return dlu_base_x_;
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
350 SIZE ave_text_size;
351 GetTextExtentPoint32(screen_dc,
352 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
353 52, &ave_text_size);
354 dlu_base_x_ = (ave_text_size.cx / 26 + 1) / 2;
356 DCHECK_NE(dlu_base_x_, -1);
357 return dlu_base_x_;
360 PlatformFontWin::HFontRef::~HFontRef() {
361 DeleteObject(hfont_);
364 ////////////////////////////////////////////////////////////////////////////////
365 // PlatformFont, public:
367 // static
368 PlatformFont* PlatformFont::CreateDefault() {
369 return new PlatformFontWin;
372 // static
373 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
374 return new PlatformFontWin(native_font);
377 // static
378 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
379 int font_size) {
380 return new PlatformFontWin(font_name, font_size);
383 } // namespace gfx