-#include <config.h>
-
#ifdef __GNUG__
#pragma implementation
#endif
+#include "math_inset.h"
#include "debug.h"
#include "array.h"
-#include "math_iter.h"
-#include "math_inset.h"
-#include "math_macro.h"
-
-#include "support/LOstream.h"
+#include "math_scriptinset.h"
+#include "math_parser.h"
+#include "mathed/support.h"
using std::ostream;
using std::endl;
-namespace {
+MathArray::MathArray()
+{}
+
-inline
-void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
+MathArray::~MathArray()
{
- char * ps = static_cast<char *>(ps_in);
- char const * pt = static_cast<char const *>(pt_in);
- while (n--) *ps++ = *pt++;
- return ps_in;
+ for (int pos = 0; pos < size(); next(pos))
+ if (isInset(pos))
+ delete nextInset(pos);
}
-} // namespace anon
+MathArray::MathArray(MathArray const & array)
+ : bf_(array.bf_)
+{
+ for (int pos = 0; pos < size(); next(pos))
+ if (isInset(pos))
+ replace(pos, nextInset(pos)->clone());
+}
-MathedArray::MathedArray()
- : bf_(1, '\0'), last_(0)
-{}
+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());
+}
-MathedArray::~MathedArray()
+bool MathArray::next(int & pos) const
{
- // deep destruction
- // let's leak for a while...
-/*
- MathedIter it;
- it.SetData(this);
- while (it.OK()) {
- if (it.IsInset()) {
- MathedInset * inset = it.GetInset();
- delete inset;
- }
- it.Next();
- }
-*/
+ if (pos >= size() - 1)
+ return false;
+
+ pos += item_size(pos);
+ return true;
}
-MathedArray::MathedArray(MathedArray const & array)
+bool MathArray::prev(int & pos) const
{
- // this "implementation" is obviously wrong: MathedIter should be
- // implemented by MathedArray (not the other way round) but I think
- // getting the _interface_ of MathedArray right is more important right
- // now (Andre')
+ if (pos == 0)
+ return false;
- // shallow copy
- bf_ = array.bf_;
- last_ = array.last_;
-
- // deep copy
- deep_copy();
+ pos -= item_size(pos - 1);
+ return true;
}
-void MathedArray::deep_copy()
+bool MathArray::last(int & pos) const
{
- MathedIter it(this);
- while (it.OK()) {
- if (it.IsInset()) {
- MathedInset * inset = it.GetInset();
- inset = inset->Clone();
- raw_pointer_insert(inset, it.getPos() + 1);
- }
- it.Next();
- }
+ pos = bf_.size();
+ return prev(pos);
}
-void MathedArray::substitute(MathMacro * m)
+int MathArray::item_size(int pos) const
{
- if (m->nargs() == 0)
- return;
+ return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
+}
+
- MathedIter it(this);
- while (it.OK()) {
- if (it.IsInset()) {
- MathedInset * inset = it.GetInset();
- if (inset->GetType() == LM_OT_MACRO_ARG) {
- int n = static_cast<MathMacroArgument *>(inset)->number() - 1;
- //lyxerr << "substituting an argument inset: " << n << "\n";
- inset = m->arg(n)->Clone();
- } else {
- inset->substitute(m);
-/*
- if (it.IsActive()) {
- MathParInset * pinset = static_cast<MathParInset *>(inset);
- int n = pinset->getMaxArgumentIdx();
- int idx = pinset->getArgumentIdx();
- for (int i = 0; i <= n; ++i) {
- pinset->setArgumentIdx(i);
- pinset->GetData().substitute(m);
- }
- pinset->setArgumentIdx(idx);
- }
-*/
- //lyxerr << "substituting in an ordinary inset\n";
- }
- raw_pointer_insert(inset, it.getPos() + 1);
- }
- it.Next();
+void MathArray::substitute(MathMacro const & m)
+{
+ MathArray tmp;
+ for (int pos = 0; pos < size(); next(pos)) {
+ if (isInset(pos))
+ nextInset(pos)->substitute(tmp, m);
+ else
+ tmp.push_back(GetChar(pos), GetCode(pos));
}
+ swap(tmp);
}
-MathedArray & MathedArray::operator=(MathedArray const & array)
+MathArray & MathArray::operator=(MathArray const & array)
{
- MathedArray tmp(array);
+ MathArray tmp(array);
swap(tmp);
return *this;
}
-void MathedArray::push_back(MathedInset * inset, int t)
+MathInset * MathArray::nextInset(int pos) const
{
- MathedIter it(this);
- while (it.Next())
- ;
- it.insertInset(inset, t);
+ if (!isInset(pos))
+ return 0;
+ MathInset * p;
+ memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
+ return p;
}
-
-void MathedArray::push_back(byte b, MathedTextCodes c)
+MathInset * MathArray::prevInset(int pos) const
{
- MathedIter it(this);
- while (it.Next())
- ;
- it.insert(b, c);
+ if (!pos)
+ return 0;
+ prev(pos);
+ return nextInset(pos);
}
-
-void MathedArray::clear()
+unsigned char MathArray::GetChar(int pos) const
{
- last_ = 0;
- bf_.resize(1);
- bf_[0] = 0;
+ return pos < size() ? bf_[pos + 1] : '\0';
}
-
-void MathedArray::swap(MathedArray & array)
+string MathArray::GetString(int & pos) const
{
- if (this != &array) {
- bf_.swap(array.bf_);
- std::swap(last_, array.last_);
- }
-}
+ 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);
-MathedArray::iterator MathedArray::begin()
-{
- return bf_.begin();
+ return s;
}
-
-MathedArray::iterator MathedArray::end()
+MathTextCodes MathArray::GetCode(int pos) const
{
- return bf_.end();
+ return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
}
-
-MathedArray::const_iterator MathedArray::begin() const
+void MathArray::setCode(int pos, MathTextCodes t)
{
- return bf_.begin();
+ if (pos > size() || isInset(pos))
+ return;
+ bf_[pos] = t;
+ bf_[pos + 2] = t;
}
-
-MathedArray::const_iterator MathedArray::end() const
+void MathArray::insert(int pos, MathInset * p)
{
- return bf_.end();
+ bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
+ memcpy(&bf_[pos + 1], &p, sizeof(p));
}
-int MathedArray::empty() const
+void MathArray::replace(int pos, MathInset * p)
{
- return (last_ == 0);
+ memcpy(&bf_[pos + 1], &p, sizeof(p));
}
-
-int MathedArray::last() const
+void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
{
- return last_;
+ bf_.insert(bf_.begin() + pos, 3, t);
+ bf_[pos + 1] = b;
}
-void MathedArray::last(int l)
+void MathArray::insert(int pos, MathArray const & array)
{
- last_ = l;
+ bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
+ for (int p = pos; p < pos + array.size(); next(p))
+ if (isInset(p))
+ replace(p, nextInset(p)->clone());
}
-void MathedArray::need_size(int needed)
-{
- if (needed >= static_cast<int>(bf_.size()))
- resize(needed);
+void MathArray::push_back(MathInset * p)
+{
+ insert(size(), p);
}
-
-void MathedArray::resize(int newsize)
+void MathArray::push_back(unsigned char b, MathTextCodes c)
{
- // still a bit smelly...
- ++newsize;
- bf_.resize(newsize + 1);
- if (last_ >= newsize)
- last_ = newsize - 1;
- bf_[last_] = 0;
+ insert(size(), b, c);
}
-
-void MathedArray::move(int p, int shift)
+void MathArray::push_back(MathArray const & array)
{
- if (p <= last_) {
- need_size(last_ + shift);
- std::memmove(&bf_[p + shift], &bf_[p], last_ - p);
- last_ += shift;
- bf_[last_] = 0;
- }
+ insert(size(), array);
}
-void MathedArray::shrink(int pos1, int pos2)
+void MathArray::clear()
{
- if (pos1 == 0 && pos2 >= last())
- return;
+ bf_.clear();
+}
- short fc = 0;
- if (pos1 > 0 && bf_[pos1] > ' ') {
- for (int p = pos1; p >= 0; --p) {
- if (MathIsFont(bf_[p])) {
- if (p != pos1 - 1)
- fc = bf_[p];
- else
- --pos1;
- break;
- }
- }
- }
- if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
- --pos2;
+void MathArray::swap(MathArray & array)
+{
+ if (this != &array)
+ bf_.swap(array.bf_);
+}
- int dx = pos2 - pos1;
- MathedArray a;
- a.resize(dx + 1);
- strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
- if (fc) {
- a[0] = fc;
- ++dx;
- }
- a.last(dx);
- a[dx] = '\0';
- swap(a);
- deep_copy();
+bool MathArray::empty() const
+{
+ return bf_.empty();
+}
+
+
+int MathArray::size() const
+{
+ return bf_.size();
}
-#if 0
-void MathedArray::insert(MathedArray::iterator pos,
- MathedArray::const_iterator beg,
- MathedArray::const_iterator end)
+void MathArray::erase()
{
- bf_.insert(pos, beg, end);
- last_ = bf_.size() - 1;
+ erase(0, size());
}
-#else
-void MathedArray::merge(MathedArray const & a, int p)
+
+
+void MathArray::erase(int pos)
{
- my_memcpy(&bf_[p], &a.bf_[0], a.last());
+ if (pos < static_cast<int>(bf_.size()))
+ erase(pos, pos + item_size(pos));
}
-#endif
-void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
+void MathArray::erase(int pos1, int pos2)
{
- my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
+ bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
}
-#if 0
-void MathedArray::insertInset(int pos, MathedInset * p, int type)
+bool MathArray::isInset(int pos) const
{
- //bf_.insert(pos, type);
- InsetTable tmp(pos, p);
- insetList_.push_back(tmp);
+ if (pos >= size())
+ return false;
+ return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
}
-MathedInset * MathedArray::getInset(int pos)
+MathInset * MathArray::back_inset() const
{
- InsetList::const_iterator cit = insetList_.begin();
- InsetList::const_iterator end = insetList_.end();
- for (; cit != end; ++cit) {
- if ((*cit).pos == pos)
- return (*cit).inset;
+ if (!empty()) {
+ int pos = size();
+ prev(pos);
+ if (isInset(pos))
+ return nextInset(pos);
}
- // not found
return 0;
- // We would really like to throw an exception instead... (Lgb)
- // throw inset_not_found();
}
-#else
-void MathedArray::raw_pointer_insert(void * p, int pos)
+
+void MathArray::dump2(ostream & os) const
{
- my_memcpy(&bf_[pos], &p, sizeof(p));
+ for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
+ os << int(*it) << ' ';
+ os << endl;
}
-#endif
-void MathedArray::strange_copy(MathedArray * dest, int dpos,
- int spos, int len)
+
+void MathArray::dump(ostream & os) const
{
- my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
+ for (int pos = 0; pos < size(); next(pos)) {
+ if (isInset(pos))
+ os << "<inset: " << nextInset(pos) << ">";
+ else
+ os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
+ }
}
-byte MathedArray::operator[](int i) const
+std::ostream & operator<<(std::ostream & os, MathArray const & ar)
{
- return bf_[i];
+ ar.dump2(os);
+ return os;
}
-byte & MathedArray::operator[](int i)
+void MathArray::Write(ostream & os, bool fragile) const
{
- return bf_[i];
+ if (empty())
+ return;
+
+ int brace = 0;
+
+ for (int pos = 0; pos < size(); next(pos)) {
+ if (isInset(pos)) {
+
+ nextInset(pos)->Write(os, fragile);
+
+ } else {
+
+ MathTextCodes fcode = GetCode(pos);
+ unsigned char c = GetChar(pos);
+
+ if (MathIsSymbol(fcode)) {
+ 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;
+ }
+ } 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
+ os << c;
+ }
+
+ if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
+ os << '}';
+
+ }
+ }
+
+ if (brace > 0)
+ os << string(brace, '}');
}
-void MathedArray::dump2(ostream & os) const
+void MathArray::WriteNormal(ostream & os) const
{
- buffer_type::const_iterator cit = bf_.begin();
- buffer_type::const_iterator end = bf_.end();
- for (; cit != end; ++cit) {
- os << (*cit);
+ if (empty()) {
+ os << "[par] ";
+ return;
}
- os << endl;
+
+ Write(os, true);
}
-void MathedArray::dump(ostream & os) const
-{
- MathedIter it( const_cast<MathedArray*>(this) );
- while (it.OK()) {
- if (it.IsInset()) {
- MathedInset * inset = it.GetInset();
- os << "<inset: " << inset << ">";
- }
- else if (it.IsTab())
- os << "<tab>";
- else if (it.IsCR())
- os << "<cr>";
- else if (it.IsScript())
- os << "<script>";
- else if (it.IsFont())
- os << "<font: " << int(it.at()) << ">";
- else if (it.at() >= 32 && it.at() < 127)
- os << it.at();
- else
- os << "<unknown: " << int(it.at()) << ">";
- it.Next();
- }
+void MathArray::Validate(LaTeXFeatures & features) const
+{
+ for (int pos = 0; pos < size(); next(pos))
+ if (isInset(pos))
+ nextInset(pos)->Validate(features);
}
+