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

Side by Side Diff: ui/gfx/render_text_mac.mm

Issue 2734333005: RenderTextMac: Fix crash when passed an invalid font. (Closed)
Patch Set: Created 3 years, 9 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
« no previous file with comments | « ui/gfx/render_text_mac.h ('k') | ui/gfx/render_text_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/render_text_mac.h" 5 #include "ui/gfx/render_text_mac.h"
6 6
7 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 #include <ApplicationServices/ApplicationServices.h> 8 #include <ApplicationServices/ApplicationServices.h>
9 #include <CoreText/CoreText.h> 9 #include <CoreText/CoreText.h>
10 10
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 base::ScopedCFTypeRef<CFStringRef> family_name(CTFontCopyFamilyName(font)); 55 base::ScopedCFTypeRef<CFStringRef> family_name(CTFontCopyFamilyName(font));
56 CFDictionarySetValue(attributes, kCTFontNameAttribute, family_name.release()); 56 CFDictionarySetValue(attributes, kCTFontNameAttribute, family_name.release());
57 57
58 base::ScopedCFTypeRef<CTFontDescriptorRef> desc( 58 base::ScopedCFTypeRef<CTFontDescriptorRef> desc(
59 CTFontDescriptorCreateWithAttributes(attributes)); 59 CTFontDescriptorCreateWithAttributes(attributes));
60 return base::ScopedCFTypeRef<CTFontRef>( 60 return base::ScopedCFTypeRef<CTFontRef>(
61 CTFontCreateWithFontDescriptor(desc, 0.0, nullptr)); 61 CTFontCreateWithFontDescriptor(desc, 0.0, nullptr));
62 } 62 }
63 63
64 // Returns whether |font_list| has a valid primary native font.
65 bool HasValidPrimaryNativeFont(const gfx::FontList& font_list) {
66 return font_list.GetPrimaryFont().GetNativeFont();
67 }
68
69 gfx::FontList GetValidFontList(const gfx::FontList& font_list) {
70 if (HasValidPrimaryNativeFont(font_list))
71 return font_list;
72
73 gfx::FontList default_font_list;
74 const int size_delta =
75 font_list.GetFontSize() - default_font_list.GetFontSize();
76 gfx::FontList derived_font_list = default_font_list.Derive(
77 size_delta, font_list.GetFontStyle(), font_list.GetFontWeight());
78 if (HasValidPrimaryNativeFont(derived_font_list))
79 return derived_font_list;
80
81 DCHECK(HasValidPrimaryNativeFont(default_font_list));
82 return default_font_list;
83 }
84
64 } // namespace 85 } // namespace
65 86
66 namespace gfx { 87 namespace gfx {
67 88
68 namespace internal { 89 namespace internal {
69 90
70 // Note: this is only used by RenderTextHarfbuzz. 91 // Note: this is only used by RenderTextHarfbuzz.
71 sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font, 92 sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font,
72 bool italic, 93 bool italic,
73 Font::Weight weight) { 94 Font::Weight weight) {
74 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL; 95 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL;
75 Font font_with_style = font.Derive(0, style, weight); 96 Font font_with_style = font.Derive(0, style, weight);
76 if (!font_with_style.GetNativeFont()) 97 if (!font_with_style.GetNativeFont())
77 return nullptr; 98 return nullptr;
78 99
79 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont( 100 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont(
80 base::mac::NSToCFCast(font_with_style.GetNativeFont()))); 101 base::mac::NSToCFCast(font_with_style.GetNativeFont())));
81 } 102 }
82 103
83 } // namespace internal 104 } // namespace internal
84 105
85 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} 106 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {}
86 107
87 RenderTextMac::~RenderTextMac() {} 108 RenderTextMac::~RenderTextMac() {}
88 109
89 std::unique_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const { 110 std::unique_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const {
90 return base::WrapUnique(new RenderTextMac); 111 return base::WrapUnique(new RenderTextMac);
91 } 112 }
92 113
114 void RenderTextMac::SetFontList(const FontList& font_list) {
115 // Ensure the font list used has a valid native font.
karandeepb 2017/03/09 01:33:32 We may need to do something similar in the base Re
116 RenderText::SetFontList(GetValidFontList(font_list));
117 }
118
93 bool RenderTextMac::MultilineSupported() const { 119 bool RenderTextMac::MultilineSupported() const {
94 return false; 120 return false;
95 } 121 }
96 122
97 const base::string16& RenderTextMac::GetDisplayText() { 123 const base::string16& RenderTextMac::GetDisplayText() {
98 return text_elided() ? display_text() : layout_text(); 124 return text_elided() ? display_text() : layout_text();
99 } 125 }
100 126
101 Size RenderTextMac::GetStringSize() { 127 Size RenderTextMac::GetStringSize() {
102 SizeF size_f = GetStringSizeF(); 128 SizeF size_f = GetStringSizeF();
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 *baseline = ascent; 302 *baseline = ascent;
277 return SizeF(width, std::max(ascent + descent + leading, 303 return SizeF(width, std::max(ascent + descent + leading,
278 static_cast<CGFloat>(min_line_height()))); 304 static_cast<CGFloat>(min_line_height())));
279 } 305 }
280 306
281 base::ScopedCFTypeRef<CTLineRef> RenderTextMac::EnsureLayoutInternal( 307 base::ScopedCFTypeRef<CTLineRef> RenderTextMac::EnsureLayoutInternal(
282 const base::string16& text, 308 const base::string16& text,
283 base::ScopedCFTypeRef<CFMutableArrayRef>* attributes_owner) { 309 base::ScopedCFTypeRef<CFMutableArrayRef>* attributes_owner) {
284 CTFontRef ct_font = 310 CTFontRef ct_font =
285 base::mac::NSToCFCast(font_list().GetPrimaryFont().GetNativeFont()); 311 base::mac::NSToCFCast(font_list().GetPrimaryFont().GetNativeFont());
312 DCHECK(ct_font);
286 313
287 const void* keys[] = {kCTFontAttributeName}; 314 const void* keys[] = {kCTFontAttributeName};
288 const void* values[] = {ct_font}; 315 const void* values[] = {ct_font};
289 base::ScopedCFTypeRef<CFDictionaryRef> attributes( 316 base::ScopedCFTypeRef<CFDictionaryRef> attributes(
290 CFDictionaryCreate(NULL, keys, values, arraysize(keys), NULL, 317 CFDictionaryCreate(NULL, keys, values, arraysize(keys), NULL,
291 &kCFTypeDictionaryValueCallBacks)); 318 &kCFTypeDictionaryValueCallBacks));
292 319
293 base::ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); 320 base::ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text));
294 base::ScopedCFTypeRef<CFAttributedStringRef> attr_text( 321 base::ScopedCFTypeRef<CFAttributedStringRef> attr_text(
295 CFAttributedStringCreate(NULL, cf_text, attributes)); 322 CFAttributedStringCreate(NULL, cf_text, attributes));
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 // TODO(karandeepb): This is not invoked on any codepath currently. Style the 485 // TODO(karandeepb): This is not invoked on any codepath currently. Style the
459 // returned text if need be. 486 // returned text if need be.
460 if (obscured()) 487 if (obscured())
461 return false; 488 return false;
462 489
463 decorated_text->text = GetTextFromRange(range); 490 decorated_text->text = GetTextFromRange(range);
464 return true; 491 return true;
465 } 492 }
466 493
467 } // namespace gfx 494 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_mac.h ('k') | ui/gfx/render_text_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698