1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/platform_font_mac.h"
#include <Cocoa/Cocoa.h>
#include "base/basictypes.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_render_params.h"
namespace gfx {
namespace {
// Returns an autoreleased NSFont created with the passed-in specifications.
NSFont* NSFontWithSpec(const std::string& font_name,
int font_size,
int font_style) {
NSFontSymbolicTraits trait_bits = 0;
if (font_style & Font::BOLD)
trait_bits |= NSFontBoldTrait;
if (font_style & Font::ITALIC)
trait_bits |= NSFontItalicTrait;
// The Mac doesn't support underline as a font trait, so just drop it.
// (Underlines must be added as an attribute on an NSAttributedString.)
NSDictionary* traits = @{ NSFontSymbolicTrait : @(trait_bits) };
NSDictionary* attrs = @{
NSFontFamilyAttribute : base::SysUTF8ToNSString(font_name),
NSFontTraitsAttribute : traits
};
NSFontDescriptor* descriptor =
[NSFontDescriptor fontDescriptorWithFontAttributes:attrs];
NSFont* font = [NSFont fontWithDescriptor:descriptor size:font_size];
if (font)
return font;
// Make one fallback attempt by looking up via font name rather than font
// family name.
attrs = @{
NSFontNameAttribute : base::SysUTF8ToNSString(font_name),
NSFontTraitsAttribute : traits
};
descriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:attrs];
return [NSFont fontWithDescriptor:descriptor size:font_size];
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// PlatformFontMac, public:
PlatformFontMac::PlatformFontMac()
: native_font_([[NSFont systemFontOfSize:[NSFont systemFontSize]] retain]),
font_name_(base::SysNSStringToUTF8([native_font_ familyName])),
font_size_([NSFont systemFontSize]),
font_style_(Font::NORMAL) {
CalculateMetricsAndInitRenderParams();
}
PlatformFontMac::PlatformFontMac(NativeFont native_font)
: native_font_([native_font retain]),
font_name_(base::SysNSStringToUTF8([native_font_ familyName])),
font_size_([native_font_ pointSize]),
font_style_(Font::NORMAL) {
NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits];
if (traits & NSFontItalicTrait)
font_style_ |= Font::ITALIC;
if (traits & NSFontBoldTrait)
font_style_ |= Font::BOLD;
CalculateMetricsAndInitRenderParams();
}
PlatformFontMac::PlatformFontMac(const std::string& font_name,
int font_size)
: native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]),
font_name_(font_name),
font_size_(font_size),
font_style_(Font::NORMAL) {
CalculateMetricsAndInitRenderParams();
}
////////////////////////////////////////////////////////////////////////////////
// PlatformFontMac, PlatformFont implementation:
Font PlatformFontMac::DeriveFont(int size_delta, int style) const {
return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style));
}
int PlatformFontMac::GetHeight() const {
return height_;
}
int PlatformFontMac::GetBaseline() const {
return ascent_;
}
int PlatformFontMac::GetCapHeight() const {
return cap_height_;
}
int PlatformFontMac::GetExpectedTextWidth(int length) const {
return length * average_width_;
}
int PlatformFontMac::GetStyle() const {
return font_style_;
}
std::string PlatformFontMac::GetFontName() const {
return font_name_;
}
std::string PlatformFontMac::GetActualFontNameForTesting() const {
return base::SysNSStringToUTF8([native_font_ familyName]);
}
int PlatformFontMac::GetFontSize() const {
return font_size_;
}
const FontRenderParams& PlatformFontMac::GetFontRenderParams() {
return render_params_;
}
NativeFont PlatformFontMac::GetNativeFont() const {
return [[native_font_.get() retain] autorelease];
}
////////////////////////////////////////////////////////////////////////////////
// PlatformFontMac, private:
PlatformFontMac::PlatformFontMac(const std::string& font_name,
int font_size,
int font_style)
: native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]),
font_name_(font_name),
font_size_(font_size),
font_style_(font_style) {
CalculateMetricsAndInitRenderParams();
}
PlatformFontMac::~PlatformFontMac() {
}
void PlatformFontMac::CalculateMetricsAndInitRenderParams() {
NSFont* font = native_font_.get();
if (!font) {
// This object was constructed from a font name that doesn't correspond to
// an actual font. Don't waste time working out metrics.
height_ = 0;
ascent_ = 0;
cap_height_ = 0;
average_width_ = 0;
return;
}
base::scoped_nsobject<NSLayoutManager> layout_manager(
[[NSLayoutManager alloc] init]);
height_ = SkScalarCeilToInt([layout_manager defaultLineHeightForFont:font]);
ascent_ = SkScalarCeilToInt([font ascender]);
cap_height_ = SkScalarCeilToInt([font capHeight]);
average_width_ =
NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
FontRenderParamsQuery query(false);
query.families.push_back(font_name_);
query.pixel_size = font_size_;
query.style = font_style_;
render_params_ = gfx::GetFontRenderParams(query, NULL);
}
////////////////////////////////////////////////////////////////////////////////
// PlatformFont, public:
// static
PlatformFont* PlatformFont::CreateDefault() {
return new PlatformFontMac;
}
// static
PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
return new PlatformFontMac(native_font);
}
// static
PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
int font_size) {
return new PlatformFontMac(font_name, font_size);
}
} // namespace gfx
|