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

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

Issue 2839873003: PlatformFontMac: better guess for average width (Closed)
Patch Set: add a bit of safety checking Created 3 years, 7 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/platform_font_mac.h ('k') | no next file » | 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/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
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_ && native_font_) {
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]
Alexei Svitkine (slow) 2017/05/03 19:24:30 Nit: |as| is too short a name and acronyms are dis
143 initWithString:@"abcdefghijklmnopqrstuvwxyz"
144 attributes:@{NSFontAttributeName : native_font_.get()}]);
145 average_width_ = [as size].width / [as length];
146 DCHECK_NE(0, average_width_);
147 }
148 return ceil(length * average_width_);
137 } 149 }
138 150
139 int PlatformFontMac::GetStyle() const { 151 int PlatformFontMac::GetStyle() const {
140 return font_style_; 152 return font_style_;
141 } 153 }
142 154
143 Font::Weight PlatformFontMac::GetWeight() const { 155 Font::Weight PlatformFontMac::GetWeight() const {
144 return font_weight_; 156 return font_weight_;
145 } 157 }
146 158
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } 207 }
196 208
197 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { 209 void PlatformFontMac::CalculateMetricsAndInitRenderParams() {
198 NSFont* font = native_font_.get(); 210 NSFont* font = native_font_.get();
199 if (!font) { 211 if (!font) {
200 // This object was constructed from a font name that doesn't correspond to 212 // This object was constructed from a font name that doesn't correspond to
201 // an actual font. Don't waste time working out metrics. 213 // an actual font. Don't waste time working out metrics.
202 height_ = 0; 214 height_ = 0;
203 ascent_ = 0; 215 ascent_ = 0;
204 cap_height_ = 0; 216 cap_height_ = 0;
205 average_width_ = 0;
206 return; 217 return;
207 } 218 }
208 219
209 ascent_ = ceil([font ascender]); 220 ascent_ = ceil([font ascender]);
210 cap_height_ = ceil([font capHeight]); 221 cap_height_ = ceil([font capHeight]);
211 222
212 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to 223 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to
213 // initialize |height_|. However, it has a silly rounding bug. Essentially, it 224 // 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 225 // 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 226 // 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 227 // 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_| 228 // simply be added, so do that. Note this uses the already-rounded |ascent_|
218 // to ensure GetBaseline() + descender fits within GetHeight() during layout. 229 // to ensure GetBaseline() + descender fits within GetHeight() during layout.
219 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); 230 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]);
220 231
221 average_width_ =
222 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
223
224 FontRenderParamsQuery query; 232 FontRenderParamsQuery query;
225 query.families.push_back(font_name_); 233 query.families.push_back(font_name_);
226 query.pixel_size = font_size_; 234 query.pixel_size = font_size_;
227 query.style = font_style_; 235 query.style = font_style_;
228 query.weight = font_weight_; 236 query.weight = font_weight_;
229 render_params_ = gfx::GetFontRenderParams(query, NULL); 237 render_params_ = gfx::GetFontRenderParams(query, NULL);
230 } 238 }
231 239
232 //////////////////////////////////////////////////////////////////////////////// 240 ////////////////////////////////////////////////////////////////////////////////
233 // PlatformFont, public: 241 // PlatformFont, public:
234 242
235 // static 243 // static
236 PlatformFont* PlatformFont::CreateDefault() { 244 PlatformFont* PlatformFont::CreateDefault() {
237 return new PlatformFontMac; 245 return new PlatformFontMac;
238 } 246 }
239 247
240 // static 248 // static
241 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { 249 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
242 return new PlatformFontMac(native_font); 250 return new PlatformFontMac(native_font);
243 } 251 }
244 252
245 // static 253 // static
246 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 254 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
247 int font_size) { 255 int font_size) {
248 return new PlatformFontMac(font_name, font_size); 256 return new PlatformFontMac(font_name, font_size);
249 } 257 }
250 258
251 } // namespace gfx 259 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/platform_font_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698