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 if (elide_behavior() != NO_ELIDE && |
| 113 elide_behavior() != FADE_TAIL && |
| 114 !layout_text().empty()) { |
| 115 UpdateDisplayText(GetContentWidth()); |
| 116 } else { |
| 117 UpdateDisplayText(0); |
| 118 } |
| 119 } |
106 line_.reset(); | 120 line_.reset(); |
107 attributes_.reset(); | 121 attributes_.reset(); |
108 runs_.clear(); | 122 runs_.clear(); |
109 runs_valid_ = false; | 123 runs_valid_ = false; |
110 } | 124 } |
111 | 125 |
| 126 void RenderTextMac::OnDisplayTextAttributeChanged() { |
| 127 OnLayoutTextAttributeChanged(true); |
| 128 } |
| 129 |
112 void RenderTextMac::EnsureLayout() { | 130 void RenderTextMac::EnsureLayout() { |
113 if (line_.get()) | 131 if (line_.get()) |
114 return; | 132 return; |
115 runs_.clear(); | 133 runs_.clear(); |
116 runs_valid_ = false; | 134 runs_valid_ = false; |
117 | 135 |
118 CTFontRef ct_font = base::mac::NSToCFCast( | 136 CTFontRef ct_font = base::mac::NSToCFCast( |
119 font_list().GetPrimaryFont().GetNativeFont()); | 137 font_list().GetPrimaryFont().GetNativeFont()); |
120 | 138 |
121 const void* keys[] = { kCTFontAttributeName }; | 139 const void* keys[] = { kCTFontAttributeName }; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 ApplyCompositionAndSelectionStyles(); | 223 ApplyCompositionAndSelectionStyles(); |
206 | 224 |
207 // Note: CFAttributedStringSetAttribute() does not appear to retain the values | 225 // Note: CFAttributedStringSetAttribute() does not appear to retain the values |
208 // passed in, as can be verified via CFGetRetainCount(). To ensure the | 226 // passed in, as can be verified via CFGetRetainCount(). To ensure the |
209 // attribute objects do not leak, they are saved to |attributes_|. | 227 // attribute objects do not leak, they are saved to |attributes_|. |
210 // Clear the attributes storage. | 228 // Clear the attributes storage. |
211 attributes_.reset(CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); | 229 attributes_.reset(CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); |
212 | 230 |
213 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor
eText_StringAttributes_Ref/Reference/reference.html | 231 // https://developer.apple.com/library/mac/#documentation/Carbon/Reference/Cor
eText_StringAttributes_Ref/Reference/reference.html |
214 internal::StyleIterator style(colors(), styles()); | 232 internal::StyleIterator style(colors(), styles()); |
215 const size_t layout_text_length = GetLayoutText().length(); | 233 const size_t layout_text_length = GetDisplayText().length(); |
216 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { | 234 for (size_t i = 0, end = 0; i < layout_text_length; i = end) { |
217 end = TextIndexToLayoutIndex(style.GetRange().end()); | 235 end = TextIndexToDisplayIndex(style.GetRange().end()); |
218 const CFRange range = CFRangeMake(i, end - i); | 236 const CFRange range = CFRangeMake(i, end - i); |
219 base::ScopedCFTypeRef<CGColorRef> foreground( | 237 base::ScopedCFTypeRef<CGColorRef> foreground( |
220 CGColorCreateFromSkColor(style.color())); | 238 CGColorCreateFromSkColor(style.color())); |
221 CFAttributedStringSetAttribute(attr_string, range, | 239 CFAttributedStringSetAttribute(attr_string, range, |
222 kCTForegroundColorAttributeName, foreground); | 240 kCTForegroundColorAttributeName, foreground); |
223 CFArrayAppendValue(attributes_, foreground); | 241 CFArrayAppendValue(attributes_, foreground); |
224 | 242 |
225 if (style.style(UNDERLINE)) { | 243 if (style.style(UNDERLINE)) { |
226 CTUnderlineStyle value = kCTUnderlineStyleSingle; | 244 CTUnderlineStyle value = kCTUnderlineStyleSingle; |
227 base::ScopedCFTypeRef<CFNumberRef> underline_value( | 245 base::ScopedCFTypeRef<CFNumberRef> underline_value( |
(...skipping 10 matching lines...) Expand all Loading... |
238 base::ScopedCFTypeRef<CTFontRef> styled_font( | 256 base::ScopedCFTypeRef<CTFontRef> styled_font( |
239 CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, traits, traits)); | 257 CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, traits, traits)); |
240 // TODO(asvitkine): Handle |styled_font| == NULL case better. | 258 // TODO(asvitkine): Handle |styled_font| == NULL case better. |
241 if (styled_font) { | 259 if (styled_font) { |
242 CFAttributedStringSetAttribute(attr_string, range, kCTFontAttributeName, | 260 CFAttributedStringSetAttribute(attr_string, range, kCTFontAttributeName, |
243 styled_font); | 261 styled_font); |
244 CFArrayAppendValue(attributes_, styled_font); | 262 CFArrayAppendValue(attributes_, styled_font); |
245 } | 263 } |
246 } | 264 } |
247 | 265 |
248 style.UpdatePosition(LayoutIndexToTextIndex(end)); | 266 style.UpdatePosition(DisplayIndexToTextIndex(end)); |
249 } | 267 } |
250 | 268 |
251 // Undo the temporarily applied composition underlines and selection colors. | 269 // Undo the temporarily applied composition underlines and selection colors. |
252 UndoCompositionAndSelectionStyles(); | 270 UndoCompositionAndSelectionStyles(); |
253 } | 271 } |
254 | 272 |
255 void RenderTextMac::ComputeRuns() { | 273 void RenderTextMac::ComputeRuns() { |
256 DCHECK(line_); | 274 DCHECK(line_); |
257 | 275 |
258 CFArrayRef ct_runs = CTLineGetGlyphRuns(line_); | 276 CFArrayRef ct_runs = CTLineGetGlyphRuns(line_); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 CTUnderlineStyle value = kCTUnderlineStyleNone; | 357 CTUnderlineStyle value = kCTUnderlineStyleNone; |
340 if (underline && CFNumberGetValue(underline, kCFNumberSInt32Type, &value)) | 358 if (underline && CFNumberGetValue(underline, kCFNumberSInt32Type, &value)) |
341 run->underline = (value == kCTUnderlineStyleSingle); | 359 run->underline = (value == kCTUnderlineStyleSingle); |
342 | 360 |
343 run_origin.offset(run_width, 0); | 361 run_origin.offset(run_width, 0); |
344 } | 362 } |
345 runs_valid_ = true; | 363 runs_valid_ = true; |
346 } | 364 } |
347 | 365 |
348 } // namespace gfx | 366 } // namespace gfx |
OLD | NEW |