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);
78 void MathArray::substitute(MathMacro const & m)
81 for (int pos = 0; pos < size(); next(pos)) {
83 nextInset(pos)->substitute(tmp, m);
85 tmp.push_back(GetChar(pos), GetCode(pos));
91 MathArray & MathArray::operator=(MathArray const & array)
99 MathInset * MathArray::nextInset(int pos) const
104 memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
108 MathInset * MathArray::prevInset(int pos) const
113 return nextInset(pos);
117 unsigned char MathArray::GetChar(int pos) const
119 return pos < size() ? bf_[pos + 1] : '\0';
123 string MathArray::GetString(int & pos) const
129 MathTextCodes const fcode = GetCode(pos);
133 } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
139 MathTextCodes MathArray::GetCode(int pos) const
141 return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
145 void MathArray::setCode(int pos, MathTextCodes t)
147 if (pos > size() || isInset(pos))
154 void MathArray::insert(int pos, MathInset * p)
156 bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
157 memcpy(&bf_[pos + 1], &p, sizeof(p));
161 void MathArray::replace(int pos, MathInset * p)
163 memcpy(&bf_[pos + 1], &p, sizeof(p));
167 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
169 bf_.insert(bf_.begin() + pos, 3, t);
174 void MathArray::insert(int pos, MathArray const & array)
176 bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
177 for (int p = pos; p < pos + array.size(); next(p))
179 replace(p, nextInset(p)->clone());
183 void MathArray::push_back(MathInset * p)
189 void MathArray::push_back(unsigned char b, MathTextCodes c)
191 insert(size(), b, c);
195 void MathArray::push_back(MathArray const & array)
197 insert(size(), array);
201 void MathArray::clear()
203 for (int pos = 0; pos < size(); next(pos))
205 delete nextInset(pos);
210 void MathArray::swap(MathArray & array)
217 bool MathArray::empty() const
223 int MathArray::size() const
229 void MathArray::erase()
235 void MathArray::erase(int pos)
237 if (pos < static_cast<int>(bf_.size()))
238 erase(pos, pos + item_size(pos));
242 void MathArray::erase(int pos1, int pos2)
244 bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
248 bool MathArray::isInset(int pos) const
252 return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
256 MathInset * MathArray::back_inset() const
262 return nextInset(pos);
268 void MathArray::dump2(ostream & os) const
270 for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
271 os << int(*it) << ' ';
276 void MathArray::dump(ostream & os) const
278 for (int pos = 0; pos < size(); next(pos)) {
280 os << "<inset: " << nextInset(pos) << ">";
282 os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
287 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
294 void MathArray::Write(ostream & os, bool fragile) const
301 for (int pos = 0; pos < size(); next(pos)) {
304 nextInset(pos)->Write(os, fragile);
308 MathTextCodes fcode = GetCode(pos);
309 unsigned char c = GetChar(pos);
311 if (MathIsSymbol(fcode)) {
312 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
315 l = lm_get_key_by_id(c, LM_TK_BIGSYM);
319 os << '\\' << l->name << ' ';
321 lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
324 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
325 os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
327 // Is there a standard logical XOR?
328 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
329 (fcode == LM_TC_SPECIAL))
337 if (c == '}' && fcode == LM_TC_TEX && brace < 0)
338 lyxerr <<"Math warning: Unexpected closing brace.\n";
343 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
350 os << string(brace, '}');
354 void MathArray::WriteNormal(ostream & os) const
365 void MathArray::Validate(LaTeXFeatures & features) const
367 for (int pos = 0; pos < size(); next(pos))
369 nextInset(pos)->Validate(features);