6 #include "math_inset.h"
9 #include "math_scriptinset.h"
10 #include "math_parser.h"
11 #include "mathed/support.h"
16 MathArray::MathArray()
20 MathArray::~MathArray()
22 for (int pos = 0; pos < size(); next(pos))
24 delete nextInset(pos);
28 MathArray::MathArray(MathArray const & array)
31 for (int pos = 0; pos < size(); next(pos))
33 replace(pos, nextInset(pos)->clone());
36 MathArray::MathArray(MathArray const & array, int from, int to)
37 : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
39 for (int pos = 0; pos < size(); next(pos))
41 replace(pos, nextInset(pos)->clone());
45 bool MathArray::next(int & pos) const
47 if (pos >= size() - 1)
50 pos += item_size(pos);
55 bool MathArray::prev(int & pos) const
60 pos -= item_size(pos - 1);
65 bool MathArray::last(int & pos) const
72 int MathArray::item_size(int pos) const
74 return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
79 void MathArray::substitute(MathMacro const & m)
82 for (int pos = 0; pos < size(); next(pos)) {
84 nextInset(pos)->substitute(tmp, m);
86 tmp.push_back(GetChar(pos), GetCode(pos));
92 MathArray & MathArray::operator=(MathArray const & array)
100 MathInset * MathArray::nextInset(int pos) const
105 memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
109 MathInset * MathArray::prevInset(int pos) const
114 return nextInset(pos);
117 unsigned char MathArray::GetChar(int pos) const
119 return pos < size() ? bf_[pos + 1] : '\0';
122 string MathArray::GetString(int & pos) const
128 MathTextCodes const fcode = GetCode(pos);
132 } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
137 MathTextCodes MathArray::GetCode(int pos) const
139 return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
142 void MathArray::setCode(int pos, MathTextCodes t)
144 if (pos > size() || isInset(pos))
150 void MathArray::insert(int pos, MathInset * p)
152 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
153 memcpy(&bf_[pos + 1], &p, sizeof(p));
157 void MathArray::replace(int pos, MathInset * p)
159 memcpy(&bf_[pos + 1], &p, sizeof(p));
162 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
164 bf_.insert(bf_.begin() + pos, 3, t);
169 void MathArray::insert(int pos, MathArray const & array)
171 bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
172 for (int p = pos; p < pos + array.size(); next(p))
174 replace(p, nextInset(p)->clone());
178 void MathArray::push_back(MathInset * p)
183 void MathArray::push_back(unsigned char b, MathTextCodes c)
185 insert(size(), b, c);
188 void MathArray::push_back(MathArray const & array)
190 insert(size(), array);
195 void MathArray::clear()
201 void MathArray::swap(MathArray & array)
208 bool MathArray::empty() const
214 int MathArray::size() const
220 void MathArray::erase()
226 void MathArray::erase(int pos)
228 if (pos < static_cast<int>(bf_.size()))
229 erase(pos, pos + item_size(pos));
233 void MathArray::erase(int pos1, int pos2)
235 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
239 bool MathArray::isInset(int pos) const
243 return MathIsInset(bf_[pos]);
247 MathInset * MathArray::back_inset() const
253 return nextInset(pos);
259 void MathArray::dump2(ostream & os) const
261 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
262 os << int(*it) << ' ';
268 void MathArray::dump(ostream & os) const
270 for (int pos = 0; pos < size(); next(pos)) {
272 os << "<inset: " << nextInset(pos) << ">";
274 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
279 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
286 void MathArray::Write(ostream & os, bool fragile) const
293 for (int pos = 0; pos < size(); next(pos)) {
296 nextInset(pos)->Write(os, fragile);
300 MathTextCodes fcode = GetCode(pos);
301 unsigned char c = GetChar(pos);
303 if (MathIsSymbol(fcode)) {
304 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
307 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
311 os << '\\' << l->name << ' ';
313 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
316 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
317 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
319 // Is there a standard logical XOR?
320 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
321 (fcode == LM_TC_SPECIAL))
329 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
330 lyxerr <<"Math warning: Unexpected closing brace.\n";
335 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
342 os << string(brace, '}');
346 void MathArray::WriteNormal(ostream & os) const
357 void MathArray::Validate(LaTeXFeatures & features) const
359 for (int pos = 0; pos < size(); next(pos))
361 nextInset(pos)->Validate(features);