#ifdef __GNUG__ #pragma implementation #endif #include "math_inset.h" #include "math_charinset.h" #include "math_scriptinset.h" #include "math_stringinset.h" #include "math_mathmlstream.h" #include "math_support.h" #include "math_data.h" #include "debug.h" #include "support/LAssert.h" MathArray::MathArray() {} MathArray::MathArray(MathArray const & ar, size_type from, size_type to) : bf_(ar.begin() + from, ar.begin() + to) {} void MathArray::substitute(MathMacro const & m) { for (iterator it = begin(); it != end(); ++it) it->nucleus()->substitute(m); } MathScriptInset const * MathArray::asScript(const_iterator it) const { if (it->nucleus()->asScriptInset()) return 0; const_iterator jt = it + 1; if (jt == end()) return 0; if (!jt->nucleus()) return 0; return jt->nucleus()->asScriptInset(); } MathAtom & MathArray::at(size_type pos) { lyx::Assert(pos < size()); return bf_[pos]; } MathAtom const & MathArray::at(size_type pos) const { lyx::Assert(pos < size()); return bf_[pos]; } void MathArray::insert(size_type pos, MathAtom const & t) { bf_.insert(begin() + pos, t); } void MathArray::insert(size_type pos, MathArray const & ar) { bf_.insert(begin() + pos, ar.begin(), ar.end()); } void MathArray::push_back(MathAtom const & t) { bf_.push_back(t); } void MathArray::push_back(MathArray const & ar) { insert(size(), ar); } void MathArray::clear() { erase(); } void MathArray::swap(MathArray & ar) { if (this != &ar) bf_.swap(ar.bf_); } bool MathArray::empty() const { return bf_.empty(); } MathArray::size_type MathArray::size() const { return bf_.size(); } void MathArray::erase() { erase(0, size()); } void MathArray::erase(size_type pos) { if (pos < size()) erase(pos, pos + 1); } void MathArray::erase(size_type pos1, size_type pos2) { bf_.erase(begin() + pos1, begin() + pos2); } MathAtom & MathArray::back() { return bf_.back(); } void MathArray::dump2() const { NormalStream ns(lyxerr); for (const_iterator it = begin(); it != end(); ++it) ns << it->nucleus() << ' '; } void MathArray::dump() const { NormalStream ns(lyxerr); for (const_iterator it = begin(); it != end(); ++it) ns << "<" << it->nucleus() << ">"; } // returns sequence of char with same code starting at it up to end // it might be less, though... string charSequence(MathArray::const_iterator it, MathArray::const_iterator end) { string s; MathCharInset const * p = it->nucleus()->asCharInset(); if (!p) return s; for (MathTextCodes c = p->code(); it != end; ++it) { if (!it->nucleus()) break; p = it->nucleus()->asCharInset(); if (!p || p->code() != c) break; s += p->getChar(); } return s; } MathArray MathArray::glueChars() const { MathArray ar; const_iterator it = begin(); while (it != end()) { if (it->nucleus() && it->nucleus()->asCharInset()) { string s = charSequence(it, end()); MathTextCodes c = it->nucleus()->asCharInset()->code(); ar.push_back(MathAtom(new MathStringInset(s, c))); it += s.size(); } else { ar.push_back(*it); ++it; } } return ar; } bool needAsterisk(MathAtom const &, MathAtom const &) { return false; } MathArray MathArray::guessAsterisks() const { if (size() <= 1) return *this; MathArray ar; ar.push_back(*begin()); for (const_iterator it = begin(), jt = begin()+1 ; jt != end(); ++it, ++jt) { if (needAsterisk(*it, *jt)) ar.push_back(MathAtom(new MathCharInset('*'))); ar.push_back(*it); } ar.push_back(*end()); return ar; } void MathArray::write(MathWriteInfo & wi) const { MathArray ar = glueChars(); for (const_iterator it = ar.begin(); it != ar.end(); ++it) { MathInset const * p = it->nucleus(); if (MathScriptInset const * q = ar.asScript(it)) { q->write(p, wi); ++it; } else { p->write(wi); } } } void MathArray::writeNormal(NormalStream & os) const { MathArray ar = glueChars(); for (const_iterator it = ar.begin(); it != ar.end(); ++it) { MathInset const * p = it->nucleus(); if (MathScriptInset const * q = ar.asScript(it)) { q->writeNormal(p, os); ++it; } else p->writeNormal(os); } } void MathArray::octavize(OctaveStream & os) const { MathArray ar = glueChars(); for (const_iterator it = ar.begin(); it != ar.end(); ++it) { MathInset const * p = it->nucleus(); if (MathScriptInset const * q = ar.asScript(it)) { q->octavize(p, os); ++it; } else p->octavize(os); } } void MathArray::maplize(MapleStream & os) const { MathArray ar = glueChars(); for (const_iterator it = ar.begin(); it != ar.end(); ++it) { MathInset const * p = it->nucleus(); if (MathScriptInset const * q = ar.asScript(it)) { q->maplize(p, os); ++it; } else p->maplize(os); } } void MathArray::mathmlize(MathMLStream & os) const { MathArray ar = glueChars(); if (ar.size() == 0) os << ""; else if (ar.size() == 1) os << ar.begin()->nucleus(); else { os << MTag("mrow"); for (const_iterator it = ar.begin(); it != ar.end(); ++it) { MathInset const * p = it->nucleus(); if (MathScriptInset const * q = ar.asScript(it)) { q->mathmlize(p, os); ++it; } else p->mathmlize(os); } os << ETag("mrow"); } } void MathArray::validate(LaTeXFeatures & features) const { for (const_iterator it = begin(); it != end(); ++it) if (it->nucleus()) it->nucleus()->validate(features); } void MathArray::pop_back() { if (!size()) { lyxerr << "pop_back from empty array!\n"; return; } bf_.pop_back(); } MathArray::const_iterator MathArray::begin() const { return bf_.begin(); } MathArray::const_iterator MathArray::end() const { return bf_.end(); } MathArray::iterator MathArray::begin() { return bf_.begin(); } MathArray::iterator MathArray::end() { return bf_.end(); } bool MathArray::isMatrix() const { return size() == 1 && begin()->nucleus() && begin()->nucleus()->isMatrix(); }