| 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 <Cocoa/Cocoa.h> | 7 #include <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 if (!font) { | 159 if (!font) { |
| 160 // This object was constructed from a font name that doesn't correspond to | 160 // This object was constructed from a font name that doesn't correspond to |
| 161 // an actual font. Don't waste time working out metrics. | 161 // an actual font. Don't waste time working out metrics. |
| 162 height_ = 0; | 162 height_ = 0; |
| 163 ascent_ = 0; | 163 ascent_ = 0; |
| 164 cap_height_ = 0; | 164 cap_height_ = 0; |
| 165 average_width_ = 0; | 165 average_width_ = 0; |
| 166 return; | 166 return; |
| 167 } | 167 } |
| 168 | 168 |
| 169 base::scoped_nsobject<NSLayoutManager> layout_manager( | 169 ascent_ = ceil([font ascender]); |
| 170 [[NSLayoutManager alloc] init]); | 170 cap_height_ = ceil([font capHeight]); |
| 171 height_ = SkScalarCeilToInt([layout_manager defaultLineHeightForFont:font]); | 171 |
| 172 ascent_ = SkScalarCeilToInt([font ascender]); | 172 // PlatformFontMac once used -[NSLayoutManager defaultLineHeightForFont:] to |
| 173 cap_height_ = SkScalarCeilToInt([font capHeight]); | 173 // initialize |height_|. However, it has a silly rounding bug. Essentially, it |
| 174 // gives round(ascent) + round(descent). E.g. Helvetica Neue at size 16 gives |
| 175 // ascent=15.4634, descent=3.38208 -> 15 + 3 = 18. When the height should be |
| 176 // at least 19. According to the OpenType specification, these values should |
| 177 // simply be added, so do that. Note this uses the already-rounded |ascent_| |
| 178 // to ensure GetBaseline() + descender fits within GetHeight() during layout. |
| 179 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); |
| 180 |
| 174 average_width_ = | 181 average_width_ = |
| 175 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); | 182 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); |
| 176 | 183 |
| 177 FontRenderParamsQuery query; | 184 FontRenderParamsQuery query; |
| 178 query.families.push_back(font_name_); | 185 query.families.push_back(font_name_); |
| 179 query.pixel_size = font_size_; | 186 query.pixel_size = font_size_; |
| 180 query.style = font_style_; | 187 query.style = font_style_; |
| 181 render_params_ = gfx::GetFontRenderParams(query, NULL); | 188 render_params_ = gfx::GetFontRenderParams(query, NULL); |
| 182 } | 189 } |
| 183 | 190 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 194 return new PlatformFontMac(native_font); | 201 return new PlatformFontMac(native_font); |
| 195 } | 202 } |
| 196 | 203 |
| 197 // static | 204 // static |
| 198 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 205 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 199 int font_size) { | 206 int font_size) { |
| 200 return new PlatformFontMac(font_name, font_size); | 207 return new PlatformFontMac(font_name, font_size); |
| 201 } | 208 } |
| 202 | 209 |
| 203 } // namespace gfx | 210 } // namespace gfx |
| OLD | NEW |