2 // This file contains most of the magic that extracts "context
3 // information" from the unstructered layout-oriented stuff in an
7 #include "math_charinset.h"
8 #include "math_deliminset.h"
9 #include "math_exfuncinset.h"
10 #include "math_funcinset.h"
11 #include "math_matrixinset.h"
12 #include "math_mathmlstream.h"
13 #include "math_scriptinset.h"
14 #include "math_stringinset.h"
18 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
26 MathScriptInset const * asScript(MathArray::const_iterator it)
28 if (it->nucleus()->asScriptInset())
33 return it->nucleus()->asScriptInset();
38 // returns sequence of char with same code starting at it up to end
39 // it might be less, though...
40 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
43 MathCharInset const * p = it->nucleus()->asCharInset();
45 for (MathTextCodes c = p->code(); it != end; ++it) {
48 p = it->nucleus()->asCharInset();
49 if (!p || p->code() != c)
58 void extractStrings(MathArray & dat)
60 //lyxerr << "\nStrings from: " << ar << "\n";
62 MathArray::const_iterator it = dat.begin();
63 while (it != dat.end()) {
64 if (it->nucleus() && it->nucleus()->asCharInset()) {
65 string s = charSequence(it, dat.end());
66 MathTextCodes c = it->nucleus()->asCharInset()->code();
67 ar.push_back(MathAtom(new MathStringInset(s, c)));
75 //lyxerr << "\nStrings to: " << ar << "\n";
79 bool needAsterisk(MathAtom const &, MathAtom const &)
85 void guessAsterisks(MathArray & dat)
90 ar.push_back(*dat.begin());
91 MathArray::const_iterator it = dat.begin();
92 MathArray::const_iterator jt = it + 1;
93 for (; jt != dat.end(); ++it, ++jt) {
94 if (needAsterisk(*it, *jt))
95 ar.push_back(MathAtom(new MathCharInset('*')));
98 ar.push_back(*dat.end());
103 MathInset * singleItem(MathArray & ar)
105 lyxerr << "ar.size: " << ar.size() << "\n";
106 //lyxerr << "ar.begin: " << ar.begin() << "\n";
107 //lyxerr << "ar.nuc: " << ar.begin()->nucleus() << "\n";
108 lyxerr << "ar.nuc: " << *ar.begin()->nucleus() << "\n";
109 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
113 void extractMatrices(MathArray & ar)
115 lyxerr << "\nMatrices from: " << ar << "\n";
116 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
119 MathDelimInset * del = it->nucleus()->asDelimInset();
122 MathInset * arr = singleItem(del->cell(0));
123 if (!arr || !arr->asArrayInset())
125 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
126 lyxerr << "\nMatrices to: " << ar << "\n";
130 // convert this inset somehow to a string
131 string extractString(MathInset * p)
133 if (p && p->getChar())
134 return string(1, p->getChar());
135 if (p && p->asStringInset())
136 return p->asStringInset()->str();
141 // replace '('...')' sequences by a real MathDelimInset
142 void extractDelims(MathArray & ar) {
143 // use indices rather than iterators for the loop because we are going
144 // to modify the array.
145 lyxerr << "\nDelims from: " << ar << "\n";
146 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
147 MathArray::iterator it = ar.begin() + i;
148 if (extractString(it->nucleus()) != "(")
151 // search matching closing paranthesis
153 MathArray::iterator jt = it + 1;
154 for (; jt != ar.end(); ++jt) {
155 string s = extractString(jt->nucleus());
166 // create a proper deliminset
167 MathAtom at(new MathDelimInset("(", ")"));
168 at->cell(0) = MathArray(it + 1, jt);
170 // replace the original stuff by the new inset
171 ar.erase(it + 1, jt + 1);
173 lyxerr << "\nDelims to: " << ar << "\n";
178 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
179 // assume 'extractDelims' ran before
180 void extractFunctions(MathArray & ar)
182 // we need at least two items...
186 lyxerr << "\nFunctions from: " << ar << "\n";
187 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
188 MathArray::iterator it = ar.begin() + i;
190 // is this a function name?
193 MathFuncInset * func = (*it)->asFuncInset();
197 // do we have an exponent?
198 // simply skippping the postion does the right thing:
199 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
200 MathArray::iterator jt = it + 1;
201 if (MathScriptInset * script = (*jt)->asScriptInset()) {
202 // allow superscripts only
203 if (script->hasDown())
210 // jt points now to the "argument". Since we had run "extractDelims"
211 // before, this could be a single argument only. Get hold of this.
213 MathDelimInset * del = (*jt)->asDelimInset();
214 if (del && del->isParanthesis())
219 // replace the function name by a real function inset
220 (*it).reset(new MathExFuncInset(func->name(), arg));
222 // remove the source of the argument from the array
224 lyxerr << "\nFunctions to: " << ar << "\n";
229 void extractStructure(MathArray & ar)
234 extractFunctions(ar);
238 void write(MathArray const & dat, WriteStream & wi)
242 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
243 MathInset const * p = it->nucleus();
244 if (it + 1 != ar.end()) {
245 if (MathScriptInset const * q = asScript(it)) {
256 void normalize(MathArray const & ar, NormalStream & os)
258 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
259 (*it)->normalize(os);
263 void octavize(MathArray const & dat, OctaveStream & os)
266 extractStructure(ar);
267 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
268 MathInset const * p = it->nucleus();
269 if (it + 1 != ar.end()) {
270 if (MathScriptInset const * q = asScript(it)) {
281 void maplize(MathArray const & dat, MapleStream & os)
284 extractStructure(ar);
285 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
286 MathInset const * p = it->nucleus();
287 if (it + 1 != ar.end()) {
288 if (MathScriptInset const * q = asScript(it)) {
299 void mathmlize(MathArray const & dat, MathMLStream & os)
302 extractStructure(ar);
305 else if (ar.size() == 1)
306 os << ar.begin()->nucleus();
309 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
310 MathInset const * p = it->nucleus();
311 if (it + 1 != ar.end()) {
312 if (MathScriptInset const * q = asScript(it)) {