third_party/blink/renderer/platform/fonts/font_unique_name_lookup.cc
[chromium-dfly.git] / 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/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"
16
17 namespace gfx {
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // PlatformFontIOS, public:
21
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]);
28   CalculateMetrics();
29 }
30
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);
35 }
36
37 PlatformFontIOS::PlatformFontIOS(const std::string& font_name, int font_size) {
38   InitWithNameSizeAndStyle(font_name, font_size, Font::NORMAL,
39                            Font::Weight::NORMAL);
40 }
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // PlatformFontIOS, PlatformFont implementation:
44
45 Font PlatformFontIOS::DeriveFont(int size_delta,
46                                  int style,
47                                  Font::Weight weight) const {
48   return Font(
49       new PlatformFontIOS(font_name_, font_size_ + size_delta, style, weight));
50 }
51
52 int PlatformFontIOS::GetHeight() {
53   return height_;
54 }
55
56 int PlatformFontIOS::GetBaseline() {
57   return ascent_;
58 }
59
60 int PlatformFontIOS::GetCapHeight() {
61   return cap_height_;
62 }
63
64 int PlatformFontIOS::GetExpectedTextWidth(int length) {
65   return length * average_width_;
66 }
67
68 int PlatformFontIOS::GetStyle() const {
69   return style_;
70 }
71
72 Font::Weight PlatformFontIOS::GetWeight() const {
73   return weight_;
74 }
75
76 const std::string& PlatformFontIOS::GetFontName() const {
77   return font_name_;
78 }
79
80 std::string PlatformFontIOS::GetActualFontName() const {
81   return base::SysNSStringToUTF8([GetNativeFont() familyName]);
82 }
83
84 int PlatformFontIOS::GetFontSize() const {
85   return font_size_;
86 }
87
88 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() {
89   NOTIMPLEMENTED();
90   static FontRenderParams params;
91   return params;
92 }
93
94 NativeFont PlatformFontIOS::GetNativeFont() const {
95   return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
96                          size:font_size_];
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////
100 // PlatformFontIOS, private:
101
102 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
103                                  int font_size,
104                                  int style,
105                                  Font::Weight weight) {
106   InitWithNameSizeAndStyle(font_name, font_size, style, weight);
107 }
108
109 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
110                                                int font_size,
111                                                int style,
112                                                Font::Weight weight) {
113   font_name_ = font_name;
114   font_size_ = font_size;
115   style_ = style;
116   weight_ = weight;
117   CalculateMetrics();
118 }
119
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;
126 }
127
128 ////////////////////////////////////////////////////////////////////////////////
129 // PlatformFont, public:
130
131 // static
132 PlatformFont* PlatformFont::CreateDefault() {
133   return new PlatformFontIOS;
134 }
135
136 // static
137 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
138   return new PlatformFontIOS(native_font);
139 }
140
141 // static
142 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
143                                                   int font_size) {
144   return new PlatformFontIOS(font_name, font_size);
145 }
146
147 }  // namespace gfx