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_mac.h"
7 #include <Cocoa/Cocoa.h>
9 #include "base/basictypes.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font.h"
18 ////////////////////////////////////////////////////////////////////////////////
19 // PlatformFontMac, public:
21 PlatformFontMac::PlatformFontMac() {
22 font_size_ = [NSFont systemFontSize];
23 style_ = gfx::Font::NORMAL;
24 NSFont* system_font = [NSFont systemFontOfSize:font_size_];
25 font_name_ = base::SysNSStringToUTF8([system_font fontName]);
29 PlatformFontMac::PlatformFontMac(NativeFont native_font) {
31 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits];
32 if (traits & NSFontItalicTrait)
33 style |= Font::ITALIC;
34 if (traits & NSFontBoldTrait)
37 InitWithNameSizeAndStyle(base::SysNSStringToUTF8([native_font familyName]),
38 [native_font pointSize],
42 PlatformFontMac::PlatformFontMac(const std::string& font_name,
44 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
47 ////////////////////////////////////////////////////////////////////////////////
48 // PlatformFontMac, PlatformFont implementation:
50 Font PlatformFontMac::DeriveFont(int size_delta, int style) const {
51 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style));
54 int PlatformFontMac::GetHeight() const {
58 int PlatformFontMac::GetBaseline() const {
62 int PlatformFontMac::GetAverageCharacterWidth() const {
63 return average_width_;
66 int PlatformFontMac::GetStringWidth(const base::string16& text) const {
67 return Canvas::GetStringWidth(text,
68 Font(const_cast<PlatformFontMac*>(this)));
71 int PlatformFontMac::GetExpectedTextWidth(int length) const {
72 return length * average_width_;
75 int PlatformFontMac::GetStyle() const {
79 std::string PlatformFontMac::GetFontName() const {
83 int PlatformFontMac::GetFontSize() const {
87 NativeFont PlatformFontMac::GetNativeFont() const {
88 // We could cache this, but then we'd have to conditionally change the
89 // dtor just for MacOS. Not sure if we want to/need to do that.
90 NSFont* font = [NSFont fontWithName:base::SysUTF8ToNSString(font_name_)
93 if (style_ & Font::BOLD) {
94 font = [[NSFontManager sharedFontManager] convertFont:font
95 toHaveTrait:NSBoldFontMask];
97 if (style_ & Font::ITALIC) {
98 font = [[NSFontManager sharedFontManager] convertFont:font
99 toHaveTrait:NSItalicFontMask];
101 // Mac doesn't support underline as a font trait, just drop it. Underlines
102 // can instead be added as an attribute on an NSAttributedString.
107 ////////////////////////////////////////////////////////////////////////////////
108 // PlatformFontMac, private:
110 PlatformFontMac::PlatformFontMac(const std::string& font_name,
113 InitWithNameSizeAndStyle(font_name, font_size, style);
116 void PlatformFontMac::InitWithNameSizeAndStyle(const std::string& font_name,
119 font_name_ = font_name;
120 font_size_ = font_size;
125 void PlatformFontMac::CalculateMetrics() {
126 NSFont* font = GetNativeFont();
127 base::scoped_nsobject<NSLayoutManager> layout_manager(
128 [[NSLayoutManager alloc] init]);
129 height_ = [layout_manager defaultLineHeightForFont:font];
130 ascent_ = [font ascender];
132 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
135 ////////////////////////////////////////////////////////////////////////////////
136 // PlatformFont, public:
139 PlatformFont* PlatformFont::CreateDefault() {
140 return new PlatformFontMac;
144 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
145 return new PlatformFontMac(native_font);
149 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
151 return new PlatformFontMac(font_name, font_size);