| 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 <Cocoa/Cocoa.h> | 7 #include <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 //////////////////////////////////////////////////////////////////////////////// | 56 //////////////////////////////////////////////////////////////////////////////// |
| 57 // PlatformFontMac, public: | 57 // PlatformFontMac, public: |
| 58 | 58 |
| 59 PlatformFontMac::PlatformFontMac() | 59 PlatformFontMac::PlatformFontMac() |
| 60 : native_font_([[NSFont systemFontOfSize:[NSFont systemFontSize]] retain]), | 60 : native_font_([[NSFont systemFontOfSize:[NSFont systemFontSize]] retain]), |
| 61 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), | 61 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), |
| 62 font_size_([NSFont systemFontSize]), | 62 font_size_([NSFont systemFontSize]), |
| 63 font_style_(Font::NORMAL) { | 63 font_style_(Font::NORMAL) { |
| 64 CalculateMetrics(); | 64 CalculateMetricsAndInitRenderParams(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 PlatformFontMac::PlatformFontMac(NativeFont native_font) | 67 PlatformFontMac::PlatformFontMac(NativeFont native_font) |
| 68 : native_font_([native_font retain]), | 68 : native_font_([native_font retain]), |
| 69 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), | 69 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), |
| 70 font_size_([native_font_ pointSize]), | 70 font_size_([native_font_ pointSize]), |
| 71 font_style_(Font::NORMAL) { | 71 font_style_(Font::NORMAL) { |
| 72 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits]; | 72 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits]; |
| 73 if (traits & NSFontItalicTrait) | 73 if (traits & NSFontItalicTrait) |
| 74 font_style_ |= Font::ITALIC; | 74 font_style_ |= Font::ITALIC; |
| 75 if (traits & NSFontBoldTrait) | 75 if (traits & NSFontBoldTrait) |
| 76 font_style_ |= Font::BOLD; | 76 font_style_ |= Font::BOLD; |
| 77 | 77 |
| 78 CalculateMetrics(); | 78 CalculateMetricsAndInitRenderParams(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 81 PlatformFontMac::PlatformFontMac(const std::string& font_name, |
| 82 int font_size) | 82 int font_size) |
| 83 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]), | 83 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]), |
| 84 font_name_(font_name), | 84 font_name_(font_name), |
| 85 font_size_(font_size), | 85 font_size_(font_size), |
| 86 font_style_(Font::NORMAL) { | 86 font_style_(Font::NORMAL) { |
| 87 CalculateMetrics(); | 87 CalculateMetricsAndInitRenderParams(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 //////////////////////////////////////////////////////////////////////////////// | 90 //////////////////////////////////////////////////////////////////////////////// |
| 91 // PlatformFontMac, PlatformFont implementation: | 91 // PlatformFontMac, PlatformFont implementation: |
| 92 | 92 |
| 93 Font PlatformFontMac::DeriveFont(int size_delta, int style) const { | 93 Font PlatformFontMac::DeriveFont(int size_delta, int style) const { |
| 94 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style)); | 94 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 int PlatformFontMac::GetHeight() const { | 97 int PlatformFontMac::GetHeight() const { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 120 | 120 |
| 121 std::string PlatformFontMac::GetActualFontNameForTesting() const { | 121 std::string PlatformFontMac::GetActualFontNameForTesting() const { |
| 122 return base::SysNSStringToUTF8([native_font_ familyName]); | 122 return base::SysNSStringToUTF8([native_font_ familyName]); |
| 123 } | 123 } |
| 124 | 124 |
| 125 int PlatformFontMac::GetFontSize() const { | 125 int PlatformFontMac::GetFontSize() const { |
| 126 return font_size_; | 126 return font_size_; |
| 127 } | 127 } |
| 128 | 128 |
| 129 const FontRenderParams& PlatformFontMac::GetFontRenderParams() const { | 129 const FontRenderParams& PlatformFontMac::GetFontRenderParams() const { |
| 130 NOTIMPLEMENTED(); | 130 return render_params_; |
| 131 static FontRenderParams params; | |
| 132 return params; | |
| 133 } | 131 } |
| 134 | 132 |
| 135 NativeFont PlatformFontMac::GetNativeFont() const { | 133 NativeFont PlatformFontMac::GetNativeFont() const { |
| 136 return [[native_font_.get() retain] autorelease]; | 134 return [[native_font_.get() retain] autorelease]; |
| 137 } | 135 } |
| 138 | 136 |
| 139 //////////////////////////////////////////////////////////////////////////////// | 137 //////////////////////////////////////////////////////////////////////////////// |
| 140 // PlatformFontMac, private: | 138 // PlatformFontMac, private: |
| 141 | 139 |
| 142 PlatformFontMac::PlatformFontMac(const std::string& font_name, | 140 PlatformFontMac::PlatformFontMac(const std::string& font_name, |
| 143 int font_size, | 141 int font_size, |
| 144 int font_style) | 142 int font_style) |
| 145 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]), | 143 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]), |
| 146 font_name_(font_name), | 144 font_name_(font_name), |
| 147 font_size_(font_size), | 145 font_size_(font_size), |
| 148 font_style_(font_style) { | 146 font_style_(font_style) { |
| 149 CalculateMetrics(); | 147 CalculateMetricsAndInitRenderParams(); |
| 150 } | 148 } |
| 151 | 149 |
| 152 PlatformFontMac::~PlatformFontMac() { | 150 PlatformFontMac::~PlatformFontMac() { |
| 153 } | 151 } |
| 154 | 152 |
| 155 void PlatformFontMac::CalculateMetrics() { | 153 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { |
| 156 NSFont* font = native_font_.get(); | 154 NSFont* font = native_font_.get(); |
| 157 if (!font) { | 155 if (!font) { |
| 158 // This object was constructed from a font name that doesn't correspond to | 156 // This object was constructed from a font name that doesn't correspond to |
| 159 // an actual font. Don't waste time working out metrics. | 157 // an actual font. Don't waste time working out metrics. |
| 160 height_ = 0; | 158 height_ = 0; |
| 161 ascent_ = 0; | 159 ascent_ = 0; |
| 162 cap_height_ = 0; | 160 cap_height_ = 0; |
| 163 average_width_ = 0; | 161 average_width_ = 0; |
| 164 return; | 162 return; |
| 165 } | 163 } |
| 166 | 164 |
| 167 base::scoped_nsobject<NSLayoutManager> layout_manager( | 165 base::scoped_nsobject<NSLayoutManager> layout_manager( |
| 168 [[NSLayoutManager alloc] init]); | 166 [[NSLayoutManager alloc] init]); |
| 169 height_ = SkScalarCeilToInt([layout_manager defaultLineHeightForFont:font]); | 167 height_ = SkScalarCeilToInt([layout_manager defaultLineHeightForFont:font]); |
| 170 ascent_ = SkScalarCeilToInt([font ascender]); | 168 ascent_ = SkScalarCeilToInt([font ascender]); |
| 171 cap_height_ = SkScalarCeilToInt([font capHeight]); | 169 cap_height_ = SkScalarCeilToInt([font capHeight]); |
| 172 average_width_ = | 170 average_width_ = |
| 173 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); | 171 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); |
| 172 |
| 173 FontRenderParamsQuery query(false); |
| 174 query.families.push_back(font_name_); |
| 175 query.pixel_size = font_size_; |
| 176 query.style = font_style_; |
| 177 render_params_ = gfx::GetFontRenderParams(query, NULL); |
| 174 } | 178 } |
| 175 | 179 |
| 176 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
| 177 // PlatformFont, public: | 181 // PlatformFont, public: |
| 178 | 182 |
| 179 // static | 183 // static |
| 180 PlatformFont* PlatformFont::CreateDefault() { | 184 PlatformFont* PlatformFont::CreateDefault() { |
| 181 return new PlatformFontMac; | 185 return new PlatformFontMac; |
| 182 } | 186 } |
| 183 | 187 |
| 184 // static | 188 // static |
| 185 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 189 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
| 186 return new PlatformFontMac(native_font); | 190 return new PlatformFontMac(native_font); |
| 187 } | 191 } |
| 188 | 192 |
| 189 // static | 193 // static |
| 190 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 194 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 191 int font_size) { | 195 int font_size) { |
| 192 return new PlatformFontMac(font_name, font_size); | 196 return new PlatformFontMac(font_name, font_size); |
| 193 } | 197 } |
| 194 | 198 |
| 195 } // namespace gfx | 199 } // namespace gfx |
| OLD | NEW |