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

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

Issue 2959913002: PlatformFontMac: Use a default NSFont when an invalid font is passed. (Closed)
Patch Set: Use a default font Created 3 years, 5 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_mac.h" 5 #include "ui/gfx/platform_font_mac.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include <Cocoa/Cocoa.h> 9 #include <Cocoa/Cocoa.h>
10 10
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 int GetFontStyleFromNSFont(NSFont* font) { 80 int GetFontStyleFromNSFont(NSFont* font) {
81 int font_style = Font::NORMAL; 81 int font_style = Font::NORMAL;
82 NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits]; 82 NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits];
83 if (traits & NSFontItalicTrait) 83 if (traits & NSFontItalicTrait)
84 font_style |= Font::ITALIC; 84 font_style |= Font::ITALIC;
85 return font_style; 85 return font_style;
86 } 86 }
87 87
88 // Returns the Font weight for |font|. 88 // Returns the Font weight for |font|.
89 Font::Weight GetFontWeightFromNSFont(NSFont* font) { 89 Font::Weight GetFontWeightFromNSFont(NSFont* font) {
90 if (!font) 90 DCHECK(font);
91 return Font::Weight::INVALID;
92 91
93 // Map CoreText weights in a manner similar to ct_weight_to_fontstyle() from 92 // Map CoreText weights in a manner similar to ct_weight_to_fontstyle() from
94 // SkFontHost_mac.cpp, but adjust for MEDIUM so that the San Francisco's 93 // SkFontHost_mac.cpp, but adjust for MEDIUM so that the San Francisco's
95 // custom MEDIUM weight can be picked out. San Francisco has weights: 94 // custom MEDIUM weight can be picked out. San Francisco has weights:
96 // [0.23, 0.23, 0.3, 0.4, 0.56, 0.62, 0.62, ...] (no thin weights). 95 // [0.23, 0.23, 0.3, 0.4, 0.56, 0.62, 0.62, ...] (no thin weights).
97 // See PlatformFontMacTest.FontWeightAPIConsistency for details. 96 // See PlatformFontMacTest.FontWeightAPIConsistency for details.
98 // Note that the table Skia uses is also determined by experiment. 97 // Note that the table Skia uses is also determined by experiment.
99 constexpr struct { 98 constexpr struct {
100 CGFloat ct_weight; 99 CGFloat ct_weight;
101 Font::Weight gfx_weight; 100 Font::Weight gfx_weight;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 // Make one fallback attempt by looking up via font name rather than font 166 // Make one fallback attempt by looking up via font name rather than font
168 // family name. 167 // family name.
169 attrs = @{ 168 attrs = @{
170 NSFontNameAttribute : base::SysUTF8ToNSString(font_name), 169 NSFontNameAttribute : base::SysUTF8ToNSString(font_name),
171 NSFontTraitsAttribute : traits 170 NSFontTraitsAttribute : traits
172 }; 171 };
173 descriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:attrs]; 172 descriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:attrs];
174 return [NSFont fontWithDescriptor:descriptor size:font_size]; 173 return [NSFont fontWithDescriptor:descriptor size:font_size];
175 } 174 }
176 175
176 // Returns |font| or a default font if |font| is nil.
177 NSFont* ValidateFont(NSFont* font) {
178 return font ? font : [NSFont systemFontOfSize:[NSFont systemFontSize]];
179 }
180
177 } // namespace 181 } // namespace
178 182
179 //////////////////////////////////////////////////////////////////////////////// 183 ////////////////////////////////////////////////////////////////////////////////
180 // PlatformFontMac, public: 184 // PlatformFontMac, public:
181 185
182 PlatformFontMac::PlatformFontMac() 186 PlatformFontMac::PlatformFontMac()
183 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) { 187 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) {
184 } 188 }
185 189
186 PlatformFontMac::PlatformFontMac(NativeFont native_font) 190 PlatformFontMac::PlatformFontMac(NativeFont native_font)
187 : PlatformFontMac(native_font, 191 : PlatformFontMac(native_font,
188 base::SysNSStringToUTF8([native_font familyName]), 192 base::SysNSStringToUTF8([native_font familyName]),
189 [native_font pointSize], 193 [native_font pointSize],
190 GetFontStyleFromNSFont(native_font), 194 GetFontStyleFromNSFont(native_font),
191 GetFontWeightFromNSFont(native_font)) {} 195 GetFontWeightFromNSFont(native_font)) {
196 DCHECK(native_font); // Null should not be passed to this constructor.
197 }
192 198
193 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size) 199 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size)
194 : PlatformFontMac(font_name, 200 : PlatformFontMac(font_name,
195 font_size, 201 font_size,
196 Font::NORMAL, 202 Font::NORMAL,
197 Font::Weight::NORMAL) {} 203 Font::Weight::NORMAL) {}
198 204
199 //////////////////////////////////////////////////////////////////////////////// 205 ////////////////////////////////////////////////////////////////////////////////
200 // PlatformFontMac, PlatformFont implementation: 206 // PlatformFontMac, PlatformFont implementation:
201 207
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 255
250 int PlatformFontMac::GetBaseline() { 256 int PlatformFontMac::GetBaseline() {
251 return ascent_; 257 return ascent_;
252 } 258 }
253 259
254 int PlatformFontMac::GetCapHeight() { 260 int PlatformFontMac::GetCapHeight() {
255 return cap_height_; 261 return cap_height_;
256 } 262 }
257 263
258 int PlatformFontMac::GetExpectedTextWidth(int length) { 264 int PlatformFontMac::GetExpectedTextWidth(int length) {
259 if (!average_width_ && native_font_) { 265 if (!average_width_) {
260 // -[NSFont boundingRectForGlyph:] seems to always return the largest 266 // -[NSFont boundingRectForGlyph:] seems to always return the largest
261 // bounding rect that could be needed, which produces very wide expected 267 // bounding rect that could be needed, which produces very wide expected
262 // widths for strings. Instead, compute the actual width of a string 268 // widths for strings. Instead, compute the actual width of a string
263 // containing all the lowercase characters to find a reasonable guess at the 269 // containing all the lowercase characters to find a reasonable guess at the
264 // average. 270 // average.
265 base::scoped_nsobject<NSAttributedString> attr_string( 271 base::scoped_nsobject<NSAttributedString> attr_string(
266 [[NSAttributedString alloc] 272 [[NSAttributedString alloc]
267 initWithString:@"abcdefghijklmnopqrstuvwxyz" 273 initWithString:@"abcdefghijklmnopqrstuvwxyz"
268 attributes:@{NSFontAttributeName : native_font_.get()}]); 274 attributes:@{NSFontAttributeName : native_font_.get()}]);
269 average_width_ = [attr_string size].width / [attr_string length]; 275 average_width_ = [attr_string size].width / [attr_string length];
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 font_name, 318 font_name,
313 font_size, 319 font_size,
314 font_style, 320 font_style,
315 font_weight) {} 321 font_weight) {}
316 322
317 PlatformFontMac::PlatformFontMac(NativeFont font, 323 PlatformFontMac::PlatformFontMac(NativeFont font,
318 const std::string& font_name, 324 const std::string& font_name,
319 int font_size, 325 int font_size,
320 int font_style, 326 int font_style,
321 Font::Weight font_weight) 327 Font::Weight font_weight)
322 : native_font_([font retain]), 328 : native_font_([ValidateFont(font) retain]),
323 font_name_(font_name), 329 font_name_(font_name),
324 font_size_(font_size), 330 font_size_(font_size),
325 font_style_(font_style), 331 font_style_(font_style),
326 font_weight_(font_weight) { 332 font_weight_(font_weight) {
327 CalculateMetricsAndInitRenderParams(); 333 CalculateMetricsAndInitRenderParams();
328 } 334 }
329 335
330 PlatformFontMac::~PlatformFontMac() { 336 PlatformFontMac::~PlatformFontMac() {
331 } 337 }
332 338
333 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { 339 void PlatformFontMac::CalculateMetricsAndInitRenderParams() {
334 NSFont* font = native_font_.get(); 340 NSFont* font = native_font_.get();
335 if (!font) { 341 DCHECK(font);
336 // This object was constructed from a font name that doesn't correspond to
337 // an actual font. Don't waste time working out metrics.
338 height_ = 0;
339 ascent_ = 0;
340 cap_height_ = 0;
341 return;
342 }
343
344 ascent_ = ceil([font ascender]); 342 ascent_ = ceil([font ascender]);
345 cap_height_ = ceil([font capHeight]); 343 cap_height_ = ceil([font capHeight]);
346 344
347 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to 345 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to
348 // initialize |height_|. However, it has a silly rounding bug. Essentially, it 346 // initialize |height_|. However, it has a silly rounding bug. Essentially, it
349 // gives round(ascent) + round(descent). E.g. Helvetica Neue at size 16 gives 347 // gives round(ascent) + round(descent). E.g. Helvetica Neue at size 16 gives
350 // ascent=15.4634, descent=3.38208 -> 15 + 3 = 18. When the height should be 348 // ascent=15.4634, descent=3.38208 -> 15 + 3 = 18. When the height should be
351 // at least 19. According to the OpenType specification, these values should 349 // at least 19. According to the OpenType specification, these values should
352 // simply be added, so do that. Note this uses the already-rounded |ascent_| 350 // simply be added, so do that. Note this uses the already-rounded |ascent_|
353 // to ensure GetBaseline() + descender fits within GetHeight() during layout. 351 // to ensure GetBaseline() + descender fits within GetHeight() during layout.
(...skipping 10 matching lines...) Expand all
364 //////////////////////////////////////////////////////////////////////////////// 362 ////////////////////////////////////////////////////////////////////////////////
365 // PlatformFont, public: 363 // PlatformFont, public:
366 364
367 // static 365 // static
368 PlatformFont* PlatformFont::CreateDefault() { 366 PlatformFont* PlatformFont::CreateDefault() {
369 return new PlatformFontMac; 367 return new PlatformFontMac;
370 } 368 }
371 369
372 // static 370 // static
373 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { 371 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
374 return new PlatformFontMac(native_font); 372 return new PlatformFontMac(ValidateFont(native_font));
375 } 373 }
376 374
377 // static 375 // static
378 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 376 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
379 int font_size) { 377 int font_size) {
380 return new PlatformFontMac(font_name, font_size); 378 return new PlatformFontMac(font_name, font_size);
381 } 379 }
382 380
383 } // namespace gfx 381 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698