6 #include "math_inset.h"
9 #include "math_scriptinset.h"
10 #include "math_parser.h"
11 #include "mathed/support.h"
16 MathArray::MathArray()
20 MathArray::~MathArray()
26 MathArray::MathArray(MathArray const & array)
33 MathArray::MathArray(MathArray const & array, int from, int to)
34 : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
40 void MathArray::deep_copy(int pos1, int pos2)
42 for (int pos = pos1; pos < pos2; next(pos))
44 MathInset * p = nextInset(pos)->clone();
45 memcpy(&bf_[pos + 1], &p, sizeof(p));
50 bool MathArray::next(int & pos) const
52 if (pos >= size() - 1)
55 pos += item_size(pos);
60 bool MathArray::prev(int & pos) const
65 pos -= item_size(pos - 1);
70 bool MathArray::last(int & pos) const
77 int MathArray::item_size(int pos) const
79 return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
83 void MathArray::substitute(MathMacro const & m)
86 for (int pos = 0; pos < size(); next(pos)) {
88 nextInset(pos)->substitute(tmp, m);
90 tmp.push_back(getChar(pos), getCode(pos));
96 MathArray & MathArray::operator=(MathArray const & array)
104 MathInset * MathArray::nextInset(int pos) const
109 memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
114 MathInset * MathArray::prevInset(int pos) const
118 return nextInset(pos);
122 unsigned char MathArray::getChar(int pos) const
124 return pos < size() ? bf_[pos + 1] : '\0';
128 string MathArray::getString(int & pos) const
134 MathTextCodes const fcode = getCode(pos);
138 } while (pos < size() && !isInset(pos) && getCode(pos) == fcode);
144 MathTextCodes MathArray::getCode(int pos) const
146 return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
150 void MathArray::setCode(int pos, MathTextCodes t)
152 if (pos > size() || isInset(pos))
160 void MathArray::insert(int pos, MathInset * p)
162 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
163 memcpy(&bf_[pos + 1], &p, sizeof(p));
167 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
169 bf_.insert(bf_.begin() + pos, 3, t);
174 void MathArray::insert(int pos, MathArray const & array)
176 bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
177 deep_copy(pos, pos + array.size());
181 void MathArray::push_back(MathInset * p)
187 void MathArray::push_back(unsigned char b, MathTextCodes c)
189 insert(size(), b, c);
193 void MathArray::push_back(MathArray const & array)
195 insert(size(), array);
199 void MathArray::clear()
205 void MathArray::swap(MathArray & array)
212 bool MathArray::empty() const
218 int MathArray::size() const
224 void MathArray::erase()
230 void MathArray::erase(int pos)
232 if (pos < static_cast<int>(bf_.size()))
233 erase(pos, pos + item_size(pos));
237 void MathArray::erase(int pos1, int pos2)
239 for (int pos = pos1; pos < pos2; next(pos))
241 delete nextInset(pos);
242 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
246 bool MathArray::isInset(int pos) const
250 return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
254 MathInset * MathArray::back_inset() const
256 return prevInset(size());
260 void MathArray::dump2(ostream & os) const
262 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
263 os << int(*it) << ' ';
268 void MathArray::dump(ostream & os) const
270 for (int pos = 0; pos < size(); next(pos)) {
272 os << "<inset: " << nextInset(pos) << ">";
274 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
279 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
286 void MathArray::write(ostream & os, bool fragile) const
293 for (int pos = 0; pos < size(); next(pos)) {
296 nextInset(pos)->write(os, fragile);
300 MathTextCodes fcode = getCode(pos);
301 unsigned char c = getChar(pos);
303 if (MathIsSymbol(fcode)) {
304 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
307 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
311 os << '\\' << l->name << ' ';
313 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
316 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
317 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
319 // Is there a standard logical XOR?
320 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
321 (fcode == LM_TC_SPECIAL))
329 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
330 lyxerr <<"Math warning: Unexpected closing brace.\n";
335 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
342 os << string(brace, '}');
346 void MathArray::writeNormal(ostream & os) const
357 void MathArray::validate(LaTeXFeatures & features) const
359 for (int pos = 0; pos < size(); next(pos))
361 nextInset(pos)->validate(features);
365 void MathArray::pop_back()