Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: ui/gfx/platform_font_mac.mm

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes for platforms other than win and targets other than chrome Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/platform_font_mac.h ('k') | ui/gfx/platform_font_mac_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/platform_font_mac.h" 5 #include "ui/gfx/platform_font_mac.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include <Cocoa/Cocoa.h> 9 #include <Cocoa/Cocoa.h>
10 10
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // PlatformFontMac, public: 58 // PlatformFontMac, public:
59 59
60 PlatformFontMac::PlatformFontMac() 60 PlatformFontMac::PlatformFontMac()
61 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) { 61 : PlatformFontMac([NSFont systemFontOfSize:[NSFont systemFontSize]]) {
62 } 62 }
63 63
64 PlatformFontMac::PlatformFontMac(NativeFont native_font) 64 PlatformFontMac::PlatformFontMac(NativeFont native_font)
65 : native_font_([native_font retain]), 65 : native_font_([native_font retain]),
66 font_name_(base::SysNSStringToUTF8([native_font_ familyName])), 66 font_name_(base::SysNSStringToUTF8([native_font_ familyName])),
67 font_size_([native_font_ pointSize]), 67 font_size_([native_font_ pointSize]),
68 font_style_(Font::NORMAL) { 68 font_style_(Font::NORMAL),
69 font_weight_(Font::Weight::NORMAL) {
69 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits]; 70 NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits];
tapted 2016/03/23 23:00:06 Without a BUG= I can't grok the wider plan for thi
Mikus 2016/03/24 07:31:25 Done.
msw 2016/03/25 01:41:31 Please cite the bug you filed in the BUG= line of
tapted 2016/03/27 23:10:01 The bug still doesn't describe the _need_ for the
70 if (traits & NSFontItalicTrait) 71 if (traits & NSFontItalicTrait)
71 font_style_ |= Font::ITALIC; 72 font_style_ |= Font::ITALIC;
72 if (traits & NSFontBoldTrait) 73 if (traits & NSFontBoldTrait)
73 font_style_ |= Font::BOLD; 74 font_weight_ = Font::BOLD;
tapted 2016/03/23 23:00:06 Font::Weight::BOLD? - what you have shouldn't comp
Mikus 2016/03/24 07:31:25 Done.
75 else
76 font_weight_ = Font::NORMAL;
74 77
75 CalculateMetricsAndInitRenderParams(); 78 CalculateMetricsAndInitRenderParams();
76 } 79 }
77 80
78 PlatformFontMac::PlatformFontMac(const std::string& font_name, 81 PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size)
79 int font_size)
80 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]), 82 : native_font_([NSFontWithSpec(font_name, font_size, Font::NORMAL) retain]),
81 font_name_(font_name), 83 font_name_(font_name),
82 font_size_(font_size), 84 font_size_(font_size),
83 font_style_(Font::NORMAL) { 85 font_style_(Font::NORMAL),
86 font_weight_(Font::Weight::NORMAL) {
84 CalculateMetricsAndInitRenderParams(); 87 CalculateMetricsAndInitRenderParams();
85 } 88 }
86 89
87 //////////////////////////////////////////////////////////////////////////////// 90 ////////////////////////////////////////////////////////////////////////////////
88 // PlatformFontMac, PlatformFont implementation: 91 // PlatformFontMac, PlatformFont implementation:
89 92
90 Font PlatformFontMac::DeriveFont(int size_delta, int style) const { 93 Font PlatformFontMac::DeriveFont(int size_delta,
91 if (native_font_ && style == font_style_) { 94 int style,
95 Font::Weight weight) const {
96 if (native_font_ && style == font_style_ && weight == font_weight_) {
92 // System fonts have special attributes starting with 10.11. They should be 97 // System fonts have special attributes starting with 10.11. They should be
93 // requested using the same descriptor to preserve these attributes. 98 // requested using the same descriptor to preserve these attributes.
94 return Font(new PlatformFontMac( 99 return Font(new PlatformFontMac([NSFont
95 [NSFont fontWithDescriptor:[native_font_ fontDescriptor] 100 fontWithDescriptor:[native_font_ fontDescriptor]
96 size:font_size_ + size_delta])); 101 size:font_size_ + size_delta]));
97 } 102 }
98 103
99 return Font(new PlatformFontMac(font_name_, font_size_ + size_delta, style)); 104 return Font(
105 new PlatformFontMac(font_name_, font_size_ + size_delta, style, weight));
100 } 106 }
101 107
102 int PlatformFontMac::GetHeight() { 108 int PlatformFontMac::GetHeight() {
103 return height_; 109 return height_;
104 } 110 }
105 111
106 int PlatformFontMac::GetBaseline() { 112 int PlatformFontMac::GetBaseline() {
107 return ascent_; 113 return ascent_;
108 } 114 }
109 115
110 int PlatformFontMac::GetCapHeight() { 116 int PlatformFontMac::GetCapHeight() {
111 return cap_height_; 117 return cap_height_;
112 } 118 }
113 119
114 int PlatformFontMac::GetExpectedTextWidth(int length) { 120 int PlatformFontMac::GetExpectedTextWidth(int length) {
115 return length * average_width_; 121 return length * average_width_;
116 } 122 }
117 123
118 int PlatformFontMac::GetStyle() const { 124 int PlatformFontMac::GetStyle() const {
119 return font_style_; 125 return font_style_;
120 } 126 }
121 127
128 Font::Weight PlatformFontMac::GetWeight() const {
129 return font_weight_;
130 }
131
122 const std::string& PlatformFontMac::GetFontName() const { 132 const std::string& PlatformFontMac::GetFontName() const {
123 return font_name_; 133 return font_name_;
124 } 134 }
125 135
126 std::string PlatformFontMac::GetActualFontNameForTesting() const { 136 std::string PlatformFontMac::GetActualFontNameForTesting() const {
127 return base::SysNSStringToUTF8([native_font_ familyName]); 137 return base::SysNSStringToUTF8([native_font_ familyName]);
128 } 138 }
129 139
130 int PlatformFontMac::GetFontSize() const { 140 int PlatformFontMac::GetFontSize() const {
131 return font_size_; 141 return font_size_;
132 } 142 }
133 143
134 const FontRenderParams& PlatformFontMac::GetFontRenderParams() { 144 const FontRenderParams& PlatformFontMac::GetFontRenderParams() {
135 return render_params_; 145 return render_params_;
136 } 146 }
137 147
138 NativeFont PlatformFontMac::GetNativeFont() const { 148 NativeFont PlatformFontMac::GetNativeFont() const {
139 return [[native_font_.get() retain] autorelease]; 149 return [[native_font_.get() retain] autorelease];
140 } 150 }
141 151
142 //////////////////////////////////////////////////////////////////////////////// 152 ////////////////////////////////////////////////////////////////////////////////
143 // PlatformFontMac, private: 153 // PlatformFontMac, private:
144 154
145 PlatformFontMac::PlatformFontMac(const std::string& font_name, 155 PlatformFontMac::PlatformFontMac(const std::string& font_name,
146 int font_size, 156 int font_size,
147 int font_style) 157 int font_style,
158 Font::Weight font_weight)
148 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]), 159 : native_font_([NSFontWithSpec(font_name, font_size, font_style) retain]),
149 font_name_(font_name), 160 font_name_(font_name),
150 font_size_(font_size), 161 font_size_(font_size),
151 font_style_(font_style) { 162 font_style_(font_style),
163 font_weight_(font_weight) {
152 CalculateMetricsAndInitRenderParams(); 164 CalculateMetricsAndInitRenderParams();
153 } 165 }
154 166
155 PlatformFontMac::~PlatformFontMac() { 167 PlatformFontMac::~PlatformFontMac() {
156 } 168 }
157 169
158 void PlatformFontMac::CalculateMetricsAndInitRenderParams() { 170 void PlatformFontMac::CalculateMetricsAndInitRenderParams() {
159 NSFont* font = native_font_.get(); 171 NSFont* font = native_font_.get();
160 if (!font) { 172 if (!font) {
161 // This object was constructed from a font name that doesn't correspond to 173 // This object was constructed from a font name that doesn't correspond to
(...skipping 17 matching lines...) Expand all
179 // to ensure GetBaseline() + descender fits within GetHeight() during layout. 191 // to ensure GetBaseline() + descender fits within GetHeight() during layout.
180 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]); 192 height_ = ceil(ascent_ + std::abs([font descender]) + [font leading]);
181 193
182 average_width_ = 194 average_width_ =
183 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]); 195 NSWidth([font boundingRectForGlyph:[font glyphWithName:@"x"]]);
184 196
185 FontRenderParamsQuery query; 197 FontRenderParamsQuery query;
186 query.families.push_back(font_name_); 198 query.families.push_back(font_name_);
187 query.pixel_size = font_size_; 199 query.pixel_size = font_size_;
188 query.style = font_style_; 200 query.style = font_style_;
201 query.weight = font_weight_;
189 render_params_ = gfx::GetFontRenderParams(query, NULL); 202 render_params_ = gfx::GetFontRenderParams(query, NULL);
190 } 203 }
191 204
192 //////////////////////////////////////////////////////////////////////////////// 205 ////////////////////////////////////////////////////////////////////////////////
193 // PlatformFont, public: 206 // PlatformFont, public:
194 207
195 // static 208 // static
196 PlatformFont* PlatformFont::CreateDefault() { 209 PlatformFont* PlatformFont::CreateDefault() {
197 return new PlatformFontMac; 210 return new PlatformFontMac;
198 } 211 }
199 212
200 // static 213 // static
201 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { 214 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
202 return new PlatformFontMac(native_font); 215 return new PlatformFontMac(native_font);
203 } 216 }
204 217
205 // static 218 // static
206 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 219 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
207 int font_size) { 220 int font_size) {
208 return new PlatformFontMac(font_name, font_size); 221 return new PlatformFontMac(font_name, font_size);
209 } 222 }
210 223
211 } // namespace gfx 224 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/platform_font_mac.h ('k') | ui/gfx/platform_font_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698