| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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); |
| 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) || (index == UINT_MAX) || !exists) { |
| 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); |
| 103 return E_FAIL; | 104 return E_FAIL; |
| 104 } | 105 } |
| 105 hr = font_collection->FindFamilyName(metrics.lfMessageFont.lfFaceName, | 106 hr = font_collection->FindFamilyName(metrics.lfMessageFont.lfFaceName, |
| 106 &index, | 107 &index, |
| 107 &exists); | 108 &exists); |
| 108 } | 109 } |
| 109 | 110 |
| 110 if (FAILED(hr)) { | 111 if (FAILED(hr) || (index == UINT_MAX) || !exists) { |
| 111 CHECK(false); | 112 CHECK(false); |
| 112 return hr; | 113 return hr; |
| 113 } | 114 } |
| 114 | 115 |
| 115 hr = font_collection->GetFontFamily(index, font_family.Receive()); | 116 hr = font_collection->GetFontFamily(index, font_family.Receive()); |
| 116 if (FAILED(hr)) { | 117 if (FAILED(hr)) { |
| 117 CHECK(false); | 118 CHECK(false); |
| 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 |
| 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 |
| 128 hr = font_family->GetFirstMatchingFont(weight, stretch, italic, | 150 hr = font_family->GetFirstMatchingFont(weight, stretch, italic, |
| 129 dwrite_font); | 151 dwrite_font); |
| 130 if (FAILED(hr)) | 152 if (FAILED(hr) && matching_font_list) |
| 153 hr = matching_font_list->GetFont(0, dwrite_font); |
| 154 |
| 155 if (FAILED(hr)) { |
| 156 base::debug::Alias(&matching_font_count); |
| 131 CHECK(false); | 157 CHECK(false); |
| 158 } |
| 132 return hr; | 159 return hr; |
| 133 } | 160 } |
| 134 | 161 |
| 135 } // namespace | 162 } // namespace |
| 136 | 163 |
| 137 namespace gfx { | 164 namespace gfx { |
| 138 | 165 |
| 139 // static | 166 // static |
| 140 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; | 167 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; |
| 141 | 168 |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 return new PlatformFontWin(native_font); | 569 return new PlatformFontWin(native_font); |
| 543 } | 570 } |
| 544 | 571 |
| 545 // static | 572 // static |
| 546 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 573 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 547 int font_size) { | 574 int font_size) { |
| 548 return new PlatformFontWin(font_name, font_size); | 575 return new PlatformFontWin(font_name, font_size); |
| 549 } | 576 } |
| 550 | 577 |
| 551 } // namespace gfx | 578 } // namespace gfx |
| OLD | NEW |