2001-02-12 08:55:14 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-07-13 09:54:32 +00:00
|
|
|
#include "math_inset.h"
|
2001-04-24 16:13:38 +00:00
|
|
|
#include "debug.h"
|
2001-02-12 08:55:14 +00:00
|
|
|
#include "array.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "math_scriptinset.h"
|
|
|
|
#include "math_parser.h"
|
|
|
|
#include "mathed/support.h"
|
2001-02-12 08:55:14 +00:00
|
|
|
|
2001-06-27 15:57:57 +00:00
|
|
|
using std::ostream;
|
|
|
|
using std::endl;
|
2001-06-01 10:53:24 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray::MathArray()
|
|
|
|
{}
|
2001-06-01 10:53:24 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray::~MathArray()
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
for (int pos = 0; pos < size(); next(pos))
|
2001-07-20 14:54:13 +00:00
|
|
|
if (isInset(pos))
|
2001-07-12 07:18:29 +00:00
|
|
|
delete nextInset(pos);
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray::MathArray(MathArray const & array)
|
|
|
|
: bf_(array.bf_)
|
|
|
|
{
|
|
|
|
for (int pos = 0; pos < size(); next(pos))
|
|
|
|
if (isInset(pos))
|
2001-07-12 07:18:29 +00:00
|
|
|
replace(pos, nextInset(pos)->clone());
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
2001-02-14 17:50:58 +00:00
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
MathArray::MathArray(MathArray const & array, int from, int to)
|
|
|
|
: bf_(array.bf_.begin() + from, array.bf_.begin() + to)
|
|
|
|
{
|
|
|
|
for (int pos = 0; pos < size(); next(pos))
|
|
|
|
if (isInset(pos))
|
|
|
|
replace(pos, nextInset(pos)->clone());
|
|
|
|
}
|
|
|
|
|
2001-02-19 18:29:10 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathArray::next(int & pos) const
|
2001-02-19 14:16:57 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (pos >= size() - 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pos += item_size(pos);
|
|
|
|
return true;
|
2001-02-19 14:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathArray::prev(int & pos) const
|
2001-02-19 14:16:57 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (pos == 0)
|
|
|
|
return false;
|
2001-02-19 14:16:57 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
pos -= item_size(pos - 1);
|
|
|
|
return true;
|
2001-03-06 10:24:45 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 10:50:09 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathArray::last(int & pos) const
|
2001-03-06 10:24:45 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
pos = bf_.size();
|
|
|
|
return prev(pos);
|
2001-02-19 14:16:57 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 10:50:09 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int MathArray::item_size(int pos) const
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
|
|
|
|
}
|
2001-04-24 16:13:38 +00:00
|
|
|
|
2001-04-27 12:35:55 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::substitute(MathMacro const & m)
|
|
|
|
{
|
|
|
|
MathArray tmp;
|
|
|
|
for (int pos = 0; pos < size(); next(pos)) {
|
|
|
|
if (isInset(pos))
|
2001-07-12 07:18:29 +00:00
|
|
|
nextInset(pos)->substitute(tmp, m);
|
2001-06-25 00:06:33 +00:00
|
|
|
else
|
|
|
|
tmp.push_back(GetChar(pos), GetCode(pos));
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
swap(tmp);
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2001-02-19 14:16:57 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray & MathArray::operator=(MathArray const & array)
|
2001-02-19 14:16:57 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray tmp(array);
|
2001-02-19 14:16:57 +00:00
|
|
|
swap(tmp);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2001-05-08 10:50:09 +00:00
|
|
|
|
2001-07-12 07:18:29 +00:00
|
|
|
MathInset * MathArray::nextInset(int pos) const
|
2001-04-27 12:35:55 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (!isInset(pos))
|
|
|
|
return 0;
|
|
|
|
MathInset * p;
|
2001-06-27 15:33:55 +00:00
|
|
|
memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
|
2001-06-25 00:06:33 +00:00
|
|
|
return p;
|
2001-04-27 12:35:55 +00:00
|
|
|
}
|
|
|
|
|
2001-07-12 07:18:29 +00:00
|
|
|
MathInset * MathArray::prevInset(int pos) const
|
|
|
|
{
|
|
|
|
if (!pos)
|
|
|
|
return 0;
|
|
|
|
prev(pos);
|
|
|
|
return nextInset(pos);
|
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-07-13 11:32:22 +00:00
|
|
|
unsigned char MathArray::GetChar(int pos) const
|
2001-04-27 12:35:55 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return pos < size() ? bf_[pos + 1] : '\0';
|
2001-04-27 12:35:55 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
string MathArray::GetString(int & pos) const
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
if (isInset(pos))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
MathTextCodes const fcode = GetCode(pos);
|
|
|
|
do {
|
|
|
|
s += GetChar(pos);
|
|
|
|
next(pos);
|
|
|
|
} while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathTextCodes MathArray::GetCode(int pos) const
|
2001-02-20 13:16:07 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
|
2001-02-20 13:16:07 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-07-03 07:56:55 +00:00
|
|
|
void MathArray::setCode(int pos, MathTextCodes t)
|
|
|
|
{
|
|
|
|
if (pos > size() || isInset(pos))
|
|
|
|
return;
|
|
|
|
bf_[pos] = t;
|
|
|
|
bf_[pos + 2] = t;
|
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-07-24 16:10:17 +00:00
|
|
|
void MathArray::replace(int pos, MathInset * p)
|
2001-02-19 14:16:57 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
memcpy(&bf_[pos + 1], &p, sizeof(p));
|
2001-02-19 14:16:57 +00:00
|
|
|
}
|
|
|
|
|
2001-02-14 17:50:58 +00:00
|
|
|
|
2001-07-24 16:10:17 +00:00
|
|
|
void MathArray::insert(int pos, MathInset * p)
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2001-07-24 16:10:17 +00:00
|
|
|
bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
|
2001-06-25 00:06:33 +00:00
|
|
|
memcpy(&bf_[pos + 1], &p, sizeof(p));
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-07-13 11:32:22 +00:00
|
|
|
void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
bf_.insert(bf_.begin() + pos, 3, t);
|
|
|
|
bf_[pos + 1] = b;
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::insert(int pos, MathArray const & array)
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2001-07-03 15:04:59 +00:00
|
|
|
bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
|
|
|
|
for (int p = pos; p < pos + array.size(); next(p))
|
|
|
|
if (isInset(p))
|
2001-07-12 07:18:29 +00:00
|
|
|
replace(p, nextInset(p)->clone());
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::push_back(MathInset * p)
|
|
|
|
{
|
|
|
|
insert(size(), p);
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-07-13 11:32:22 +00:00
|
|
|
void MathArray::push_back(unsigned char b, MathTextCodes c)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
insert(size(), b, c);
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 11:39:38 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::push_back(MathArray const & array)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
insert(size(), array);
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::clear()
|
2001-02-12 11:25:44 +00:00
|
|
|
{
|
2001-07-24 11:39:38 +00:00
|
|
|
for (int pos = 0; pos < size(); next(pos))
|
|
|
|
if (isInset(pos))
|
|
|
|
delete nextInset(pos);
|
2001-06-25 00:06:33 +00:00
|
|
|
bf_.clear();
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::swap(MathArray & array)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (this != &array)
|
|
|
|
bf_.swap(array.bf_);
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathArray::empty() const
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return bf_.empty();
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-02-12 08:55:14 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int MathArray::size() const
|
2001-02-20 13:16:07 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return bf_.size();
|
2001-02-20 13:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
void MathArray::erase()
|
|
|
|
{
|
|
|
|
erase(0, size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::erase(int pos)
|
2001-02-14 17:50:58 +00:00
|
|
|
{
|
2001-06-27 14:10:35 +00:00
|
|
|
if (pos < static_cast<int>(bf_.size()))
|
2001-06-25 00:06:33 +00:00
|
|
|
erase(pos, pos + item_size(pos));
|
2001-02-14 17:50:58 +00:00
|
|
|
}
|
2001-02-12 08:55:14 +00:00
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::erase(int pos1, int pos2)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathArray::isInset(int pos) const
|
2001-02-17 18:52:53 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (pos >= size())
|
|
|
|
return false;
|
2001-07-20 14:54:13 +00:00
|
|
|
return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
|
2001-02-17 18:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathInset * MathArray::back_inset() const
|
2001-02-17 18:52:53 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (!empty()) {
|
|
|
|
int pos = size();
|
|
|
|
prev(pos);
|
|
|
|
if (isInset(pos))
|
2001-07-12 07:18:29 +00:00
|
|
|
return nextInset(pos);
|
2001-02-17 18:52:53 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
void MathArray::dump2(ostream & os) const
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
|
|
|
|
os << int(*it) << ' ';
|
|
|
|
os << endl;
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathArray::dump(ostream & os) const
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
for (int pos = 0; pos < size(); next(pos)) {
|
|
|
|
if (isInset(pos))
|
2001-07-12 07:18:29 +00:00
|
|
|
os << "<inset: " << nextInset(pos) << ">";
|
2001-06-25 00:06:33 +00:00
|
|
|
else
|
|
|
|
os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
|
|
|
|
}
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
std::ostream & operator<<(std::ostream & os, MathArray const & ar)
|
2001-02-12 08:55:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
ar.dump2(os);
|
|
|
|
return os;
|
2001-02-12 08:55:14 +00:00
|
|
|
}
|
2001-02-28 17:21:16 +00:00
|
|
|
|
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
void MathArray::write(ostream & os, bool fragile) const
|
2001-02-28 17:21:16 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int brace = 0;
|
|
|
|
|
|
|
|
for (int pos = 0; pos < size(); next(pos)) {
|
|
|
|
if (isInset(pos)) {
|
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
nextInset(pos)->write(os, fragile);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
MathTextCodes fcode = GetCode(pos);
|
2001-07-13 11:32:22 +00:00
|
|
|
unsigned char c = GetChar(pos);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
if (MathIsSymbol(fcode)) {
|
2001-06-27 14:10:35 +00:00
|
|
|
latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
|
|
|
|
|
|
|
|
if (l == 0) {
|
|
|
|
l = lm_get_key_by_id(c, LM_TK_BIGSYM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l) {
|
|
|
|
os << '\\' << l->name << ' ';
|
|
|
|
} else {
|
|
|
|
lyxerr << "Could not find the LaTeX name for " << c << " and fcode " << fcode << "!" << std::endl;
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
} else {
|
|
|
|
if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
|
|
|
|
os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
|
|
|
|
|
|
|
|
// Is there a standard logical XOR?
|
|
|
|
if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
|
|
|
|
(fcode == LM_TC_SPECIAL))
|
|
|
|
os << '\\';
|
|
|
|
else {
|
|
|
|
if (c == '{')
|
|
|
|
++brace;
|
|
|
|
if (c == '}')
|
|
|
|
--brace;
|
|
|
|
}
|
|
|
|
if (c == '}' && fcode == LM_TC_TEX && brace < 0)
|
|
|
|
lyxerr <<"Math warning: Unexpected closing brace.\n";
|
|
|
|
else
|
2001-06-27 14:10:35 +00:00
|
|
|
os << c;
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
|
|
|
|
os << '}';
|
|
|
|
|
|
|
|
}
|
2001-02-28 17:21:16 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
if (brace > 0)
|
|
|
|
os << string(brace, '}');
|
2001-02-28 17:21:16 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 10:50:09 +00:00
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
void MathArray::writeNormal(ostream & os) const
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (empty()) {
|
|
|
|
os << "[par] ";
|
|
|
|
return;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
write(os, true);
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-07-13 14:54:56 +00:00
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
void MathArray::validate(LaTeXFeatures & features) const
|
2001-07-13 14:54:56 +00:00
|
|
|
{
|
|
|
|
for (int pos = 0; pos < size(); next(pos))
|
|
|
|
if (isInset(pos))
|
2001-07-26 06:56:43 +00:00
|
|
|
nextInset(pos)->validate(features);
|
2001-07-13 14:54:56 +00:00
|
|
|
}
|
|
|
|
|
2001-07-26 06:46:50 +00:00
|
|
|
|
|
|
|
void MathArray::pop_back()
|
|
|
|
{
|
|
|
|
int pos = size();
|
|
|
|
prev(pos);
|
|
|
|
erase(pos);
|
|
|
|
}
|
|
|
|
|