Disable flaky test ExtensionActionContextMenuTest.RunInspectPopup
[chromium-blink-merge.git] / ui / gfx / platform_font_mac.mm
blob5aaaddd818a05ef005bc3bfcb9bea69c258bd602
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"
16 namespace gfx {
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]);
26   CalculateMetrics();
29 PlatformFontMac::PlatformFontMac(NativeFont native_font) {
32 PlatformFontMac::PlatformFontMac(const std::string& font_name,
33                                  int font_size) {
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 {
45   return height_;
48 int PlatformFontMac::GetBaseline() const {
49   return ascent_;
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 {
66   return style_;
69 std::string PlatformFontMac::GetFontName() const {
70   return font_name_;
73 int PlatformFontMac::GetFontSize() const {
74   return font_size_;
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_)
81                                  size:font_size_];
83   if (style_ & Font::BOLD) {
84     font = [[NSFontManager sharedFontManager] convertFont:font
85                                               toHaveTrait:NSBoldFontMask];
86   }
87   if (style_ & Font::ITALIC) {
88     font = [[NSFontManager sharedFontManager] convertFont:font
89                                               toHaveTrait:NSItalicFontMask];
90   }
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.
94   return font;
97 ////////////////////////////////////////////////////////////////////////////////
98 // PlatformFontMac, private:
100 PlatformFontMac::PlatformFontMac(const std::string& font_name,
101                                  int font_size,
102                                  int style) {
103   InitWithNameSizeAndStyle(font_name, font_size, style);
106 void PlatformFontMac::InitWithNameSizeAndStyle(const std::string& font_name,
107                                                int font_size,
108                                                int style) {
109   font_name_ = font_name;
110   font_size_ = font_size;
111   style_ = style;
112   CalculateMetrics();
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];
121   average_width_ =
122       NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
125 ////////////////////////////////////////////////////////////////////////////////
126 // PlatformFont, public:
128 // static
129 PlatformFont* PlatformFont::CreateDefault() {
130   return new PlatformFontMac;
133 // static
134 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
135   return new PlatformFontMac(native_font);
138 // static
139 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
140                                                   int font_size) {
141   return new PlatformFontMac(font_name, font_size);
144 }  // namespace gfx