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

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

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