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 gfx::Font::Weight font_weight) { | |
msw
2016/03/25 01:33:10
nit: no need for 'gfx::'
Mikus
2016/03/25 11:49:00
Done.
| |
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; |
77 else | |
78 font_weight_ = Font::Weight::NORMAL; | |
msw
2016/03/25 01:33:10
nit: no need for this else statement with the init
Mikus
2016/03/25 11:49:00
Done.
| |
74 | 79 |
75 CalculateMetricsAndInitRenderParams(); | 80 CalculateMetricsAndInitRenderParams(); |
76 } | 81 } |
77 | 82 |
78 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 83 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size) |
79 int font_size) | 84 : native_font_([NSFontWithSpec(font_name, |
80 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]), | 85 font_size, |
86 Font::NORMAL, | |
87 Font::Weight::NORMAL) retain]), | |
81 font_name_(font_name), | 88 font_name_(font_name), |
82 font_size_(font_size), | 89 font_size_(font_size), |
83 font_style_(Font::NORMAL) { | 90 font_style_(Font::NORMAL), |
91 font_weight_(Font::Weight::NORMAL) { | |
84 CalculateMetricsAndInitRenderParams(); | 92 CalculateMetricsAndInitRenderParams(); |
85 } | 93 } |
86 | 94 |
87 //////////////////////////////////////////////////////////////////////////////// | 95 //////////////////////////////////////////////////////////////////////////////// |
88 // PlatformFontMac, PlatformFont implementation: | 96 // PlatformFontMac, PlatformFont implementation: |
89 | 97 |
90 Font PlatformFontMac::DeriveFont(int size_delta, int style) const { | 98 Font PlatformFontMac::DeriveFont(int size_delta, |
91 if (native_font_ && style == font_style_) { | 99 int style, |
100 Font::Weight weight) const { | |
101 if (native_font_ && style == font_style_ && weight == font_weight_) { | |
92 // System fonts have special attributes starting with 10.11. They should be | 102 // System fonts have special attributes starting with 10.11. They should be |
93 // requested using the same descriptor to preserve these attributes. | 103 // requested using the same descriptor to preserve these attributes. |
94 return Font(new PlatformFontMac( | 104 return Font(new PlatformFontMac([NSFont |
95 [NSFont fontWithDescriptor:[native_font_ fontDescriptor] | 105 fontWithDescriptor:[native_font_ fontDescriptor] |
96 size:font_size_ + size_delta])); | 106 size:font_size_ + size_delta])); |
97 } | 107 } |
98 | 108 |
99 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style)); | 109 return Font( |
110 new PlatformFontMac(font_name_, font_size_ + size_delta, style, weight)); | |
100 } | 111 } |
101 | 112 |
102 int PlatformFontMac::GetHeight() { | 113 int PlatformFontMac::GetHeight() { |
103 return height_; | 114 return height_; |
104 } | 115 } |
105 | 116 |
106 int PlatformFontMac::GetBaseline() { | 117 int PlatformFontMac::GetBaseline() { |
107 return ascent_; | 118 return ascent_; |
108 } | 119 } |
109 | 120 |
110 int PlatformFontMac::GetCapHeight() { | 121 int PlatformFontMac::GetCapHeight() { |
111 return cap_height_; | 122 return cap_height_; |
112 } | 123 } |
113 | 124 |
114 int PlatformFontMac::GetExpectedTextWidth(int length) { | 125 int PlatformFontMac::GetExpectedTextWidth(int length) { |
115 return length * average_width_; | 126 return length * average_width_; |
116 } | 127 } |
117 | 128 |
118 int PlatformFontMac::GetStyle() const { | 129 int PlatformFontMac::GetStyle() const { |
119 return font_style_; | 130 return font_style_; |
120 } | 131 } |
121 | 132 |
133 Font::Weight PlatformFontMac::GetWeight() const { | |
134 return font_weight_; | |
135 } | |
136 | |
122 const std::string& PlatformFontMac::GetFontName() const { | 137 const std::string& PlatformFontMac::GetFontName() const { |
123 return font_name_; | 138 return font_name_; |
124 } | 139 } |
125 | 140 |
126 std::string PlatformFontMac::GetActualFontNameForTesting() const { | 141 std::string PlatformFontMac::GetActualFontNameForTesting() const { |
127 return base::SysNSStringToUTF8([native_font_ familyName]); | 142 return base::SysNSStringToUTF8([native_font_ familyName]); |
128 } | 143 } |
129 | 144 |
130 int PlatformFontMac::GetFontSize() const { | 145 int PlatformFontMac::GetFontSize() const { |
131 return font_size_; | 146 return font_size_; |
132 } | 147 } |
133 | 148 |
134 const FontRenderParams& PlatformFontMac::GetFontRenderParams() { | 149 const FontRenderParams& PlatformFontMac::GetFontRenderParams() { |
135 return render_params_; | 150 return render_params_; |
136 } | 151 } |
137 | 152 |
138 NativeFont PlatformFontMac::GetNativeFont() const { | 153 NativeFont PlatformFontMac::GetNativeFont() const { |
139 return [[native_font_.get() retain] autorelease]; | 154 return [[native_font_.get() retain] autorelease]; |
140 } | 155 } |
141 | 156 |
142 //////////////////////////////////////////////////////////////////////////////// | 157 //////////////////////////////////////////////////////////////////////////////// |
143 // PlatformFontMac, private: | 158 // PlatformFontMac, private: |
144 | 159 |
145 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 160 PlatformFontMac::PlatformFontMac(const std::string& font_name, |
146 int font_size, | 161 int font_size, |
147 int font_style) | 162 int font_style, |
148 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]), | 163 Font::Weight font_weight) |
164 : native_font_( | |
165 [NSFontWithSpec(font_name, font_size, font_style, font_weight) | |
166 retain]), | |
149 font_name_(font_name), | 167 font_name_(font_name), |
150 font_size_(font_size), | 168 font_size_(font_size), |
151 font_style_(font_style) { | 169 font_style_(font_style), |
170 font_weight_(font_weight) { | |
152 CalculateMetricsAndInitRenderParams(); | 171 CalculateMetricsAndInitRenderParams(); |
153 } | 172 } |
154 | 173 |
155 PlatformFontMac::~PlatformFontMac() { | 174 PlatformFontMac::~PlatformFontMac() { |
156 } | 175 } |
157 | 176 |
158 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { | 177 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { |
159 NSFont* font = native_font_.get(); | 178 NSFont* font = native_font_.get(); |
160 if (!font) { | 179 if (!font) { |
161 // This object was constructed from a font name that doesn't correspond to | 180 // 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. | 198 // to ensure GetBaseline() + descender fits within GetHeight() during layout. |
180 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); | 199 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); |
181 | 200 |
182 average_width_ = | 201 average_width_ = |
183 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); | 202 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); |
184 | 203 |
185 FontRenderParamsQuery query; | 204 FontRenderParamsQuery query; |
186 query.families.push_back(font_name_); | 205 query.families.push_back(font_name_); |
187 query.pixel_size = font_size_; | 206 query.pixel_size = font_size_; |
188 query.style = font_style_; | 207 query.style = font_style_; |
208 query.weight = font_weight_; | |
189 render_params_ = gfx::GetFontRenderParams(query, NULL); | 209 render_params_ = gfx::GetFontRenderParams(query, NULL); |
190 } | 210 } |
191 | 211 |
192 //////////////////////////////////////////////////////////////////////////////// | 212 //////////////////////////////////////////////////////////////////////////////// |
193 // PlatformFont, public: | 213 // PlatformFont, public: |
194 | 214 |
195 // static | 215 // static |
196 PlatformFont* PlatformFont::CreateDefault() { | 216 PlatformFont* PlatformFont::CreateDefault() { |
197 return new PlatformFontMac; | 217 return new PlatformFontMac; |
198 } | 218 } |
199 | 219 |
200 // static | 220 // static |
201 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 221 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
202 return new PlatformFontMac(native_font); | 222 return new PlatformFontMac(native_font); |
203 } | 223 } |
204 | 224 |
205 // static | 225 // static |
206 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 226 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
207 int font_size) { | 227 int font_size) { |
208 return new PlatformFontMac(font_name, font_size); | 228 return new PlatformFontMac(font_name, font_size); |
209 } | 229 } |
210 | 230 |
211 } // namespace gfx | 231 } // namespace gfx |
OLD | NEW |