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/chrome_font.h" | 5 #include "app/gfx/chrome_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/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
11 | 11 |
| 12 namespace gfx { |
| 13 |
12 // static | 14 // static |
13 ChromeFont ChromeFont::CreateFont(const std::wstring& font_name, | 15 Font Font::CreateFont(const std::wstring& font_name, int font_size) { |
14 int font_size) { | 16 return Font(font_name, font_size, NORMAL); |
15 return ChromeFont(font_name, font_size, NORMAL); | |
16 } | 17 } |
17 | 18 |
18 ChromeFont::ChromeFont(const std::wstring& font_name, int font_size, int style) | 19 Font::Font(const std::wstring& font_name, int font_size, int style) |
19 : font_name_(font_name), | 20 : font_name_(font_name), |
20 font_size_(font_size), | 21 font_size_(font_size), |
21 style_(style) { | 22 style_(style) { |
22 calculateMetrics(); | 23 calculateMetrics(); |
23 } | 24 } |
24 | 25 |
25 ChromeFont::ChromeFont() | 26 Font::Font() |
26 : font_size_([NSFont systemFontSize]), | 27 : font_size_([NSFont systemFontSize]), |
27 style_(NORMAL) { | 28 style_(NORMAL) { |
28 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; | 29 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; |
29 font_name_ = base::SysNSStringToWide([system_font fontName]); | 30 font_name_ = base::SysNSStringToWide([system_font fontName]); |
30 calculateMetrics(); | 31 calculateMetrics(); |
31 } | 32 } |
32 | 33 |
33 void ChromeFont::calculateMetrics() { | 34 void Font::calculateMetrics() { |
34 NSFont* font = nativeFont(); | 35 NSFont* font = nativeFont(); |
35 height_ = [font xHeight]; | 36 height_ = [font xHeight]; |
36 ascent_ = [font ascender]; | 37 ascent_ = [font ascender]; |
37 avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; | 38 avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; |
38 } | 39 } |
39 | 40 |
40 ChromeFont ChromeFont::DeriveFont(int size_delta, int style) const { | 41 Font Font::DeriveFont(int size_delta, int style) const { |
41 return ChromeFont(font_name_, font_size_ + size_delta, style); | 42 return Font(font_name_, font_size_ + size_delta, style); |
42 } | 43 } |
43 | 44 |
44 int ChromeFont::height() const { | 45 int Font::height() const { |
45 return height_; | 46 return height_; |
46 } | 47 } |
47 | 48 |
48 int ChromeFont::baseline() const { | 49 int Font::baseline() const { |
49 return ascent_; | 50 return ascent_; |
50 } | 51 } |
51 | 52 |
52 int ChromeFont::ave_char_width() const { | 53 int Font::ave_char_width() const { |
53 return avg_width_; | 54 return avg_width_; |
54 } | 55 } |
55 | 56 |
56 int ChromeFont::GetStringWidth(const std::wstring& text) const { | 57 int Font::GetStringWidth(const std::wstring& text) const { |
57 NSFont* font = nativeFont(); | 58 NSFont* font = nativeFont(); |
58 NSString* ns_string = base::SysWideToNSString(text); | 59 NSString* ns_string = base::SysWideToNSString(text); |
59 NSDictionary* attributes = | 60 NSDictionary* attributes = |
60 [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; | 61 [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; |
61 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | 62 NSSize string_size = [ns_string sizeWithAttributes:attributes]; |
62 return string_size.width; | 63 return string_size.width; |
63 } | 64 } |
64 | 65 |
65 int ChromeFont::GetExpectedTextWidth(int length) const { | 66 int Font::GetExpectedTextWidth(int length) const { |
66 return length * avg_width_; | 67 return length * avg_width_; |
67 } | 68 } |
68 | 69 |
69 int ChromeFont::style() const { | 70 int Font::style() const { |
70 return style_; | 71 return style_; |
71 } | 72 } |
72 | 73 |
73 std::wstring ChromeFont::FontName() { | 74 std::wstring Font::FontName() { |
74 return font_name_; | 75 return font_name_; |
75 } | 76 } |
76 | 77 |
77 int ChromeFont::FontSize() { | 78 int Font::FontSize() { |
78 return font_size_; | 79 return font_size_; |
79 } | 80 } |
80 | 81 |
81 NativeFont ChromeFont::nativeFont() const { | 82 NativeFont Font::nativeFont() const { |
82 // TODO(pinkerton): apply |style_| to font. | 83 // TODO(pinkerton): apply |style_| to font. |
83 // We could cache this, but then we'd have to conditionally change the | 84 // We could cache this, but then we'd have to conditionally change the |
84 // dtor just for MacOS. Not sure if we want to/need to do that. | 85 // dtor just for MacOS. Not sure if we want to/need to do that. |
85 return [NSFont fontWithName:base::SysWideToNSString(font_name_) | 86 return [NSFont fontWithName:base::SysWideToNSString(font_name_) |
86 size:font_size_]; | 87 size:font_size_]; |
87 } | 88 } |
| 89 |
| 90 } // namespace gfx |
OLD | NEW |