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 string MathArray::GetString(int & pos) const
114 MathTextCodes const fcode = GetCode(pos);
118 } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
123 MathTextCodes MathArray::GetCode(int pos) const
125 return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
128 void MathArray::setCode(int pos, MathTextCodes t)
130 if (pos > size() || isInset(pos))
136 void MathArray::insert(int pos, MathInset * p)
138 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
139 memcpy(&bf_[pos + 1], &p, sizeof(p));
143 void MathArray::replace(int pos, MathInset * p)
145 memcpy(&bf_[pos + 1], &p, sizeof(p));
148 void MathArray::insert(int pos, byte b, MathTextCodes t)
150 bf_.insert(bf_.begin() + pos, 3, t);
155 void MathArray::insert(int pos, MathArray const & array)
157 bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
158 for (int p = pos; p < pos + array.size(); next(p))
160 replace(p, GetInset(p)->clone());
164 void MathArray::push_back(MathInset * p)
169 void MathArray::push_back(byte b, MathTextCodes c)
171 insert(size(), b, c);
174 void MathArray::push_back(MathArray const & array)
176 insert(size(), array);
181 void MathArray::clear()
187 void MathArray::swap(MathArray & array)
194 bool MathArray::empty() const
200 int MathArray::size() const
206 void MathArray::erase(int pos)
208 if (pos < static_cast<int>(bf_.size()))
209 erase(pos, pos + item_size(pos));
213 void MathArray::erase(int pos1, int pos2)
215 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
219 bool MathArray::isInset(int pos) const
223 return MathIsInset(bf_[pos]);
227 MathInset * MathArray::back_inset() const
233 return GetInset(pos);
239 MathScriptInset * MathArray::prevScriptInset(int pos) const
245 MathInset * inset = GetInset(pos);
246 if (inset && inset->isScriptInset())
247 return static_cast<MathScriptInset *>(inset);
252 MathScriptInset * MathArray::nextScriptInset(int pos) const
254 MathInset * inset = GetInset(pos);
255 if (inset && inset->isScriptInset())
256 return static_cast<MathScriptInset *>(inset);
262 void MathArray::dump2(ostream & os) const
264 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
265 os << int(*it) << ' ';
271 void MathArray::dump(ostream & os) const
273 for (int pos = 0; pos < size(); next(pos)) {
275 os << "<inset: " << GetInset(pos) << ">";
277 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
282 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
289 void MathArray::Write(ostream & os, bool fragile) const
296 for (int pos = 0; pos < size(); next(pos)) {
299 GetInset(pos)->Write(os, fragile);
303 MathTextCodes fcode = GetCode(pos);
304 byte c = GetChar(pos);
306 if (MathIsSymbol(fcode)) {
307 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
310 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
314 os << '\\' << l->name << ' ';
316 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
319 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
320 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
322 // Is there a standard logical XOR?
323 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
324 (fcode == LM_TC_SPECIAL))
332 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
333 lyxerr <<"Math warning: Unexpected closing brace.\n";
338 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
345 os << string(brace, '}');
349 void MathArray::WriteNormal(ostream & os) const