2001-12-18 03:16:46 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2001-02-12 08:55:14 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-11-16 09:55:37 +00:00
|
|
|
#include "math_data.h"
|
2001-07-13 09:54:32 +00:00
|
|
|
#include "math_inset.h"
|
2001-11-09 08:35:57 +00:00
|
|
|
#include "math_deliminset.h"
|
2001-08-17 13:18:10 +00:00
|
|
|
#include "math_charinset.h"
|
2001-09-11 10:58:17 +00:00
|
|
|
#include "math_scriptinset.h"
|
2001-11-05 17:08:45 +00:00
|
|
|
#include "math_stringinset.h"
|
2001-11-09 08:35:57 +00:00
|
|
|
#include "math_matrixinset.h"
|
2001-11-08 12:06:56 +00:00
|
|
|
#include "math_mathmlstream.h"
|
2001-11-08 12:15:12 +00:00
|
|
|
#include "math_support.h"
|
2001-11-16 09:55:37 +00:00
|
|
|
#include "math_replace.h"
|
2001-04-24 16:13:38 +00:00
|
|
|
#include "debug.h"
|
2001-10-12 12:02:49 +00:00
|
|
|
#include "support/LAssert.h"
|
2002-08-02 14:29:42 +00:00
|
|
|
#include "math_metricsinfo.h"
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
#include "textpainter.h"
|
|
|
|
|
|
|
|
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
using std::abs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MathArray::MathArray()
|
|
|
|
: xo_(0), yo_(0), clean_(false), drawn_(false)
|
|
|
|
{}
|
2001-02-12 08:55:14 +00:00
|
|
|
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2002-07-30 13:56:02 +00:00
|
|
|
MathArray::MathArray(const_iterator from, const_iterator to)
|
2002-08-02 14:29:42 +00:00
|
|
|
: base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
|
2001-11-09 10:44:24 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::substitute(MathMacro const & m)
|
|
|
|
{
|
2001-08-09 08:53:16 +00:00
|
|
|
for (iterator it = begin(); it != end(); ++it)
|
2001-10-12 12:02:49 +00:00
|
|
|
it->nucleus()->substitute(m);
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2001-02-19 14:16:57 +00:00
|
|
|
|
2002-07-30 13:56:02 +00:00
|
|
|
MathAtom & MathArray::operator[](size_type pos)
|
2001-04-27 12:35:55 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
lyx::Assert(pos < size());
|
2002-07-30 13:56:02 +00:00
|
|
|
return base_type::operator[](pos);
|
2001-07-12 07:18:29 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2002-07-30 13:56:02 +00:00
|
|
|
MathAtom const & MathArray::operator[](size_type pos) const
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
lyx::Assert(pos < size());
|
2002-07-30 13:56:02 +00:00
|
|
|
return base_type::operator[](pos);
|
2001-10-12 12:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathArray::insert(size_type pos, MathAtom const & t)
|
|
|
|
{
|
2002-07-30 13:56:02 +00:00
|
|
|
base_type::insert(begin() + pos, t);
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-11-08 12:55:58 +00:00
|
|
|
void MathArray::insert(size_type pos, MathArray const & ar)
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2002-07-30 13:56:02 +00:00
|
|
|
base_type::insert(begin() + pos, ar.begin(), ar.end());
|
2001-10-10 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-30 13:56:02 +00:00
|
|
|
void MathArray::append(MathArray const & ar)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-11-08 12:55:58 +00:00
|
|
|
insert(size(), ar);
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-14 14:05:57 +00:00
|
|
|
void MathArray::erase(size_type pos)
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2001-08-03 17:10:22 +00:00
|
|
|
if (pos < size())
|
2001-08-07 12:02:21 +00:00
|
|
|
erase(pos, pos + 1);
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
2001-02-12 08:55:14 +00:00
|
|
|
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
void MathArray::erase(iterator pos1, iterator pos2)
|
|
|
|
{
|
2002-07-30 13:56:02 +00:00
|
|
|
base_type::erase(pos1, pos2);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathArray::erase(iterator pos)
|
|
|
|
{
|
2002-07-30 13:56:02 +00:00
|
|
|
base_type::erase(pos);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-14 14:05:57 +00:00
|
|
|
void MathArray::erase(size_type pos1, size_type pos2)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2002-07-30 13:56:02 +00:00
|
|
|
base_type::erase(begin() + pos1, begin() + pos2);
|
2001-11-09 16:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-08 12:06:56 +00:00
|
|
|
void MathArray::dump2() const
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-11-08 12:06:56 +00:00
|
|
|
NormalStream ns(lyxerr);
|
2001-08-09 08:53:16 +00:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
2002-08-08 17:11:30 +00:00
|
|
|
ns << *it << ' ';
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-08 12:06:56 +00:00
|
|
|
void MathArray::dump() const
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-11-08 12:06:56 +00:00
|
|
|
NormalStream ns(lyxerr);
|
2001-08-09 08:53:16 +00:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
2002-08-08 17:11:30 +00:00
|
|
|
ns << "<" << *it << ">";
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
2001-02-28 17:21:16 +00:00
|
|
|
|
2001-09-11 10:58:17 +00:00
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
void MathArray::validate(LaTeXFeatures & features) const
|
2001-07-13 14:54:56 +00:00
|
|
|
{
|
2001-08-09 08:53:16 +00:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
2002-08-08 17:11:30 +00:00
|
|
|
(*it)->validate(features);
|
2001-07-13 14:54:56 +00:00
|
|
|
}
|
|
|
|
|
2001-07-26 06:46:50 +00:00
|
|
|
|
2001-11-16 09:07:40 +00:00
|
|
|
bool MathArray::match(MathArray const & ar) const
|
|
|
|
{
|
2002-01-03 09:41:26 +00:00
|
|
|
return size() == ar.size() && matchpart(ar, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
|
|
|
|
{
|
|
|
|
if (size() < ar.size() + pos)
|
2001-11-16 09:07:40 +00:00
|
|
|
return false;
|
2002-03-21 17:42:56 +00:00
|
|
|
const_iterator it = begin() + pos;
|
2002-01-03 09:41:26 +00:00
|
|
|
for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!(*jt)->match(*it))
|
2001-11-16 09:07:40 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2001-11-16 09:55:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
void MathArray::replace(ReplaceData & rep)
|
|
|
|
{
|
|
|
|
for (size_type i = 0; i < size(); ++i) {
|
2002-06-18 15:44:30 +00:00
|
|
|
if (find1(rep.from, i)) {
|
2001-11-16 09:55:37 +00:00
|
|
|
// match found
|
|
|
|
lyxerr << "match found!\n";
|
2002-06-18 15:44:30 +00:00
|
|
|
erase(i, i + rep.from.size());
|
2001-11-16 09:55:37 +00:00
|
|
|
insert(i, rep.to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-08 16:08:11 +00:00
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
#warning temporarily disabled
|
|
|
|
// for (const_iterator it = begin(); it != end(); ++it)
|
|
|
|
// it->nucleus()->replace(rep);
|
|
|
|
#endif
|
2001-11-16 09:55:37 +00:00
|
|
|
}
|
2002-02-01 17:01:30 +00:00
|
|
|
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
bool MathArray::find1(MathArray const & ar, size_type pos) const
|
2002-02-01 17:01:30 +00:00
|
|
|
{
|
2002-06-18 15:44:30 +00:00
|
|
|
//lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
|
|
|
|
for (size_type i = 0, n = ar.size(); i < n; ++i)
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!operator[](pos + i)->match(ar[i]))
|
2002-06-18 15:44:30 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray::size_type MathArray::find(MathArray const & ar) const
|
|
|
|
{
|
|
|
|
for (int i = 0, last = size() - ar.size(); i < last; ++i)
|
|
|
|
if (find1(ar, i))
|
|
|
|
return i;
|
|
|
|
return size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray::size_type MathArray::find_last(MathArray const & ar) const
|
|
|
|
{
|
|
|
|
for (int i = size() - ar.size(); i >= 0; --i)
|
|
|
|
if (find1(ar, i))
|
|
|
|
return i;
|
|
|
|
return size();
|
|
|
|
}
|
2002-02-01 17:01:30 +00:00
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
bool MathArray::contains(MathArray const & ar) const
|
|
|
|
{
|
|
|
|
if (find(ar) != size())
|
|
|
|
return true;
|
2002-02-01 17:01:30 +00:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
2002-08-09 10:22:35 +00:00
|
|
|
if ((*it)->contains(ar))
|
2002-02-01 17:01:30 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2002-08-02 14:29:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
void MathArray::touch() const
|
|
|
|
{
|
|
|
|
clean_ = false;
|
|
|
|
drawn_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Dimension const & MathArray::metrics(MathMetricsInfo & mi) const
|
|
|
|
{
|
|
|
|
//if (clean_)
|
|
|
|
// return;
|
|
|
|
clean_ = true;
|
|
|
|
drawn_ = false;
|
|
|
|
|
|
|
|
if (empty()) {
|
|
|
|
mathed_char_dim(mi.base.font, 'I', dim_);
|
|
|
|
return dim_;
|
|
|
|
}
|
|
|
|
|
|
|
|
dim_.clear();
|
|
|
|
for (const_iterator it = begin(), et = end(); it != et; ++it) {
|
|
|
|
(*it)->metrics(mi);
|
|
|
|
dim_ += (*it)->dimensions();
|
|
|
|
}
|
|
|
|
return dim_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathArray::draw(MathPainterInfo & pi, int x, int y) const
|
|
|
|
{
|
|
|
|
//if (drawn_ && x == xo_ && y == yo_)
|
|
|
|
// return;
|
|
|
|
//lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
|
|
|
|
|
|
|
|
xo_ = x;
|
|
|
|
yo_ = y;
|
|
|
|
drawn_ = true;
|
|
|
|
|
|
|
|
if (y + descent() <= 0) // don't draw above the workarea
|
|
|
|
return;
|
|
|
|
if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
|
|
|
|
return;
|
|
|
|
if (x + width() <= 0) // don't draw left of workarea
|
|
|
|
return;
|
|
|
|
if (x >= pi.pain.paperWidth()) // don't draw right of workarea
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (empty()) {
|
|
|
|
pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const_iterator it = begin(), et = end(); it != et; ++it) {
|
|
|
|
(*it)->draw(pi, x, y);
|
|
|
|
x += (*it)->width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
|
|
|
|
{
|
|
|
|
//if (clean_)
|
|
|
|
// return;
|
|
|
|
dim_.clear();
|
|
|
|
for (const_iterator it = begin(); it != end(); ++it) {
|
|
|
|
(*it)->metricsT(mi);
|
|
|
|
dim_ += (*it)->dimensions();
|
|
|
|
}
|
|
|
|
return dim_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathArray::drawT(TextPainter & pain, int x, int y) const
|
|
|
|
{
|
|
|
|
//if (drawn_ && x == xo_ && y == yo_)
|
|
|
|
// return;
|
|
|
|
//lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
|
|
|
|
xo_ = x;
|
|
|
|
yo_ = y;
|
|
|
|
drawn_ = true;
|
|
|
|
|
|
|
|
for (const_iterator it = begin(), et = end(); it != et; ++it) {
|
|
|
|
(*it)->drawT(pain, x, y);
|
|
|
|
x += (*it)->width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MathArray::pos2x(size_type pos) const
|
|
|
|
{
|
|
|
|
return pos2x(0, pos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
size_type target = min(pos2, size());
|
|
|
|
for (size_type i = pos1; i < target; ++i) {
|
|
|
|
const_iterator it = begin() + i;
|
2002-08-09 10:22:35 +00:00
|
|
|
if ((*it)->getChar() == ' ')
|
2002-08-02 14:29:42 +00:00
|
|
|
x += glue;
|
2002-08-09 10:22:35 +00:00
|
|
|
x += (*it)->width();
|
2002-08-02 14:29:42 +00:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray::size_type MathArray::x2pos(int targetx) const
|
|
|
|
{
|
|
|
|
return x2pos(0, targetx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
|
|
|
|
int glue) const
|
|
|
|
{
|
|
|
|
const_iterator it = begin() + startpos;
|
|
|
|
int lastx = 0;
|
|
|
|
int currx = 0;
|
|
|
|
for (; currx < targetx && it < end(); ++it) {
|
|
|
|
lastx = currx;
|
2002-08-09 10:22:35 +00:00
|
|
|
if ((*it)->getChar() == ' ')
|
2002-08-02 14:29:42 +00:00
|
|
|
currx += glue;
|
2002-08-09 10:22:35 +00:00
|
|
|
currx += (*it)->width();
|
2002-08-02 14:29:42 +00:00
|
|
|
}
|
|
|
|
if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
|
|
|
|
--it;
|
|
|
|
return it - begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MathArray::dist(int x, int y) const
|
|
|
|
{
|
|
|
|
int xx = 0;
|
|
|
|
int yy = 0;
|
|
|
|
|
|
|
|
if (x < xo_)
|
|
|
|
xx = xo_ - x;
|
|
|
|
else if (x > xo_ + width())
|
|
|
|
xx = x - xo_ - width();
|
|
|
|
|
|
|
|
if (y < yo_ - ascent())
|
|
|
|
yy = yo_ - ascent() - y;
|
|
|
|
else if (y > yo_ + descent())
|
|
|
|
yy = y - yo_ - descent();
|
|
|
|
|
|
|
|
return xx + yy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
|
|
|
|
{
|
|
|
|
x1 = xo_;
|
|
|
|
x2 = xo_ + width();
|
|
|
|
y1 = yo_ - ascent();
|
|
|
|
y2 = yo_ + descent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathArray::center(int & x, int & y) const
|
|
|
|
{
|
|
|
|
x = xo_ + width() / 2;
|
|
|
|
y = yo_ + (descent() - ascent()) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathArray::towards(int & x, int & y) const
|
|
|
|
{
|
|
|
|
int cx = 0;
|
|
|
|
int cy = 0;
|
|
|
|
center(cx, cy);
|
|
|
|
|
|
|
|
double r = 1.0;
|
|
|
|
//int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
|
|
|
|
|
|
|
|
x = cx + int(r * (x - cx));
|
|
|
|
y = cy + int(r * (y - cy));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-09 08:14:18 +00:00
|
|
|
void MathArray::setXY(int x, int y)
|
2002-08-02 14:29:42 +00:00
|
|
|
{
|
2002-08-09 08:14:18 +00:00
|
|
|
xo_ = x;
|
|
|
|
yo_ = y;
|
2002-08-02 14:29:42 +00:00
|
|
|
}
|
|
|
|
|