| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 base::ScopedCFTypeRef<CTFontDescriptorRef> desc( | 57 base::ScopedCFTypeRef<CTFontDescriptorRef> desc( |
| 58 CTFontDescriptorCreateWithAttributes(attributes)); | 58 CTFontDescriptorCreateWithAttributes(attributes)); |
| 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 { | |
| 68 | |
| 69 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, int style) { | |
| 70 gfx::Font font_with_style = font.Derive(0, style); | |
| 71 if (!font_with_style.GetNativeFont()) | |
| 72 return nullptr; | |
| 73 return skia::AdoptRef(SkCreateTypefaceFromCTFont( | |
| 74 static_cast<CTFontRef>(font_with_style.GetNativeFont()))); | |
| 75 } | |
| 76 | |
| 77 } // namespace internal | |
| 78 | |
| 79 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} | 67 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) {} |
| 80 | 68 |
| 81 RenderTextMac::~RenderTextMac() {} | 69 RenderTextMac::~RenderTextMac() {} |
| 82 | 70 |
| 83 std::unique_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const { | 71 std::unique_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const { |
| 84 return base::WrapUnique(new RenderTextMac); | 72 return base::WrapUnique(new RenderTextMac); |
| 85 } | 73 } |
| 86 | 74 |
| 87 bool RenderTextMac::MultilineSupported() const { | 75 bool RenderTextMac::MultilineSupported() const { |
| 88 return false; | 76 return false; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 ApplyFadeEffects(renderer); | 193 ApplyFadeEffects(renderer); |
| 206 ApplyTextShadows(renderer); | 194 ApplyTextShadows(renderer); |
| 207 | 195 |
| 208 for (size_t i = 0; i < runs_.size(); ++i) { | 196 for (size_t i = 0; i < runs_.size(); ++i) { |
| 209 const TextRun& run = runs_[i]; | 197 const TextRun& run = runs_[i]; |
| 210 renderer->SetForegroundColor(run.foreground); | 198 renderer->SetForegroundColor(run.foreground); |
| 211 | 199 |
| 212 CTFontRef ct_font = static_cast<CTFontRef>(run.font.GetNativeFont()); | 200 CTFontRef ct_font = static_cast<CTFontRef>(run.font.GetNativeFont()); |
| 213 renderer->SetTextSize(CTFontGetSize(ct_font)); | 201 renderer->SetTextSize(CTFontGetSize(ct_font)); |
| 214 | 202 |
| 215 int font_style = Font::NORMAL; | 203 // The painter adds its own ref. So don't |release()| it from the ref ptr in |
| 216 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(ct_font); | 204 // TextRun. |
| 217 if (traits & kCTFontBoldTrait) | 205 renderer->SetTypeface(run.typeface.get()); |
| 218 font_style |= Font::BOLD; | |
| 219 if (traits & kCTFontItalicTrait) | |
| 220 font_style |= Font::ITALIC; | |
| 221 renderer->SetFontWithStyle(run.font, font_style); | |
| 222 | 206 |
| 223 renderer->DrawPosText(&run.glyph_positions[0], &run.glyphs[0], | 207 renderer->DrawPosText(&run.glyph_positions[0], &run.glyphs[0], |
| 224 run.glyphs.size()); | 208 run.glyphs.size()); |
| 225 renderer->DrawDecorations(run.origin.x(), run.origin.y(), run.width, | 209 renderer->DrawDecorations(run.origin.x(), run.origin.y(), run.width, |
| 226 run.underline, run.strike, run.diagonal_strike); | 210 run.underline, run.strike, run.diagonal_strike); |
| 227 } | 211 } |
| 228 | 212 |
| 229 renderer->EndDiagonalStrike(); | 213 renderer->EndDiagonalStrike(); |
| 230 } | 214 } |
| 231 | 215 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 point->set(x + SkDoubleToScalar(positions_ptr[glyph].x), | 394 point->set(x + SkDoubleToScalar(positions_ptr[glyph].x), |
| 411 y + SkDoubleToScalar(positions_ptr[glyph].y)); | 395 y + SkDoubleToScalar(positions_ptr[glyph].y)); |
| 412 } | 396 } |
| 413 | 397 |
| 414 // TODO(asvitkine): Style boundaries are not necessarily per-run. Handle | 398 // TODO(asvitkine): Style boundaries are not necessarily per-run. Handle |
| 415 // this better. Also, support strike and diagonal_strike. | 399 // this better. Also, support strike and diagonal_strike. |
| 416 CFDictionaryRef attributes = CTRunGetAttributes(ct_run); | 400 CFDictionaryRef attributes = CTRunGetAttributes(ct_run); |
| 417 CTFontRef ct_font = base::mac::GetValueFromDictionary<CTFontRef>( | 401 CTFontRef ct_font = base::mac::GetValueFromDictionary<CTFontRef>( |
| 418 attributes, kCTFontAttributeName); | 402 attributes, kCTFontAttributeName); |
| 419 run->font = Font(static_cast<NSFont*>(ct_font)); | 403 run->font = Font(static_cast<NSFont*>(ct_font)); |
| 404 run->typeface.reset(SkCreateTypefaceFromCTFont(ct_font)); |
| 420 | 405 |
| 421 const CGColorRef foreground = base::mac::GetValueFromDictionary<CGColorRef>( | 406 const CGColorRef foreground = base::mac::GetValueFromDictionary<CGColorRef>( |
| 422 attributes, kCTForegroundColorAttributeName); | 407 attributes, kCTForegroundColorAttributeName); |
| 423 if (foreground) | 408 if (foreground) |
| 424 run->foreground = skia::CGColorRefToSkColor(foreground); | 409 run->foreground = skia::CGColorRefToSkColor(foreground); |
| 425 | 410 |
| 426 const CFNumberRef underline = | 411 const CFNumberRef underline = |
| 427 base::mac::GetValueFromDictionary<CFNumberRef>( | 412 base::mac::GetValueFromDictionary<CFNumberRef>( |
| 428 attributes, kCTUnderlineStyleAttributeName); | 413 attributes, kCTUnderlineStyleAttributeName); |
| 429 CTUnderlineStyle value = kCTUnderlineStyleNone; | 414 CTUnderlineStyle value = kCTUnderlineStyleNone; |
| 430 if (underline && CFNumberGetValue(underline, kCFNumberSInt32Type, &value)) | 415 if (underline && CFNumberGetValue(underline, kCFNumberSInt32Type, &value)) |
| 431 run->underline = (value == kCTUnderlineStyleSingle); | 416 run->underline = (value == kCTUnderlineStyleSingle); |
| 432 | 417 |
| 433 run_origin.offset(run_width, 0); | 418 run_origin.offset(run_width, 0); |
| 434 } | 419 } |
| 435 runs_valid_ = true; | 420 runs_valid_ = true; |
| 436 } | 421 } |
| 437 | 422 |
| 438 void RenderTextMac::InvalidateStyle() { | 423 void RenderTextMac::InvalidateStyle() { |
| 439 line_.reset(); | 424 line_.reset(); |
| 440 attributes_.reset(); | 425 attributes_.reset(); |
| 441 runs_.clear(); | 426 runs_.clear(); |
| 442 runs_valid_ = false; | 427 runs_valid_ = false; |
| 443 } | 428 } |
| 444 | 429 |
| 445 } // namespace gfx | 430 } // namespace gfx |
| OLD | NEW |