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"
15 #include "ui/gfx/font_render_params.h"
21 // Returns an autoreleased NSFont created with the passed-in specifications.
22 NSFont* NSFontWithSpec(const std::string& font_name,
25 NSFontSymbolicTraits trait_bits = 0;
26 if (font_style & Font::BOLD)
27 trait_bits |= NSFontBoldTrait;
28 if (font_style & Font::ITALIC)
29 trait_bits |= NSFontItalicTrait;
30 // The Mac doesn't support underline as a font trait, so just drop it.
31 // (Underlines must be added as an attribute on an NSAttributedString.)
32 NSDictionary* traits = @{ NSFontSymbolicTrait : @(trait_bits) };
34 NSDictionary* attrs = @{
35 NSFontFamilyAttribute : base::SysUTF8ToNSString(font_name),
36 NSFontTraitsAttribute : traits
38 NSFontDescriptor* descriptor =
39 [NSFontDescriptor fontDescriptorWithFontAttributes:attrs];
40 NSFont* font = [NSFont fontWithDescriptor:descriptor size:font_size];
44 // Make one fallback attempt by looking up via font name rather than font
47 NSFontNameAttribute : base::SysUTF8ToNSString(font_name),
48 NSFontTraitsAttribute : traits
50 descriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:attrs];
51 return [NSFont fontWithDescriptor:descriptor size:font_size];
56 ////////////////////////////////////////////////////////////////////////////////
57 // PlatformFontMac, public:
59 PlatformFontMac::PlatformFontMac()
60 : native_font_([[NSFont systemFontOfSize:[NSFont systemFontSize]] retain]),
61 font_name_(base::SysNSStringToUTF8([native_font_ familyName])),
62 font_size_([NSFont systemFontSize]),
63 font_style_(Font::NORMAL) {
67 PlatformFontMac::PlatformFontMac(NativeFont native_font)
68 : native_font_([native_font retain]),
69 font_name_(base::SysNSStringToUTF8([native_font_ familyName])),
70 font_size_([native_font_ pointSize]),
71 font_style_(Font::NORMAL) {
72 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits];
73 if (traits & NSFontItalicTrait)
74 font_style_ |= Font::ITALIC;
75 if (traits & NSFontBoldTrait)
76 font_style_ |= Font::BOLD;
81 PlatformFontMac::PlatformFontMac(const std::string& font_name,
83 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]),
84 font_name_(font_name),
85 font_size_(font_size),
86 font_style_(Font::NORMAL) {
90 ////////////////////////////////////////////////////////////////////////////////
91 // PlatformFontMac, PlatformFont implementation:
93 Font PlatformFontMac::DeriveFont(int size_delta, int style) const {
94 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style));
97 int PlatformFontMac::GetHeight() const {
101 int PlatformFontMac::GetBaseline() const {
105 int PlatformFontMac::GetCapHeight() const {
109 int PlatformFontMac::GetExpectedTextWidth(int length) const {
110 return length * average_width_;
113 int PlatformFontMac::GetStyle() const {
117 std::string PlatformFontMac::GetFontName() const {
121 std::string PlatformFontMac::GetActualFontNameForTesting() const {
122 return base::SysNSStringToUTF8([native_font_ familyName]);
125 int PlatformFontMac::GetFontSize() const {
129 const FontRenderParams& PlatformFontMac::GetFontRenderParams() const {
131 static FontRenderParams params;
135 NativeFont PlatformFontMac::GetNativeFont() const {
136 return [[native_font_.get() retain] autorelease];
139 ////////////////////////////////////////////////////////////////////////////////
140 // PlatformFontMac, private:
142 PlatformFontMac::PlatformFontMac(const std::string& font_name,
145 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]),
146 font_name_(font_name),
147 font_size_(font_size),
148 font_style_(font_style) {
152 PlatformFontMac::~PlatformFontMac() {
155 void PlatformFontMac::CalculateMetrics() {
156 NSFont* font = native_font_.get();
158 // This object was constructed from a font name that doesn't correspond to
159 // an actual font. Don't waste time working out metrics.
167 base::scoped_nsobject<NSLayoutManager> layout_manager(
168 [[NSLayoutManager alloc] init]);
169 height_ = SkScalarCeilToInt([layout_manager defaultLineHeightForFont:font]);
170 ascent_ = SkScalarCeilToInt([font ascender]);
171 cap_height_ = SkScalarCeilToInt([font capHeight]);
173 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
176 ////////////////////////////////////////////////////////////////////////////////
177 // PlatformFont, public:
180 PlatformFont* PlatformFont::CreateDefault() {
181 return new PlatformFontMac;
185 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
186 return new PlatformFontMac(native_font);
190 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
192 return new PlatformFontMac(font_name, font_size);