| 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 the font style for |font|. Disregards Font::UNDERLINE, since NSFont |
| 23 // does not support it as a trait. |
| 24 int GetFontStyleFromNSFont(NSFont* font) { |
| 25 int font_style = Font::NORMAL; |
| 26 NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits]; |
| 27 if (traits & NSFontItalicTrait) |
| 28 font_style |= Font::ITALIC; |
| 29 return font_style; |
| 30 } |
| 31 |
| 32 // Returns the Font weight for |font|. |
| 33 Font::Weight GetFontWeightFromNSFont(NSFont* font) { |
| 34 NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits]; |
| 35 return (traits & NSFontBoldTrait) ? Font::Weight::BOLD : Font::Weight::NORMAL; |
| 36 } |
| 37 |
| 22 // Returns an autoreleased NSFont created with the passed-in specifications. | 38 // Returns an autoreleased NSFont created with the passed-in specifications. |
| 23 NSFont* NSFontWithSpec(const std::string& font_name, | 39 NSFont* NSFontWithSpec(const std::string& font_name, |
| 24 int font_size, | 40 int font_size, |
| 25 int font_style, | 41 int font_style, |
| 26 Font::Weight font_weight) { | 42 Font::Weight font_weight) { |
| 27 NSFontSymbolicTraits trait_bits = 0; | 43 NSFontSymbolicTraits trait_bits = 0; |
| 28 // TODO(mboc): Add support for other weights as well. | 44 // TODO(mboc): Add support for other weights as well. |
| 29 if (font_weight >= Font::Weight::BOLD) | 45 if (font_weight >= Font::Weight::BOLD) |
| 30 trait_bits |= NSFontBoldTrait; | 46 trait_bits |= NSFontBoldTrait; |
| 31 if (font_style & Font::ITALIC) | 47 if (font_style & Font::ITALIC) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 57 } // namespace | 73 } // namespace |
| 58 | 74 |
| 59 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
| 60 // PlatformFontMac, public: | 76 // PlatformFontMac, public: |
| 61 | 77 |
| 62 PlatformFontMac::PlatformFontMac() | 78 PlatformFontMac::PlatformFontMac() |
| 63 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) { | 79 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) { |
| 64 } | 80 } |
| 65 | 81 |
| 66 PlatformFontMac::PlatformFontMac(NativeFont native_font) | 82 PlatformFontMac::PlatformFontMac(NativeFont native_font) |
| 67 : native_font_([native_font retain]), | 83 : PlatformFontMac(native_font, |
| 68 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), | 84 base::SysNSStringToUTF8([native_font familyName]), |
| 69 font_size_([native_font_ pointSize]), | 85 [native_font pointSize], |
| 70 font_style_(Font::NORMAL), | 86 GetFontStyleFromNSFont(native_font), |
| 71 font_weight_(Font::Weight::NORMAL) { | 87 GetFontWeightFromNSFont(native_font)) {} |
| 72 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits]; | |
| 73 if (traits & NSFontItalicTrait) | |
| 74 font_style_ |= Font::ITALIC; | |
| 75 if (traits & NSFontBoldTrait) | |
| 76 font_weight_ = Font::Weight::BOLD; | |
| 77 | |
| 78 CalculateMetricsAndInitRenderParams(); | |
| 79 } | |
| 80 | 88 |
| 81 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size) | 89 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size) |
| 82 : native_font_([NSFontWithSpec(font_name, | 90 : PlatformFontMac(font_name, |
| 83 font_size, | 91 font_size, |
| 84 Font::NORMAL, | 92 Font::NORMAL, |
| 85 Font::Weight::NORMAL) retain]), | 93 Font::Weight::NORMAL) {} |
| 86 font_name_(font_name), | |
| 87 font_size_(font_size), | |
| 88 font_style_(Font::NORMAL), | |
| 89 font_weight_(Font::Weight::NORMAL) { | |
| 90 CalculateMetricsAndInitRenderParams(); | |
| 91 } | |
| 92 | 94 |
| 93 //////////////////////////////////////////////////////////////////////////////// | 95 //////////////////////////////////////////////////////////////////////////////// |
| 94 // PlatformFontMac, PlatformFont implementation: | 96 // PlatformFontMac, PlatformFont implementation: |
| 95 | 97 |
| 96 Font PlatformFontMac::DeriveFont(int size_delta, | 98 Font PlatformFontMac::DeriveFont(int size_delta, |
| 97 int style, | 99 int style, |
| 98 Font::Weight weight) const { | 100 Font::Weight weight) const { |
| 99 if (native_font_ && style == font_style_ && weight == font_weight_) { | 101 // For some reason, creating fonts using the NSFontDescriptor API's seem to be |
| 100 // System fonts have special attributes starting with 10.11. They should be | 102 // unreliable. Hence use the NSFontManager. |
| 101 // requested using the same descriptor to preserve these attributes. | 103 NSFont* derived_font = native_font_; |
| 102 return Font(new PlatformFontMac([NSFont | 104 NSFontManager* font_manager = [NSFontManager sharedFontManager]; |
| 103 fontWithDescriptor:[native_font_ fontDescriptor] | |
| 104 size:font_size_ + size_delta])); | |
| 105 } | |
| 106 | 105 |
| 107 return Font( | 106 NSFontTraitMask bold_trait_mask = |
| 108 new PlatformFontMac(font_name_, font_size_ + size_delta, style, weight)); | 107 weight >= Font::Weight::BOLD ? NSBoldFontMask : NSUnboldFontMask; |
| 108 derived_font = |
| 109 [font_manager convertFont:derived_font toHaveTrait:bold_trait_mask]; |
| 110 |
| 111 NSFontTraitMask italic_trait_mask = |
| 112 (style & Font::ITALIC) ? NSItalicFontMask : NSUnitalicFontMask; |
| 113 derived_font = |
| 114 [font_manager convertFont:derived_font toHaveTrait:italic_trait_mask]; |
| 115 |
| 116 derived_font = |
| 117 [font_manager convertFont:derived_font toSize:font_size_ + size_delta]; |
| 118 |
| 119 return Font(new PlatformFontMac(derived_font, font_name_, |
| 120 font_size_ + size_delta, style, weight)); |
| 109 } | 121 } |
| 110 | 122 |
| 111 int PlatformFontMac::GetHeight() { | 123 int PlatformFontMac::GetHeight() { |
| 112 return height_; | 124 return height_; |
| 113 } | 125 } |
| 114 | 126 |
| 115 int PlatformFontMac::GetBaseline() { | 127 int PlatformFontMac::GetBaseline() { |
| 116 return ascent_; | 128 return ascent_; |
| 117 } | 129 } |
| 118 | 130 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return [[native_font_.get() retain] autorelease]; | 164 return [[native_font_.get() retain] autorelease]; |
| 153 } | 165 } |
| 154 | 166 |
| 155 //////////////////////////////////////////////////////////////////////////////// | 167 //////////////////////////////////////////////////////////////////////////////// |
| 156 // PlatformFontMac, private: | 168 // PlatformFontMac, private: |
| 157 | 169 |
| 158 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 170 PlatformFontMac::PlatformFontMac(const std::string& font_name, |
| 159 int font_size, | 171 int font_size, |
| 160 int font_style, | 172 int font_style, |
| 161 Font::Weight font_weight) | 173 Font::Weight font_weight) |
| 162 : native_font_( | 174 : PlatformFontMac( |
| 163 [NSFontWithSpec(font_name, font_size, font_style, font_weight) | 175 NSFontWithSpec(font_name, font_size, font_style, font_weight), |
| 164 retain]), | 176 font_name, |
| 177 font_size, |
| 178 font_style, |
| 179 font_weight) {} |
| 180 |
| 181 PlatformFontMac::PlatformFontMac(NativeFont font, |
| 182 const std::string& font_name, |
| 183 int font_size, |
| 184 int font_style, |
| 185 Font::Weight font_weight) |
| 186 : native_font_([font retain]), |
| 165 font_name_(font_name), | 187 font_name_(font_name), |
| 166 font_size_(font_size), | 188 font_size_(font_size), |
| 167 font_style_(font_style), | 189 font_style_(font_style), |
| 168 font_weight_(font_weight) { | 190 font_weight_(font_weight) { |
| 169 CalculateMetricsAndInitRenderParams(); | 191 CalculateMetricsAndInitRenderParams(); |
| 170 } | 192 } |
| 171 | 193 |
| 172 PlatformFontMac::~PlatformFontMac() { | 194 PlatformFontMac::~PlatformFontMac() { |
| 173 } | 195 } |
| 174 | 196 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 return new PlatformFontMac(native_font); | 242 return new PlatformFontMac(native_font); |
| 221 } | 243 } |
| 222 | 244 |
| 223 // static | 245 // static |
| 224 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 246 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 225 int font_size) { | 247 int font_size) { |
| 226 return new PlatformFontMac(font_name, font_size); | 248 return new PlatformFontMac(font_name, font_size); |
| 227 } | 249 } |
| 228 | 250 |
| 229 } // namespace gfx | 251 } // namespace gfx |
| OLD | NEW |