Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / platform_font_ios.mm
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.
4
5 #include "ui/gfx/platform_font_ios.h"
6
7 #import <UIKit/UIKit.h>
8
9 #include <cmath>
10
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"
16
17 namespace gfx {
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // PlatformFontIOS, public:
21
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]);
27   CalculateMetrics();
28 }
29
30 PlatformFontIOS::PlatformFontIOS(NativeFont native_font) {
31   std::string font_name = base::SysNSStringToUTF8([native_font fontName]);
32   InitWithNameSizeAndStyle(font_name,
33                            [native_font pointSize],
34                            gfx::Font::NORMAL);
35 }
36
37 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
38                                  int font_size) {
39   InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
40 }
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // PlatformFontIOS, PlatformFont implementation:
44
45 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const {
46   return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style));
47 }
48
49 int PlatformFontIOS::GetHeight() const {
50   return height_;
51 }
52
53 int PlatformFontIOS::GetBaseline() const {
54   return ascent_;
55 }
56
57 int PlatformFontIOS::GetCapHeight() const {
58   return cap_height_;
59 }
60
61 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
62   return length * average_width_;
63 }
64
65 int PlatformFontIOS::GetStyle() const {
66   return style_;
67 }
68
69 std::string PlatformFontIOS::GetFontName() const {
70   return font_name_;
71 }
72
73 std::string PlatformFontIOS::GetActualFontNameForTesting() const {
74   return base::SysNSStringToUTF8([GetNativeFont() familyName]);
75 }
76
77 int PlatformFontIOS::GetFontSize() const {
78   return font_size_;
79 }
80
81 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() const {
82   NOTIMPLEMENTED();
83   static FontRenderParams params;
84   return params;
85 }
86
87 NativeFont PlatformFontIOS::GetNativeFont() const {
88   return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
89                          size:font_size_];
90 }
91
92 ////////////////////////////////////////////////////////////////////////////////
93 // PlatformFontIOS, private:
94
95 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
96                                  int font_size,
97                                  int style) {
98   InitWithNameSizeAndStyle(font_name, font_size, style);
99 }
100
101 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
102                                                int font_size,
103                                                int style) {
104   font_name_ = font_name;
105   font_size_ = font_size;
106   style_ = style;
107   CalculateMetrics();
108 }
109
110 void PlatformFontIOS::CalculateMetrics() {
111   UIFont* font = GetNativeFont();
112   height_ = font.lineHeight;
113   ascent_ = font.ascender;
114   cap_height_ = font.capHeight;
115   if (font) {
116     NSDictionary* attributes = @{ NSFontAttributeName : font };
117     average_width_ = std::ceil([@"x" sizeWithAttributes:attributes].width);
118   } else {
119     average_width_ = 0;
120   }
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////
124 // PlatformFont, public:
125
126 // static
127 PlatformFont* PlatformFont::CreateDefault() {
128   return new PlatformFontIOS;
129 }
130
131 // static
132 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
133   return new PlatformFontIOS(native_font);
134 }
135
136 // static
137 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
138                                                   int font_size) {
139   return new PlatformFontIOS(font_name, font_size);
140 }
141
142 }  // namespace gfx