Chromium Code Reviews| 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_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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 | 211 |
| 212 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to | 212 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to |
| 213 // initialize |height_|. However, it has a silly rounding bug. Essentially, it | 213 // initialize |height_|. However, it has a silly rounding bug. Essentially, it |
| 214 // gives round(ascent) + round(descent). E.g. Helvetica Neue at size 16 gives | 214 // gives round(ascent) + round(descent). E.g. Helvetica Neue at size 16 gives |
| 215 // ascent=15.4634, descent=3.38208 -> 15 + 3 = 18. When the height should be | 215 // ascent=15.4634, descent=3.38208 -> 15 + 3 = 18. When the height should be |
| 216 // at least 19. According to the OpenType specification, these values should | 216 // at least 19. According to the OpenType specification, these values should |
| 217 // simply be added, so do that. Note this uses the already-rounded |ascent_| | 217 // simply be added, so do that. Note this uses the already-rounded |ascent_| |
| 218 // to ensure GetBaseline() + descender fits within GetHeight() during layout. | 218 // to ensure GetBaseline() + descender fits within GetHeight() during layout. |
| 219 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); | 219 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); |
| 220 | 220 |
| 221 average_width_ = | 221 // -[NSFont boundingRectForGlyph:] seems to always return the largest bounding |
| 222 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); | 222 // rect that could be needed, which produces very wide expected widths for |
| 223 // strings. Instead, compute the actual width of a string containing all the | |
| 224 // lowercase characters to find a reasonable guess at the average. | |
| 225 NSAttributedString* as = [[[NSAttributedString alloc] | |
|
tapted
2017/05/01 06:48:05
since this is a bit heavyweight, and barely anythi
Elly Fong-Jones
2017/05/02 14:41:23
Done.
| |
| 226 initWithString:@"abcdefghijklmnopqrstuvwxyz" | |
| 227 attributes:@{NSFontAttributeName : font}] autorelease]; | |
| 228 average_width_ = [as size].width / [as length]; | |
|
tapted
2017/05/01 06:48:05
rounding is occurring here, and GetExpectedTextWid
Elly Fong-Jones
2017/05/02 14:41:23
Done.
| |
| 223 | 229 |
| 224 FontRenderParamsQuery query; | 230 FontRenderParamsQuery query; |
| 225 query.families.push_back(font_name_); | 231 query.families.push_back(font_name_); |
| 226 query.pixel_size = font_size_; | 232 query.pixel_size = font_size_; |
| 227 query.style = font_style_; | 233 query.style = font_style_; |
| 228 query.weight = font_weight_; | 234 query.weight = font_weight_; |
| 229 render_params_ = gfx::GetFontRenderParams(query, NULL); | 235 render_params_ = gfx::GetFontRenderParams(query, NULL); |
| 230 } | 236 } |
| 231 | 237 |
| 232 //////////////////////////////////////////////////////////////////////////////// | 238 //////////////////////////////////////////////////////////////////////////////// |
| 233 // PlatformFont, public: | 239 // PlatformFont, public: |
| 234 | 240 |
| 235 // static | 241 // static |
| 236 PlatformFont* PlatformFont::CreateDefault() { | 242 PlatformFont* PlatformFont::CreateDefault() { |
| 237 return new PlatformFontMac; | 243 return new PlatformFontMac; |
| 238 } | 244 } |
| 239 | 245 |
| 240 // static | 246 // static |
| 241 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 247 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
| 242 return new PlatformFontMac(native_font); | 248 return new PlatformFontMac(native_font); |
| 243 } | 249 } |
| 244 | 250 |
| 245 // static | 251 // static |
| 246 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 252 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 247 int font_size) { | 253 int font_size) { |
| 248 return new PlatformFontMac(font_name, font_size); | 254 return new PlatformFontMac(font_name, font_size); |
| 249 } | 255 } |
| 250 | 256 |
| 251 } // namespace gfx | 257 } // namespace gfx |
| OLD | NEW |