Home | History | Annotate | Download | only in gfx
      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 #include "ui/ios/NSString+CrStringDrawing.h"
     17 
     18 namespace gfx {
     19 
     20 ////////////////////////////////////////////////////////////////////////////////
     21 // PlatformFontIOS, public:
     22 
     23 PlatformFontIOS::PlatformFontIOS() {
     24   font_size_ = [UIFont systemFontSize];
     25   style_ = gfx::Font::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,
     34                            [native_font pointSize],
     35                            gfx::Font::NORMAL);
     36 }
     37 
     38 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
     39                                  int font_size) {
     40   InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
     41 }
     42 
     43 ////////////////////////////////////////////////////////////////////////////////
     44 // PlatformFontIOS, PlatformFont implementation:
     45 
     46 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const {
     47   return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style));
     48 }
     49 
     50 int PlatformFontIOS::GetHeight() const {
     51   return height_;
     52 }
     53 
     54 int PlatformFontIOS::GetBaseline() const {
     55   return ascent_;
     56 }
     57 
     58 int PlatformFontIOS::GetCapHeight() const {
     59   return cap_height_;
     60 }
     61 
     62 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
     63   return length * average_width_;
     64 }
     65 
     66 int PlatformFontIOS::GetStyle() const {
     67   return style_;
     68 }
     69 
     70 std::string PlatformFontIOS::GetFontName() const {
     71   return font_name_;
     72 }
     73 
     74 std::string PlatformFontIOS::GetActualFontNameForTesting() const {
     75   return base::SysNSStringToUTF8([GetNativeFont() familyName]);
     76 }
     77 
     78 int PlatformFontIOS::GetFontSize() const {
     79   return font_size_;
     80 }
     81 
     82 const FontRenderParams& PlatformFontIOS::GetFontRenderParams() const {
     83   NOTIMPLEMENTED();
     84   static FontRenderParams params;
     85   return params;
     86 }
     87 
     88 NativeFont PlatformFontIOS::GetNativeFont() const {
     89   return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
     90                          size:font_size_];
     91 }
     92 
     93 ////////////////////////////////////////////////////////////////////////////////
     94 // PlatformFontIOS, private:
     95 
     96 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
     97                                  int font_size,
     98                                  int style) {
     99   InitWithNameSizeAndStyle(font_name, font_size, style);
    100 }
    101 
    102 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
    103                                                int font_size,
    104                                                int style) {
    105   font_name_ = font_name;
    106   font_size_ = font_size;
    107   style_ = style;
    108   CalculateMetrics();
    109 }
    110 
    111 void PlatformFontIOS::CalculateMetrics() {
    112   UIFont* font = GetNativeFont();
    113   height_ = font.lineHeight;
    114   ascent_ = font.ascender;
    115   cap_height_ = font.capHeight;
    116   average_width_ = [@"x" cr_sizeWithFont:font].width;
    117 }
    118 
    119 ////////////////////////////////////////////////////////////////////////////////
    120 // PlatformFont, public:
    121 
    122 // static
    123 PlatformFont* PlatformFont::CreateDefault() {
    124   return new PlatformFontIOS;
    125 }
    126 
    127 // static
    128 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
    129   return new PlatformFontIOS(native_font);
    130 }
    131 
    132 // static
    133 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
    134                                                   int font_size) {
    135   return new PlatformFontIOS(font_name, font_size);
    136 }
    137 
    138 }  // namespace gfx
    139