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)
112 void MathArray::dump() const
114 NormalStream ns(lyxerr);
115 for (const_iterator it = begin(); it != end(); ++it)
116 ns << "<" << *it << ">";
120 void MathArray::validate(LaTeXFeatures & features) const
122 for (const_iterator it = begin(); it != end(); ++it)
123 (*it)->validate(features);
127 bool MathArray::match(MathArray const & ar) const
129 return size() == ar.size() && matchpart(ar, 0);
133 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
135 if (size() < ar.size() + pos)
137 const_iterator it = begin() + pos;
138 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
139 if (!(*jt)->match(*it))
145 void MathArray::replace(ReplaceData & rep)
147 for (size_type i = 0; i < size(); ++i) {
148 if (find1(rep.from, i)) {
150 lyxerr << "match found!\n";
151 erase(i, i + rep.from.size());
157 #warning temporarily disabled
158 // for (const_iterator it = begin(); it != end(); ++it)
159 // it->nucleus()->replace(rep);
164 bool MathArray::find1(MathArray const & ar, size_type pos) const
166 //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
167 for (size_type i = 0, n = ar.size(); i < n; ++i)
168 if (!operator[](pos + i)->match(ar[i]))
174 MathArray::size_type MathArray::find(MathArray const & ar) const
176 for (int i = 0, last = size() - ar.size(); i < last; ++i)
183 MathArray::size_type MathArray::find_last(MathArray const & ar) const
185 for (int i = size() - ar.size(); i >= 0; --i)
192 bool MathArray::contains(MathArray const & ar) const
194 if (find(ar) != size())
196 for (const_iterator it = begin(); it != end(); ++it)
197 if ((*it)->contains(ar))
203 void MathArray::touch() const
210 Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
217 mathed_char_dim(mi.base.font, 'I', dim_);
222 for (const_iterator it = begin(), et = end(); it != et; ++it) {
224 dim_ += (*it)->dimensions();
230 void MathArray::draw(MathPainterInfo & pi, int x, int y) const
232 //if (drawn_ && x == xo_ && y == yo_)
234 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
240 if (y + descent() <= 0) // don't draw above the workarea
242 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
244 if (x + width() <= 0) // don't draw left of workarea
246 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
250 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
254 for (const_iterator it = begin(), et = end(); it != et; ++it) {
255 (*it)->draw(pi, x, y);
261 Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
266 for (const_iterator it = begin(); it != end(); ++it) {
268 dim_ += (*it)->dimensions();
274 void MathArray::drawT(TextPainter & pain, int x, int y) const
276 //if (drawn_ && x == xo_ && y == yo_)
278 //lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
283 for (const_iterator it = begin(), et = end(); it != et; ++it) {
284 (*it)->drawT(pain, x, y);
290 int MathArray::pos2x(size_type pos) const
292 return pos2x(0, pos, 0);
296 int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
299 size_type target = min(pos2, size());
300 for (size_type i = pos1; i < target; ++i) {
301 const_iterator it = begin() + i;
302 if ((*it)->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 if ((*it)->getChar() == ' ')
326 currx += (*it)->width();
328 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
334 int MathArray::dist(int x, int y) const
341 else if (x > xo_ + width())
342 xx = x - xo_ - width();
344 if (y < yo_ - ascent())
345 yy = yo_ - ascent() - y;
346 else if (y > yo_ + descent())
347 yy = y - yo_ - descent();
353 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
358 y2 = yo_ + descent();
361 void MathArray::center(int & x, int & y) const
363 x = xo_ + width() / 2;
364 y = yo_ + (descent() - ascent()) / 2;
368 void MathArray::towards(int & x, int & y) const
375 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
377 x = cx + int(r * (x - cx));
378 y = cy + int(r * (y - cy));
382 void MathArray::setXY(int x, int y) const