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 char 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, char 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(char 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 < 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->GetType() == LM_OT_SCRIPT)
225 return static_cast<MathScriptInset *>(inset);
231 void MathArray::dump2(ostream & os) const
233 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
234 os << int(*it) << ' ';
240 void MathArray::dump(ostream & os) const
242 for (int pos = 0; pos < size(); next(pos)) {
244 os << "<inset: " << GetInset(pos) << ">";
246 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
251 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
258 void MathArray::Write(ostream & os, bool fragile) const
266 for (int pos = 0; pos < size(); next(pos)) {
269 GetInset(pos)->Write(os, fragile);
273 char fcode = GetCode(pos);
274 char c = GetChar(pos);
276 if (MathIsSymbol(fcode)) {
277 l = lm_get_key_by_id(c, fcode == LM_TC_BSYM ? LM_TK_BIGSYM : LM_TK_SYM);
279 os << '\\' << l->name << ' ';
282 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
283 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
285 // Is there a standard logical XOR?
286 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
287 (fcode == LM_TC_SPECIAL))
295 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
296 lyxerr <<"Math warning: Unexpected closing brace.\n";
301 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
308 os << string(brace, '}');
312 void MathArray::WriteNormal(ostream & os) const