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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 | 126 |
| 127 int PlatformFontMac::GetBaseline() { | 127 int PlatformFontMac::GetBaseline() { |
| 128 return ascent_; | 128 return ascent_; |
| 129 } | 129 } |
| 130 | 130 |
| 131 int PlatformFontMac::GetCapHeight() { | 131 int PlatformFontMac::GetCapHeight() { |
| 132 return cap_height_; | 132 return cap_height_; |
| 133 } | 133 } |
| 134 | 134 |
| 135 int PlatformFontMac::GetExpectedTextWidth(int length) { | 135 int PlatformFontMac::GetExpectedTextWidth(int length) { |
| 136 return length * average_width_; | 136 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.
| |
| 137 // -[NSFont boundingRectForGlyph:] seems to always return the largest | |
| 138 // bounding rect that could be needed, which produces very wide expected | |
| 139 // widths for strings. Instead, compute the actual width of a string | |
| 140 // containing all the lowercase characters to find a reasonable guess at the | |
| 141 // average. | |
| 142 base::scoped_nsobject<NSAttributedString> as([[NSAttributedString alloc] | |
| 143 initWithString:@"abcdefghijklmnopqrstuvwxyz" | |
| 144 attributes:@{NSFontAttributeName : native_font_.get()}]); | |
| 145 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.
| |
| 146 } | |
| 147 return ceil(length * average_width_); | |
| 137 } | 148 } |
| 138 | 149 |
| 139 int PlatformFontMac::GetStyle() const { | 150 int PlatformFontMac::GetStyle() const { |
| 140 return font_style_; | 151 return font_style_; |
| 141 } | 152 } |
| 142 | 153 |
| 143 Font::Weight PlatformFontMac::GetWeight() const { | 154 Font::Weight PlatformFontMac::GetWeight() const { |
| 144 return font_weight_; | 155 return font_weight_; |
| 145 } | 156 } |
| 146 | 157 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 } | 206 } |
| 196 | 207 |
| 197 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { | 208 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { |
| 198 NSFont* font = native_font_.get(); | 209 NSFont* font = native_font_.get(); |
| 199 if (!font) { | 210 if (!font) { |
| 200 // This object was constructed from a font name that doesn't correspond to | 211 // This object was constructed from a font name that doesn't correspond to |
| 201 // an actual font. Don't waste time working out metrics. | 212 // an actual font. Don't waste time working out metrics. |
| 202 height_ = 0; | 213 height_ = 0; |
| 203 ascent_ = 0; | 214 ascent_ = 0; |
| 204 cap_height_ = 0; | 215 cap_height_ = 0; |
| 205 average_width_ = 0; | |
| 206 return; | 216 return; |
| 207 } | 217 } |
| 208 | 218 |
| 209 ascent_ = ceil([font ascender]); | 219 ascent_ = ceil([font ascender]); |
| 210 cap_height_ = ceil([font capHeight]); | 220 cap_height_ = ceil([font capHeight]); |
| 211 | 221 |
| 212 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to | 222 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to |
| 213 // initialize |height_|. However, it has a silly rounding bug. Essentially, it | 223 // 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 | 224 // 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 | 225 // 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 | 226 // 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_| | 227 // simply be added, so do that. Note this uses the already-rounded |ascent_| |
| 218 // to ensure GetBaseline() + descender fits within GetHeight() during layout. | 228 // to ensure GetBaseline() + descender fits within GetHeight() during layout. |
| 219 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); | 229 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); |
| 220 | 230 |
| 221 average_width_ = | |
| 222 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); | |
| 223 | |
| 224 FontRenderParamsQuery query; | 231 FontRenderParamsQuery query; |
| 225 query.families.push_back(font_name_); | 232 query.families.push_back(font_name_); |
| 226 query.pixel_size = font_size_; | 233 query.pixel_size = font_size_; |
| 227 query.style = font_style_; | 234 query.style = font_style_; |
| 228 query.weight = font_weight_; | 235 query.weight = font_weight_; |
| 229 render_params_ = gfx::GetFontRenderParams(query, NULL); | 236 render_params_ = gfx::GetFontRenderParams(query, NULL); |
| 230 } | 237 } |
| 231 | 238 |
| 232 //////////////////////////////////////////////////////////////////////////////// | 239 //////////////////////////////////////////////////////////////////////////////// |
| 233 // PlatformFont, public: | 240 // PlatformFont, public: |
| 234 | 241 |
| 235 // static | 242 // static |
| 236 PlatformFont* PlatformFont::CreateDefault() { | 243 PlatformFont* PlatformFont::CreateDefault() { |
| 237 return new PlatformFontMac; | 244 return new PlatformFontMac; |
| 238 } | 245 } |
| 239 | 246 |
| 240 // static | 247 // static |
| 241 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 248 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
| 242 return new PlatformFontMac(native_font); | 249 return new PlatformFontMac(native_font); |
| 243 } | 250 } |
| 244 | 251 |
| 245 // static | 252 // static |
| 246 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 253 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 247 int font_size) { | 254 int font_size) { |
| 248 return new PlatformFontMac(font_name, font_size); | 255 return new PlatformFontMac(font_name, font_size); |
| 249 } | 256 } |
| 250 | 257 |
| 251 } // namespace gfx | 258 } // namespace gfx |
| OLD | NEW |