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. 4 5 #include "ui/gfx/platform_font_win.h" 6 7 #include <windows.h> 8 #include <math.h> 9 10 #include <algorithm> 11 #include <string> 12 13 #include "base/logging.h" 14 #include "base/macros.h" 15 #include "base/strings/string_util.h" 16 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h" 18 #include "base/win/scoped_gdi_object.h" 19 #include "base/win/scoped_hdc.h" 20 #include "base/win/scoped_select_object.h" 21 #include "base/win/win_util.h" 22 #include "ui/gfx/canvas.h" 23 #include "ui/gfx/font.h" 24 #include "ui/gfx/font_render_params.h" 25 #include "ui/gfx/win/scoped_set_map_mode.h" 26 27 namespace { 28 29 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the 30 // font is bold. 31 const int kTextMetricWeightBold = 700; 32 33 // Returns the minimum font size, using the minimum size callback, if set. 34 int GetMinimumFontSize() { 35 int min_font_size = 0; 36 if (gfx::PlatformFontWin::get_minimum_font_size_callback) 37 min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback(); 38 return min_font_size; 39 } 40 41 // Returns either minimum font allowed for a current locale or 42 // lf_height + size_delta value. 43 int AdjustFontSize(int lf_height, int size_delta) { 44 if (lf_height < 0) { 45 lf_height -= size_delta; 46 } else { 47 lf_height += size_delta; 48 } 49 const int min_font_size = GetMinimumFontSize(); 50 // Make sure lf_height is not smaller than allowed min font size for current 51 // locale. 52 if (abs(lf_height) < min_font_size) { 53 return lf_height < 0 ? -min_font_size : min_font_size; 54 } else { 55 return lf_height; 56 } 57 } 58 59 // Sets style properties on |font_info| based on |font_style|. 60 void SetLogFontStyle(int font_style, LOGFONT* font_info) { 61 font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0; 62 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0; 63 font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; 64 } 65 66 void GetTextMetricsForFont(HDC hdc, HFONT font, TEXTMETRIC* text_metrics) { 67 base::win::ScopedSelectObject scoped_font(hdc, font); 68 GetTextMetrics(hdc, text_metrics); 69 } 70 71 } // namespace 72 73 namespace gfx { 74 75 // static 76 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; 77 78 // static 79 PlatformFontWin::AdjustFontCallback 80 PlatformFontWin::adjust_font_callback = NULL; 81 PlatformFontWin::GetMinimumFontSizeCallback 82 PlatformFontWin::get_minimum_font_size_callback = NULL; 83 84 //////////////////////////////////////////////////////////////////////////////// 85 // PlatformFontWin, public 86 87 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { 88 } 89 90 PlatformFontWin::PlatformFontWin(NativeFont native_font) { 91 InitWithCopyOfHFONT(native_font); 92 } 93 94 PlatformFontWin::PlatformFontWin(const std::string& font_name, 95 int font_size) { 96 InitWithFontNameAndSize(font_name, font_size); 97 } 98 99 Font PlatformFontWin::DeriveFontWithHeight(int height, int style) { 100 DCHECK_GE(height, 0); 101 if (GetHeight() == height && GetStyle() == style) 102 return Font(this); 103 104 // CreateFontIndirect() doesn't return the largest size for the given height 105 // when decreasing the height. Iterate to find it. 106 if (GetHeight() > height) { 107 const int min_font_size = GetMinimumFontSize(); 108 Font font = DeriveFont(-1, style); 109 int font_height = font.GetHeight(); 110 int font_size = font.GetFontSize(); 111 while (font_height > height && font_size != min_font_size) { 112 font = font.Derive(-1, style); 113 if (font_height == font.GetHeight() && font_size == font.GetFontSize()) 114 break; 115 font_height = font.GetHeight(); 116 font_size = font.GetFontSize(); 117 } 118 return font; 119 } 120 121 LOGFONT font_info; 122 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); 123 font_info.lfHeight = height; 124 SetLogFontStyle(style, &font_info); 125 126 HFONT hfont = CreateFontIndirect(&font_info); 127 return DeriveWithCorrectedSize(hfont); 128 } 129 130 //////////////////////////////////////////////////////////////////////////////// 131 // PlatformFontWin, PlatformFont implementation: 132 133 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { 134 LOGFONT font_info; 135 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); 136 const int requested_font_size = font_ref_->requested_font_size(); 137 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta); 138 SetLogFontStyle(style, &font_info); 139 140 HFONT hfont = CreateFontIndirect(&font_info); 141 return Font(new PlatformFontWin(CreateHFontRef(hfont))); 142 } 143 144 int PlatformFontWin::GetHeight() const { 145 return font_ref_->height(); 146 } 147 148 int PlatformFontWin::GetBaseline() const { 149 return font_ref_->baseline(); 150 } 151 152 int PlatformFontWin::GetCapHeight() const { 153 return font_ref_->cap_height(); 154 } 155 156 int PlatformFontWin::GetExpectedTextWidth(int length) const { 157 return length * std::min(font_ref_->GetDluBaseX(), 158 font_ref_->ave_char_width()); 159 } 160 161 int PlatformFontWin::GetStyle() const { 162 return font_ref_->style(); 163 } 164 165 std::string PlatformFontWin::GetFontName() const { 166 return font_ref_->font_name(); 167 } 168 169 std::string PlatformFontWin::GetActualFontNameForTesting() const { 170 // With the current implementation on Windows, HFontRef::font_name() returns 171 // the font name taken from the HFONT handle, but it's not the name that comes 172 // from the font's metadata. See http://crbug.com/327287 173 return font_ref_->font_name(); 174 } 175 176 std::string PlatformFontWin::GetLocalizedFontName() const { 177 base::win::ScopedCreateDC memory_dc(CreateCompatibleDC(NULL)); 178 if (!memory_dc.Get()) 179 return GetFontName(); 180 181 // When a font has a localized name for a language matching the system 182 // locale, GetTextFace() returns the localized name. 183 base::win::ScopedSelectObject font(memory_dc.Get(), font_ref_->hfont()); 184 wchar_t localized_font_name[LF_FACESIZE]; 185 int length = GetTextFace(memory_dc.Get(), arraysize(localized_font_name), 186 &localized_font_name[0]); 187 if (length <= 0) 188 return GetFontName(); 189 return base::SysWideToUTF8(localized_font_name); 190 } 191 192 int PlatformFontWin::GetFontSize() const { 193 return font_ref_->font_size(); 194 } 195 196 const FontRenderParams& PlatformFontWin::GetFontRenderParams() const { 197 CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams, params, 198 (gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(false), NULL))); 199 return params; 200 } 201 202 NativeFont PlatformFontWin::GetNativeFont() const { 203 return font_ref_->hfont(); 204 } 205 206 //////////////////////////////////////////////////////////////////////////////// 207 // Font, private: 208 209 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) { 210 DCHECK(hfont); 211 LOGFONT font_info; 212 GetObject(hfont, sizeof(LOGFONT), &font_info); 213 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info)); 214 } 215 216 void PlatformFontWin::InitWithFontNameAndSize(const std::string& font_name, 217 int font_size) { 218 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, 219 DEFAULT_CHARSET, 220 OUT_DEFAULT_PRECIS, 221 CLIP_DEFAULT_PRECIS, 222 DEFAULT_QUALITY, 223 DEFAULT_PITCH | FF_DONTCARE, 224 base::UTF8ToUTF16(font_name).c_str()); 225 font_ref_ = CreateHFontRef(hf); 226 } 227 228 // static 229 PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() { 230 if (base_font_ref_ == NULL) { 231 NONCLIENTMETRICS metrics; 232 base::win::GetNonClientMetrics(&metrics); 233 234 if (adjust_font_callback) 235 adjust_font_callback(&metrics.lfMessageFont); 236 metrics.lfMessageFont.lfHeight = 237 AdjustFontSize(metrics.lfMessageFont.lfHeight, 0); 238 HFONT font = CreateFontIndirect(&metrics.lfMessageFont); 239 DLOG_ASSERT(font); 240 base_font_ref_ = PlatformFontWin::CreateHFontRef(font); 241 // base_font_ref_ is global, up the ref count so it's never deleted. 242 base_font_ref_->AddRef(); 243 } 244 return base_font_ref_; 245 } 246 247 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { 248 TEXTMETRIC font_metrics; 249 250 { 251 base::win::ScopedGetDC screen_dc(NULL); 252 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); 253 GetTextMetricsForFont(screen_dc, font, &font_metrics); 254 } 255 256 return CreateHFontRef(font, font_metrics); 257 } 258 259 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef( 260 HFONT font, 261 const TEXTMETRIC& font_metrics) { 262 const int height = std::max<int>(1, font_metrics.tmHeight); 263 const int baseline = std::max<int>(1, font_metrics.tmAscent); 264 const int cap_height = 265 std::max<int>(1, font_metrics.tmAscent - font_metrics.tmInternalLeading); 266 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); 267 const int font_size = 268 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); 269 int style = 0; 270 if (font_metrics.tmItalic) 271 style |= Font::ITALIC; 272 if (font_metrics.tmUnderlined) 273 style |= Font::UNDERLINE; 274 if (font_metrics.tmWeight >= kTextMetricWeightBold) 275 style |= Font::BOLD; 276 277 return new HFontRef(font, font_size, height, baseline, cap_height, 278 ave_char_width, style); 279 } 280 281 Font PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font) { 282 base::win::ScopedGetDC screen_dc(NULL); 283 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); 284 285 base::win::ScopedGDIObject<HFONT> best_font(base_font); 286 TEXTMETRIC best_font_metrics; 287 GetTextMetricsForFont(screen_dc, best_font, &best_font_metrics); 288 289 LOGFONT font_info; 290 GetObject(base_font, sizeof(LOGFONT), &font_info); 291 292 // Set |lfHeight| to negative value to indicate it's the size, not the height. 293 font_info.lfHeight = 294 -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading); 295 296 do { 297 // Increment font size. Prefer font with greater size if its height isn't 298 // greater than height of base font. 299 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, 1); 300 base::win::ScopedGDIObject<HFONT> font(CreateFontIndirect(&font_info)); 301 TEXTMETRIC font_metrics; 302 GetTextMetricsForFont(screen_dc, font, &font_metrics); 303 if (font_metrics.tmHeight > best_font_metrics.tmHeight) 304 break; 305 best_font.Set(font.release()); 306 best_font_metrics = font_metrics; 307 } while (true); 308 309 return Font(new PlatformFontWin(CreateHFontRef(best_font.release()))); 310 } 311 312 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { 313 } 314 315 //////////////////////////////////////////////////////////////////////////////// 316 // PlatformFontWin::HFontRef: 317 318 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, 319 int font_size, 320 int height, 321 int baseline, 322 int cap_height, 323 int ave_char_width, 324 int style) 325 : hfont_(hfont), 326 font_size_(font_size), 327 height_(height), 328 baseline_(baseline), 329 cap_height_(cap_height), 330 ave_char_width_(ave_char_width), 331 style_(style), 332 dlu_base_x_(-1), 333 requested_font_size_(font_size) { 334 DLOG_ASSERT(hfont); 335 336 LOGFONT font_info; 337 GetObject(hfont_, sizeof(LOGFONT), &font_info); 338 font_name_ = base::UTF16ToUTF8(base::string16(font_info.lfFaceName)); 339 if (font_info.lfHeight < 0) 340 requested_font_size_ = -font_info.lfHeight; 341 } 342 343 int PlatformFontWin::HFontRef::GetDluBaseX() { 344 if (dlu_base_x_ != -1) 345 return dlu_base_x_; 346 347 base::win::ScopedGetDC screen_dc(NULL); 348 base::win::ScopedSelectObject font(screen_dc, hfont_); 349 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); 350 351 // Yes, this is how Microsoft recommends calculating the dialog unit 352 // conversions. See: http://support.microsoft.com/kb/125681 353 SIZE ave_text_size; 354 GetTextExtentPoint32(screen_dc, 355 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 356 52, &ave_text_size); 357 dlu_base_x_ = (ave_text_size.cx / 26 + 1) / 2; 358 359 DCHECK_NE(dlu_base_x_, -1); 360 return dlu_base_x_; 361 } 362 363 PlatformFontWin::HFontRef::~HFontRef() { 364 DeleteObject(hfont_); 365 } 366 367 //////////////////////////////////////////////////////////////////////////////// 368 // PlatformFont, public: 369 370 // static 371 PlatformFont* PlatformFont::CreateDefault() { 372 return new PlatformFontWin; 373 } 374 375 // static 376 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { 377 return new PlatformFontWin(native_font); 378 } 379 380 // static 381 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 382 int font_size) { 383 return new PlatformFontWin(font_name, font_size); 384 } 385 386 } // namespace gfx 387