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/memory/scoped_nsobject.h"
11 #include "base/sys_string_conversions.h"
12 #include "base/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) {
32 PlatformFontMac::PlatformFontMac(const std::string& font_name,
34 InitWithNameSizeAndStyle(font_name, font_size, gfx::Font::NORMAL);
37 ////////////////////////////////////////////////////////////////////////////////
38 // PlatformFontMac, PlatformFont implementation:
40 Font PlatformFontMac::DeriveFont(int size_delta, int style) const {
41 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style));
44 int PlatformFontMac::GetHeight() const {
48 int PlatformFontMac::GetBaseline() const {
52 int PlatformFontMac::GetAverageCharacterWidth() const {
53 return average_width_;
56 int PlatformFontMac::GetStringWidth(const string16& text) const {
57 return Canvas::GetStringWidth(text,
58 Font(const_cast<PlatformFontMac*>(this)));
61 int PlatformFontMac::GetExpectedTextWidth(int length) const {
62 return length * average_width_;
65 int PlatformFontMac::GetStyle() const {
69 std::string PlatformFontMac::GetFontName() const {
73 int PlatformFontMac::GetFontSize() const {
77 NativeFont PlatformFontMac::GetNativeFont() const {
78 // We could cache this, but then we'd have to conditionally change the
79 // dtor just for MacOS. Not sure if we want to/need to do that.
80 NSFont* font = [NSFont fontWithName:base::SysUTF8ToNSString(font_name_)
83 if (style_ & Font::BOLD) {
84 font = [[NSFontManager sharedFontManager] convertFont:font
85 toHaveTrait:NSBoldFontMask];
87 if (style_ & Font::ITALIC) {
88 font = [[NSFontManager sharedFontManager] convertFont:font
89 toHaveTrait:NSItalicFontMask];
91 // Mac doesn't support underline as a font trait, just drop it. Underlines
92 // can instead be added as an attribute on an NSAttributedString.
97 ////////////////////////////////////////////////////////////////////////////////
98 // PlatformFontMac, private:
100 PlatformFontMac::PlatformFontMac(const std::string& font_name,
103 InitWithNameSizeAndStyle(font_name, font_size, style);
106 void PlatformFontMac::InitWithNameSizeAndStyle(const std::string& font_name,
109 font_name_ = font_name;
110 font_size_ = font_size;
115 void PlatformFontMac::CalculateMetrics() {
116 NSFont* font = GetNativeFont();
117 scoped_nsobject<NSLayoutManager> layout_manager(
118 [[NSLayoutManager alloc] init]);
119 height_ = [layout_manager defaultLineHeightForFont:font];
120 ascent_ = [font ascender];
122 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
125 ////////////////////////////////////////////////////////////////////////////////
126 // PlatformFont, public:
129 PlatformFont* PlatformFont::CreateDefault() {
130 return new PlatformFontMac;
134 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
135 return new PlatformFontMac(native_font);
139 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
141 return new PlatformFontMac(font_name, font_size);