Chromium Code Reviews| Index: ui/gfx/platform_font_mac.mm |
| diff --git a/ui/gfx/platform_font_mac.mm b/ui/gfx/platform_font_mac.mm |
| index 53781f8e2309e74b7c72fb319b85c430132c83aa..ec234d513916febb8624c96613e3a65f4577bbed 100644 |
| --- a/ui/gfx/platform_font_mac.mm |
| +++ b/ui/gfx/platform_font_mac.mm |
| @@ -133,7 +133,18 @@ int GetFontStyleFromNSFont(NSFont* font) { |
| } |
| int PlatformFontMac::GetExpectedTextWidth(int length) { |
| - return length * average_width_; |
| + if (!average_width_) { |
|
tapted
2017/05/03 00:43:35
I think we need a `&& native_font_` in this condit
Elly Fong-Jones
2017/05/03 18:06:07
Done.
|
| + // -[NSFont boundingRectForGlyph:] seems to always return the largest |
| + // bounding rect that could be needed, which produces very wide expected |
| + // widths for strings. Instead, compute the actual width of a string |
| + // containing all the lowercase characters to find a reasonable guess at the |
| + // average. |
| + base::scoped_nsobject<NSAttributedString> as([[NSAttributedString alloc] |
| + initWithString:@"abcdefghijklmnopqrstuvwxyz" |
| + attributes:@{NSFontAttributeName : native_font_.get()}]); |
| + average_width_ = [as size].width / [as length]; |
|
tapted
2017/05/03 00:43:35
nit: DCHECK(!!average_width_) ?
Elly Fong-Jones
2017/05/03 18:06:07
Done.
|
| + } |
| + return ceil(length * average_width_); |
| } |
| int PlatformFontMac::GetStyle() const { |
| @@ -202,7 +213,6 @@ int GetFontStyleFromNSFont(NSFont* font) { |
| height_ = 0; |
| ascent_ = 0; |
| cap_height_ = 0; |
| - average_width_ = 0; |
| return; |
| } |
| @@ -218,9 +228,6 @@ int GetFontStyleFromNSFont(NSFont* font) { |
| // to ensure GetBaseline() + descender fits within GetHeight() during layout. |
| height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); |
| - average_width_ = |
| - NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); |
| - |
| FontRenderParamsQuery query; |
| query.families.push_back(font_name_); |
| query.pixel_size = font_size_; |