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)
142 bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
143 for (int p = pos; p < pos + array.size(); next(p))
145 replace(p, GetInset(p)->clone());
149 void MathArray::push_back(MathInset * p)
154 void MathArray::push_back(byte b, MathTextCodes c)
156 insert(size(), b, c);
159 void MathArray::push_back(MathArray const & array)
161 insert(size(), array);
166 void MathArray::clear()
172 void MathArray::swap(MathArray & array)
179 bool MathArray::empty() const
185 int MathArray::size() const
191 void MathArray::erase(int pos)
193 if (pos < static_cast<int>(bf_.size()))
194 erase(pos, pos + item_size(pos));
198 void MathArray::erase(int pos1, int pos2)
200 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
204 bool MathArray::isInset(int pos) const
208 return MathIsInset(bf_[pos]);
212 MathInset * MathArray::back_inset() const
218 return GetInset(pos);
224 MathScriptInset * MathArray::prevScriptInset(int pos) const
230 MathInset * inset = GetInset(pos);
231 if (inset && inset->isScriptInset())
232 return static_cast<MathScriptInset *>(inset);
237 MathScriptInset * MathArray::nextScriptInset(int pos) const
239 MathInset * inset = GetInset(pos);
240 if (inset && inset->isScriptInset())
241 return static_cast<MathScriptInset *>(inset);
247 void MathArray::dump2(ostream & os) const
249 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
250 os << int(*it) << ' ';
256 void MathArray::dump(ostream & os) const
258 for (int pos = 0; pos < size(); next(pos)) {
260 os << "<inset: " << GetInset(pos) << ">";
262 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
267 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
274 void MathArray::Write(ostream & os, bool fragile) const
281 for (int pos = 0; pos < size(); next(pos)) {
284 GetInset(pos)->Write(os, fragile);
288 MathTextCodes fcode = GetCode(pos);
289 byte c = GetChar(pos);
291 if (MathIsSymbol(fcode)) {
292 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
295 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
299 os << '\\' << l->name << ' ';
301 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
304 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
305 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
307 // Is there a standard logical XOR?
308 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
309 (fcode == LM_TC_SPECIAL))
317 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
318 lyxerr <<"Math warning: Unexpected closing brace.\n";
323 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
330 os << string(brace, '}');
334 void MathArray::WriteNormal(ostream & os) const