]> git.lyx.org Git - features.git/blob - src/FontList.cpp
Optimisation: away creation of FontTable for each FontList::fontIterator() call.
[features.git] / src / FontList.cpp
1 /**
2  * \file FontList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  * \author Abdelrazak Younes
15  *
16  * Full author contact details are available in file CREDITS.
17  */
18
19 #include <config.h>
20
21 #include "FontList.h"
22
23 #include <boost/next_prior.hpp>
24
25 #include <algorithm>
26
27 using namespace std;
28
29 namespace lyx {
30
31
32 FontList::iterator FontList::fontIterator(pos_type pos)
33 {
34         FontList::iterator it = list_.begin();
35         FontList::iterator end = list_.end();
36         for (; it != end; ++it) {
37                 if (it->pos() >= pos)
38                         break;
39         }
40         return it;
41 }
42
43
44 FontList::const_iterator FontList::fontIterator(pos_type pos) const
45 {
46         FontList::const_iterator it = list_.begin();
47         FontList::const_iterator end = list_.end();
48         for (; it != end; ++it) {
49                 if (it->pos() >= pos)
50                         break;
51         }
52         return it;
53 }
54
55
56 Font & FontList::get(pos_type pos)
57 {
58         iterator end = list_.end();
59         iterator it = fontIterator(pos);
60         if (it != end && it->pos() == pos)
61                 return it->font_;
62         static Font dummy;
63         return dummy;
64 }
65
66
67 void FontList::erase(pos_type pos)
68 {
69         // Erase entries in the tables.
70         iterator it = fontIterator(pos);
71         iterator beg = list_.begin();
72         if (it != list_.end() && it->pos() == pos
73                 && (pos == 0 
74                         || (it != list_.begin() && boost::prior(it)->pos() == pos - 1))) {
75
76                 // If it is a multi-character font
77                 // entry, we just make it smaller
78                 // (see update below), otherwise we
79                 // should delete it.
80                 unsigned int const i = it - list_.begin();
81                 list_.erase(it);
82                 if (i >= list_.size())
83                         return;
84                 it = list_.begin() + i;
85                 if (i > 0 && i < list_.size() &&
86                     list_[i - 1].font() == list_[i].font()) {
87                         list_.erase(beg + i - 1);
88                         it = list_.begin() + i - 1;
89                 }
90         }
91
92         // Update all other entries
93         iterator end = list_.end();
94         for (; it != end; ++it)
95                 it->pos(it->pos() - 1);
96 }
97
98 void FontList::increasePosAfterPos(pos_type pos)
99 {
100         List::iterator end = list_.end();
101         List::iterator it = fontIterator(pos);
102         for (; it != end; ++it)
103                 ++it->pos_;
104 }
105
106
107 void FontList::decreasePosAfterPos(pos_type pos)
108 {
109         List::iterator end = list_.end();
110         List::iterator it = fontIterator(pos);
111         for (; it != end; ++it)
112                 --it->pos_;
113 }
114
115
116 void FontList::setRange(pos_type startpos, pos_type endpos, Font const & font)
117 {
118         // FIXME: Optimize!!!
119         for (pos_type pos = startpos; pos != endpos; ++pos)
120                 set(pos, font);
121 }
122
123
124 void FontList::set(pos_type pos, Font const & font)
125 {
126         // No need to simplify this because it will disappear
127         // in a new kernel. (Asger)
128         // Next search font table
129
130         iterator beg = list_.begin();
131         iterator it = beg;
132         iterator endit = list_.end();
133         bool found = false;
134         for (; it != endit; ++it) {
135                 if (it->pos() >= pos) {
136                         found = true;
137                         break;
138                 }
139         }
140         if (found && it->font() == font)
141                 return;
142
143         size_t const i = distance(beg, it);
144
145         // Is position pos is a beginning of a font block?
146         bool begin = pos == 0 || !found 
147                 || (i > 0 && list_[i - 1].pos() == pos - 1);
148
149         // Is position pos is the end of a font block?
150         bool end = found && list_[i].pos() == pos;
151
152         if (!begin && !end) {
153                 // The general case: The block is splitted into 3 blocks
154                 list_.insert(list_.begin() + i,
155                                 FontTable(pos - 1, list_[i].font()));
156                 list_.insert(list_.begin() + i + 1,
157                                 FontTable(pos, font));
158                 return;
159         }
160
161         if (begin && end) {
162                 // A single char block
163                 if (i + 1 < list_.size() &&
164                     list_[i + 1].font() == font) {
165                         // Merge the singleton block with the next block
166                         list_.erase(list_.begin() + i);
167                         if (i > 0 && list_[i - 1].font() == font)
168                                 list_.erase(list_.begin() + i - 1);
169                 } else if (i > 0 && list_[i - 1].font() == font) {
170                         // Merge the singleton block with the previous block
171                         list_[i - 1].pos(pos);
172                         list_.erase(list_.begin() + i);
173                 } else
174                         list_[i].font(font);
175         } else if (begin) {
176                 if (i > 0 && list_[i - 1].font() == font)
177                         list_[i - 1].pos(pos);
178                 else
179                         list_.insert(list_.begin() + i,
180                                         FontTable(pos, font));
181         } else if (end) {
182                 list_[i].pos(pos - 1);
183                 if (!(i + 1 < list_.size() &&
184                       list_[i + 1].font() == font))
185                         list_.insert(list_.begin() + i + 1,
186                                         FontTable(pos, font));
187         }
188 }
189
190
191 FontSize FontList::highestInRange
192         (pos_type startpos, pos_type endpos, FontSize def_size) const
193 {
194         if (list_.empty())
195                 return def_size;
196
197         const_iterator end_it = list_.begin();
198         const_iterator const end = list_.end();
199         for (; end_it != end; ++end_it) {
200                 if (end_it->pos() >= endpos)
201                         break;
202         }
203
204         if (end_it != end)
205                 ++end_it;
206
207         FontList::const_iterator cit = list_.begin();
208         for (; cit != end; ++cit) {
209                 if (cit->pos() >= startpos)
210                         break;
211         }
212
213         FontSize maxsize = FONT_SIZE_TINY;
214         for (; cit != end_it; ++cit) {
215                 FontSize size = cit->font().fontInfo().size();
216                 if (size == FONT_SIZE_INHERIT)
217                         size = def_size;
218                 if (size > maxsize && size <= FONT_SIZE_HUGER)
219                         maxsize = size;
220         }
221         return maxsize;
222 }
223
224
225 bool FontList::hasChangeInRange(pos_type pos, int len) const
226 {
227         // FIXME: can't we use fontIterator(pos) instead?
228         const_iterator cit = list_.begin();
229         const_iterator end = list_.end();
230         for (; cit != end; ++cit) {
231                 if (cit->pos() >= pos)
232                         break;
233         }
234         if (cit != end && pos + len - 1 > cit->pos())
235                 return false;
236
237         return true;
238 }
239
240
241 void FontList::validate(LaTeXFeatures & features) const
242 {
243         const_iterator fcit = list_.begin();
244         const_iterator fend = list_.end();
245         for (; fcit != fend; ++fcit)
246                 fcit->font().validate(features);
247 }
248
249 } // namespace lyx