OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/gfx/font.h" | 5 #include "app/gfx/font.h" |
6 | 6 |
7 #include <Cocoa/Cocoa.h> | 7 #include <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/scoped_nsobject.h" |
10 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
11 | 12 |
12 namespace gfx { | 13 namespace gfx { |
13 | 14 |
14 // static | 15 // static |
15 Font Font::CreateFont(const std::wstring& font_name, int font_size) { | 16 Font Font::CreateFont(const std::wstring& font_name, int font_size) { |
16 return Font(font_name, font_size, NORMAL); | 17 return Font(font_name, font_size, NORMAL); |
17 } | 18 } |
18 | 19 |
19 Font::Font(const std::wstring& font_name, int font_size, int style) | 20 Font::Font(const std::wstring& font_name, int font_size, int style) |
20 : font_name_(font_name), | 21 : font_name_(font_name), |
21 font_size_(font_size), | 22 font_size_(font_size), |
22 style_(style) { | 23 style_(style) { |
23 calculateMetrics(); | 24 calculateMetrics(); |
24 } | 25 } |
25 | 26 |
26 Font::Font() | 27 Font::Font() |
27 : font_size_([NSFont systemFontSize]), | 28 : font_size_([NSFont systemFontSize]), |
28 style_(NORMAL) { | 29 style_(NORMAL) { |
29 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; | 30 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; |
30 font_name_ = base::SysNSStringToWide([system_font fontName]); | 31 font_name_ = base::SysNSStringToWide([system_font fontName]); |
31 calculateMetrics(); | 32 calculateMetrics(); |
32 } | 33 } |
33 | 34 |
34 void Font::calculateMetrics() { | 35 void Font::calculateMetrics() { |
35 NSFont* font = nativeFont(); | 36 NSFont* font = nativeFont(); |
36 // TODO(akalin): This is the wrong height to use! Use either the height | 37 scoped_nsobject<NSLayoutManager> layout_manager( |
37 // of the bounding rect for the font or ascender - descender; this needs | 38 [[NSLayoutManager alloc] init]); |
38 // further investigation. Width may be wrong, too. | 39 height_ = [layout_manager defaultLineHeightForFont:font]; |
39 height_ = [font xHeight]; | |
40 ascent_ = [font ascender]; | 40 ascent_ = [font ascender]; |
41 avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; | 41 avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; |
42 } | 42 } |
43 | 43 |
44 Font Font::DeriveFont(int size_delta, int style) const { | 44 Font Font::DeriveFont(int size_delta, int style) const { |
45 return Font(font_name_, font_size_ + size_delta, style); | 45 return Font(font_name_, font_size_ + size_delta, style); |
46 } | 46 } |
47 | 47 |
48 int Font::height() const { | 48 int Font::height() const { |
49 return height_; | 49 return height_; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 84 |
85 NativeFont Font::nativeFont() const { | 85 NativeFont Font::nativeFont() const { |
86 // TODO(pinkerton): apply |style_| to font. | 86 // TODO(pinkerton): apply |style_| to font. |
87 // We could cache this, but then we'd have to conditionally change the | 87 // We could cache this, but then we'd have to conditionally change the |
88 // dtor just for MacOS. Not sure if we want to/need to do that. | 88 // dtor just for MacOS. Not sure if we want to/need to do that. |
89 return [NSFont fontWithName:base::SysWideToNSString(font_name_) | 89 return [NSFont fontWithName:base::SysWideToNSString(font_name_) |
90 size:font_size_]; | 90 size:font_size_]; |
91 } | 91 } |
92 | 92 |
93 } // namespace gfx | 93 } // namespace gfx |
OLD | NEW |