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()
22 for (int pos = 0; pos < size(); next(pos))
24 delete nextInset(pos);
28 MathArray::MathArray(MathArray const & array)
31 for (int pos = 0; pos < size(); next(pos))
33 replace(pos, nextInset(pos)->clone());
37 bool MathArray::next(int & pos) const
39 if (pos >= size() - 1)
42 pos += item_size(pos);
47 bool MathArray::prev(int & pos) const
52 pos -= item_size(pos - 1);
57 bool MathArray::last(int & pos) const
64 int MathArray::item_size(int pos) const
66 return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
71 void MathArray::substitute(MathMacro const & m)
74 for (int pos = 0; pos < size(); next(pos)) {
76 nextInset(pos)->substitute(tmp, m);
78 tmp.push_back(GetChar(pos), GetCode(pos));
84 MathArray & MathArray::operator=(MathArray const & array)
92 MathInset * MathArray::nextInset(int pos) const
97 memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
101 MathInset * MathArray::prevInset(int pos) const
106 return nextInset(pos);
109 byte MathArray::GetChar(int pos) const
111 return pos < size() ? bf_[pos + 1] : '\0';
114 string MathArray::GetString(int & pos) const
120 MathTextCodes const fcode = GetCode(pos);
124 } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
129 MathTextCodes MathArray::GetCode(int pos) const
131 return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
134 void MathArray::setCode(int pos, MathTextCodes t)
136 if (pos > size() || isInset(pos))
142 void MathArray::insert(int pos, MathInset * p)
144 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
145 memcpy(&bf_[pos + 1], &p, sizeof(p));
149 void MathArray::replace(int pos, MathInset * p)
151 memcpy(&bf_[pos + 1], &p, sizeof(p));
154 void MathArray::insert(int pos, byte b, MathTextCodes t)
156 bf_.insert(bf_.begin() + pos, 3, t);
161 void MathArray::insert(int pos, MathArray const & array)
163 bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
164 for (int p = pos; p < pos + array.size(); next(p))
166 replace(p, nextInset(p)->clone());
170 void MathArray::push_back(MathInset * p)
175 void MathArray::push_back(byte b, MathTextCodes c)
177 insert(size(), b, c);
180 void MathArray::push_back(MathArray const & array)
182 insert(size(), array);
187 void MathArray::clear()
193 void MathArray::swap(MathArray & array)
200 bool MathArray::empty() const
206 int MathArray::size() const
212 void MathArray::erase(int pos)
214 if (pos < static_cast<int>(bf_.size()))
215 erase(pos, pos + item_size(pos));
219 void MathArray::erase(int pos1, int pos2)
221 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
225 bool MathArray::isInset(int pos) const
229 return MathIsInset(bf_[pos]);
233 MathInset * MathArray::back_inset() const
239 return nextInset(pos);
245 void MathArray::dump2(ostream & os) const
247 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
248 os << int(*it) << ' ';
254 void MathArray::dump(ostream & os) const
256 for (int pos = 0; pos < size(); next(pos)) {
258 os << "<inset: " << nextInset(pos) << ">";
260 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
265 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
272 void MathArray::Write(ostream & os, bool fragile) const
279 for (int pos = 0; pos < size(); next(pos)) {
282 nextInset(pos)->Write(os, fragile);
286 MathTextCodes fcode = GetCode(pos);
287 byte c = GetChar(pos);
289 if (MathIsSymbol(fcode)) {
290 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
293 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
297 os << '\\' << l->name << ' ';
299 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
302 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
303 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
305 // Is there a standard logical XOR?
306 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
307 (fcode == LM_TC_SPECIAL))
315 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
316 lyxerr <<"Math warning: Unexpected closing brace.\n";
321 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
328 os << string(brace, '}');
332 void MathArray::WriteNormal(ostream & os) const