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) {
64 CalculateMetricsAndInitRenderParams();
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;
78 CalculateMetricsAndInitRenderParams();
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) {
87 CalculateMetricsAndInitRenderParams();
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 {
130 return render_params_;
133 NativeFont PlatformFontMac::GetNativeFont() const {
134 return [[native_font_.get() retain] autorelease];
137 ////////////////////////////////////////////////////////////////////////////////
138 // PlatformFontMac, private:
140 PlatformFontMac::PlatformFontMac(const std::string& font_name,
143 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]),
144 font_name_(font_name),
145 font_size_(font_size),
146 font_style_(font_style) {
147 CalculateMetricsAndInitRenderParams();
150 PlatformFontMac::~PlatformFontMac() {
153 void PlatformFontMac::CalculateMetricsAndInitRenderParams() {
154 NSFont* font = native_font_.get();
156 // This object was constructed from a font name that doesn't correspond to
157 // an actual font. Don't waste time working out metrics.
165 base::scoped_nsobject<NSLayoutManager> layout_manager(
166 [[NSLayoutManager alloc] init]);
167 height_ = SkScalarCeilToInt([layout_manager defaultLineHeightForFont:font]);
168 ascent_ = SkScalarCeilToInt([font ascender]);
169 cap_height_ = SkScalarCeilToInt([font capHeight]);
171 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
173 FontRenderParamsQuery query(false);
174 query.families.push_back(font_name_);
175 query.pixel_size = font_size_;
176 query.style = font_style_;
177 render_params_ = gfx::GetFontRenderParams(query, NULL);
180 ////////////////////////////////////////////////////////////////////////////////
181 // PlatformFont, public:
184 PlatformFont* PlatformFont::CreateDefault() {
185 return new PlatformFontMac;
189 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
190 return new PlatformFontMac(native_font);
194 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
196 return new PlatformFontMac(font_name, font_size);