Pass localized font name of prefs to font settings.
[chromium-blink-merge.git] / ui / gfx / platform_font_win.cc
blob1f4bd4d483b03d41652acd10e752776db9bcde8e
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/string_util.h"
15 #include "base/sys_string_conversions.h"
16 #include "base/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"
23 namespace {
25 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
26 // font is bold.
27 const int kTextMetricWeightBold = 700;
29 // Returns either minimum font allowed for a current locale or
30 // lf_height + size_delta value.
31 int AdjustFontSize(int lf_height, int size_delta) {
32 if (lf_height < 0) {
33 lf_height -= size_delta;
34 } else {
35 lf_height += size_delta;
37 int min_font_size = 0;
38 if (gfx::PlatformFontWin::get_minimum_font_size_callback)
39 min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback();
40 // Make sure lf_height is not smaller than allowed min font size for current
41 // locale.
42 if (abs(lf_height) < min_font_size) {
43 return lf_height < 0 ? -min_font_size : min_font_size;
44 } else {
45 return lf_height;
49 } // namespace
51 namespace gfx {
53 // static
54 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
56 // static
57 PlatformFontWin::AdjustFontCallback
58 PlatformFontWin::adjust_font_callback = NULL;
59 PlatformFontWin::GetMinimumFontSizeCallback
60 PlatformFontWin::get_minimum_font_size_callback = NULL;
62 ////////////////////////////////////////////////////////////////////////////////
63 // PlatformFontWin, public
65 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
68 PlatformFontWin::PlatformFontWin(const Font& other) {
69 InitWithCopyOfHFONT(other.GetNativeFont());
72 PlatformFontWin::PlatformFontWin(NativeFont native_font) {
73 InitWithCopyOfHFONT(native_font);
76 PlatformFontWin::PlatformFontWin(const std::string& font_name,
77 int font_size) {
78 InitWithFontNameAndSize(font_name, font_size);
81 ////////////////////////////////////////////////////////////////////////////////
82 // PlatformFontWin, PlatformFont implementation:
84 Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
85 LOGFONT font_info;
86 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
87 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta);
88 font_info.lfUnderline =
89 ((style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED);
90 font_info.lfItalic = ((style & gfx::Font::ITALIC) == gfx::Font::ITALIC);
91 font_info.lfWeight = (style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL;
93 HFONT hfont = CreateFontIndirect(&font_info);
94 return Font(new PlatformFontWin(CreateHFontRef(hfont)));
97 int PlatformFontWin::GetHeight() const {
98 return font_ref_->height();
101 int PlatformFontWin::GetBaseline() const {
102 return font_ref_->baseline();
105 int PlatformFontWin::GetAverageCharacterWidth() const {
106 return font_ref_->ave_char_width();
109 int PlatformFontWin::GetStringWidth(const string16& text) const {
110 return Canvas::GetStringWidth(text,
111 Font(const_cast<PlatformFontWin*>(this)));
114 int PlatformFontWin::GetExpectedTextWidth(int length) const {
115 return length * std::min(font_ref_->GetDluBaseX(),
116 GetAverageCharacterWidth());
119 int PlatformFontWin::GetStyle() const {
120 return font_ref_->style();
123 std::string PlatformFontWin::GetFontName() const {
124 return font_ref_->font_name();
127 std::string PlatformFontWin::GetLocalizedFontName() const {
128 base::win::ScopedCreateDC memory_dc(CreateCompatibleDC(NULL));
129 if (!memory_dc.Get())
130 return GetFontName();
132 // When a font has a localized name for a language matching the system
133 // locale, GetTextFace() returns the localized name.
134 base::win::ScopedSelectObject font(memory_dc, font_ref_->hfont());
135 wchar_t localized_font_name[LF_FACESIZE];
136 int length = GetTextFace(memory_dc, arraysize(localized_font_name),
137 &localized_font_name[0]);
138 if (length <= 0)
139 return GetFontName();
140 return base::SysWideToUTF8(localized_font_name);
143 int PlatformFontWin::GetFontSize() const {
144 return font_ref_->font_size();
147 NativeFont PlatformFontWin::GetNativeFont() const {
148 return font_ref_->hfont();
151 ////////////////////////////////////////////////////////////////////////////////
152 // Font, private:
154 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) {
155 DCHECK(hfont);
156 LOGFONT font_info;
157 GetObject(hfont, sizeof(LOGFONT), &font_info);
158 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info));
161 void PlatformFontWin::InitWithFontNameAndSize(const std::string& font_name,
162 int font_size) {
163 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 UTF8ToUTF16(font_name).c_str());
165 font_ref_ = CreateHFontRef(hf);
168 // static
169 PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() {
170 if (base_font_ref_ == NULL) {
171 NONCLIENTMETRICS metrics;
172 base::win::GetNonClientMetrics(&metrics);
174 if (adjust_font_callback)
175 adjust_font_callback(&metrics.lfMessageFont);
176 metrics.lfMessageFont.lfHeight =
177 AdjustFontSize(metrics.lfMessageFont.lfHeight, 0);
178 HFONT font = CreateFontIndirect(&metrics.lfMessageFont);
179 DLOG_ASSERT(font);
180 base_font_ref_ = PlatformFontWin::CreateHFontRef(font);
181 // base_font_ref_ is global, up the ref count so it's never deleted.
182 base_font_ref_->AddRef();
184 return base_font_ref_;
187 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
188 TEXTMETRIC font_metrics;
189 HDC screen_dc = GetDC(NULL);
190 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font));
191 int last_map_mode = SetMapMode(screen_dc, MM_TEXT);
192 GetTextMetrics(screen_dc, &font_metrics);
193 // To avoid the DC referencing font_handle_, select the previous font.
194 SelectObject(screen_dc, previous_font);
195 SetMapMode(screen_dc, last_map_mode);
196 ReleaseDC(NULL, screen_dc);
198 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight));
199 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent));
200 const int ave_char_width =
201 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth));
202 int style = 0;
203 if (font_metrics.tmItalic)
204 style |= Font::ITALIC;
205 if (font_metrics.tmUnderlined)
206 style |= Font::UNDERLINED;
207 if (font_metrics.tmWeight >= kTextMetricWeightBold)
208 style |= Font::BOLD;
210 return new HFontRef(font, height, baseline, ave_char_width, style);
213 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
216 ////////////////////////////////////////////////////////////////////////////////
217 // PlatformFontWin::HFontRef:
219 PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
220 int height,
221 int baseline,
222 int ave_char_width,
223 int style)
224 : hfont_(hfont),
225 height_(height),
226 baseline_(baseline),
227 ave_char_width_(ave_char_width),
228 style_(style),
229 dlu_base_x_(-1) {
230 DLOG_ASSERT(hfont);
232 LOGFONT font_info;
233 GetObject(hfont_, sizeof(LOGFONT), &font_info);
234 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName));
235 DCHECK_LT(font_info.lfHeight, 0);
236 font_size_ = -font_info.lfHeight;
239 int PlatformFontWin::HFontRef::GetDluBaseX() {
240 if (dlu_base_x_ != -1)
241 return dlu_base_x_;
243 HDC screen_dc = GetDC(NULL);
244 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, hfont_));
245 int last_map_mode = SetMapMode(screen_dc, MM_TEXT);
246 // Yes, this is how Microsoft recommends calculating the dialog unit
247 // conversions. See: http://support.microsoft.com/kb/125681
248 SIZE ave_text_size;
249 GetTextExtentPoint32(screen_dc,
250 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
251 52, &ave_text_size);
252 dlu_base_x_ = (ave_text_size.cx / 26 + 1) / 2;
253 // To avoid the DC referencing font_handle_, select the previous font.
254 SelectObject(screen_dc, previous_font);
255 SetMapMode(screen_dc, last_map_mode);
256 ReleaseDC(NULL, screen_dc);
258 DCHECK_NE(dlu_base_x_, -1);
259 return dlu_base_x_;
262 PlatformFontWin::HFontRef::~HFontRef() {
263 DeleteObject(hfont_);
266 ////////////////////////////////////////////////////////////////////////////////
267 // PlatformFont, public:
269 // static
270 PlatformFont* PlatformFont::CreateDefault() {
271 return new PlatformFontWin;
274 // static
275 PlatformFont* PlatformFont::CreateFromFont(const Font& other) {
276 return new PlatformFontWin(other);
279 // static
280 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
281 return new PlatformFontWin(native_font);
284 // static
285 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
286 int font_size) {
287 return new PlatformFontWin(font_name, font_size);
290 } // namespace gfx