Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: ui/gfx/break_list.h

Issue 11535014: Replace StyleRange with BreakList; update RenderText, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments and lint errors. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef UI_GFX_BREAK_LIST_H_
6 #define UI_GFX_BREAK_LIST_H_
7
8 #include <utility>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "ui/base/range/range.h"
14
15 namespace gfx {
16
17 // BreakLists manage ordered, non-overlapping, and non-repeating ranged values.
18 // These may be used to apply ranged colors and styles to text, for an example.
19 //
20 // Each break stores the start position and value of its associated range.
21 // A solitary break at position 0 applies to the entire space [0, max_).
Alexei Svitkine (slow) 2013/01/31 21:25:07 Can you document what happens if max_ is not speci
msw 2013/02/01 00:08:08 Done.
22 // The first break always has position 0, to ensure all positions have a value.
23 // The value of the terminal break applies to the range [break.first, max_).
24 // The value of other breaks apply to the range [break.first, (break+1).first).
25 template <typename T>
26 class BreakList {
27 public:
28 // The break type and const iterator, typedef'ed for convenience.
29 typedef std::pair<size_t, T> Break;
30 typedef typename std::vector<Break>::const_iterator const_iterator;
31
32 // Initialize a break at position 0 with the default or supplied |value|.
33 BreakList();
34 explicit BreakList(T value);
35
36 const std::vector<Break>& breaks() const { return breaks_; }
37
38 // Clear the breaks and set a break at position 0 with the supplied |value|.
39 void SetValue(T value);
40
41 // Adjust the breaks to apply |value| over the supplied |range|.
42 void ApplyValue(T value, const ui::Range& range);
43
44 // Set the max position and trim any breaks at or beyond that position.
45 void SetMax(size_t max);
46
47 // Get the break applicable to |position| (at or preceeding |position|).
48 typename std::vector<Break>::iterator GetBreak(size_t position);
49
50 // Get the range of the supplied break; returns the break's start position and
51 // the next break's start position (or |max_| for the terminal break).
52 ui::Range GetRange(const typename BreakList<T>::const_iterator& i) const;
53
54 // Comparison functions for testing purposes.
55 bool EqualsValueForTesting(T value) const;
56 bool EqualsForTesting(const std::vector<Break>& breaks) const;
57
58 private:
59 #ifndef NDEBUG
60 // Check for ordered breaks [0, |max_|) with no adjacent equivalent values.
61 void CheckBreaks();
62 #endif
63
64 std::vector<Break> breaks_;
65 size_t max_;
66 };
67
68 template<class T>
69 BreakList<T>::BreakList() : breaks_(1, Break(0, T())), max_(0) {
Alexei Svitkine (slow) 2013/01/31 21:25:07 Nit: Remove spurious indent.
msw 2013/02/01 00:08:08 Done.
70 }
71
72 template<class T>
73 BreakList<T>::BreakList(T value) : breaks_(1, Break(0, value)), max_(0) {
74 }
75
76 template<class T>
77 void BreakList<T>::SetValue(T value) {
78 breaks_.clear();
79 breaks_.push_back(Break(0, value));
80 }
81
82 template<class T>
83 void BreakList<T>::ApplyValue(T value, const ui::Range& range) {
84 if (!range.IsValid() || range.is_empty())
85 return;
86 DCHECK(!breaks_.empty());
87 DCHECK(!range.is_reversed());
88 DCHECK(ui::Range(0, max_).Contains(range));
89
90 // Erase any breaks in |range|, then add start and end breaks as needed.
91 typename std::vector<Break>::iterator start = GetBreak(range.start());
92 start += start->first < range.start() ? 1 : 0;
93 typename std::vector<Break>::iterator end = GetBreak(range.end());
94 T trailing_value = end->second;
95 typename std::vector<Break>::iterator i =
96 start == breaks_.end() ? start : breaks_.erase(start, end + 1);
97 if (range.start() == 0 || (i - 1)->second != value)
98 i = breaks_.insert(i, Break(range.start(), value)) + 1;
99 if (trailing_value != value && range.end() != max_)
100 breaks_.insert(i, Break(range.end(), trailing_value));
101
102 #ifndef NDEBUG
103 CheckBreaks();
104 #endif
105 }
106
107 template<class T>
108 void BreakList<T>::SetMax(size_t max) {
109 typename std::vector<Break>::iterator i = GetBreak(max);
110 i += (i == breaks_.begin() || i->first < max) ? 1 : 0;
111 breaks_.erase(i, breaks_.end());
112 max_ = max;
113
114 #ifndef NDEBUG
115 CheckBreaks();
116 #endif
117 }
118
119 template<class T>
120 typename std::vector<std::pair<size_t, T> >::iterator BreakList<T>::GetBreak(
121 size_t position) {
122 typename std::vector<Break>::iterator i = breaks_.end() - 1;
123 for (; i != breaks_.begin() && i->first > position; --i);
124 return i;
125 }
126
127 template<class T>
128 ui::Range BreakList<T>::GetRange(
129 const typename BreakList<T>::const_iterator& i) const {
130 const typename BreakList<T>::const_iterator next = i + 1;
131 return ui::Range(i->first, next == breaks_.end() ? max_ : next->first);
132 }
133
134 template<class T>
135 bool BreakList<T>::EqualsValueForTesting(T value) const {
136 return breaks_.size() == 1 && breaks_[0] == Break(0, value);
137 }
138
139 template<class T>
140 bool BreakList<T>::EqualsForTesting(const std::vector<Break>& breaks) const {
141 if (breaks_.size() != breaks.size())
142 return false;
143 for (size_t i = 0; i < breaks.size(); ++i)
144 if (breaks_[i] != breaks[i])
145 return false;
146 return true;
147 }
148
149 #ifndef NDEBUG
150 template <class T>
151 void BreakList<T>::CheckBreaks() {
152 DCHECK_EQ(breaks_[0].first, 0U) << "The first break must be at position 0.";
153 for (size_t i = 0; i < breaks_.size() - 1; ++i) {
154 DCHECK_LT(breaks_[i].first, breaks_[i + 1].first) << "Break out of order.";
155 DCHECK_NE(breaks_[i].second, breaks_[i + 1].second) << "Redundant break.";
156 }
157 if (max_ > 0)
158 DCHECK_LT(breaks_.back().first, max_) << "Break beyond max position.";
159 }
160 #endif
161
162 } // namespace gfx
163
164 #endif // UI_GFX_BREAK_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698