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<<(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 glueChars(MathArray & dat)
61 MathArray::const_iterator it = dat.begin();
62 while (it != dat.end()) {
63 if (it->nucleus() && it->nucleus()->asCharInset()) {
64 string s = charSequence(it, dat.end());
65 MathTextCodes c = it->nucleus()->asCharInset()->code();
66 ar.push_back(MathAtom(new MathStringInset(s, c)));
77 bool needAsterisk(MathAtom const &, MathAtom const &)
83 void guessAsterisks(MathArray & dat)
88 ar.push_back(*dat.begin());
89 MathArray::const_iterator it = dat.begin();
90 MathArray::const_iterator jt = it + 1;
91 for (; jt != dat.end(); ++it, ++jt) {
92 if (needAsterisk(*it, *jt))
93 ar.push_back(MathAtom(new MathCharInset('*')));
96 ar.push_back(*dat.end());
101 MathInset * singleItem(MathArray & ar)
103 lyxerr << "ar.size: " << ar.size() << "\n";
104 //lyxerr << "ar.begin: " << ar.begin() << "\n";
105 //lyxerr << "ar.nuc: " << ar.begin()->nucleus() << "\n";
106 lyxerr << "ar.nuc: " << *ar.begin()->nucleus() << "\n";
107 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
111 void extractMatrices(MathArray & ar)
113 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
116 MathDelimInset * del = it->nucleus()->asDelimInset();
119 MathInset * arr = singleItem(del->cell(0));
120 if (!arr || !arr->asArrayInset())
122 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
127 void extractDelims(MathArray & ar) {
128 // use indices rather than iterators for the loop because we are going
129 // to modify the array.
130 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
131 MathArray::iterator it = ar.begin() + i;
134 if ((*it)->getChar() != '(')
137 // search last closing paranthesis
138 MathArray::iterator et = ar.end();
139 for (MathArray::iterator jt = it + 1; jt != ar.end(); ++jt)
140 if ((*jt)->getChar() == ')')
145 // create a proper deliminset
146 MathAtom at(new MathDelimInset("(", ")"));
147 at->cell(0) = MathArray(it + 1, et);
149 // replace the original stuff by the new inset
150 ar.erase(it + 1, et + 1);
156 // assume 'extractDelims' ran before
157 void extractFunctions(MathArray & ar)
159 // we need at least two items...
163 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
164 MathArray::iterator it = ar.begin() + i;
166 // is this a function name?
169 MathFuncInset * func = (*it)->asFuncInset();
173 // do we have an exponent?
174 // simply skippping the postion does the right thing:
175 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
176 MathArray::iterator jt = it + 1;
177 if (MathScriptInset * script = (*jt)->asScriptInset()) {
178 // allow superscripts only
179 if (script->hasDown())
186 // jt points now to the "argument". Since we had run "extractDelims"
187 // before, this could be a single argument only. Get hold of this.
189 MathDelimInset * del = (*jt)->asDelimInset();
190 if (del && del->isParanthesis())
195 // replace the function name by a real function inset
196 (*it).reset(new MathExFuncInset(func->name(), arg));
198 // remove the source of the argument from the array
204 void extractStructure(MathArray & ar)
209 extractFunctions(ar);
213 void write(MathArray const & dat, WriteStream & wi)
217 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
218 MathInset const * p = it->nucleus();
219 if (it + 1 != ar.end()) {
220 if (MathScriptInset const * q = asScript(it)) {
231 void normalize(MathArray const & dat, NormalStream & os)
235 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
236 MathInset const * p = it->nucleus();
237 if (it + 1 != ar.end()) {
238 if (MathScriptInset const * q = asScript(it)) {
249 void octavize(MathArray const & dat, OctaveStream & os)
252 extractStructure(ar);
253 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
254 MathInset const * p = it->nucleus();
255 if (it + 1 != ar.end()) {
256 if (MathScriptInset const * q = asScript(it)) {
267 void maplize(MathArray const & dat, MapleStream & os)
270 extractStructure(ar);
271 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
272 MathInset const * p = it->nucleus();
273 if (it + 1 != ar.end()) {
274 if (MathScriptInset const * q = asScript(it)) {
285 void mathmlize(MathArray const & dat, MathMLStream & os)
288 extractStructure(ar);
291 else if (ar.size() == 1)
292 os << ar.begin()->nucleus();
295 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
296 MathInset const * p = it->nucleus();
297 if (it + 1 != ar.end()) {
298 if (MathScriptInset const * q = asScript(it)) {