| 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_ios.h" | 5 #include "ui/gfx/platform_font_ios.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "ui/gfx/font.h" | 13 #include "ui/gfx/font.h" |
| 14 #include "ui/gfx/font_render_params.h" | 14 #include "ui/gfx/font_render_params.h" |
| 15 #include "ui/gfx/ios/NSString+CrStringDrawing.h" | 15 #include "ui/gfx/ios/NSString+CrStringDrawing.h" |
| 16 | 16 |
| 17 namespace gfx { | 17 namespace gfx { |
| 18 | 18 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
| 20 // PlatformFontIOS, public: | 20 // PlatformFontIOS, public: |
| 21 | 21 |
| 22 PlatformFontIOS::PlatformFontIOS() { | 22 PlatformFontIOS::PlatformFontIOS() { |
| 23 font_size_ = [UIFont systemFontSize]; | 23 font_size_ = [UIFont systemFontSize]; |
| 24 style_ = gfx::Font::NORMAL; | 24 style_ = Font::NORMAL; |
| 25 weight_ = Font::Weight::NORMAL; |
| 25 UIFont* system_font = [UIFont systemFontOfSize:font_size_]; | 26 UIFont* system_font = [UIFont systemFontOfSize:font_size_]; |
| 26 font_name_ = base::SysNSStringToUTF8([system_font fontName]); | 27 font_name_ = base::SysNSStringToUTF8([system_font fontName]); |
| 27 CalculateMetrics(); | 28 CalculateMetrics(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) { | 31 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) { |
| 31 std::string font_name = base::SysNSStringToUTF8([native_font fontName]); | 32 std::string font_name = base::SysNSStringToUTF8([native_font fontName]); |
| 32 InitWithNameSizeAndStyle(font_name, | 33 InitWithNameSizeAndStyle(font_name, [native_font pointSize], |
| 33 [native_font pointSize], | 34 Font::NORMAL, Font::Weight::NORMAL); |
| 34 gfx::Font::NORMAL); | |
| 35 } | 35 } |
| 36 | 36 |
| 37 PlatformFontIOS::PlatformFontIOS(const std::string& font_name, | 37 PlatformFontIOS::PlatformFontIOS(const std::string& font_name, int font_size) { |
| 38 int font_size) { | 38 InitWithNameSizeAndStyle(font_name, font_size, Font::NORMAL, |
| 39 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL); | 39 Font::Weight::NORMAL); |
| 40 } | 40 } |
| 41 | 41 |
| 42 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 43 // PlatformFontIOS, PlatformFont implementation: | 43 // PlatformFontIOS, PlatformFont implementation: |
| 44 | 44 |
| 45 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const { | 45 Font PlatformFontIOS::DeriveFont(int size_delta, |
| 46 return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style)); | 46 int style, |
| 47 Font::Weight weight) const { |
| 48 return Font( |
| 49 new PlatformFontIOS(font_name_, font_size_ + size_delta, style, weight)); |
| 47 } | 50 } |
| 48 | 51 |
| 49 int PlatformFontIOS::GetHeight() { | 52 int PlatformFontIOS::GetHeight() { |
| 50 return height_; | 53 return height_; |
| 51 } | 54 } |
| 52 | 55 |
| 53 int PlatformFontIOS::GetBaseline() { | 56 int PlatformFontIOS::GetBaseline() { |
| 54 return ascent_; | 57 return ascent_; |
| 55 } | 58 } |
| 56 | 59 |
| 57 int PlatformFontIOS::GetCapHeight() { | 60 int PlatformFontIOS::GetCapHeight() { |
| 58 return cap_height_; | 61 return cap_height_; |
| 59 } | 62 } |
| 60 | 63 |
| 61 int PlatformFontIOS::GetExpectedTextWidth(int length) { | 64 int PlatformFontIOS::GetExpectedTextWidth(int length) { |
| 62 return length * average_width_; | 65 return length * average_width_; |
| 63 } | 66 } |
| 64 | 67 |
| 65 int PlatformFontIOS::GetStyle() const { | 68 int PlatformFontIOS::GetStyle() const { |
| 66 return style_; | 69 return style_; |
| 67 } | 70 } |
| 68 | 71 |
| 72 Font::Weight PlatformFontIOS::GetWeight() const { |
| 73 return weight_; |
| 74 } |
| 75 |
| 69 const std::string& PlatformFontIOS::GetFontName() const { | 76 const std::string& PlatformFontIOS::GetFontName() const { |
| 70 return font_name_; | 77 return font_name_; |
| 71 } | 78 } |
| 72 | 79 |
| 73 std::string PlatformFontIOS::GetActualFontNameForTesting() const { | 80 std::string PlatformFontIOS::GetActualFontNameForTesting() const { |
| 74 return base::SysNSStringToUTF8([GetNativeFont() familyName]); | 81 return base::SysNSStringToUTF8([GetNativeFont() familyName]); |
| 75 } | 82 } |
| 76 | 83 |
| 77 int PlatformFontIOS::GetFontSize() const { | 84 int PlatformFontIOS::GetFontSize() const { |
| 78 return font_size_; | 85 return font_size_; |
| 79 } | 86 } |
| 80 | 87 |
| 81 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() { | 88 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() { |
| 82 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
| 83 static FontRenderParams params; | 90 static FontRenderParams params; |
| 84 return params; | 91 return params; |
| 85 } | 92 } |
| 86 | 93 |
| 87 NativeFont PlatformFontIOS::GetNativeFont() const { | 94 NativeFont PlatformFontIOS::GetNativeFont() const { |
| 88 return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_) | 95 return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_) |
| 89 size:font_size_]; | 96 size:font_size_]; |
| 90 } | 97 } |
| 91 | 98 |
| 92 //////////////////////////////////////////////////////////////////////////////// | 99 //////////////////////////////////////////////////////////////////////////////// |
| 93 // PlatformFontIOS, private: | 100 // PlatformFontIOS, private: |
| 94 | 101 |
| 95 PlatformFontIOS::PlatformFontIOS(const std::string& font_name, | 102 PlatformFontIOS::PlatformFontIOS(const std::string& font_name, |
| 96 int font_size, | 103 int font_size, |
| 97 int style) { | 104 int style, |
| 98 InitWithNameSizeAndStyle(font_name, font_size, style); | 105 Font::Weight weight) { |
| 106 InitWithNameSizeAndStyle(font_name, font_size, style, weight); |
| 99 } | 107 } |
| 100 | 108 |
| 101 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name, | 109 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name, |
| 102 int font_size, | 110 int font_size, |
| 103 int style) { | 111 int style, |
| 112 Font::Weight weight) { |
| 104 font_name_ = font_name; | 113 font_name_ = font_name; |
| 105 font_size_ = font_size; | 114 font_size_ = font_size; |
| 106 style_ = style; | 115 style_ = style; |
| 116 weight_ = weight; |
| 107 CalculateMetrics(); | 117 CalculateMetrics(); |
| 108 } | 118 } |
| 109 | 119 |
| 110 void PlatformFontIOS::CalculateMetrics() { | 120 void PlatformFontIOS::CalculateMetrics() { |
| 111 UIFont* font = GetNativeFont(); | 121 UIFont* font = GetNativeFont(); |
| 112 height_ = font.lineHeight; | 122 height_ = font.lineHeight; |
| 113 ascent_ = font.ascender; | 123 ascent_ = font.ascender; |
| 114 cap_height_ = font.capHeight; | 124 cap_height_ = font.capHeight; |
| 115 average_width_ = [@"x" cr_sizeWithFont:font].width; | 125 average_width_ = [@"x" cr_sizeWithFont:font].width; |
| 116 } | 126 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 128 return new PlatformFontIOS(native_font); | 138 return new PlatformFontIOS(native_font); |
| 129 } | 139 } |
| 130 | 140 |
| 131 // static | 141 // static |
| 132 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 142 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 133 int font_size) { | 143 int font_size) { |
| 134 return new PlatformFontIOS(font_name, font_size); | 144 return new PlatformFontIOS(font_name, font_size); |
| 135 } | 145 } |
| 136 | 146 |
| 137 } // namespace gfx | 147 } // namespace gfx |
| OLD | NEW |