3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
14 * \author Abdelrazak Younes
16 * Full author contact details are available in file CREDITS.
23 #include <boost/next_prior.hpp>
39 /// used by lower_bound and upper_bound
40 int operator()(FontTable const & a, FontTable const & b) const {
41 return a.pos() < b.pos();
47 FontList::iterator FontList::fontIterator(pos_type pos)
50 FontTable search_elem(pos, dummy);
51 return lower_bound(list_.begin(), list_.end(), search_elem,
56 FontList::const_iterator FontList::fontIterator(pos_type pos) const
59 FontTable search_elem(pos, dummy);
60 return lower_bound(list_.begin(), list_.end(), search_elem,
65 Font & FontList::get(pos_type pos)
67 iterator end = list_.end();
68 iterator it = fontIterator(pos);
69 if (it != end && it->pos() == pos)
76 void FontList::erase(pos_type pos)
78 // Erase entries in the tables.
79 iterator it = fontIterator(pos);
80 iterator beg = list_.begin();
81 if (it != list_.end() && it->pos() == pos
83 || (it != beg && boost::prior(it)->pos() == pos - 1))) {
85 // If it is a multi-character font
86 // entry, we just make it smaller
87 // (see update below), otherwise we
89 unsigned int const i = it - beg;
92 if (i > 0 && i < list_.size() &&
93 list_[i - 1].font() == list_[i].font()) {
94 list_.erase(beg + i - 1);
99 // Update all other entries
100 iterator end = list_.end();
101 for (; it != end; ++it)
102 it->pos(it->pos() - 1);
105 void FontList::increasePosAfterPos(pos_type pos)
107 List::iterator end = list_.end();
108 List::iterator it = fontIterator(pos);
109 for (; it != end; ++it)
114 void FontList::decreasePosAfterPos(pos_type pos)
116 List::iterator end = list_.end();
117 List::iterator it = fontIterator(pos);
118 for (; it != end; ++it)
123 void FontList::set(pos_type pos, Font const & font)
125 // No need to simplify this because it will disappear
126 // in a new kernel. (Asger)
127 // Next search font table
129 iterator beg = list_.begin();
131 iterator endit = list_.end();
132 for (; it != endit; ++it) {
133 if (it->pos() >= pos)
136 size_t const i = distance(beg, it);
137 bool notfound = (it == endit);
139 if (!notfound && list_[i].font() == font)
142 bool begin = pos == 0 || notfound ||
143 (i > 0 && list_[i - 1].pos() == pos - 1);
144 // Is position pos is a beginning of a font block?
145 bool end = !notfound && list_[i].pos() == pos;
146 // Is position pos is the end of a font block?
147 if (begin && end) { // A single char block
148 if (i + 1 < list_.size() &&
149 list_[i + 1].font() == font) {
150 // Merge the singleton block with the next block
151 list_.erase(list_.begin() + i);
152 if (i > 0 && list_[i - 1].font() == font)
153 list_.erase(list_.begin() + i - 1);
154 } else if (i > 0 && list_[i - 1].font() == font) {
155 // Merge the singleton block with the previous block
156 list_[i - 1].pos(pos);
157 list_.erase(list_.begin() + i);
161 if (i > 0 && list_[i - 1].font() == font)
162 list_[i - 1].pos(pos);
164 list_.insert(list_.begin() + i,
165 FontTable(pos, font));
167 list_[i].pos(pos - 1);
168 if (!(i + 1 < list_.size() &&
169 list_[i + 1].font() == font))
170 list_.insert(list_.begin() + i + 1,
171 FontTable(pos, font));
172 } else { // The general case. The block is splitted into 3 blocks
173 list_.insert(list_.begin() + i,
174 FontTable(pos - 1, list_[i].font()));
175 list_.insert(list_.begin() + i + 1,
176 FontTable(pos, font));
181 Font_size FontList::highestInRange
182 (pos_type startpos, pos_type endpos, Font_size def_size) const
187 const_iterator end_it = list_.begin();
188 const_iterator const end = list_.end();
189 for (; end_it != end; ++end_it) {
190 if (end_it->pos() >= endpos)
197 FontList::const_iterator cit = list_.begin();
198 for (; cit != end; ++cit) {
199 if (cit->pos() >= startpos)
203 Font::FONT_SIZE maxsize = Font::SIZE_TINY;
204 for (; cit != end_it; ++cit) {
205 Font::FONT_SIZE size = cit->font().size();
206 if (size == Font::INHERIT_SIZE)
208 if (size > maxsize && size <= Font::SIZE_HUGER)
215 bool FontList::hasChangeInRange(pos_type pos, int len) const
217 // FIXME: can't we use fontIterator(pos) instead?
218 const_iterator cit = list_.begin();
219 const_iterator end = list_.end();
220 for (; cit != end; ++cit) {
221 if (cit->pos() >= pos)
224 if (cit != end && pos + len - 1 > cit->pos())