8 #include "math_inset.h"
9 #include "math_deliminset.h"
10 #include "math_charinset.h"
11 #include "math_scriptinset.h"
12 #include "math_stringinset.h"
13 #include "math_matrixinset.h"
14 #include "math_mathmlstream.h"
15 #include "math_support.h"
16 #include "math_replace.h"
18 #include "support/LAssert.h"
19 #include "math_metricsinfo.h"
20 #include "frontends/Painter.h"
21 #include "textpainter.h"
30 MathArray::MathArray()
31 : xo_(0), yo_(0), clean_(false), drawn_(false)
35 MathArray::MathArray(const_iterator from, const_iterator to)
36 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
40 void MathArray::substitute(MathMacro const & m)
42 for (iterator it = begin(); it != end(); ++it)
43 it->nucleus()->substitute(m);
47 MathAtom & MathArray::operator[](size_type pos)
49 lyx::Assert(pos < size());
50 return base_type::operator[](pos);
54 MathAtom const & MathArray::operator[](size_type pos) const
56 lyx::Assert(pos < size());
57 return base_type::operator[](pos);
61 void MathArray::insert(size_type pos, MathAtom const & t)
63 base_type::insert(begin() + pos, t);
67 void MathArray::insert(size_type pos, MathArray const & ar)
69 base_type::insert(begin() + pos, ar.begin(), ar.end());
73 void MathArray::append(MathArray const & ar)
79 void MathArray::erase(size_type pos)
86 void MathArray::erase(iterator pos1, iterator pos2)
88 base_type::erase(pos1, pos2);
92 void MathArray::erase(iterator pos)
94 base_type::erase(pos);
98 void MathArray::erase(size_type pos1, size_type pos2)
100 base_type::erase(begin() + pos1, begin() + pos2);
104 void MathArray::dump2() const
106 NormalStream ns(lyxerr);
107 for (const_iterator it = begin(); it != end(); ++it)
108 ns << it->nucleus() << ' ';
112 void MathArray::dump() const
114 NormalStream ns(lyxerr);
115 for (const_iterator it = begin(); it != end(); ++it)
116 ns << "<" << it->nucleus() << ">";
120 void MathArray::validate(LaTeXFeatures & features) const
122 for (const_iterator it = begin(); it != end(); ++it)
124 it->nucleus()->validate(features);
128 bool MathArray::match(MathArray const & ar) const
130 return size() == ar.size() && matchpart(ar, 0);
134 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
136 if (size() < ar.size() + pos)
138 const_iterator it = begin() + pos;
139 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
140 if (!jt->nucleus()->match(it->nucleus()))
146 void MathArray::replace(ReplaceData & rep)
148 for (size_type i = 0; i < size(); ++i) {
149 if (find1(rep.from, i)) {
151 lyxerr << "match found!\n";
152 erase(i, i + rep.from.size());
157 for (const_iterator it = begin(); it != end(); ++it)
158 it->nucleus()->replace(rep);
162 bool MathArray::find1(MathArray const & ar, size_type pos) const
164 //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
165 for (size_type i = 0, n = ar.size(); i < n; ++i)
166 if (!operator[](pos + i)->match(ar[i].nucleus()))
172 MathArray::size_type MathArray::find(MathArray const & ar) const
174 for (int i = 0, last = size() - ar.size(); i < last; ++i)
181 MathArray::size_type MathArray::find_last(MathArray const & ar) const
183 for (int i = size() - ar.size(); i >= 0; --i)
190 bool MathArray::contains(MathArray const & ar) const
192 if (find(ar) != size())
194 for (const_iterator it = begin(); it != end(); ++it)
195 if (it->nucleus()->contains(ar))
201 void MathArray::touch() const
208 Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
216 mathed_char_dim(mi.base.font, 'I', dim_);
221 for (const_iterator it = begin(), et = end(); it != et; ++it) {
223 dim_ += (*it)->dimensions();
229 void MathArray::draw(MathPainterInfo & pi, int x, int y) const
231 //if (drawn_ && x == xo_ && y == yo_)
233 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
239 if (y + descent() <= 0) // don't draw above the workarea
241 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
243 if (x + width() <= 0) // don't draw left of workarea
245 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
249 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
253 for (const_iterator it = begin(), et = end(); it != et; ++it) {
254 (*it)->draw(pi, x, y);
260 Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
265 for (const_iterator it = begin(); it != end(); ++it) {
267 dim_ += (*it)->dimensions();
273 void MathArray::drawT(TextPainter & pain, int x, int y) const
275 //if (drawn_ && x == xo_ && y == yo_)
277 //lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
282 for (const_iterator it = begin(), et = end(); it != et; ++it) {
283 (*it)->drawT(pain, x, y);
289 int MathArray::pos2x(size_type pos) const
291 return pos2x(0, pos, 0);
295 int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
298 size_type target = min(pos2, size());
299 for (size_type i = pos1; i < target; ++i) {
300 const_iterator it = begin() + i;
301 MathInset const * p = it->nucleus();
302 if (p->getChar() == ' ')
310 MathArray::size_type MathArray::x2pos(int targetx) const
312 return x2pos(0, targetx, 0);
316 MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
319 const_iterator it = begin() + startpos;
322 for (; currx < targetx && it < end(); ++it) {
324 MathInset const * p = it->nucleus();
325 if (p->getChar() == ' ')
329 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
335 int MathArray::dist(int x, int y) const
342 else if (x > xo_ + width())
343 xx = x - xo_ - width();
345 if (y < yo_ - ascent())
346 yy = yo_ - ascent() - y;
347 else if (y > yo_ + descent())
348 yy = y - yo_ - descent();
354 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
359 y2 = yo_ + descent();
362 void MathArray::center(int & x, int & y) const
364 x = xo_ + width() / 2;
365 y = yo_ + (descent() - ascent()) / 2;
369 void MathArray::towards(int & x, int & y) const
376 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
378 x = cx + int(r * (x - cx));
379 y = cy + int(r * (y - cy));
384 std::ostream & operator<<(std::ostream & os, MathArray const & ar)