Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 return base::ScopedCFTypeRef<CTFontRef>( | 58 return base::ScopedCFTypeRef<CTFontRef>( |
| 59 CTFontCreateWithFontDescriptor(desc, 0.0, nullptr)); | 59 CTFontCreateWithFontDescriptor(desc, 0.0, nullptr)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 namespace gfx { | 64 namespace gfx { |
| 65 | 65 |
| 66 namespace internal { | 66 namespace internal { |
| 67 | 67 |
| 68 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, int style) { | 68 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, |
|
Alexei Svitkine (slow)
2016/04/05 16:38:53
Remove gfx::
| |
| 69 gfx::Font font_with_style = font.Derive(0, style); | 69 bool italic, |
| 70 gfx::Font::Weight weight) { | |
|
Alexei Svitkine (slow)
2016/04/05 16:38:53
Remove gfx::
| |
| 71 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL; | |
| 72 gfx::Font font_with_style = font.Derive(0, style, weight); | |
| 70 if (!font_with_style.GetNativeFont()) | 73 if (!font_with_style.GetNativeFont()) |
| 71 return nullptr; | 74 return nullptr; |
| 72 return skia::AdoptRef(SkCreateTypefaceFromCTFont( | 75 return skia::AdoptRef(SkCreateTypefaceFromCTFont( |
| 73 static_cast<CTFontRef>(font_with_style.GetNativeFont()))); | 76 static_cast<CTFontRef>(font_with_style.GetNativeFont()))); |
| 74 } | 77 } |
| 75 | 78 |
| 76 } // namespace internal | 79 } // namespace internal |
| 77 | 80 |
| 78 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} | 81 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} |
| 79 | 82 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 ApplyFadeEffects(renderer); | 207 ApplyFadeEffects(renderer); |
| 205 ApplyTextShadows(renderer); | 208 ApplyTextShadows(renderer); |
| 206 | 209 |
| 207 for (size_t i = 0; i < runs_.size(); ++i) { | 210 for (size_t i = 0; i < runs_.size(); ++i) { |
| 208 const TextRun& run = runs_[i]; | 211 const TextRun& run = runs_[i]; |
| 209 renderer->SetForegroundColor(run.foreground); | 212 renderer->SetForegroundColor(run.foreground); |
| 210 | 213 |
| 211 CTFontRef ct_font = static_cast<CTFontRef>(run.font.GetNativeFont()); | 214 CTFontRef ct_font = static_cast<CTFontRef>(run.font.GetNativeFont()); |
| 212 renderer->SetTextSize(CTFontGetSize(ct_font)); | 215 renderer->SetTextSize(CTFontGetSize(ct_font)); |
| 213 | 216 |
| 214 int font_style = Font::NORMAL; | 217 // TODO(mboc): Apply font weights. |
| 218 Font::Weight font_weight = Font::Weight::NORMAL; | |
| 215 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(ct_font); | 219 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(ct_font); |
| 216 if (traits & kCTFontBoldTrait) | 220 if (traits & kCTFontBoldTrait) |
| 217 font_style |= Font::BOLD; | 221 font_weight = Font::Weight::BOLD; |
| 218 if (traits & kCTFontItalicTrait) | 222 renderer->SetFont(run.font, traits & kCTFontItalicTrait, font_weight); |
| 219 font_style |= Font::ITALIC; | |
| 220 renderer->SetFontWithStyle(run.font, font_style); | |
| 221 | 223 |
| 222 renderer->DrawPosText(&run.glyph_positions[0], &run.glyphs[0], | 224 renderer->DrawPosText(&run.glyph_positions[0], &run.glyphs[0], |
| 223 run.glyphs.size()); | 225 run.glyphs.size()); |
| 224 renderer->DrawDecorations(run.origin.x(), run.origin.y(), run.width, | 226 renderer->DrawDecorations(run.origin.x(), run.origin.y(), run.width, |
| 225 run.underline, run.strike, run.diagonal_strike); | 227 run.underline, run.strike, run.diagonal_strike); |
| 226 } | 228 } |
| 227 | 229 |
| 228 renderer->EndDiagonalStrike(); | 230 renderer->EndDiagonalStrike(); |
| 229 } | 231 } |
| 230 | 232 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 ApplyCompositionAndSelectionStyles(); | 303 ApplyCompositionAndSelectionStyles(); |
| 302 | 304 |
| 303 // Note: CFAttributedStringSetAttribute() does not appear to retain the values | 305 // Note: CFAttributedStringSetAttribute() does not appear to retain the values |
| 304 // passed in, as can be verified via CFGetRetainCount(). To ensure the | 306 // passed in, as can be verified via CFGetRetainCount(). To ensure the |
| 305 // attribute objects do not leak, they are saved to |attributes_|. | 307 // attribute objects do not leak, they are saved to |attributes_|. |
| 306 // Clear the attributes storage. | 308 // Clear the attributes storage. |
| 307 base::ScopedCFTypeRef<CFMutableArrayRef> attributes( | 309 base::ScopedCFTypeRef<CFMutableArrayRef> attributes( |
| 308 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); | 310 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); |
| 309 | 311 |
| 310 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor eText_StringAttributes_Ref/Reference/reference.html | 312 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor eText_StringAttributes_Ref/Reference/reference.html |
| 311 internal::StyleIterator style(colors(), baselines(), styles()); | 313 internal::StyleIterator style(colors(), baselines(), weights(), styles()); |
| 312 const size_t layout_text_length = CFAttributedStringGetLength(attr_string); | 314 const size_t layout_text_length = CFAttributedStringGetLength(attr_string); |
| 313 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { | 315 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { |
| 314 end = TextIndexToGivenTextIndex(text, style.GetRange().end()); | 316 end = TextIndexToGivenTextIndex(text, style.GetRange().end()); |
| 315 const CFRange range = CFRangeMake(i, end - i); | 317 const CFRange range = CFRangeMake(i, end - i); |
| 316 base::ScopedCFTypeRef<CGColorRef> foreground( | 318 base::ScopedCFTypeRef<CGColorRef> foreground( |
| 317 skia::CGColorCreateFromSkColor(style.color())); | 319 skia::CGColorCreateFromSkColor(style.color())); |
| 318 CFAttributedStringSetAttribute(attr_string, range, | 320 CFAttributedStringSetAttribute(attr_string, range, |
| 319 kCTForegroundColorAttributeName, foreground); | 321 kCTForegroundColorAttributeName, foreground); |
| 320 CFArrayAppendValue(attributes, foreground); | 322 CFArrayAppendValue(attributes, foreground); |
| 321 | 323 |
| 322 if (style.style(UNDERLINE)) { | 324 if (style.style(UNDERLINE)) { |
| 323 CTUnderlineStyle value = kCTUnderlineStyleSingle; | 325 CTUnderlineStyle value = kCTUnderlineStyleSingle; |
| 324 base::ScopedCFTypeRef<CFNumberRef> underline_value( | 326 base::ScopedCFTypeRef<CFNumberRef> underline_value( |
| 325 CFNumberCreate(NULL, kCFNumberSInt32Type, &value)); | 327 CFNumberCreate(NULL, kCFNumberSInt32Type, &value)); |
| 326 CFAttributedStringSetAttribute( | 328 CFAttributedStringSetAttribute( |
| 327 attr_string, range, kCTUnderlineStyleAttributeName, underline_value); | 329 attr_string, range, kCTUnderlineStyleAttributeName, underline_value); |
| 328 CFArrayAppendValue(attributes, underline_value); | 330 CFArrayAppendValue(attributes, underline_value); |
| 329 } | 331 } |
| 330 | 332 |
| 331 const int traits = (style.style(BOLD) ? kCTFontBoldTrait : 0) | | 333 // TODO(mboc): Apply font weights other than bold below. |
| 332 (style.style(ITALIC) ? kCTFontItalicTrait : 0); | 334 const int traits = |
| 335 (style.style(ITALIC) ? kCTFontItalicTrait : 0) | | |
| 336 (style.weight() >= Font::Weight::BOLD ? kCTFontBoldTrait : 0); | |
| 333 if (traits != 0) { | 337 if (traits != 0) { |
| 334 base::ScopedCFTypeRef<CTFontRef> styled_font = | 338 base::ScopedCFTypeRef<CTFontRef> styled_font = |
| 335 CopyFontWithSymbolicTraits(font, traits); | 339 CopyFontWithSymbolicTraits(font, traits); |
| 336 // TODO(asvitkine): Handle |styled_font| == NULL case better. | 340 // TODO(asvitkine): Handle |styled_font| == NULL case better. |
| 337 if (styled_font) { | 341 if (styled_font) { |
| 338 CFAttributedStringSetAttribute(attr_string, range, kCTFontAttributeName, | 342 CFAttributedStringSetAttribute(attr_string, range, kCTFontAttributeName, |
| 339 styled_font); | 343 styled_font); |
| 340 CFArrayAppendValue(attributes, styled_font); | 344 CFArrayAppendValue(attributes, styled_font); |
| 341 } | 345 } |
| 342 } | 346 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 } | 437 } |
| 434 | 438 |
| 435 void RenderTextMac::InvalidateStyle() { | 439 void RenderTextMac::InvalidateStyle() { |
| 436 line_.reset(); | 440 line_.reset(); |
| 437 attributes_.reset(); | 441 attributes_.reset(); |
| 438 runs_.clear(); | 442 runs_.clear(); |
| 439 runs_valid_ = false; | 443 runs_valid_ = false; |
| 440 } | 444 } |
| 441 | 445 |
| 442 } // namespace gfx | 446 } // namespace gfx |
| OLD | NEW |