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 <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include <Cocoa/Cocoa.h> | 9 #include <Cocoa/Cocoa.h> |
10 | 10 |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
15 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
16 #include "ui/gfx/font_render_params.h" | 16 #include "ui/gfx/font_render_params.h" |
17 | 17 |
18 namespace gfx { | 18 namespace gfx { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Returns an autoreleased NSFont created with the passed-in specifications. | 22 // Returns an autoreleased NSFont created with the passed-in specifications. |
23 NSFont* NSFontWithSpec(const std::string& font_name, | 23 NSFont* NSFontWithSpec(const std::string& font_name, |
24 int font_size, | 24 int font_size, |
25 int font_style) { | 25 int font_style, |
| 26 Font::Weight font_weight) { |
26 NSFontSymbolicTraits trait_bits = 0; | 27 NSFontSymbolicTraits trait_bits = 0; |
27 if (font_style & Font::BOLD) | 28 // TODO(mboc): Add support for other weights as well. |
| 29 if (font_weight >= Font::Weight::BOLD) |
28 trait_bits |= NSFontBoldTrait; | 30 trait_bits |= NSFontBoldTrait; |
29 if (font_style & Font::ITALIC) | 31 if (font_style & Font::ITALIC) |
30 trait_bits |= NSFontItalicTrait; | 32 trait_bits |= NSFontItalicTrait; |
31 // The Mac doesn't support underline as a font trait, so just drop it. | 33 // The Mac doesn't support underline as a font trait, so just drop it. |
32 // (Underlines must be added as an attribute on an NSAttributedString.) | 34 // (Underlines must be added as an attribute on an NSAttributedString.) |
33 NSDictionary* traits = @{ NSFontSymbolicTrait : @(trait_bits) }; | 35 NSDictionary* traits = @{ NSFontSymbolicTrait : @(trait_bits) }; |
34 | 36 |
35 NSDictionary* attrs = @{ | 37 NSDictionary* attrs = @{ |
36 NSFontFamilyAttribute : base::SysUTF8ToNSString(font_name), | 38 NSFontFamilyAttribute : base::SysUTF8ToNSString(font_name), |
37 NSFontTraitsAttribute : traits | 39 NSFontTraitsAttribute : traits |
(...skipping 20 matching lines...) Expand all Loading... |
58 // PlatformFontMac, public: | 60 // PlatformFontMac, public: |
59 | 61 |
60 PlatformFontMac::PlatformFontMac() | 62 PlatformFontMac::PlatformFontMac() |
61 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) { | 63 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) { |
62 } | 64 } |
63 | 65 |
64 PlatformFontMac::PlatformFontMac(NativeFont native_font) | 66 PlatformFontMac::PlatformFontMac(NativeFont native_font) |
65 : native_font_([native_font retain]), | 67 : native_font_([native_font retain]), |
66 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), | 68 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), |
67 font_size_([native_font_ pointSize]), | 69 font_size_([native_font_ pointSize]), |
68 font_style_(Font::NORMAL) { | 70 font_style_(Font::NORMAL), |
| 71 font_weight_(Font::Weight::NORMAL) { |
69 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits]; | 72 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits]; |
70 if (traits & NSFontItalicTrait) | 73 if (traits & NSFontItalicTrait) |
71 font_style_ |= Font::ITALIC; | 74 font_style_ |= Font::ITALIC; |
72 if (traits & NSFontBoldTrait) | 75 if (traits & NSFontBoldTrait) |
73 font_style_ |= Font::BOLD; | 76 font_weight_ = Font::Weight::BOLD; |
74 | 77 |
75 CalculateMetricsAndInitRenderParams(); | 78 CalculateMetricsAndInitRenderParams(); |
76 } | 79 } |
77 | 80 |
78 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 81 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size) |
79 int font_size) | 82 : native_font_([NSFontWithSpec(font_name, |
80 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]), | 83 font_size, |
| 84 Font::NORMAL, |
| 85 Font::Weight::NORMAL) retain]), |
81 font_name_(font_name), | 86 font_name_(font_name), |
82 font_size_(font_size), | 87 font_size_(font_size), |
83 font_style_(Font::NORMAL) { | 88 font_style_(Font::NORMAL), |
| 89 font_weight_(Font::Weight::NORMAL) { |
84 CalculateMetricsAndInitRenderParams(); | 90 CalculateMetricsAndInitRenderParams(); |
85 } | 91 } |
86 | 92 |
87 //////////////////////////////////////////////////////////////////////////////// | 93 //////////////////////////////////////////////////////////////////////////////// |
88 // PlatformFontMac, PlatformFont implementation: | 94 // PlatformFontMac, PlatformFont implementation: |
89 | 95 |
90 Font PlatformFontMac::DeriveFont(int size_delta, int style) const { | 96 Font PlatformFontMac::DeriveFont(int size_delta, |
91 if (native_font_ && style == font_style_) { | 97 int style, |
| 98 Font::Weight weight) const { |
| 99 if (native_font_ && style == font_style_ && weight == font_weight_) { |
92 // System fonts have special attributes starting with 10.11. They should be | 100 // System fonts have special attributes starting with 10.11. They should be |
93 // requested using the same descriptor to preserve these attributes. | 101 // requested using the same descriptor to preserve these attributes. |
94 return Font(new PlatformFontMac( | 102 return Font(new PlatformFontMac([NSFont |
95 [NSFont fontWithDescriptor:[native_font_ fontDescriptor] | 103 fontWithDescriptor:[native_font_ fontDescriptor] |
96 size:font_size_ + size_delta])); | 104 size:font_size_ + size_delta])); |
97 } | 105 } |
98 | 106 |
99 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style)); | 107 return Font( |
| 108 new PlatformFontMac(font_name_, font_size_ + size_delta, style, weight)); |
100 } | 109 } |
101 | 110 |
102 int PlatformFontMac::GetHeight() { | 111 int PlatformFontMac::GetHeight() { |
103 return height_; | 112 return height_; |
104 } | 113 } |
105 | 114 |
106 int PlatformFontMac::GetBaseline() { | 115 int PlatformFontMac::GetBaseline() { |
107 return ascent_; | 116 return ascent_; |
108 } | 117 } |
109 | 118 |
110 int PlatformFontMac::GetCapHeight() { | 119 int PlatformFontMac::GetCapHeight() { |
111 return cap_height_; | 120 return cap_height_; |
112 } | 121 } |
113 | 122 |
114 int PlatformFontMac::GetExpectedTextWidth(int length) { | 123 int PlatformFontMac::GetExpectedTextWidth(int length) { |
115 return length * average_width_; | 124 return length * average_width_; |
116 } | 125 } |
117 | 126 |
118 int PlatformFontMac::GetStyle() const { | 127 int PlatformFontMac::GetStyle() const { |
119 return font_style_; | 128 return font_style_; |
120 } | 129 } |
121 | 130 |
| 131 Font::Weight PlatformFontMac::GetWeight() const { |
| 132 return font_weight_; |
| 133 } |
| 134 |
122 const std::string& PlatformFontMac::GetFontName() const { | 135 const std::string& PlatformFontMac::GetFontName() const { |
123 return font_name_; | 136 return font_name_; |
124 } | 137 } |
125 | 138 |
126 std::string PlatformFontMac::GetActualFontNameForTesting() const { | 139 std::string PlatformFontMac::GetActualFontNameForTesting() const { |
127 return base::SysNSStringToUTF8([native_font_ familyName]); | 140 return base::SysNSStringToUTF8([native_font_ familyName]); |
128 } | 141 } |
129 | 142 |
130 int PlatformFontMac::GetFontSize() const { | 143 int PlatformFontMac::GetFontSize() const { |
131 return font_size_; | 144 return font_size_; |
132 } | 145 } |
133 | 146 |
134 const FontRenderParams& PlatformFontMac::GetFontRenderParams() { | 147 const FontRenderParams& PlatformFontMac::GetFontRenderParams() { |
135 return render_params_; | 148 return render_params_; |
136 } | 149 } |
137 | 150 |
138 NativeFont PlatformFontMac::GetNativeFont() const { | 151 NativeFont PlatformFontMac::GetNativeFont() const { |
139 return [[native_font_.get() retain] autorelease]; | 152 return [[native_font_.get() retain] autorelease]; |
140 } | 153 } |
141 | 154 |
142 //////////////////////////////////////////////////////////////////////////////// | 155 //////////////////////////////////////////////////////////////////////////////// |
143 // PlatformFontMac, private: | 156 // PlatformFontMac, private: |
144 | 157 |
145 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 158 PlatformFontMac::PlatformFontMac(const std::string& font_name, |
146 int font_size, | 159 int font_size, |
147 int font_style) | 160 int font_style, |
148 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]), | 161 Font::Weight font_weight) |
| 162 : native_font_( |
| 163 [NSFontWithSpec(font_name, font_size, font_style, font_weight) |
| 164 retain]), |
149 font_name_(font_name), | 165 font_name_(font_name), |
150 font_size_(font_size), | 166 font_size_(font_size), |
151 font_style_(font_style) { | 167 font_style_(font_style), |
| 168 font_weight_(font_weight) { |
152 CalculateMetricsAndInitRenderParams(); | 169 CalculateMetricsAndInitRenderParams(); |
153 } | 170 } |
154 | 171 |
155 PlatformFontMac::~PlatformFontMac() { | 172 PlatformFontMac::~PlatformFontMac() { |
156 } | 173 } |
157 | 174 |
158 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { | 175 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { |
159 NSFont* font = native_font_.get(); | 176 NSFont* font = native_font_.get(); |
160 if (!font) { | 177 if (!font) { |
161 // This object was constructed from a font name that doesn't correspond to | 178 // This object was constructed from a font name that doesn't correspond to |
(...skipping 17 matching lines...) Expand all Loading... |
179 // to ensure GetBaseline() + descender fits within GetHeight() during layout. | 196 // to ensure GetBaseline() + descender fits within GetHeight() during layout. |
180 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); | 197 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); |
181 | 198 |
182 average_width_ = | 199 average_width_ = |
183 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); | 200 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); |
184 | 201 |
185 FontRenderParamsQuery query; | 202 FontRenderParamsQuery query; |
186 query.families.push_back(font_name_); | 203 query.families.push_back(font_name_); |
187 query.pixel_size = font_size_; | 204 query.pixel_size = font_size_; |
188 query.style = font_style_; | 205 query.style = font_style_; |
| 206 query.weight = font_weight_; |
189 render_params_ = gfx::GetFontRenderParams(query, NULL); | 207 render_params_ = gfx::GetFontRenderParams(query, NULL); |
190 } | 208 } |
191 | 209 |
192 //////////////////////////////////////////////////////////////////////////////// | 210 //////////////////////////////////////////////////////////////////////////////// |
193 // PlatformFont, public: | 211 // PlatformFont, public: |
194 | 212 |
195 // static | 213 // static |
196 PlatformFont* PlatformFont::CreateDefault() { | 214 PlatformFont* PlatformFont::CreateDefault() { |
197 return new PlatformFontMac; | 215 return new PlatformFontMac; |
198 } | 216 } |
199 | 217 |
200 // static | 218 // static |
201 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 219 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
202 return new PlatformFontMac(native_font); | 220 return new PlatformFontMac(native_font); |
203 } | 221 } |
204 | 222 |
205 // static | 223 // static |
206 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 224 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
207 int font_size) { | 225 int font_size) { |
208 return new PlatformFontMac(font_name, font_size); | 226 return new PlatformFontMac(font_name, font_size); |
209 } | 227 } |
210 | 228 |
211 } // namespace gfx | 229 } // namespace gfx |
OLD | NEW |