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"
13 #include "ui/gfx/font_render_params.h"
17 ////////////////////////////////////////////////////////////////////////////////
18 // PlatformFontIOS, public:
20 PlatformFontIOS::PlatformFontIOS() {
21 font_size_ = [UIFont systemFontSize];
22 style_ = gfx::Font::NORMAL;
23 UIFont* system_font = [UIFont systemFontOfSize:font_size_];
24 font_name_ = base::SysNSStringToUTF8([system_font fontName]);
28 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) {
29 std::string font_name = base::SysNSStringToUTF8([native_font fontName]);
30 InitWithNameSizeAndStyle(font_name,
31 [native_font pointSize],
35 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
37 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
40 ////////////////////////////////////////////////////////////////////////////////
41 // PlatformFontIOS, PlatformFont implementation:
43 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const {
44 return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style));
47 int PlatformFontIOS::GetHeight() const {
51 int PlatformFontIOS::GetBaseline() const {
55 int PlatformFontIOS::GetCapHeight() const {
59 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
60 return length * average_width_;
63 int PlatformFontIOS::GetStyle() const {
67 std::string PlatformFontIOS::GetFontName() const {
71 std::string PlatformFontIOS::GetActualFontNameForTesting() const {
72 return base::SysNSStringToUTF8([GetNativeFont() familyName]);
75 int PlatformFontIOS::GetFontSize() const {
79 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() const {
81 static FontRenderParams params;
85 NativeFont PlatformFontIOS::GetNativeFont() const {
86 return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
90 ////////////////////////////////////////////////////////////////////////////////
91 // PlatformFontIOS, private:
93 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
96 InitWithNameSizeAndStyle(font_name, font_size, style);
99 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
102 font_name_ = font_name;
103 font_size_ = font_size;
108 void PlatformFontIOS::CalculateMetrics() {
109 UIFont* font = GetNativeFont();
110 height_ = font.lineHeight;
111 ascent_ = font.ascender;
112 cap_height_ = font.capHeight;
113 average_width_ = [@"x" sizeWithFont:font].width;
116 ////////////////////////////////////////////////////////////////////////////////
117 // PlatformFont, public:
120 PlatformFont* PlatformFont::CreateDefault() {
121 return new PlatformFontIOS;
125 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
126 return new PlatformFontIOS(native_font);
130 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
132 return new PlatformFontIOS(font_name, font_size);