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

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

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Attempt to fix yet another unittest on Linux. Created 4 years, 6 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/render_text_harfbuzz.cc ('k') | ui/gfx/render_text_unittest.cc » ('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/render_text_mac.h" 5 #include "ui/gfx/render_text_mac.h"
6 6
7 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 #include <ApplicationServices/ApplicationServices.h> 8 #include <ApplicationServices/ApplicationServices.h>
9 #include <CoreText/CoreText.h> 9 #include <CoreText/CoreText.h>
10 10
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 return base::ScopedCFTypeRef<CTFontRef>( 59 return base::ScopedCFTypeRef<CTFontRef>(
60 CTFontCreateWithFontDescriptor(desc, 0.0, nullptr)); 60 CTFontCreateWithFontDescriptor(desc, 0.0, nullptr));
61 } 61 }
62 62
63 } // namespace 63 } // namespace
64 64
65 namespace gfx { 65 namespace gfx {
66 66
67 namespace internal { 67 namespace internal {
68 68
69 // Note: this is only used by RenderTextHarfbuzz. 69 sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font,
tapted 2016/06/02 01:02:23 looks like this comment got lost while resolving m
Mikus 2016/06/02 10:35:20 Done.
70 sk_sp<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, int style) { 70 bool italic,
71 gfx::Font font_with_style = font.Derive(0, style); 71 Font::Weight weight) {
72 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL;
73 Font font_with_style = font.Derive(0, style, weight);
72 if (!font_with_style.GetNativeFont()) 74 if (!font_with_style.GetNativeFont())
73 return nullptr; 75 return nullptr;
74 76
75 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont( 77 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont(
76 base::mac::NSToCFCast(font_with_style.GetNativeFont()))); 78 base::mac::NSToCFCast(font_with_style.GetNativeFont())));
77 } 79 }
78 80
79 } // namespace internal 81 } // namespace internal
80 82
81 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} 83 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {}
(...skipping 30 matching lines...) Expand all
112 std::vector<RenderText::FontSpan> RenderTextMac::GetFontSpansForTesting() { 114 std::vector<RenderText::FontSpan> RenderTextMac::GetFontSpansForTesting() {
113 EnsureLayout(); 115 EnsureLayout();
114 if (!runs_valid_) 116 if (!runs_valid_)
115 ComputeRuns(); 117 ComputeRuns();
116 118
117 std::vector<RenderText::FontSpan> spans; 119 std::vector<RenderText::FontSpan> spans;
118 for (size_t i = 0; i < runs_.size(); ++i) { 120 for (size_t i = 0; i < runs_.size(); ++i) {
119 const CFRange cf_range = CTRunGetStringRange(runs_[i].ct_run); 121 const CFRange cf_range = CTRunGetStringRange(runs_[i].ct_run);
120 const Range range(cf_range.location, cf_range.location + cf_range.length); 122 const Range range(cf_range.location, cf_range.location + cf_range.length);
121 spans.push_back(RenderText::FontSpan( 123 spans.push_back(RenderText::FontSpan(
122 gfx::Font(base::mac::CFToNSCast(runs_[i].ct_font.get())), range)); 124 Font(base::mac::CFToNSCast(runs_[i].ct_font.get())), range));
123 } 125 }
124 126
125 return spans; 127 return spans;
126 } 128 }
127 129
128 int RenderTextMac::GetDisplayTextBaseline() { 130 int RenderTextMac::GetDisplayTextBaseline() {
129 EnsureLayout(); 131 EnsureLayout();
130 return common_baseline_; 132 return common_baseline_;
131 } 133 }
132 134
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 RenderTextMac::TextRun::~TextRun() {} 245 RenderTextMac::TextRun::~TextRun() {}
244 246
245 float RenderTextMac::GetLayoutTextWidth() { 247 float RenderTextMac::GetLayoutTextWidth() {
246 base::ScopedCFTypeRef<CFMutableArrayRef> attributes_owner; 248 base::ScopedCFTypeRef<CFMutableArrayRef> attributes_owner;
247 base::ScopedCFTypeRef<CTLineRef> line( 249 base::ScopedCFTypeRef<CTLineRef> line(
248 EnsureLayoutInternal(layout_text(), &attributes_owner)); 250 EnsureLayoutInternal(layout_text(), &attributes_owner));
249 SkScalar baseline; 251 SkScalar baseline;
250 return GetCTLineSize(line.get(), &baseline).width(); 252 return GetCTLineSize(line.get(), &baseline).width();
251 } 253 }
252 254
253 gfx::SizeF RenderTextMac::GetCTLineSize(CTLineRef line, SkScalar* baseline) { 255 SizeF RenderTextMac::GetCTLineSize(CTLineRef line, SkScalar* baseline) {
254 CGFloat ascent = 0; 256 CGFloat ascent = 0;
255 CGFloat descent = 0; 257 CGFloat descent = 0;
256 CGFloat leading = 0; 258 CGFloat leading = 0;
257 // TODO(asvitkine): Consider using CTLineGetBoundsWithOptions() on 10.8+. 259 // TODO(asvitkine): Consider using CTLineGetBoundsWithOptions() on 10.8+.
258 double width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading); 260 double width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
259 // Ensure ascent and descent are not smaller than ones of the font list. 261 // Ensure ascent and descent are not smaller than ones of the font list.
260 // Keep them tall enough to draw often-used characters. 262 // Keep them tall enough to draw often-used characters.
261 // For example, if a text field contains a Japanese character, which is 263 // For example, if a text field contains a Japanese character, which is
262 // smaller than Latin ones, and then later a Latin one is inserted, this 264 // smaller than Latin ones, and then later a Latin one is inserted, this
263 // ensures that the text baseline does not shift. 265 // ensures that the text baseline does not shift.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 ApplyCompositionAndSelectionStyles(); 306 ApplyCompositionAndSelectionStyles();
305 307
306 // Note: CFAttributedStringSetAttribute() does not appear to retain the values 308 // Note: CFAttributedStringSetAttribute() does not appear to retain the values
307 // passed in, as can be verified via CFGetRetainCount(). To ensure the 309 // passed in, as can be verified via CFGetRetainCount(). To ensure the
308 // attribute objects do not leak, they are saved to |attributes_|. 310 // attribute objects do not leak, they are saved to |attributes_|.
309 // Clear the attributes storage. 311 // Clear the attributes storage.
310 base::ScopedCFTypeRef<CFMutableArrayRef> attributes( 312 base::ScopedCFTypeRef<CFMutableArrayRef> attributes(
311 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); 313 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks));
312 314
313 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor eText_StringAttributes_Ref/Reference/reference.html 315 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor eText_StringAttributes_Ref/Reference/reference.html
314 internal::StyleIterator style(colors(), baselines(), styles()); 316 internal::StyleIterator style(colors(), baselines(), weights(), styles());
315 const size_t layout_text_length = CFAttributedStringGetLength(attr_string); 317 const size_t layout_text_length = CFAttributedStringGetLength(attr_string);
316 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { 318 for (size_t i = 0, end = 0; i < layout_text_length; i = end) {
317 end = TextIndexToGivenTextIndex(text, style.GetRange().end()); 319 end = TextIndexToGivenTextIndex(text, style.GetRange().end());
318 const CFRange range = CFRangeMake(i, end - i); 320 const CFRange range = CFRangeMake(i, end - i);
319 base::ScopedCFTypeRef<CGColorRef> foreground( 321 base::ScopedCFTypeRef<CGColorRef> foreground(
320 skia::CGColorCreateFromSkColor(style.color())); 322 skia::CGColorCreateFromSkColor(style.color()));
321 CFAttributedStringSetAttribute(attr_string, range, 323 CFAttributedStringSetAttribute(attr_string, range,
322 kCTForegroundColorAttributeName, foreground); 324 kCTForegroundColorAttributeName, foreground);
323 CFArrayAppendValue(attributes, foreground); 325 CFArrayAppendValue(attributes, foreground);
324 326
325 if (style.style(UNDERLINE)) { 327 if (style.style(UNDERLINE)) {
326 CTUnderlineStyle value = kCTUnderlineStyleSingle; 328 CTUnderlineStyle value = kCTUnderlineStyleSingle;
327 base::ScopedCFTypeRef<CFNumberRef> underline_value( 329 base::ScopedCFTypeRef<CFNumberRef> underline_value(
328 CFNumberCreate(NULL, kCFNumberSInt32Type, &value)); 330 CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
329 CFAttributedStringSetAttribute( 331 CFAttributedStringSetAttribute(
330 attr_string, range, kCTUnderlineStyleAttributeName, underline_value); 332 attr_string, range, kCTUnderlineStyleAttributeName, underline_value);
331 CFArrayAppendValue(attributes, underline_value); 333 CFArrayAppendValue(attributes, underline_value);
332 } 334 }
333 335
334 const int traits = (style.style(BOLD) ? kCTFontBoldTrait : 0) | 336 // TODO(mboc): Apply font weights other than bold below.
335 (style.style(ITALIC) ? kCTFontItalicTrait : 0); 337 const int traits =
338 (style.style(ITALIC) ? kCTFontItalicTrait : 0) |
339 (style.weight() >= Font::Weight::BOLD ? kCTFontBoldTrait : 0);
336 if (traits != 0) { 340 if (traits != 0) {
337 base::ScopedCFTypeRef<CTFontRef> styled_font = 341 base::ScopedCFTypeRef<CTFontRef> styled_font =
338 CopyFontWithSymbolicTraits(font, traits); 342 CopyFontWithSymbolicTraits(font, traits);
339 // TODO(asvitkine): Handle |styled_font| == NULL case better. 343 // TODO(asvitkine): Handle |styled_font| == NULL case better.
340 if (styled_font) { 344 if (styled_font) {
341 CFAttributedStringSetAttribute(attr_string, range, kCTFontAttributeName, 345 CFAttributedStringSetAttribute(attr_string, range, kCTFontAttributeName,
342 styled_font); 346 styled_font);
343 CFArrayAppendValue(attributes, styled_font); 347 CFArrayAppendValue(attributes, styled_font);
344 } 348 }
345 } 349 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 } 441 }
438 442
439 void RenderTextMac::InvalidateStyle() { 443 void RenderTextMac::InvalidateStyle() {
440 line_.reset(); 444 line_.reset();
441 attributes_.reset(); 445 attributes_.reset();
442 runs_.clear(); 446 runs_.clear();
443 runs_valid_ = false; 447 runs_valid_ = false;
444 } 448 }
445 449
446 } // namespace gfx 450 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_harfbuzz.cc ('k') | ui/gfx/render_text_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698