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

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

Issue 2734333005: RenderTextMac: Fix crash when passed an invalid font. (Closed)
Patch Set: Nits. 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 // Checks whether |font_list| is valid. If it isn't, returns the default font
70 // list (or a font list derived from it). The returned font list will have a
71 // valid primary native font.
72 gfx::FontList GetValidFontList(const gfx::FontList& font_list) {
73 if (HasValidPrimaryNativeFont(font_list))
74 return font_list;
75
76 const gfx::FontList default_font_list;
77 const int size_delta =
78 font_list.GetFontSize() - default_font_list.GetFontSize();
79 const gfx::FontList derived_font_list = default_font_list.Derive(
80 size_delta, font_list.GetFontStyle(), font_list.GetFontWeight());
81 if (HasValidPrimaryNativeFont(derived_font_list))
82 return derived_font_list;
83
84 DCHECK(HasValidPrimaryNativeFont(default_font_list));
85 return default_font_list;
86 }
87
64 } // namespace 88 } // namespace
65 89
66 namespace gfx { 90 namespace gfx {
67 91
68 namespace internal { 92 namespace internal {
69 93
70 // Note: this is only used by RenderTextHarfbuzz. 94 // Note: this is only used by RenderTextHarfbuzz.
71 sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font, 95 sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font,
72 bool italic, 96 bool italic,
73 Font::Weight weight) { 97 Font::Weight weight) {
74 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL; 98 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL;
75 Font font_with_style = font.Derive(0, style, weight); 99 Font font_with_style = font.Derive(0, style, weight);
76 if (!font_with_style.GetNativeFont()) 100 if (!font_with_style.GetNativeFont())
77 return nullptr; 101 return nullptr;
78 102
79 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont( 103 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont(
80 base::mac::NSToCFCast(font_with_style.GetNativeFont()))); 104 base::mac::NSToCFCast(font_with_style.GetNativeFont())));
81 } 105 }
82 106
83 } // namespace internal 107 } // namespace internal
84 108
85 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} 109 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {}
86 110
87 RenderTextMac::~RenderTextMac() {} 111 RenderTextMac::~RenderTextMac() {}
88 112
89 std::unique_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const { 113 std::unique_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const {
90 return base::WrapUnique(new RenderTextMac); 114 return base::WrapUnique(new RenderTextMac);
91 } 115 }
92 116
117 void RenderTextMac::SetFontList(const FontList& font_list) {
118 // Ensure the font list used has a valid native font.
119 RenderText::SetFontList(GetValidFontList(font_list));
120 }
121
93 bool RenderTextMac::MultilineSupported() const { 122 bool RenderTextMac::MultilineSupported() const {
94 return false; 123 return false;
95 } 124 }
96 125
97 const base::string16& RenderTextMac::GetDisplayText() { 126 const base::string16& RenderTextMac::GetDisplayText() {
98 return text_elided() ? display_text() : layout_text(); 127 return text_elided() ? display_text() : layout_text();
99 } 128 }
100 129
101 Size RenderTextMac::GetStringSize() { 130 Size RenderTextMac::GetStringSize() {
102 SizeF size_f = GetStringSizeF(); 131 SizeF size_f = GetStringSizeF();
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 *baseline = ascent; 305 *baseline = ascent;
277 return SizeF(width, std::max(ascent + descent + leading, 306 return SizeF(width, std::max(ascent + descent + leading,
278 static_cast<CGFloat>(min_line_height()))); 307 static_cast<CGFloat>(min_line_height())));
279 } 308 }
280 309
281 base::ScopedCFTypeRef<CTLineRef> RenderTextMac::EnsureLayoutInternal( 310 base::ScopedCFTypeRef<CTLineRef> RenderTextMac::EnsureLayoutInternal(
282 const base::string16& text, 311 const base::string16& text,
283 base::ScopedCFTypeRef<CFMutableArrayRef>* attributes_owner) { 312 base::ScopedCFTypeRef<CFMutableArrayRef>* attributes_owner) {
284 CTFontRef ct_font = 313 CTFontRef ct_font =
285 base::mac::NSToCFCast(font_list().GetPrimaryFont().GetNativeFont()); 314 base::mac::NSToCFCast(font_list().GetPrimaryFont().GetNativeFont());
315 DCHECK(ct_font);
286 316
287 const void* keys[] = {kCTFontAttributeName}; 317 const void* keys[] = {kCTFontAttributeName};
288 const void* values[] = {ct_font}; 318 const void* values[] = {ct_font};
289 base::ScopedCFTypeRef<CFDictionaryRef> attributes( 319 base::ScopedCFTypeRef<CFDictionaryRef> attributes(
290 CFDictionaryCreate(NULL, keys, values, arraysize(keys), NULL, 320 CFDictionaryCreate(NULL, keys, values, arraysize(keys), NULL,
291 &kCFTypeDictionaryValueCallBacks)); 321 &kCFTypeDictionaryValueCallBacks));
292 322
293 base::ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); 323 base::ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text));
294 base::ScopedCFTypeRef<CFAttributedStringRef> attr_text( 324 base::ScopedCFTypeRef<CFAttributedStringRef> attr_text(
295 CFAttributedStringCreate(NULL, cf_text, attributes)); 325 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 488 // TODO(karandeepb): This is not invoked on any codepath currently. Style the
459 // returned text if need be. 489 // returned text if need be.
460 if (obscured()) 490 if (obscured())
461 return false; 491 return false;
462 492
463 decorated_text->text = GetTextFromRange(range); 493 decorated_text->text = GetTextFromRange(range);
464 return true; 494 return true;
465 } 495 }
466 496
467 } // namespace gfx 497 } // 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