Update V8 to version 3.28.7 (based on bleeding_edge revision r22083).
[chromium-blink-merge.git] / ui / gfx / platform_font_ios.mm
blobaba239b732dfddf5b17c0cc9af9e90109fb3fe3e
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"
14 namespace gfx {
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]);
24   CalculateMetrics();
27 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) {
28   std::string font_name = base::SysNSStringToUTF8([native_font fontName]);
29   InitWithNameSizeAndStyle(font_name,
30                            [native_font pointSize],
31                            gfx::Font::NORMAL);
34 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
35                                  int font_size) {
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 {
47   return height_;
50 int PlatformFontIOS::GetBaseline() const {
51   return ascent_;
54 int PlatformFontIOS::GetCapHeight() const {
55   return cap_height_;
58 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
59   return length * average_width_;
62 int PlatformFontIOS::GetStyle() const {
63   return style_;
66 std::string PlatformFontIOS::GetFontName() const {
67   return font_name_;
70 std::string PlatformFontIOS::GetActualFontNameForTesting() const {
71   return base::SysNSStringToUTF8([GetNativeFont() familyName]);
74 int PlatformFontIOS::GetFontSize() const {
75   return font_size_;
78 NativeFont PlatformFontIOS::GetNativeFont() const {
79   return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
80                          size:font_size_];
83 ////////////////////////////////////////////////////////////////////////////////
84 // PlatformFontIOS, private:
86 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
87                                  int font_size,
88                                  int style) {
89   InitWithNameSizeAndStyle(font_name, font_size, style);
92 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
93                                                int font_size,
94                                                int style) {
95   font_name_ = font_name;
96   font_size_ = font_size;
97   style_ = style;
98   CalculateMetrics();
101 void PlatformFontIOS::CalculateMetrics() {
102   UIFont* font = GetNativeFont();
103   height_ = font.lineHeight;
104   ascent_ = font.ascender;
105   cap_height_ = font.capHeight;
106   average_width_ = [@"x" sizeWithFont:font].width;
109 ////////////////////////////////////////////////////////////////////////////////
110 // PlatformFont, public:
112 // static
113 PlatformFont* PlatformFont::CreateDefault() {
114   return new PlatformFontIOS;
117 // static
118 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
119   return new PlatformFontIOS(native_font);
122 // static
123 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
124                                                   int font_size) {
125   return new PlatformFontIOS(font_name, font_size);
128 }  // namespace gfx