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 lyx::Assert(pos <= size());
70 base_type::insert(begin() + pos, ar.begin(), ar.end());
74 void MathArray::append(MathArray const & ar)
80 void MathArray::erase(size_type pos)
87 void MathArray::erase(iterator pos1, iterator pos2)
89 base_type::erase(pos1, pos2);
93 void MathArray::erase(iterator pos)
95 base_type::erase(pos);
99 void MathArray::erase(size_type pos1, size_type pos2)
101 base_type::erase(begin() + pos1, begin() + pos2);
105 void MathArray::dump2() const
107 NormalStream ns(lyxerr);
108 for (const_iterator it = begin(); it != end(); ++it)
113 void MathArray::dump() const
115 NormalStream ns(lyxerr);
116 for (const_iterator it = begin(); it != end(); ++it)
117 ns << "<" << *it << ">";
121 void MathArray::validate(LaTeXFeatures & features) const
123 for (const_iterator it = begin(); it != end(); ++it)
124 (*it)->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)->match(*it))
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());
158 #warning temporarily disabled
159 // for (const_iterator it = begin(); it != end(); ++it)
160 // it->nucleus()->replace(rep);
165 bool MathArray::find1(MathArray const & ar, size_type pos) const
167 //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
168 for (size_type i = 0, n = ar.size(); i < n; ++i)
169 if (!operator[](pos + i)->match(ar[i]))
175 MathArray::size_type MathArray::find(MathArray const & ar) const
177 for (int i = 0, last = size() - ar.size(); i < last; ++i)
184 MathArray::size_type MathArray::find_last(MathArray const & ar) const
186 for (int i = size() - ar.size(); i >= 0; --i)
193 bool MathArray::contains(MathArray const & ar) const
195 if (find(ar) != size())
197 for (const_iterator it = begin(); it != end(); ++it)
198 if ((*it)->contains(ar))
204 void MathArray::touch() const
211 Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
218 mathed_char_dim(mi.base.font, 'I', dim_);
223 for (const_iterator it = begin(), et = end(); it != et; ++it) {
225 dim_ += (*it)->dimensions();
231 void MathArray::draw(MathPainterInfo & pi, int x, int y) const
233 //if (drawn_ && x == xo_ && y == yo_)
235 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
241 if (y + descent() <= 0) // don't draw above the workarea
243 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
245 if (x + width() <= 0) // don't draw left of workarea
247 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
251 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
255 for (const_iterator it = begin(), et = end(); it != et; ++it) {
256 (*it)->draw(pi, x, y);
262 Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
267 for (const_iterator it = begin(); it != end(); ++it) {
269 dim_ += (*it)->dimensions();
275 void MathArray::drawT(TextPainter & pain, int x, int y) const
277 //if (drawn_ && x == xo_ && y == yo_)
279 //lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
284 for (const_iterator it = begin(), et = end(); it != et; ++it) {
285 (*it)->drawT(pain, x, y);
291 int MathArray::pos2x(size_type pos) const
293 return pos2x(0, pos, 0);
297 int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
300 size_type target = min(pos2, size());
301 for (size_type i = pos1; i < target; ++i) {
302 const_iterator it = begin() + i;
303 if ((*it)->getChar() == ' ')
311 MathArray::size_type MathArray::x2pos(int targetx) const
313 return x2pos(0, targetx, 0);
317 MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
320 const_iterator it = begin() + startpos;
323 for (; currx < targetx && it < end(); ++it) {
325 if ((*it)->getChar() == ' ')
327 currx += (*it)->width();
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));
383 void MathArray::setXY(int x, int y) const