10 #include "math_inset.h"
11 #include "math_scriptinset.h"
12 #include "math_parser.h"
13 #include "mathed/support.h"
18 MathArray::MathArray()
22 MathArray::~MathArray()
24 for (int pos = 0; pos < size(); next(pos))
30 MathArray::MathArray(MathArray const & array)
33 for (int pos = 0; pos < size(); next(pos))
35 replace(pos, GetInset(pos)->clone());
39 bool MathArray::next(int & pos) const
41 if (pos >= size() - 1)
44 pos += item_size(pos);
49 bool MathArray::prev(int & pos) const
54 pos -= item_size(pos - 1);
59 bool MathArray::last(int & pos) const
66 int MathArray::item_size(int pos) const
68 return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
73 void MathArray::substitute(MathMacro const & m)
76 for (int pos = 0; pos < size(); next(pos)) {
78 GetInset(pos)->substitute(tmp, m);
80 tmp.push_back(GetChar(pos), GetCode(pos));
86 MathArray & MathArray::operator=(MathArray const & array)
94 MathInset * MathArray::GetInset(int pos) const
99 memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
103 byte MathArray::GetChar(int pos) const
105 return pos < size() ? bf_[pos + 1] : '\0';
108 MathTextCodes MathArray::GetCode(int pos) const
110 return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
113 void MathArray::setCode(int pos, MathTextCodes t)
115 if (pos > size() || isInset(pos))
121 void MathArray::insert(int pos, MathInset * p)
123 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
124 memcpy(&bf_[pos + 1], &p, sizeof(p));
128 void MathArray::replace(int pos, MathInset * p)
130 memcpy(&bf_[pos + 1], &p, sizeof(p));
133 void MathArray::insert(int pos, byte b, MathTextCodes t)
135 bf_.insert(bf_.begin() + pos, 3, t);
140 void MathArray::insert(int pos, MathArray const & array)
143 #warning quick and really dirty: make sure that we really own our insets
146 bf_.insert(bf_.begin() + pos, a.bf_.begin(), a.bf_.end());
150 void MathArray::push_back(MathInset * p)
155 void MathArray::push_back(byte b, MathTextCodes c)
157 insert(size(), b, c);
160 void MathArray::push_back(MathArray const & array)
162 insert(size(), array);
167 void MathArray::clear()
173 void MathArray::swap(MathArray & array)
180 bool MathArray::empty() const
186 int MathArray::size() const
192 void MathArray::erase(int pos)
194 if (pos < static_cast<int>(bf_.size()))
195 erase(pos, pos + item_size(pos));
199 void MathArray::erase(int pos1, int pos2)
201 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
205 bool MathArray::isInset(int pos) const
209 return MathIsInset(bf_[pos]);
213 MathInset * MathArray::back_inset() const
219 return GetInset(pos);
225 MathScriptInset * MathArray::prevScriptInset(int pos) const
231 MathInset * inset = GetInset(pos);
232 if (inset && inset->isScriptInset())
233 return static_cast<MathScriptInset *>(inset);
238 MathScriptInset * MathArray::nextScriptInset(int pos) const
240 MathInset * inset = GetInset(pos);
241 if (inset && inset->isScriptInset())
242 return static_cast<MathScriptInset *>(inset);
248 void MathArray::dump2(ostream & os) const
250 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
251 os << int(*it) << ' ';
257 void MathArray::dump(ostream & os) const
259 for (int pos = 0; pos < size(); next(pos)) {
261 os << "<inset: " << GetInset(pos) << ">";
263 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
268 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
275 void MathArray::Write(ostream & os, bool fragile) const
282 for (int pos = 0; pos < size(); next(pos)) {
285 GetInset(pos)->Write(os, fragile);
289 MathTextCodes fcode = GetCode(pos);
290 byte c = GetChar(pos);
292 if (MathIsSymbol(fcode)) {
293 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
296 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
300 os << '\\' << l->name << ' ';
302 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
305 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
306 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
308 // Is there a standard logical XOR?
309 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
310 (fcode == LM_TC_SPECIAL))
318 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
319 lyxerr <<"Math warning: Unexpected closing brace.\n";
324 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
331 os << string(brace, '}');
335 void MathArray::WriteNormal(ostream & os) const