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_.begin() + 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::insert(int pos, MathInset * p)
115 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
116 memcpy(&bf_[pos + 1], &p, sizeof(p));
120 void MathArray::replace(int pos, MathInset * p)
122 memcpy(&bf_[pos + 1], &p, sizeof(p));
125 void MathArray::insert(int pos, byte b, MathTextCodes t)
127 bf_.insert(bf_.begin() + pos, 3, t);
132 void MathArray::insert(int pos, MathArray const & array)
135 #warning quick and really dirty: make sure that we really own our insets
138 bf_.insert(bf_.begin() + pos, a.bf_.begin(), a.bf_.end());
142 void MathArray::push_back(MathInset * p)
147 void MathArray::push_back(byte b, MathTextCodes c)
149 insert(size(), b, c);
152 void MathArray::push_back(MathArray const & array)
154 insert(size(), array);
159 void MathArray::clear()
165 void MathArray::swap(MathArray & array)
172 bool MathArray::empty() const
178 int MathArray::size() const
184 void MathArray::erase(int pos)
186 if (pos < static_cast<int>(bf_.size()))
187 erase(pos, pos + item_size(pos));
191 void MathArray::erase(int pos1, int pos2)
193 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
197 bool MathArray::isInset(int pos) const
201 return MathIsInset(bf_[pos]);
205 MathInset * MathArray::back_inset() const
211 return GetInset(pos);
217 MathScriptInset * MathArray::prevScriptInset(int pos) const
223 MathInset * inset = GetInset(pos);
224 if (inset && inset->isScriptInset())
225 return static_cast<MathScriptInset *>(inset);
230 MathScriptInset * MathArray::nextScriptInset(int pos) const
232 MathInset * inset = GetInset(pos);
233 if (inset && inset->isScriptInset())
234 return static_cast<MathScriptInset *>(inset);
240 void MathArray::dump2(ostream & os) const
242 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
243 os << int(*it) << ' ';
249 void MathArray::dump(ostream & os) const
251 for (int pos = 0; pos < size(); next(pos)) {
253 os << "<inset: " << GetInset(pos) << ">";
255 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
260 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
267 void MathArray::Write(ostream & os, bool fragile) const
274 for (int pos = 0; pos < size(); next(pos)) {
277 GetInset(pos)->Write(os, fragile);
281 MathTextCodes fcode = GetCode(pos);
282 byte c = GetChar(pos);
284 if (MathIsSymbol(fcode)) {
285 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
288 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
292 os << '\\' << l->name << ' ';
294 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
297 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
298 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
300 // Is there a standard logical XOR?
301 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
302 (fcode == LM_TC_SPECIAL))
310 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
311 lyxerr <<"Math warning: Unexpected closing brace.\n";
316 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
323 os << string(brace, '}');
327 void MathArray::WriteNormal(ostream & os) const