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 "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"
     13 
     14 namespace gfx {
     15 
     16 ////////////////////////////////////////////////////////////////////////////////
     17 // PlatformFontIOS, public:
     18 
     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();
     25 }
     26 
     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);
     32 }
     33 
     34 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
     35                                  int font_size) {
     36   InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
     37 }
     38 
     39 ////////////////////////////////////////////////////////////////////////////////
     40 // PlatformFontIOS, PlatformFont implementation:
     41 
     42 Font PlatformFontIOS::DeriveFont(int size_delta, int style) const {
     43   return Font(new PlatformFontIOS(font_name_, font_size_ + size_delta, style));
     44 }
     45 
     46 int PlatformFontIOS::GetHeight() const {
     47   return height_;
     48 }
     49 
     50 int PlatformFontIOS::GetBaseline() const {
     51   return ascent_;
     52 }
     53 
     54 int PlatformFontIOS::GetAverageCharacterWidth() const {
     55   return average_width_;
     56 }
     57 
     58 int PlatformFontIOS::GetStringWidth(const base::string16& text) const {
     59   NSString* ns_text = base::SysUTF16ToNSString(text);
     60   return [ns_text sizeWithFont:GetNativeFont()].width;
     61 }
     62 
     63 int PlatformFontIOS::GetExpectedTextWidth(int length) const {
     64   return length * average_width_;
     65 }
     66 
     67 int PlatformFontIOS::GetStyle() const {
     68   return style_;
     69 }
     70 
     71 std::string PlatformFontIOS::GetFontName() const {
     72   return font_name_;
     73 }
     74 
     75 int PlatformFontIOS::GetFontSize() const {
     76   return font_size_;
     77 }
     78 
     79 NativeFont PlatformFontIOS::GetNativeFont() const {
     80   return [UIFont fontWithName:base::SysUTF8ToNSString(font_name_)
     81                          size:font_size_];
     82 }
     83 
     84 ////////////////////////////////////////////////////////////////////////////////
     85 // PlatformFontIOS, private:
     86 
     87 PlatformFontIOS::PlatformFontIOS(const std::string& font_name,
     88                                  int font_size,
     89                                  int style) {
     90   InitWithNameSizeAndStyle(font_name, font_size, style);
     91 }
     92 
     93 void PlatformFontIOS::InitWithNameSizeAndStyle(const std::string& font_name,
     94                                                int font_size,
     95                                                int style) {
     96   font_name_ = font_name;
     97   font_size_ = font_size;
     98   style_ = style;
     99   CalculateMetrics();
    100 }
    101 
    102 void PlatformFontIOS::CalculateMetrics() {
    103   UIFont* font = GetNativeFont();
    104   height_ = font.lineHeight;
    105   ascent_ = font.ascender;
    106   average_width_ = [@"x" sizeWithFont:font].width;
    107 }
    108 
    109 ////////////////////////////////////////////////////////////////////////////////
    110 // PlatformFont, public:
    111 
    112 // static
    113 PlatformFont* PlatformFont::CreateDefault() {
    114   return new PlatformFontIOS;
    115 }
    116 
    117 // static
    118 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
    119   return new PlatformFontIOS(native_font);
    120 }
    121 
    122 // static
    123 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
    124                                                   int font_size) {
    125   return new PlatformFontIOS(font_name, font_size);
    126 }
    127 
    128 }  // namespace gfx
    129