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