2 // This file contains most of the magic that extracts "context
3 // information" from the unstructered layout-oriented stuff in an
8 #include "math_charinset.h"
9 #include "math_deliminset.h"
10 #include "math_diffinset.h"
11 #include "math_exfuncinset.h"
12 #include "math_exintinset.h"
13 #include "math_funcinset.h"
14 #include "math_fracinset.h"
15 #include "math_matrixinset.h"
16 #include "math_mathmlstream.h"
17 #include "math_scriptinset.h"
18 #include "math_stringinset.h"
19 #include "math_symbolinset.h"
23 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
31 // define a function for tests
32 typedef bool TestItemFunc(MathInset *);
34 // define a function for replacing subexpressions
35 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
39 // try to extract an "argument" to some function.
40 // returns position behind the argument
41 MathArray::iterator extractArgument(MathArray & ar,
42 MathArray::iterator pos, MathArray::iterator last, string const & = "")
44 // nothing to get here
48 // something deliminited _is_ an argument
49 if ((*pos)->asDelimInset()) {
54 // always take the first thing, no matter what it is
57 // go ahead if possible
62 // if the next item is a subscript, it most certainly belongs to the
64 if ((*pos)->asScriptInset()) {
66 // go ahead if possible
72 // but it might be more than that.
73 // FIXME: not implemented
74 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
75 // // always take the first thing, no matter
85 MathScriptInset const * asScript(MathArray::const_iterator it)
87 if (it->nucleus()->asScriptInset())
92 return it->nucleus()->asScriptInset();
97 // returns sequence of char with same code starting at it up to end
98 // it might be less, though...
99 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
102 MathCharInset const * p = it->nucleus()->asCharInset();
104 for (MathTextCodes c = p->code(); it != end; ++it) {
105 p = it->nucleus()->asCharInset();
106 if (!p || p->code() != c)
115 void extractStrings(MathArray & dat)
117 //lyxerr << "\nStrings from: " << ar << "\n";
119 MathArray::const_iterator it = dat.begin();
120 while (it != dat.end()) {
121 if (it->nucleus() && it->nucleus()->asCharInset()) {
122 string s = charSequence(it, dat.end());
123 MathTextCodes c = it->nucleus()->asCharInset()->code();
124 ar.push_back(MathAtom(new MathStringInset(s, c)));
132 //lyxerr << "\nStrings to: " << ar << "\n";
136 MathInset * singleItem(MathArray & ar)
138 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
142 void extractMatrices(MathArray & ar)
144 lyxerr << "\nMatrices from: " << ar << "\n";
145 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
146 MathDelimInset * del = (*it)->asDelimInset();
149 MathInset * arr = singleItem(del->cell(0));
150 if (!arr || !arr->asArrayInset())
152 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
154 lyxerr << "\nMatrices to: " << ar << "\n";
158 // convert this inset somehow to a string
159 string extractString(MathInset * p)
161 if (p && p->getChar())
162 return string(1, p->getChar());
163 if (p && p->asStringInset())
164 return p->asStringInset()->str();
169 bool stringTest(MathInset * p, const string & str)
171 return extractString(p) == str;
175 // search end of nested sequence
176 MathArray::iterator endNestSearch(
177 MathArray::iterator it,
178 MathArray::iterator last,
179 TestItemFunc testOpen,
180 TestItemFunc testClose
183 for (int level = 0; it != last; ++it) {
184 if (testOpen(it->nucleus()))
186 if (testClose(it->nucleus()))
195 // replace nested sequences by a real Insets
198 TestItemFunc testOpen,
199 TestItemFunc testClose,
200 ReplaceArgumentFunc replaceArg
203 // use indices rather than iterators for the loop because we are going
204 // to modify the array.
205 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
206 // check whether this is the begin of the sequence
207 MathArray::iterator it = ar.begin() + i;
208 if (!testOpen(it->nucleus()))
211 // search end of sequence
212 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
216 // create a proper inset as replacement
217 MathInset * p = replaceArg(MathArray(it + 1, jt));
219 // replace the original stuff by the new inset
220 ar.erase(it + 1, jt + 1);
227 // search deliminiters
230 bool openParanTest(MathInset * p)
232 return stringTest(p, "(");
236 bool closeParanTest(MathInset * p)
238 return stringTest(p, ")");
242 MathInset * delimReplacement(const MathArray & ar)
244 MathDelimInset * del = new MathDelimInset("(", ")");
250 // replace '('...')' sequences by a real MathDelimInset
251 void extractDelims(MathArray & ar)
253 lyxerr << "\nDelims from: " << ar << "\n";
254 replaceNested(ar, openParanTest, closeParanTest, delimReplacement);
255 lyxerr << "\nDelims to: " << ar << "\n";
261 // search well-known functions
265 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
266 // assume 'extractDelims' ran before
267 void extractFunctions(MathArray & ar)
269 // we need at least two items...
273 lyxerr << "\nFunctions from: " << ar << "\n";
274 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
275 MathArray::iterator it = ar.begin() + i;
277 // is this a function name?
278 MathFuncInset * func = (*it)->asFuncInset();
282 // do we have an exponent?
283 // simply skippping the postion does the right thing:
284 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
285 MathArray::iterator jt = it + 1;
286 if (MathScriptInset * script = (*jt)->asScriptInset()) {
287 // allow superscripts only
288 if (script->hasDown())
295 // create a proper inset as replacement
296 MathExFuncInset * p = new MathExFuncInset(func->name());
298 // jt points to the "argument". Get hold of this.
299 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
301 // replace the function name by a real function inset
304 // remove the source of the argument from the array
306 lyxerr << "\nFunctions to: " << ar << "\n";
315 bool symbolTest(MathInset * p, string const & name)
317 return p->asSymbolInset() && p->asSymbolInset()->name() == name;
321 bool intSymbolTest(MathInset * p)
323 return symbolTest(p, "int");
327 bool intDiffTest(MathInset * p)
329 return stringTest(p, "d");
333 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
334 // assume 'extractDelims' ran before
335 void extractIntegrals(MathArray & ar)
337 // we need at least three items...
341 lyxerr << "\nIntegrals from: " << ar << "\n";
342 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
343 MathArray::iterator it = ar.begin() + i;
345 // is this a integral name?
346 if (!intSymbolTest(it->nucleus()))
350 MathArray::iterator jt =
351 endNestSearch(it, ar.end(), intSymbolTest, intDiffTest);
353 // something sensible found?
357 // create a proper inset as replacement
358 MathExIntInset * p = new MathExIntInset("int");
361 MathArray::iterator st = it + 1;
362 if ((*st)->asScriptInset()) {
364 p->cell(0) = MathArray(st + 1, jt);
366 p->cell(0) = MathArray(st, jt);
369 // use the atom behind the 'd' as differential
370 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
373 ar.erase(it + 1, tt);
376 lyxerr << "\nIntegrals to: " << ar << "\n";
384 bool sumSymbolTest(MathInset * p)
386 return p->asSymbolInset() && p->asSymbolInset()->name() == "sum";
390 bool equalSign(MathInset * p)
392 return stringTest(p, "=");
396 bool equalSign1(MathAtom const & at)
398 return equalSign(at.nucleus());
403 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
404 // assume 'extractDelims' ran before
405 void extractSums(MathArray & ar)
407 // we need at least two items...
411 lyxerr << "\nSums from: " << ar << "\n";
412 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
413 MathArray::iterator it = ar.begin() + i;
415 // is this a sum name?
416 if (!sumSymbolTest(it->nucleus()))
419 // create a proper inset as replacement
420 MathExIntInset * p = new MathExIntInset("sum");
423 MathArray::iterator st = it + 1;
424 if (st != ar.end() && (*st)->asScriptInset()) {
428 // try to figure out the summation index from the subscript
429 MathScriptInset * script = p->scripts()->asScriptInset();
430 if (script->hasDown()) {
431 MathArray & ar = script->down().data_;
432 MathArray::iterator it =
433 std::find_if(ar.begin(), ar.end(), &equalSign1);
434 if (it != ar.end()) {
435 // we found a '=', use everything in front of that as index,
436 // and everything behind as start value
437 p->cell(1) = MathArray(ar.begin(), it);
438 ar.erase(ar.begin(), it + 1);
440 // use everything as summation index, don't use scripts.
442 p->scripts().reset(0);
447 // use some behind the script as core
448 MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
451 ar.erase(it + 1, tt);
454 lyxerr << "\nSums to: " << ar << "\n";
459 // search differential stuff
462 // tests for 'd' or '\partial'
463 bool diffItemTest(MathInset * p)
465 return stringTest(p, "d");
469 bool diffItemTest(MathArray const & ar)
471 return ar.size() && diffItemTest(ar.front().nucleus());
475 bool diffFracTest(MathInset * p)
479 diffItemTest(p->asFracInset()->cell(0)) &&
480 diffItemTest(p->asFracInset()->cell(1));
483 void extractDiff(MathArray & ar)
485 lyxerr << "\nDiffs from: " << ar << "\n";
486 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
487 MathArray::iterator it = ar.begin() + i;
489 // is this a "differential fraction"?
490 if (!diffFracTest(it->nucleus()))
493 MathFracInset * f = (*it)->asFracInset();
495 lyxerr << "should not happen\n";
499 // create a proper diff inset
500 MathDiffInset * p = new MathDiffInset;
503 MathArray::iterator jt = it + 1;
504 if (f->cell(0).size() > 1)
505 p->cell(0) = MathArray(f->cell(0).begin() + 1, f->cell(0).end());
507 jt = extractArgument(p->cell(0), jt, ar.end());
509 // collect denominator
513 ar.erase(it + 1, jt);
516 lyxerr << "\nDiffs to: " << ar << "\n";
523 void extractStructure(MathArray & ar)
527 extractFunctions(ar);
528 extractIntegrals(ar);
535 void write(MathArray const & dat, WriteStream & wi)
539 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
540 wi.firstitem = (it == ar.begin());
541 MathInset const * p = it->nucleus();
542 if (it + 1 != ar.end()) {
543 if (MathScriptInset const * q = asScript(it)) {
554 void normalize(MathArray const & ar, NormalStream & os)
556 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
557 (*it)->normalize(os);
561 void octavize(MathArray const & dat, OctaveStream & os)
564 extractStructure(ar);
565 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
566 MathInset const * p = it->nucleus();
567 if (it + 1 != ar.end()) {
568 if (MathScriptInset const * q = asScript(it)) {
579 void maplize(MathArray const & dat, MapleStream & os)
582 extractStructure(ar);
583 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
584 MathInset const * p = it->nucleus();
585 if (it + 1 != ar.end()) {
586 if (MathScriptInset const * q = asScript(it)) {
597 void mathmlize(MathArray const & dat, MathMLStream & os)
600 extractStructure(ar);
603 else if (ar.size() == 1)
604 os << ar.begin()->nucleus();
607 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
608 MathInset const * p = it->nucleus();
609 if (it + 1 != ar.end()) {
610 if (MathScriptInset const * q = asScript(it)) {