2001-06-25 00:06:33 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
#include "math_scriptinset.h"
|
2001-11-08 12:15:12 +00:00
|
|
|
#include "math_support.h"
|
2002-05-23 09:21:32 +00:00
|
|
|
#include "frontends/Painter.h"
|
2002-03-18 11:45:53 +00:00
|
|
|
#include "textpainter.h"
|
2001-08-06 17:20:26 +00:00
|
|
|
#include "debug.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-07-12 07:18:29 +00:00
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
|
2001-11-09 08:35:57 +00:00
|
|
|
extern MathScriptInset const * asScript(MathArray::const_iterator it);
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathXArray::MathXArray()
|
2002-03-12 14:59:08 +00:00
|
|
|
: width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), size_(),
|
|
|
|
clean_(false), drawn_(false)
|
2001-06-25 00:06:33 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2002-03-12 14:59:08 +00:00
|
|
|
void MathXArray::touch() const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2002-03-12 14:59:08 +00:00
|
|
|
clean_ = false;
|
|
|
|
drawn_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 12:51:43 +00:00
|
|
|
void MathXArray::metrics(MathMetricsInfo const & mi) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2002-03-13 17:31:18 +00:00
|
|
|
//if (clean_)
|
|
|
|
// return;
|
2002-03-12 14:59:08 +00:00
|
|
|
|
|
|
|
size_ = mi;
|
|
|
|
clean_ = true;
|
|
|
|
drawn_ = false;
|
2001-09-03 08:55:56 +00:00
|
|
|
|
2002-02-01 15:53:34 +00:00
|
|
|
if (data_.empty()) {
|
2002-03-19 16:55:58 +00:00
|
|
|
LyXFont font;
|
|
|
|
whichFont(font, LM_TC_VAR, mi);
|
|
|
|
mathed_char_dim(font, 'I', ascent_, descent_, width_);
|
2001-06-25 00:06:33 +00:00
|
|
|
return;
|
2002-02-01 15:53:34 +00:00
|
|
|
}
|
2001-09-03 11:38:04 +00:00
|
|
|
|
2002-02-01 15:53:34 +00:00
|
|
|
ascent_ = 0;
|
|
|
|
descent_ = 0;
|
|
|
|
width_ = 0;
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it) {
|
|
|
|
MathInset const * p = it->nucleus();
|
2001-11-09 08:35:57 +00:00
|
|
|
MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
|
|
|
|
if (q) {
|
2001-11-13 12:51:43 +00:00
|
|
|
q->metrics(p, mi);
|
2002-02-16 15:59:55 +00:00
|
|
|
ascent_ = max(ascent_, q->ascent2(p));
|
|
|
|
descent_ = max(descent_, q->descent2(p));
|
2002-03-21 17:42:56 +00:00
|
|
|
width_ += q->width2(p);
|
2001-10-12 12:02:49 +00:00
|
|
|
++it;
|
|
|
|
} else {
|
2001-11-13 12:51:43 +00:00
|
|
|
p->metrics(mi);
|
2002-02-16 15:59:55 +00:00
|
|
|
ascent_ = max(ascent_, p->ascent());
|
|
|
|
descent_ = max(descent_, p->descent());
|
2002-03-21 17:42:56 +00:00
|
|
|
width_ += p->width();
|
2001-10-12 12:02:49 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
2002-03-21 17:42:56 +00:00
|
|
|
//lyxerr << "MathXArray::metrics(): '" << ascent_ << " "
|
2001-09-11 14:15:30 +00:00
|
|
|
// << descent_ << " " << width_ << "'\n";
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 17:20:26 +00:00
|
|
|
void MathXArray::draw(Painter & pain, int x, int y) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2002-03-12 14:59:08 +00:00
|
|
|
//if (drawn_ && x == xo_ && y == yo_)
|
|
|
|
// return;
|
|
|
|
|
2002-03-13 11:17:21 +00:00
|
|
|
//lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
|
|
|
|
|
2002-03-12 14:59:08 +00:00
|
|
|
xo_ = x;
|
|
|
|
yo_ = y;
|
|
|
|
drawn_ = true;
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2002-03-13 11:17:21 +00:00
|
|
|
if (y + descent_ <= 0) // don't draw above the workarea
|
|
|
|
return;
|
|
|
|
if (y - ascent_ >= pain.paperHeight()) // don't draw below the workarea
|
|
|
|
return;
|
|
|
|
if (x + width_ <= 0) // don't draw left of workarea
|
|
|
|
return;
|
|
|
|
if (x >= pain.paperWidth()) // don't draw right of workarea
|
|
|
|
return;
|
|
|
|
|
2002-03-12 16:39:27 +00:00
|
|
|
const_iterator it = begin(), et = end();
|
|
|
|
|
|
|
|
if (it == et) {
|
2001-06-25 00:06:33 +00:00
|
|
|
pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-12 16:39:27 +00:00
|
|
|
for (; it != et; ++it) {
|
2001-10-12 12:02:49 +00:00
|
|
|
MathInset const * p = it->nucleus();
|
2002-03-12 16:39:27 +00:00
|
|
|
MathScriptInset const * q = (it + 1 == et) ? 0 : asScript(it);
|
2001-11-09 08:35:57 +00:00
|
|
|
if (q) {
|
2001-10-12 12:02:49 +00:00
|
|
|
q->draw(p, pain, x, y);
|
2002-01-08 11:03:34 +00:00
|
|
|
x += q->width2(p);
|
2001-10-12 12:02:49 +00:00
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
p->draw(pain, x, y);
|
|
|
|
x += p->width();
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-21 06:57:13 +00:00
|
|
|
void MathXArray::metricsT(TextMetricsInfo const & mi) const
|
2002-03-18 11:45:53 +00:00
|
|
|
{
|
|
|
|
//if (clean_)
|
|
|
|
// return;
|
|
|
|
|
|
|
|
ascent_ = 0;
|
|
|
|
descent_ = 0;
|
|
|
|
width_ = 0;
|
|
|
|
|
|
|
|
for (const_iterator it = begin(); it != end(); ++it) {
|
|
|
|
MathInset const * p = it->nucleus();
|
|
|
|
MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
|
|
|
|
if (q) {
|
2002-03-21 06:57:13 +00:00
|
|
|
q->metricsT(p, mi);
|
2002-03-18 11:45:53 +00:00
|
|
|
ascent_ = max(ascent_, q->ascent2(p));
|
|
|
|
descent_ = max(descent_, q->descent2(p));
|
2002-03-21 17:42:56 +00:00
|
|
|
width_ += q->width2(p);
|
2002-03-18 11:45:53 +00:00
|
|
|
++it;
|
|
|
|
} else {
|
2002-03-21 06:57:13 +00:00
|
|
|
p->metricsT(mi);
|
2002-03-18 11:45:53 +00:00
|
|
|
ascent_ = max(ascent_, p->ascent());
|
|
|
|
descent_ = max(descent_, p->descent());
|
2002-03-21 17:42:56 +00:00
|
|
|
width_ += p->width();
|
2002-03-18 11:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-21 06:57:13 +00:00
|
|
|
void MathXArray::drawT(TextPainter & pain, int x, int y) const
|
2002-03-18 11:45:53 +00:00
|
|
|
{
|
|
|
|
//if (drawn_ && x == xo_ && y == yo_)
|
|
|
|
// return;
|
|
|
|
|
|
|
|
//lyxerr << "x: " << x << " y: " << y << " " << pain.workAreaHeight() << endl;
|
|
|
|
|
|
|
|
xo_ = x;
|
|
|
|
yo_ = y;
|
|
|
|
drawn_ = true;
|
|
|
|
|
|
|
|
const_iterator it = begin(), et = end();
|
|
|
|
|
|
|
|
for (; it != et; ++it) {
|
|
|
|
MathInset const * p = it->nucleus();
|
|
|
|
MathScriptInset const * q = (it + 1 == et) ? 0 : asScript(it);
|
|
|
|
if (q) {
|
2002-03-21 06:57:13 +00:00
|
|
|
q->drawT(p, pain, x, y);
|
2002-03-18 11:45:53 +00:00
|
|
|
x += q->width2(p);
|
|
|
|
++it;
|
|
|
|
} else {
|
2002-03-21 06:57:13 +00:00
|
|
|
p->drawT(pain, x, y);
|
2002-03-18 11:45:53 +00:00
|
|
|
x += p->width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
int MathXArray::pos2x(size_type targetpos) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
|
|
|
int x = 0;
|
2002-02-16 15:59:55 +00:00
|
|
|
const_iterator target = min(begin() + targetpos, end());
|
2001-10-12 12:02:49 +00:00
|
|
|
for (const_iterator it = begin(); it < target; ++it) {
|
|
|
|
MathInset const * p = it->nucleus();
|
2001-11-09 08:35:57 +00:00
|
|
|
MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
|
|
|
|
if (q) {
|
2001-10-12 12:02:49 +00:00
|
|
|
++it;
|
|
|
|
if (it < target)
|
2002-01-08 11:03:34 +00:00
|
|
|
x += q->width2(p);
|
2001-10-12 12:02:49 +00:00
|
|
|
else // "half" position
|
|
|
|
x += q->dxx(p) + q->nwid(p);
|
|
|
|
} else
|
|
|
|
x += p->width();
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-14 14:05:57 +00:00
|
|
|
MathArray::size_type MathXArray::x2pos(int targetx) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
const_iterator it = begin();
|
|
|
|
int lastx = 0;
|
|
|
|
int currx = 0;
|
2002-02-16 15:59:55 +00:00
|
|
|
for (; currx < targetx && it < end(); ++it) {
|
2001-07-24 16:39:55 +00:00
|
|
|
lastx = currx;
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
int wid = 0;
|
|
|
|
MathInset const * p = it->nucleus();
|
2001-11-09 08:35:57 +00:00
|
|
|
MathScriptInset const * q = 0;
|
|
|
|
if (it + 1 != end())
|
|
|
|
q = asScript(it);
|
|
|
|
if (q) {
|
2002-01-08 11:03:34 +00:00
|
|
|
wid = q->width2(p);
|
2001-10-12 12:02:49 +00:00
|
|
|
++it;
|
|
|
|
} else
|
|
|
|
wid = p->width();
|
2001-08-03 17:10:22 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
currx += wid;
|
|
|
|
}
|
|
|
|
if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
|
|
|
|
--it;
|
|
|
|
return it - begin();
|
2001-10-10 13:20:51 +00:00
|
|
|
}
|
2001-11-22 10:14:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
int MathXArray::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_;
|
|
|
|
|
2002-03-21 17:42:56 +00:00
|
|
|
return xx + yy;
|
2001-11-22 10:14:19 +00:00
|
|
|
}
|
2001-11-26 13:35:08 +00:00
|
|
|
|
|
|
|
|
2001-12-05 17:50:18 +00:00
|
|
|
void MathXArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
|
|
|
|
{
|
|
|
|
x1 = xo_;
|
|
|
|
x2 = xo_ + width_;
|
|
|
|
y1 = yo_ - ascent_;
|
|
|
|
y2 = yo_ + descent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void MathXArray::findPos(MathPosFinder & f) const
|
|
|
|
{
|
|
|
|
double x = xo_;
|
2002-03-21 17:42:56 +00:00
|
|
|
double y = yo_;
|
2001-12-05 17:50:18 +00:00
|
|
|
for (const_iterator it = begin(); it < end(); ++it) {
|
|
|
|
// check this position in the cell first
|
|
|
|
f.visit(x, y);
|
|
|
|
f.nextPos();
|
|
|
|
|
|
|
|
// check inset
|
|
|
|
MathInset const * p = it->nucleus();
|
|
|
|
p->findPos(f);
|
|
|
|
|
|
|
|
// move on
|
|
|
|
MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
|
|
|
|
if (q) {
|
|
|
|
x += q->width(p);
|
|
|
|
f.nextPos();
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
x += p->width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2002-02-13 13:15:15 +00:00
|
|
|
|
|
|
|
void MathXArray::center(int & x, int & y) const
|
|
|
|
{
|
|
|
|
x = xo_ + width_ / 2;
|
|
|
|
y = yo_ + (descent_ - ascent_) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathXArray::towards(int & x, int & y) const
|
|
|
|
{
|
|
|
|
int cx = 0;
|
|
|
|
int cy = 0;
|
|
|
|
center(cx, cy);
|
|
|
|
|
|
|
|
double r = 1.0;
|
2002-02-16 15:59:55 +00:00
|
|
|
//int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
|
2002-02-13 13:15:15 +00:00
|
|
|
|
|
|
|
x = cx + int(r * (x - cx));
|
|
|
|
y = cy + int(r * (y - cy));
|
|
|
|
}
|