1 // This file contains most of the magic that extracts "context
2 // information" from the unstructered layout-oriented stuff in an
7 #include "math_charinset.h"
8 #include "math_deliminset.h"
9 #include "math_diffinset.h"
10 #include "math_exfuncinset.h"
11 #include "math_exintinset.h"
12 #include "math_funcinset.h"
13 #include "math_fracinset.h"
14 #include "math_matrixinset.h"
15 #include "math_mathmlstream.h"
16 #include "math_scriptinset.h"
17 #include "math_stringinset.h"
18 #include "math_symbolinset.h"
24 using std::istringstream;
28 ostream & operator<<(ostream & os, MathArray const & ar)
36 // define a function for tests
37 typedef bool TestItemFunc(MathInset *);
39 // define a function for replacing subexpressions
40 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
44 // try to extract a super/subscript
45 // modify iterator position to point behind the thing
46 bool extractScript(MathArray & ar,
47 MathArray::iterator & pos, MathArray::iterator last)
49 // nothing to get here
53 // is this a scriptinset?
54 if (!(*pos)->asScriptInset())
57 // it is a scriptinset, use it.
64 // try to extract an "argument" to some function.
65 // returns position behind the argument
66 MathArray::iterator extractArgument(MathArray & ar,
67 MathArray::iterator pos, MathArray::iterator last, string const & = "")
69 // nothing to get here
73 // something deliminited _is_ an argument
74 if ((*pos)->asDelimInset()) {
79 // always take the first thing, no matter what it is
82 // go ahead if possible
87 // if the next item is a subscript, it most certainly belongs to the
89 extractScript(ar, pos, last);
93 // but it might be more than that.
94 // FIXME: not implemented
95 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
96 // // always take the first thing, no matter
106 MathScriptInset const * asScript(MathArray::const_iterator it)
108 if (it->nucleus()->asScriptInset())
113 return it->nucleus()->asScriptInset();
118 // returns sequence of char with same code starting at it up to end
119 // it might be less, though...
120 MathArray::const_iterator charSequence(MathArray::const_iterator it,
121 MathArray::const_iterator end, string & s, MathTextCodes & c)
123 MathCharInset const * p = (*it)->asCharInset();
125 for (; it != end; ++it) {
126 p = (*it)->asCharInset();
127 if (!p || p->code() != c)
135 void extractStrings(MathArray & ar)
137 //lyxerr << "\nStrings from: " << ar << "\n";
138 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
139 MathArray::iterator it = ar.begin() + i;
140 if (!(*it)->asCharInset())
143 // create proper string inset
144 MathStringInset * p = new MathStringInset;
145 MathArray::const_iterator
146 jt = charSequence(it, ar.end(), p->str_, p->code_);
150 ar.erase(i + 1, jt - ar.begin());
152 //lyxerr << "\nStrings to: " << ar << "\n";
156 MathInset * singleItem(MathArray & ar)
158 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
162 void extractMatrices(MathArray & ar)
164 //lyxerr << "\nMatrices from: " << ar << "\n";
165 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
166 MathDelimInset * del = (*it)->asDelimInset();
169 MathInset * arr = singleItem(del->cell(0));
170 if (!arr || !arr->asArrayInset())
172 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
174 //lyxerr << "\nMatrices to: " << ar << "\n";
178 // convert this inset somehow to a string
179 bool extractString(MathInset * p, string & str)
184 str = string(1, p->getChar());
187 if (p->asStringInset()) {
188 str = p->asStringInset()->str();
195 // convert this inset somehow to a number
196 bool extractNumber(MathArray const & ar, int & i)
200 charSequence(ar.begin(), ar.end(), s, c);
201 istringstream is(s.c_str());
207 bool testString(MathInset * p, const string & str)
210 return extractString(p, s) && str == s;
214 // search end of nested sequence
215 MathArray::iterator endNestSearch(
216 MathArray::iterator it,
217 MathArray::iterator last,
218 TestItemFunc testOpen,
219 TestItemFunc testClose
222 for (int level = 0; it != last; ++it) {
223 if (testOpen(it->nucleus()))
225 if (testClose(it->nucleus()))
234 // replace nested sequences by a real Insets
237 TestItemFunc testOpen,
238 TestItemFunc testClose,
239 ReplaceArgumentFunc replaceArg
242 // use indices rather than iterators for the loop because we are going
243 // to modify the array.
244 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
245 // check whether this is the begin of the sequence
246 MathArray::iterator it = ar.begin() + i;
247 if (!testOpen(it->nucleus()))
250 // search end of sequence
251 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
255 // create a proper inset as replacement
256 MathInset * p = replaceArg(MathArray(it + 1, jt));
258 // replace the original stuff by the new inset
259 ar.erase(it + 1, jt + 1);
267 // split scripts into seperate super- and subscript insets. sub goes in
271 void splitScripts(MathArray & ar)
273 //lyxerr << "\nScripts from: " << ar << "\n";
274 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
275 MathArray::iterator it = ar.begin() + i;
277 // is this script inset?
278 MathScriptInset * p = (*it)->asScriptInset();
282 // no problem if we don't have both...
283 if (!p->hasUp() || !p->hasDown())
286 // create extra script inset and move superscript over
287 MathScriptInset * q = new MathScriptInset;
289 q->up().data_.swap(p->up().data_);
290 p->removeScript(true);
292 // insert new inset behind
294 ar.insert(i, MathAtom(q));
296 //lyxerr << "\nScripts to: " << ar << "\n";
304 void extractExps(MathArray & ar)
306 //lyxerr << "\nExps from: " << ar << "\n";
308 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
309 MathArray::iterator it = ar.begin() + i;
312 MathCharInset const * p = (*it)->asCharInset();
313 if (!p || p->getChar() != 'e')
316 // we need an exponent but no subscript
317 MathScriptInset * sup = (*(it + 1))->asScriptInset();
318 if (!sup || sup->hasDown())
321 // create a proper exp-inset as replacement
322 MathExFuncInset * func = new MathExFuncInset("exp");
323 func->cell(0) = sup->cell(1);
329 //lyxerr << "\nExps to: " << ar << "\n";
334 // search deliminiters
337 bool testOpenParan(MathInset * p)
339 return testString(p, "(");
343 bool testCloseParan(MathInset * p)
345 return testString(p, ")");
349 MathInset * replaceDelims(const MathArray & ar)
351 MathDelimInset * del = new MathDelimInset("(", ")");
357 // replace '('...')' sequences by a real MathDelimInset
358 void extractDelims(MathArray & ar)
360 //lyxerr << "\nDelims from: " << ar << "\n";
361 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
362 //lyxerr << "\nDelims to: " << ar << "\n";
368 // search well-known functions
372 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
373 // assume 'extractDelims' ran before
374 void extractFunctions(MathArray & ar)
376 // we need at least two items...
380 //lyxerr << "\nFunctions from: " << ar << "\n";
381 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
382 MathArray::iterator it = ar.begin() + i;
383 MathArray::iterator jt = it + 1;
387 if ((*it)->asFuncInset()) {
388 // it certainly is if it is well known...
389 name = (*it)->asFuncInset()->name();
391 // is this a user defined function?
392 // it it probably not, if it doesn't have a name.
393 if (!extractString((*it).nucleus(), name))
395 // it is not if it has no argument
398 // guess so, if this is followed by
399 // a DelimInset with a single item in the cell
400 MathDelimInset * del = (*jt)->asDelimInset();
401 if (!del || del->cell(0).size() != 1)
403 // fall trough into main branch
406 // do we have an exponent like in
407 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
409 extractScript(exp, jt, ar.end());
411 // create a proper inset as replacement
412 MathExFuncInset * p = new MathExFuncInset(name);
414 // jt points to the "argument". Get hold of this.
415 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
417 // replace the function name by a real function inset
420 // remove the source of the argument from the array
421 ar.erase(it + 1, st);
423 // re-insert exponent
424 ar.insert(i + 1, exp);
425 //lyxerr << "\nFunctions to: " << ar << "\n";
434 bool testSymbol(MathInset * p, string const & name)
436 return p->asSymbolInset() && p->asSymbolInset()->name() == name;
440 bool testIntSymbol(MathInset * p)
442 return testSymbol(p, "int");
446 bool testIntDiff(MathInset * p)
448 return testString(p, "d");
452 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
453 // assume 'extractDelims' ran before
454 void extractIntegrals(MathArray & ar)
456 // we need at least three items...
460 //lyxerr << "\nIntegrals from: " << ar << "\n";
461 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
462 MathArray::iterator it = ar.begin() + i;
464 // is this a integral name?
465 if (!testIntSymbol(it->nucleus()))
469 MathArray::iterator jt =
470 endNestSearch(it, ar.end(), testIntSymbol, testIntDiff);
472 // something sensible found?
476 // create a proper inset as replacement
477 MathExIntInset * p = new MathExIntInset("int");
479 // collect subscript if any
480 MathArray::iterator st = it + 1;
482 if (MathScriptInset * sub = (*st)->asScriptInset())
483 if (sub->hasDown()) {
484 p->cell(2) = sub->down().data_;
488 // collect superscript if any
490 if (MathScriptInset * sup = (*st)->asScriptInset())
492 p->cell(3) = sup->up().data_;
496 // core ist part from behind the scripts to the 'd'
497 p->cell(0) = MathArray(st, jt);
499 // use the "thing" behind the 'd' as differential
500 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
503 ar.erase(it + 1, tt);
506 //lyxerr << "\nIntegrals to: " << ar << "\n";
514 bool testSumSymbol(MathInset * p)
516 return testSymbol(p, "sum");
520 bool testEqualSign(MathAtom const & at)
522 return testString(at.nucleus(), "=");
527 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
528 // assume 'extractDelims' ran before
529 void extractSums(MathArray & ar)
531 // we need at least two items...
535 //lyxerr << "\nSums from: " << ar << "\n";
536 for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) {
537 MathArray::iterator it = ar.begin() + i;
539 // is this a sum name?
540 if (!testSumSymbol(it->nucleus()))
543 // create a proper inset as replacement
544 MathExIntInset * p = new MathExIntInset("sum");
546 // collect lower bound and summation index
547 MathArray::iterator st = it + 1;
549 if (MathScriptInset * sub = (*st)->asScriptInset())
550 if (sub->hasDown()) {
551 // try to figure out the summation index from the subscript
552 MathArray & ar = sub->down().data_;
553 MathArray::iterator it =
554 find_if(ar.begin(), ar.end(), &testEqualSign);
555 if (it != ar.end()) {
556 // we found a '=', use everything in front of that as index,
557 // and everything behind as lower index
558 p->cell(1) = MathArray(ar.begin(), it);
559 p->cell(2) = MathArray(it + 1, ar.end());
561 // use everything as summation index, don't use scripts.
567 // collect upper bound
569 if (MathScriptInset * sup = (*st)->asScriptInset())
571 p->cell(3) = sup->up().data_;
575 // use some behind the script as core
576 MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
579 ar.erase(it + 1, tt);
582 //lyxerr << "\nSums to: " << ar << "\n";
587 // search differential stuff
590 // tests for 'd' or '\partial'
591 bool testDiffItem(MathAtom const & at)
593 return testString(at.nucleus(), "d");
597 bool testDiffArray(MathArray const & ar)
599 return ar.size() && testDiffItem(ar.front());
603 bool testDiffFrac(MathInset * p)
605 MathFracInset * f = p->asFracInset();
606 return f && testDiffArray(f->cell(0)) && testDiffArray(f->cell(1));
610 // is this something like ^number?
611 bool extractDiffExponent(MathArray::iterator it, int & i)
613 if (!(*it)->asScriptInset())
617 if (!extractString((*it).nucleus(), s))
619 istringstream is(s.c_str());
625 void extractDiff(MathArray & ar)
627 //lyxerr << "\nDiffs from: " << ar << "\n";
628 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
629 MathArray::iterator it = ar.begin() + i;
631 // is this a "differential fraction"?
632 if (!testDiffFrac(it->nucleus()))
635 MathFracInset * f = (*it)->asFracInset();
637 lyxerr << "should not happen\n";
641 // create a proper diff inset
642 MathDiffInset * diff = new MathDiffInset;
644 // collect function, let jt point behind last used item
645 MathArray::iterator jt = it + 1;
647 MathArray & numer = f->cell(0);
648 if (numer.size() > 1 && numer.at(1)->asScriptInset()) {
649 // this is something like d^n f(x) / d... or d^n / d...
652 if (numer.size() > 2)
653 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
655 jt = extractArgument(diff->cell(0), jt, ar.end());
657 // simply d f(x) / d... or d/d...
658 if (numer.size() > 1)
659 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
661 jt = extractArgument(diff->cell(0), jt, ar.end());
664 // collect denominator parts
665 MathArray & denom = f->cell(1);
666 for (MathArray::iterator dt = denom.begin(); dt != denom.end();) {
668 MathArray::iterator et = find_if(dt + 1, denom.end(), &testDiffItem);
671 MathArray::iterator st = et - 1;
672 MathScriptInset * script = (*st)->asScriptInset();
673 if (script && script->hasUp()) {
674 // things like d.../dx^n
676 if (extractNumber(script->up().data_, mult)) {
677 //lyxerr << "mult: " << mult << endl;
678 for (int i = 0; i < mult; ++i)
679 diff->addDer(MathArray(dt + 1, st));
683 diff->addDer(MathArray(dt + 1, et));
689 ar.erase(it + 1, jt);
692 //lyxerr << "\nDiffs to: " << ar << "\n";
701 void extractStructure(MathArray & ar)
706 extractFunctions(ar);
707 extractIntegrals(ar);
715 void write(MathArray const & dat, WriteStream & wi)
719 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
720 wi.firstitem() = (it == ar.begin());
721 MathInset const * p = it->nucleus();
722 if (it + 1 != ar.end()) {
723 if (MathScriptInset const * q = asScript(it)) {
734 void normalize(MathArray const & ar, NormalStream & os)
736 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
737 (*it)->normalize(os);
741 void octavize(MathArray const & dat, OctaveStream & os)
744 extractStructure(ar);
745 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
746 MathInset const * p = it->nucleus();
747 if (it + 1 != ar.end()) {
748 if (MathScriptInset const * q = asScript(it)) {
759 void maplize(MathArray const & dat, MapleStream & os)
762 extractStructure(ar);
763 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
764 MathInset const * p = it->nucleus();
765 if (it + 1 != ar.end()) {
766 if (MathScriptInset const * q = asScript(it)) {
777 void mathmlize(MathArray const & dat, MathMLStream & os)
780 extractStructure(ar);
783 else if (ar.size() == 1)
784 os << ar.begin()->nucleus();
787 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
788 MathInset const * p = it->nucleus();
789 if (it + 1 != ar.end()) {
790 if (MathScriptInset const * q = asScript(it)) {
791 q->mathmlize2(p, os);