OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "gfx/font.h" | 5 #include "gfx/platform_font_mac.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/scoped_nsobject.h" |
11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
12 #include "gfx/canvas_skia.h" | 12 #include "gfx/canvas_skia.h" |
| 13 #include "gfx/font.h" |
13 | 14 |
14 namespace gfx { | 15 namespace gfx { |
15 | 16 |
16 // static | 17 //////////////////////////////////////////////////////////////////////////////// |
17 Font Font::CreateFont(const std::wstring& font_name, int font_size) { | 18 // PlatformFontMac, public: |
18 return Font(font_name, font_size, NORMAL); | 19 |
| 20 PlatformFontMac::PlatformFontMac() { |
| 21 font_size_ = [NSFont systemFontSize]; |
| 22 style_ = gfx::Font::NORMAL; |
| 23 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; |
| 24 font_name_ = base::SysNSStringToWide([system_font fontName]); |
| 25 CalculateMetrics(); |
19 } | 26 } |
20 | 27 |
21 Font::Font(const std::wstring& font_name, int font_size, int style) | 28 PlatformFontMac::PlatformFontMac(const Font& other) { |
22 : font_name_(font_name), | |
23 font_size_(font_size), | |
24 style_(style) { | |
25 calculateMetrics(); | |
26 } | 29 } |
27 | 30 |
28 Font::Font() | 31 PlatformFontMac::PlatformFontMac(NativeFont native_font) { |
29 : font_size_([NSFont systemFontSize]), | |
30 style_(NORMAL) { | |
31 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; | |
32 font_name_ = base::SysNSStringToWide([system_font fontName]); | |
33 calculateMetrics(); | |
34 } | 32 } |
35 | 33 |
36 void Font::calculateMetrics() { | 34 PlatformFontMac::PlatformFontMac(const std::wstring& font_name, |
37 NSFont* font = nativeFont(); | 35 int font_size) { |
38 scoped_nsobject<NSLayoutManager> layout_manager( | 36 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL); |
39 [[NSLayoutManager alloc] init]); | |
40 height_ = [layout_manager defaultLineHeightForFont:font]; | |
41 ascent_ = [font ascender]; | |
42 avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; | |
43 } | 37 } |
44 | 38 |
45 Font Font::DeriveFont(int size_delta, int style) const { | 39 //////////////////////////////////////////////////////////////////////////////// |
46 return Font(font_name_, font_size_ + size_delta, style); | 40 // PlatformFontMac, PlatformFont implementation: |
| 41 |
| 42 Font PlatformFontMac::DeriveFont(int size_delta, int style) const { |
| 43 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style)); |
47 } | 44 } |
48 | 45 |
49 int Font::height() const { | 46 int PlatformFontMac::GetHeight() const { |
50 return height_; | 47 return height_; |
51 } | 48 } |
52 | 49 |
53 int Font::baseline() const { | 50 int PlatformFontMac::GetBaseline() const { |
54 return ascent_; | 51 return ascent_; |
55 } | 52 } |
56 | 53 |
57 int Font::ave_char_width() const { | 54 int PlatformFontMac::GetAverageCharacterWidth() const { |
58 return avg_width_; | 55 return average_width_; |
59 } | 56 } |
60 | 57 |
61 int Font::GetStringWidth(const std::wstring& text) const { | 58 int PlatformFontMac::GetStringWidth(const std::wstring& text) const { |
62 int width = 0, height = 0; | 59 int width = 0, height = 0; |
63 CanvasSkia::SizeStringInt(text, *this, &width, &height, | 60 CanvasSkia::SizeStringInt(text, Font(const_cast<PlatformFontMac*>(this)), |
64 gfx::Canvas::NO_ELLIPSIS); | 61 &width, &height, gfx::Canvas::NO_ELLIPSIS); |
65 return width; | 62 return width; |
66 } | 63 } |
67 | 64 |
68 int Font::GetExpectedTextWidth(int length) const { | 65 int PlatformFontMac::GetExpectedTextWidth(int length) const { |
69 return length * avg_width_; | 66 return length * average_width_; |
70 } | 67 } |
71 | 68 |
72 int Font::style() const { | 69 int PlatformFontMac::GetStyle() const { |
73 return style_; | 70 return style_; |
74 } | 71 } |
75 | 72 |
76 const std::wstring& Font::FontName() const { | 73 const std::wstring& PlatformFontMac::GetFontName() const { |
77 return font_name_; | 74 return font_name_; |
78 } | 75 } |
79 | 76 |
80 int Font::FontSize() { | 77 int PlatformFontMac::GetFontSize() const { |
81 return font_size_; | 78 return font_size_; |
82 } | 79 } |
83 | 80 |
84 NativeFont Font::nativeFont() const { | 81 NativeFont PlatformFontMac::GetNativeFont() const { |
85 // TODO(pinkerton): apply |style_| to font. http://crbug.com/34667 | 82 // TODO(pinkerton): apply |style_| to font. http://crbug.com/34667 |
86 // We could cache this, but then we'd have to conditionally change the | 83 // We could cache this, but then we'd have to conditionally change the |
87 // dtor just for MacOS. Not sure if we want to/need to do that. | 84 // dtor just for MacOS. Not sure if we want to/need to do that. |
88 return [NSFont fontWithName:base::SysWideToNSString(font_name_) | 85 return [NSFont fontWithName:base::SysWideToNSString(font_name_) |
89 size:font_size_]; | 86 size:font_size_]; |
90 } | 87 } |
91 | 88 |
| 89 //////////////////////////////////////////////////////////////////////////////// |
| 90 // PlatformFontMac, private: |
| 91 |
| 92 PlatformFontMac::PlatformFontMac(const std::wstring& font_name, |
| 93 int font_size, |
| 94 int style) { |
| 95 InitWithNameSizeAndStyle(font_name, font_size, style); |
| 96 } |
| 97 |
| 98 void PlatformFontMac::InitWithNameSizeAndStyle(const std::wstring& font_name, |
| 99 int font_size, |
| 100 int style) { |
| 101 font_name_ = font_name; |
| 102 font_size_ = font_size; |
| 103 style_ = style; |
| 104 CalculateMetrics(); |
| 105 } |
| 106 |
| 107 void PlatformFontMac::CalculateMetrics() { |
| 108 NSFont* font = GetNativeFont(); |
| 109 scoped_nsobject<NSLayoutManager> layout_manager( |
| 110 [[NSLayoutManager alloc] init]); |
| 111 height_ = [layout_manager defaultLineHeightForFont:font]; |
| 112 ascent_ = [font ascender]; |
| 113 average_width_ = |
| 114 [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; |
| 115 } |
| 116 |
| 117 //////////////////////////////////////////////////////////////////////////////// |
| 118 // PlatformFont, public: |
| 119 |
| 120 // static |
| 121 PlatformFont* PlatformFont::CreateDefault() { |
| 122 return new PlatformFontMac; |
| 123 } |
| 124 |
| 125 // static |
| 126 PlatformFont* PlatformFont::CreateFromFont(const Font& other) { |
| 127 return new PlatformFontMac(other); |
| 128 } |
| 129 |
| 130 // static |
| 131 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
| 132 return new PlatformFontMac(native_font); |
| 133 } |
| 134 |
| 135 // static |
| 136 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::wstring& font_name, |
| 137 int font_size) { |
| 138 return new PlatformFontMac(font_name, font_size); |
| 139 } |
| 140 |
92 } // namespace gfx | 141 } // namespace gfx |
| 142 |
OLD | NEW |