| 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 #include <ApplicationServices/ApplicationServices.h> | 7 #include <ApplicationServices/ApplicationServices.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
| 14 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "skia/ext/skia_utils_mac.h" | 16 #include "skia/ext/skia_utils_mac.h" |
| 17 | 17 |
| 18 namespace gfx { | 18 namespace gfx { |
| 19 | 19 |
| 20 RenderTextMac::RenderTextMac() : common_baseline_(0), runs_valid_(false) { | 20 RenderTextMac::RenderTextMac() |
| 21 : common_baseline_(0), runs_valid_(false) { |
| 21 } | 22 } |
| 22 | 23 |
| 23 RenderTextMac::~RenderTextMac() { | 24 RenderTextMac::~RenderTextMac() { |
| 24 } | 25 } |
| 25 | 26 |
| 26 scoped_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const { | 27 scoped_ptr<RenderText> RenderTextMac::CreateInstanceOfSameType() const { |
| 27 return scoped_ptr<RenderTextMac>(new RenderTextMac); | 28 return scoped_ptr<RenderTextMac>(new RenderTextMac); |
| 28 } | 29 } |
| 29 | 30 |
| 31 const base::string16& RenderTextMac::GetDisplayText() { |
| 32 return text_elided() ? display_text() : layout_text(); |
| 33 } |
| 34 |
| 30 Size RenderTextMac::GetStringSize() { | 35 Size RenderTextMac::GetStringSize() { |
| 31 EnsureLayout(); | 36 EnsureLayout(); |
| 32 return Size(std::ceil(string_size_.width()), string_size_.height()); | 37 return Size(std::ceil(string_size_.width()), string_size_.height()); |
| 33 } | 38 } |
| 34 | 39 |
| 35 SizeF RenderTextMac::GetStringSizeF() { | 40 SizeF RenderTextMac::GetStringSizeF() { |
| 36 EnsureLayout(); | 41 EnsureLayout(); |
| 37 return string_size_; | 42 return string_size_; |
| 38 } | 43 } |
| 39 | 44 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 51 for (size_t i = 0; i < runs_.size(); ++i) { | 56 for (size_t i = 0; i < runs_.size(); ++i) { |
| 52 Font font(runs_[i].font_name, runs_[i].text_size); | 57 Font font(runs_[i].font_name, runs_[i].text_size); |
| 53 const CFRange cf_range = CTRunGetStringRange(runs_[i].ct_run); | 58 const CFRange cf_range = CTRunGetStringRange(runs_[i].ct_run); |
| 54 const Range range(cf_range.location, cf_range.location + cf_range.length); | 59 const Range range(cf_range.location, cf_range.location + cf_range.length); |
| 55 spans.push_back(RenderText::FontSpan(font, range)); | 60 spans.push_back(RenderText::FontSpan(font, range)); |
| 56 } | 61 } |
| 57 | 62 |
| 58 return spans; | 63 return spans; |
| 59 } | 64 } |
| 60 | 65 |
| 61 int RenderTextMac::GetLayoutTextBaseline() { | 66 int RenderTextMac::GetDisplayTextBaseline() { |
| 62 EnsureLayout(); | 67 EnsureLayout(); |
| 63 return common_baseline_; | 68 return common_baseline_; |
| 64 } | 69 } |
| 65 | 70 |
| 66 SelectionModel RenderTextMac::AdjacentCharSelectionModel( | 71 SelectionModel RenderTextMac::AdjacentCharSelectionModel( |
| 67 const SelectionModel& selection, | 72 const SelectionModel& selection, |
| 68 VisualCursorDirection direction) { | 73 VisualCursorDirection direction) { |
| 69 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 74 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 70 return SelectionModel(); | 75 return SelectionModel(); |
| 71 } | 76 } |
| 72 | 77 |
| 73 SelectionModel RenderTextMac::AdjacentWordSelectionModel( | 78 SelectionModel RenderTextMac::AdjacentWordSelectionModel( |
| 74 const SelectionModel& selection, | 79 const SelectionModel& selection, |
| 75 VisualCursorDirection direction) { | 80 VisualCursorDirection direction) { |
| 76 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 81 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 77 return SelectionModel(); | 82 return SelectionModel(); |
| 78 } | 83 } |
| 79 | 84 |
| 80 Range RenderTextMac::GetGlyphBounds(size_t index) { | 85 Range RenderTextMac::GetGlyphBounds(size_t index) { |
| 81 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 86 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 82 return Range(); | 87 return Range(); |
| 83 } | 88 } |
| 84 | 89 |
| 85 std::vector<Rect> RenderTextMac::GetSubstringBounds(const Range& range) { | 90 std::vector<Rect> RenderTextMac::GetSubstringBounds(const Range& range) { |
| 86 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 91 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 87 return std::vector<Rect>(); | 92 return std::vector<Rect>(); |
| 88 } | 93 } |
| 89 | 94 |
| 90 size_t RenderTextMac::TextIndexToLayoutIndex(size_t index) const { | 95 size_t RenderTextMac::TextIndexToDisplayIndex(size_t index) { |
| 91 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 96 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 92 return index; | 97 return index; |
| 93 } | 98 } |
| 94 | 99 |
| 95 size_t RenderTextMac::LayoutIndexToTextIndex(size_t index) const { | 100 size_t RenderTextMac::DisplayIndexToTextIndex(size_t index) { |
| 96 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 101 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 97 return index; | 102 return index; |
| 98 } | 103 } |
| 99 | 104 |
| 100 bool RenderTextMac::IsValidCursorIndex(size_t index) { | 105 bool RenderTextMac::IsValidCursorIndex(size_t index) { |
| 101 // TODO(asvitkine): Implement this. http://crbug.com/131618 | 106 // TODO(asvitkine): Implement this. http://crbug.com/131618 |
| 102 return IsValidLogicalIndex(index); | 107 return IsValidLogicalIndex(index); |
| 103 } | 108 } |
| 104 | 109 |
| 105 void RenderTextMac::ResetLayout() { | 110 void RenderTextMac::OnLayoutTextAttributeChanged(bool text_changed) { |
| 111 if (text_changed && |
| 112 elide_behavior() != NO_ELIDE && |
| 113 elide_behavior() != FADE_TAIL && |
| 114 !layout_text().empty()) { |
| 115 UpdateDisplayText(GetContentWidth()); |
| 116 } |
| 106 line_.reset(); | 117 line_.reset(); |
| 107 attributes_.reset(); | 118 attributes_.reset(); |
| 108 runs_.clear(); | 119 runs_.clear(); |
| 109 runs_valid_ = false; | 120 runs_valid_ = false; |
| 110 } | 121 } |
| 111 | 122 |
| 123 void RenderTextMac::OnDisplayTextAttributeChanged() { |
| 124 OnLayoutTextAttributeChanged(true); |
| 125 } |
| 126 |
| 112 void RenderTextMac::EnsureLayout() { | 127 void RenderTextMac::EnsureLayout() { |
| 113 if (line_.get()) | 128 if (line_.get()) |
| 114 return; | 129 return; |
| 115 runs_.clear(); | 130 runs_.clear(); |
| 116 runs_valid_ = false; | 131 runs_valid_ = false; |
| 117 | 132 |
| 118 CTFontRef ct_font = base::mac::NSToCFCast( | 133 CTFontRef ct_font = base::mac::NSToCFCast( |
| 119 font_list().GetPrimaryFont().GetNativeFont()); | 134 font_list().GetPrimaryFont().GetNativeFont()); |
| 120 | 135 |
| 121 const void* keys[] = { kCTFontAttributeName }; | 136 const void* keys[] = { kCTFontAttributeName }; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 ApplyCompositionAndSelectionStyles(); | 220 ApplyCompositionAndSelectionStyles(); |
| 206 | 221 |
| 207 // Note: CFAttributedStringSetAttribute() does not appear to retain the values | 222 // Note: CFAttributedStringSetAttribute() does not appear to retain the values |
| 208 // passed in, as can be verified via CFGetRetainCount(). To ensure the | 223 // passed in, as can be verified via CFGetRetainCount(). To ensure the |
| 209 // attribute objects do not leak, they are saved to |attributes_|. | 224 // attribute objects do not leak, they are saved to |attributes_|. |
| 210 // Clear the attributes storage. | 225 // Clear the attributes storage. |
| 211 attributes_.reset(CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); | 226 attributes_.reset(CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); |
| 212 | 227 |
| 213 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor
eText_StringAttributes_Ref/Reference/reference.html | 228 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor
eText_StringAttributes_Ref/Reference/reference.html |
| 214 internal::StyleIterator style(colors(), styles()); | 229 internal::StyleIterator style(colors(), styles()); |
| 215 const size_t layout_text_length = GetLayoutText().length(); | 230 const size_t layout_text_length = GetDisplayText().length(); |
| 216 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { | 231 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { |
| 217 end = TextIndexToLayoutIndex(style.GetRange().end()); | 232 end = TextIndexToLayoutIndex(style.GetRange().end()); |
| 218 const CFRange range = CFRangeMake(i, end - i); | 233 const CFRange range = CFRangeMake(i, end - i); |
| 219 base::ScopedCFTypeRef<CGColorRef> foreground( | 234 base::ScopedCFTypeRef<CGColorRef> foreground( |
| 220 CGColorCreateFromSkColor(style.color())); | 235 CGColorCreateFromSkColor(style.color())); |
| 221 CFAttributedStringSetAttribute(attr_string, range, | 236 CFAttributedStringSetAttribute(attr_string, range, |
| 222 kCTForegroundColorAttributeName, foreground); | 237 kCTForegroundColorAttributeName, foreground); |
| 223 CFArrayAppendValue(attributes_, foreground); | 238 CFArrayAppendValue(attributes_, foreground); |
| 224 | 239 |
| 225 if (style.style(UNDERLINE)) { | 240 if (style.style(UNDERLINE)) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 CTUnderlineStyle value = kCTUnderlineStyleNone; | 354 CTUnderlineStyle value = kCTUnderlineStyleNone; |
| 340 if (underline && CFNumberGetValue(underline, kCFNumberSInt32Type, &value)) | 355 if (underline && CFNumberGetValue(underline, kCFNumberSInt32Type, &value)) |
| 341 run->underline = (value == kCTUnderlineStyleSingle); | 356 run->underline = (value == kCTUnderlineStyleSingle); |
| 342 | 357 |
| 343 run_origin.offset(run_width, 0); | 358 run_origin.offset(run_width, 0); |
| 344 } | 359 } |
| 345 runs_valid_ = true; | 360 runs_valid_ = true; |
| 346 } | 361 } |
| 347 | 362 |
| 348 } // namespace gfx | 363 } // namespace gfx |
| OLD | NEW |