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