OLD | NEW |
---|---|
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 <algorithm> | 7 #include <algorithm> |
8 #include <dwrite.h> | 8 #include <dwrite.h> |
9 #include <math.h> | 9 #include <math.h> |
10 #include <windows.h> | 10 #include <windows.h> |
11 | 11 |
12 #include "base/debug/alias.h" | |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/win/scoped_comptr.h" | 18 #include "base/win/scoped_comptr.h" |
18 #include "base/win/scoped_gdi_object.h" | 19 #include "base/win/scoped_gdi_object.h" |
19 #include "base/win/scoped_hdc.h" | 20 #include "base/win/scoped_hdc.h" |
20 #include "base/win/scoped_select_object.h" | 21 #include "base/win/scoped_select_object.h" |
21 #include "base/win/win_util.h" | 22 #include "base/win/win_util.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 base::win::ScopedComPtr<IDWriteFontCollection> font_collection; | 83 base::win::ScopedComPtr<IDWriteFontCollection> font_collection; |
83 HRESULT hr = factory->GetSystemFontCollection(font_collection.Receive()); | 84 HRESULT hr = factory->GetSystemFontCollection(font_collection.Receive()); |
84 if (FAILED(hr)) { | 85 if (FAILED(hr)) { |
85 CHECK(false); | 86 CHECK(false); |
86 return hr; | 87 return hr; |
87 } | 88 } |
88 | 89 |
89 base::win::ScopedComPtr<IDWriteFontFamily> font_family; | 90 base::win::ScopedComPtr<IDWriteFontFamily> font_family; |
90 BOOL exists = FALSE; | 91 BOOL exists = FALSE; |
91 uint32 index = 0; | 92 uint32 index = 0; |
92 hr = font_collection->FindFamilyName(face_name, &index, &exists); | 93 hr = font_collection->FindFamilyName(face_name, &index, &exists); |
Shrikant Kelkar
2014/11/19 22:59:25
Besides FAILED(hr) can you add check for index ==
ananta
2014/11/19 23:19:28
Done.
| |
93 // If we fail to find a match then fallback to the default font on the | 94 // If we fail to find a match then fallback to the default font on the |
94 // system. This is what skia does as well. | 95 // system. This is what skia does as well. |
95 if (FAILED(hr)) { | 96 if (FAILED(hr)) { |
96 NONCLIENTMETRICS metrics = {0}; | 97 NONCLIENTMETRICS metrics = {0}; |
97 metrics.cbSize = sizeof(metrics); | 98 metrics.cbSize = sizeof(metrics); |
98 if (!SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, | 99 if (!SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, |
99 sizeof(metrics), | 100 sizeof(metrics), |
100 &metrics, | 101 &metrics, |
101 0)) { | 102 0)) { |
102 CHECK(false); | 103 CHECK(false); |
(...skipping 15 matching lines...) Expand all Loading... | |
118 return hr; | 119 return hr; |
119 } | 120 } |
120 | 121 |
121 DWRITE_FONT_WEIGHT weight = (font_style & SkTypeface::kBold) | 122 DWRITE_FONT_WEIGHT weight = (font_style & SkTypeface::kBold) |
122 ? DWRITE_FONT_WEIGHT_BOLD | 123 ? DWRITE_FONT_WEIGHT_BOLD |
123 : DWRITE_FONT_WEIGHT_NORMAL; | 124 : DWRITE_FONT_WEIGHT_NORMAL; |
124 DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_NORMAL; | 125 DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_NORMAL; |
125 DWRITE_FONT_STYLE italic = (font_style & SkTypeface::kItalic) | 126 DWRITE_FONT_STYLE italic = (font_style & SkTypeface::kItalic) |
126 ? DWRITE_FONT_STYLE_ITALIC | 127 ? DWRITE_FONT_STYLE_ITALIC |
127 : DWRITE_FONT_STYLE_NORMAL; | 128 : DWRITE_FONT_STYLE_NORMAL; |
129 | |
130 // The IDWriteFontFamily::GetFirstMatchingFont call fails on certain machines | |
131 // for fonts like MS UI Gothic, Segoe UI, etc. It is not clear why these | |
132 // fonts could be accessible to GDI and not to DirectWrite. | |
133 // The code below adds some debug fields to help track down these failures. | |
134 // 1. We get the matching font list for the font attributes passed in. | |
135 // 2. We get the font count in the family with a debug alias variable. | |
136 // 3. If we fail to get the font via the GetFirstMatchingFont call then we | |
137 // try to get the first matching font in the IDWriteFontList. | |
138 // 4. If GetFirstMatchingFont and IDWriteFontList both fail then we CHECK as | |
139 // before. | |
140 // Next step would be to remove the CHECKs in this function and fallback to | |
141 // GDI. | |
142 // http://crbug.com/434425 | |
Shrikant Kelkar
2014/11/19 23:30:47
If you want to look for specific fonts that are ca
ananta
2014/11/19 23:38:58
Will do that in a followup. Dunno what the clean u
| |
143 base::win::ScopedComPtr<IDWriteFontList> matching_font_list; | |
144 hr = font_family->GetMatchingFonts(weight, stretch, italic, | |
145 matching_font_list.Receive()); | |
146 uint32 matching_font_count = 0; | |
147 if (SUCCEEDED(hr)) | |
148 matching_font_count = matching_font_list->GetFontCount(); | |
149 | |
150 bool fallback_to_matching_font = false; | |
151 | |
128 hr = font_family->GetFirstMatchingFont(weight, stretch, italic, | 152 hr = font_family->GetFirstMatchingFont(weight, stretch, italic, |
129 dwrite_font); | 153 dwrite_font); |
130 if (FAILED(hr)) | 154 if (FAILED(hr)) { |
155 if (matching_font_list) { | |
156 hr = matching_font_list->GetFont(0, dwrite_font); | |
157 if (SUCCEEDED(hr)) | |
158 fallback_to_matching_font = true; | |
159 } | |
160 } | |
161 | |
162 if (FAILED(hr)) { | |
163 base::debug::Alias(&matching_font_count); | |
164 base::debug::Alias(&fallback_to_matching_font); | |
Shrikant Kelkar
2014/11/19 23:30:47
fallback_to_matching_font = true if SUCCEEDED(hr)
ananta
2014/11/19 23:38:58
Moved the alias for fallback_to_matching_font abov
| |
131 CHECK(false); | 165 CHECK(false); |
166 } | |
132 return hr; | 167 return hr; |
133 } | 168 } |
134 | 169 |
135 } // namespace | 170 } // namespace |
136 | 171 |
137 namespace gfx { | 172 namespace gfx { |
138 | 173 |
139 // static | 174 // static |
140 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; | 175 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; |
141 | 176 |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
542 return new PlatformFontWin(native_font); | 577 return new PlatformFontWin(native_font); |
543 } | 578 } |
544 | 579 |
545 // static | 580 // static |
546 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 581 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
547 int font_size) { | 582 int font_size) { |
548 return new PlatformFontWin(font_name, font_size); | 583 return new PlatformFontWin(font_name, font_size); |
549 } | 584 } |
550 | 585 |
551 } // namespace gfx | 586 } // namespace gfx |
OLD | NEW |