Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: ui/gfx/platform_font_win.cc

Issue 2054273002: Font fallback for UI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unittests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/platform_font_win.h" 5 #include "ui/gfx/platform_font_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <dwrite.h> 8 #include <dwrite.h>
9 #include <limits.h> 9 #include <limits.h>
10 #include <math.h> 10 #include <math.h>
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // Sets style properties on |font_info| based on |font_style|. 64 // Sets style properties on |font_info| based on |font_style|.
65 void SetLogFontStyle(int font_style, LOGFONT* font_info) { 65 void SetLogFontStyle(int font_style, LOGFONT* font_info) {
66 font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0; 66 font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0;
67 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0; 67 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0;
68 } 68 }
69 69
70 gfx::Font::Weight ToGfxFontWeight(int weight) { 70 gfx::Font::Weight ToGfxFontWeight(int weight) {
71 return static_cast<gfx::Font::Weight>(weight); 71 return static_cast<gfx::Font::Weight>(weight);
72 } 72 }
73 73
74 // Returns the family name for the |IDWriteFont| interface passed in.
75 // The family name is returned in the |family_name_ret| parameter.
76 // Returns S_OK on success.
77 // TODO(ananta)
78 // Remove the CHECKs in this function once this stabilizes on the field.
79 HRESULT GetFamilyNameFromDirectWriteFont(IDWriteFont* dwrite_font,
80 base::string16* family_name_ret) {
81 base::win::ScopedComPtr<IDWriteFontFamily> font_family;
82 HRESULT hr = dwrite_font->GetFontFamily(font_family.Receive());
83 if (FAILED(hr))
84 CHECK(false);
85
86 base::win::ScopedComPtr<IDWriteLocalizedStrings> family_name;
87 hr = font_family->GetFamilyNames(family_name.Receive());
88 if (FAILED(hr))
89 CHECK(false);
90
91 // TODO(ananta)
92 // Add support for retrieving the family for the current locale.
93 wchar_t family_name_for_locale[MAX_PATH] = {0};
94 hr = family_name->GetString(0,
95 family_name_for_locale,
96 arraysize(family_name_for_locale));
97 if (FAILED(hr))
98 CHECK(false);
99
100 *family_name_ret = family_name_for_locale;
101 return hr;
102 }
103
104 // Uses the GDI interop functionality exposed by DirectWrite to find a 74 // Uses the GDI interop functionality exposed by DirectWrite to find a
105 // matching DirectWrite font for the LOGFONT passed in. If we fail to 75 // matching DirectWrite font for the LOGFONT passed in. If we fail to
106 // find a direct match then we try the DirectWrite font substitution 76 // find a direct match then we try the DirectWrite font substitution
107 // route to find a match. 77 // route to find a match.
108 // The contents of the LOGFONT pointer |font_info| may be modified on 78 // The contents of the LOGFONT pointer |font_info| may be modified on
109 // return. 79 // return.
110 HRESULT FindDirectWriteFontForLOGFONT(IDWriteFactory* factory, 80 HRESULT FindDirectWriteFontForLOGFONT(IDWriteFactory* factory,
111 LOGFONT* font_info, 81 LOGFONT* font_info,
112 IDWriteFont** dwrite_font) { 82 IDWriteFont** dwrite_font) {
113 base::win::ScopedComPtr<IDWriteGdiInterop> gdi_interop; 83 base::win::ScopedComPtr<IDWriteGdiInterop> gdi_interop;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 if (SUCCEEDED(hr)) 234 if (SUCCEEDED(hr))
265 matching_font_count = matching_font_list->GetFontCount(); 235 matching_font_count = matching_font_list->GetFontCount();
266 236
267 hr = font_family->GetFirstMatchingFont(weight, stretch, style, dwrite_font); 237 hr = font_family->GetFirstMatchingFont(weight, stretch, style, dwrite_font);
268 if (FAILED(hr)) { 238 if (FAILED(hr)) {
269 base::debug::Alias(&matching_font_count); 239 base::debug::Alias(&matching_font_count);
270 CHECK(false); 240 CHECK(false);
271 } 241 }
272 242
273 base::string16 font_name; 243 base::string16 font_name;
274 GetFamilyNameFromDirectWriteFont(*dwrite_font, &font_name); 244 gfx::GetFamilyNameFromDirectWriteFont(*dwrite_font, &font_name);
275 wcscpy_s(font_info->lfFaceName, arraysize(font_info->lfFaceName), 245 wcscpy_s(font_info->lfFaceName, arraysize(font_info->lfFaceName),
276 font_name.c_str()); 246 font_name.c_str());
277 return hr; 247 return hr;
278 } 248 }
279 249
280 } // namespace 250 } // namespace
281 251
282 namespace gfx { 252 namespace gfx {
283 253
284 // static 254 // static
285 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; 255 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
286 256
287 // static 257 // static
288 PlatformFontWin::AdjustFontCallback 258 PlatformFontWin::AdjustFontCallback
289 PlatformFontWin::adjust_font_callback = nullptr; 259 PlatformFontWin::adjust_font_callback = nullptr;
290 PlatformFontWin::GetMinimumFontSizeCallback 260 PlatformFontWin::GetMinimumFontSizeCallback
291 PlatformFontWin::get_minimum_font_size_callback = NULL; 261 PlatformFontWin::get_minimum_font_size_callback = NULL;
292 262
293 IDWriteFactory* PlatformFontWin::direct_write_factory_ = nullptr; 263 IDWriteFactory* PlatformFontWin::direct_write_factory_ = nullptr;
294 264
265 // TODO(ananta)
266 // Remove the CHECKs in this function once this stabilizes on the field.
267 HRESULT GetFamilyNameFromDirectWriteFont(IDWriteFont* dwrite_font,
268 base::string16* family_name_ret) {
269 base::win::ScopedComPtr<IDWriteFontFamily> font_family;
270 HRESULT hr = dwrite_font->GetFontFamily(font_family.Receive());
271 if (FAILED(hr))
272 CHECK(false);
273
274 base::win::ScopedComPtr<IDWriteLocalizedStrings> family_name;
275 hr = font_family->GetFamilyNames(family_name.Receive());
276 if (FAILED(hr))
277 CHECK(false);
278
279 // TODO(ananta)
280 // Add support for retrieving the family for the current locale.
281 wchar_t family_name_for_locale[MAX_PATH] = {0};
282 hr = family_name->GetString(0, family_name_for_locale,
283 arraysize(family_name_for_locale));
284 if (FAILED(hr))
285 CHECK(false);
286
287 *family_name_ret = family_name_for_locale;
288 return hr;
289 }
290
295 //////////////////////////////////////////////////////////////////////////////// 291 ////////////////////////////////////////////////////////////////////////////////
296 // PlatformFontWin, public 292 // PlatformFontWin, public
297 293
298 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { 294 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
299 } 295 }
300 296
301 PlatformFontWin::PlatformFontWin(NativeFont native_font) { 297 PlatformFontWin::PlatformFontWin(NativeFont native_font) {
302 InitWithCopyOfHFONT(native_font); 298 InitWithCopyOfHFONT(native_font);
303 } 299 }
304 300
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 return new PlatformFontWin(native_font); 679 return new PlatformFontWin(native_font);
684 } 680 }
685 681
686 // static 682 // static
687 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 683 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
688 int font_size) { 684 int font_size) {
689 return new PlatformFontWin(font_name, font_size); 685 return new PlatformFontWin(font_name, font_size);
690 } 686 }
691 687
692 } // namespace gfx 688 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698