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/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/gfx/font.h"
14 #include "ui/gfx/font_render_params.h"
15 #include "ui/gfx/ios/NSString+CrStringDrawing.h"
19 ////////////////////////////////////////////////////////////////////////////////
20 // PlatformFontIOS, public:
22 PlatformFontIOS::PlatformFontIOS() {
23 font_size_ = [UIFont systemFontSize];
24 style_ = Font::NORMAL;
25 weight_ = Font::Weight::NORMAL;
26 UIFont* system_font = [UIFont systemFontOfSize:font_size_];
27 font_name_ = base::SysNSStringToUTF8([system_font fontName]);
31 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) {
32 std::string font_name = base::SysNSStringToUTF8([native_font fontName]);
33 InitWithNameSizeAndStyle(font_name, [native_font pointSize],
34 Font::NORMAL, Font::Weight::NORMAL);
37 PlatformFontIOS::PlatformFontIOS(const std::string& font_name, int font_size) {
38 InitWithNameSizeAndStyle(font_name, font_size, Font::NORMAL,
39 Font::Weight::NORMAL);
42 ////////////////////////////////////////////////////////////////////////////////
43 // PlatformFontIOS, PlatformFont implementation:
45 Font PlatformFontIOS::DeriveFont(int size_delta,
47 Font::Weight weight) const {
49 new PlatformFontIOS(font_name_, font_size_ + size_delta, style, weight));
52 int PlatformFontIOS::GetHeight() {
56 int PlatformFontIOS::GetBaseline() {
60 int PlatformFontIOS::GetCapHeight() {
64 int PlatformFontIOS::GetExpectedTextWidth(int length) {
65 return length * average_width_;
68 int PlatformFontIOS::GetStyle() const {
72 Font::Weight PlatformFontIOS::GetWeight() const {
76 const std::string& PlatformFontIOS::GetFontName() const {
80 std::string PlatformFontIOS::GetActualFontName() const {
81 return base::SysNSStringToUTF8([GetNativeFont() familyName]);
84 int PlatformFontIOS::GetFontSize() const {
88 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() {
90 static FontRenderParams params;
94 NativeFont PlatformFontIOS::GetNativeFont() const {
95 return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
99 ////////////////////////////////////////////////////////////////////////////////
100 // PlatformFontIOS, private:
102 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
105 Font::Weight weight) {
106 InitWithNameSizeAndStyle(font_name, font_size, style, weight);
109 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
112 Font::Weight weight) {
113 font_name_ = font_name;
114 font_size_ = font_size;
120 void PlatformFontIOS::CalculateMetrics() {
121 UIFont* font = GetNativeFont();
122 height_ = font.lineHeight;
123 ascent_ = font.ascender;
124 cap_height_ = font.capHeight;
125 average_width_ = [@"x" cr_sizeWithFont:font].width;
128 ////////////////////////////////////////////////////////////////////////////////
129 // PlatformFont, public:
132 PlatformFont* PlatformFont::CreateDefault() {
133 return new PlatformFontIOS;
137 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
138 return new PlatformFontIOS(native_font);
142 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
144 return new PlatformFontIOS(font_name, font_size);