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 "support/lyxalgo.h"
30 FontList::iterator FontList::fontIterator(pos_type pos)
32 FontList::iterator it = list_.begin();
33 FontList::iterator end = list_.end();
34 for (; it != end; ++it) {
42 FontList::const_iterator FontList::fontIterator(pos_type pos) const
44 FontList::const_iterator it = list_.begin();
45 FontList::const_iterator end = list_.end();
46 for (; it != end; ++it) {
54 Font const & FontList::get(pos_type pos)
56 iterator end = list_.end();
57 iterator it = fontIterator(pos);
58 if (it != end && it->pos() == pos)
61 static Font const dummy;
66 void FontList::erase(pos_type pos)
68 // Erase entries in the tables.
69 iterator it = fontIterator(pos);
70 iterator beg = list_.begin();
71 if (it != list_.end() && it->pos() == pos
73 || (it != list_.begin() && prev(it, 1)->pos() == pos - 1))) {
75 // If it is a multi-character font
76 // entry, we just make it smaller
77 // (see update below), otherwise we
79 unsigned int const i = it - list_.begin();
81 if (i >= list_.size())
83 it = list_.begin() + i;
84 if (i > 0 && i < list_.size() &&
85 list_[i - 1].font() == list_[i].font()) {
86 list_.erase(beg + i - 1);
87 it = list_.begin() + i - 1;
91 // Update all other entries
92 iterator end = list_.end();
93 for (; it != end; ++it)
94 it->pos(it->pos() - 1);
97 void FontList::increasePosAfterPos(pos_type pos)
99 List::iterator end = list_.end();
100 List::iterator it = fontIterator(pos);
101 for (; it != end; ++it)
106 void FontList::decreasePosAfterPos(pos_type pos)
108 List::iterator end = list_.end();
109 List::iterator it = fontIterator(pos);
110 for (; it != end; ++it)
115 void FontList::setRange(pos_type startpos, pos_type endpos, Font const & font)
117 // FIXME: Optimize!!!
118 for (pos_type pos = startpos; pos != endpos; ++pos)
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 List::iterator it = fontIterator(pos);
130 bool const found = it != list_.end();
131 if (found && it->font() == font)
132 // Font is already set.
135 size_t const i = distance(list_.begin(), it);
137 // Is position pos a beginning of a font block?
138 bool const begin = pos == 0 || !found
139 || (i > 0 && list_[i - 1].pos() == pos - 1);
141 // Is position pos at the end of a font block?
142 bool const end = found && list_[i].pos() == pos;
144 if (!begin && !end) {
145 // The general case: The block is split into 3 blocks
146 list_.insert(list_.begin() + i,
147 FontTable(pos - 1, list_[i].font()));
148 list_.insert(list_.begin() + i + 1,
149 FontTable(pos, font));
154 // A single char block
155 if (i + 1 < list_.size() &&
156 list_[i + 1].font() == font) {
157 // Merge the singleton block with the next block
158 list_.erase(list_.begin() + i);
159 if (i > 0 && list_[i - 1].font() == font)
160 list_.erase(list_.begin() + i - 1);
161 } else if (i > 0 && list_[i - 1].font() == font) {
162 // Merge the singleton block with the previous block
163 list_[i - 1].pos(pos);
164 list_.erase(list_.begin() + i);
168 if (i > 0 && list_[i - 1].font() == font)
169 list_[i - 1].pos(pos);
171 list_.insert(list_.begin() + i,
172 FontTable(pos, font));
174 list_[i].pos(pos - 1);
175 if (!(i + 1 < list_.size() &&
176 list_[i + 1].font() == font))
177 list_.insert(list_.begin() + i + 1,
178 FontTable(pos, font));
183 void FontList::validate(LaTeXFeatures & features) const
185 const_iterator fcit = list_.begin();
186 const_iterator fend = list_.end();
187 for (; fcit != fend; ++fcit)
188 fcit->font().validate(features);