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>
11 #include "base/basictypes.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/gfx/font.h"
15 #include "ui/gfx/font_render_params.h"
19 ////////////////////////////////////////////////////////////////////////////////
20 // PlatformFontIOS, public:
22 PlatformFontIOS::PlatformFontIOS() {
23 font_size_ = [UIFont systemFontSize];
24 style_ = gfx::Font::NORMAL;
25 UIFont* system_font = [UIFont systemFontOfSize:font_size_];
26 font_name_ = base::SysNSStringToUTF8([system_font fontName]);
30 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) {
31 std::string font_name = base::SysNSStringToUTF8([native_font fontName]);
32 InitWithNameSizeAndStyle(font_name,
33 [native_font pointSize],
37 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
39 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
42 ////////////////////////////////////////////////////////////////////////////////
43 // PlatformFontIOS, PlatformFont implementation:
45 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const {
46 return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style));
49 int PlatformFontIOS::GetHeight() const {
53 int PlatformFontIOS::GetBaseline() const {
57 int PlatformFontIOS::GetCapHeight() const {
61 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
62 return length * average_width_;
65 int PlatformFontIOS::GetStyle() const {
69 std::string PlatformFontIOS::GetFontName() const {
73 std::string PlatformFontIOS::GetActualFontNameForTesting() const {
74 return base::SysNSStringToUTF8([GetNativeFont() familyName]);
77 int PlatformFontIOS::GetFontSize() const {
81 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() const {
83 static FontRenderParams params;
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;
116 NSDictionary* attributes = @{ NSFontAttributeName : font };
117 average_width_ = std::ceil([@"x" sizeWithAttributes:attributes].width);
123 ////////////////////////////////////////////////////////////////////////////////
124 // PlatformFont, public:
127 PlatformFont* PlatformFont::CreateDefault() {
128 return new PlatformFontIOS;
132 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
133 return new PlatformFontIOS(native_font);
137 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
139 return new PlatformFontIOS(font_name, font_size);