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
3 // found in the LICENSE file.
5 #include "ui/gfx/platform_font_ios.h"
7 #import <UIKit/UIKit.h>
9 #include "base/basictypes.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/gfx/font.h"
16 ////////////////////////////////////////////////////////////////////////////////
17 // PlatformFontIOS, public:
19 PlatformFontIOS::PlatformFontIOS() {
20 font_size_ = [UIFont systemFontSize];
21 style_ = gfx::Font::NORMAL;
22 UIFont* system_font = [UIFont systemFontOfSize:font_size_];
23 font_name_ = base::SysNSStringToUTF8([system_font fontName]);
27 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) {
28 std::string font_name = base::SysNSStringToUTF8([native_font fontName]);
29 InitWithNameSizeAndStyle(font_name,
30 [native_font pointSize],
34 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
36 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
39 ////////////////////////////////////////////////////////////////////////////////
40 // PlatformFontIOS, PlatformFont implementation:
42 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const {
43 return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style));
46 int PlatformFontIOS::GetHeight() const {
50 int PlatformFontIOS::GetBaseline() const {
54 int PlatformFontIOS::GetCapHeight() const {
58 int PlatformFontIOS::GetAverageCharacterWidth() const {
59 return average_width_;
62 int PlatformFontIOS::GetStringWidth(const base::string16& text) const {
63 NSString* ns_text = base::SysUTF16ToNSString(text);
64 return [ns_text sizeWithFont:GetNativeFont()].width;
67 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
68 return length * average_width_;
71 int PlatformFontIOS::GetStyle() const {
75 std::string PlatformFontIOS::GetFontName() const {
79 std::string PlatformFontIOS::GetActualFontNameForTesting() const {
80 return base::SysNSStringToUTF8([GetNativeFont() familyName]);
83 int PlatformFontIOS::GetFontSize() const {
87 NativeFont PlatformFontIOS::GetNativeFont() const {
88 return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
92 ////////////////////////////////////////////////////////////////////////////////
93 // PlatformFontIOS, private:
95 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
98 InitWithNameSizeAndStyle(font_name, font_size, style);
101 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
104 font_name_ = font_name;
105 font_size_ = font_size;
110 void PlatformFontIOS::CalculateMetrics() {
111 UIFont* font = GetNativeFont();
112 height_ = font.lineHeight;
113 ascent_ = font.ascender;
114 cap_height_ = font.capHeight;
115 average_width_ = [@"x" sizeWithFont:font].width;
118 ////////////////////////////////////////////////////////////////////////////////
119 // PlatformFont, public:
122 PlatformFont* PlatformFont::CreateDefault() {
123 return new PlatformFontIOS;
127 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
128 return new PlatformFontIOS(native_font);
132 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
134 return new PlatformFontIOS(font_name, font_size);