1 // This file contains most of the magic that extracts "context
2 // information" from the unstructered layout-oriented stuff in an
7 #include "math_amsarrayinset.h"
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_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"
19 #include "math_unknowninset.h"
25 using std::istringstream;
29 ostream & operator<<(ostream & os, MathArray const & ar)
37 // define a function for tests
38 typedef bool TestItemFunc(MathInset *);
40 // define a function for replacing subexpressions
41 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
45 // try to extract a super/subscript
46 // modify iterator position to point behind the thing
47 bool extractScript(MathArray & ar,
48 MathArray::iterator & pos, MathArray::iterator last)
50 // nothing to get here
54 // is this a scriptinset?
55 if (!(*pos)->asScriptInset())
58 // it is a scriptinset, use it.
65 // try to extract an "argument" to some function.
66 // returns position behind the argument
67 MathArray::iterator extractArgument(MathArray & ar,
68 MathArray::iterator pos, MathArray::iterator last, string const & = "")
70 // nothing to get here
74 // something deliminited _is_ an argument
75 if ((*pos)->asDelimInset()) {
80 // always take the first thing, no matter what it is
83 // go ahead if possible
88 // if the next item is a subscript, it most certainly belongs to the
90 extractScript(ar, pos, last);
94 // but it might be more than that.
95 // FIXME: not implemented
96 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
97 // // always take the first thing, no matter
107 MathScriptInset const * asScript(MathArray::const_iterator it)
111 if (it->nucleus()->asScriptInset())
116 return it->nucleus()->asScriptInset();
121 // returns sequence of char with same code starting at it up to end
122 // it might be less, though...
123 MathArray::const_iterator charSequence(MathArray::const_iterator it,
124 MathArray::const_iterator end, string & s)
126 for (; it != end && (*it)->asCharInset(); ++it)
127 s += (*it)->getChar();
132 void extractStrings(MathArray & ar)
134 //lyxerr << "\nStrings from: " << ar << "\n";
135 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
136 MathArray::iterator it = ar.begin() + i;
137 if (!(*it)->asCharInset())
140 // create proper string inset
141 MathStringInset * p = new MathStringInset;
142 MathArray::const_iterator
143 jt = charSequence(it, ar.end(), p->str_);
147 ar.erase(i + 1, jt - ar.begin());
149 //lyxerr << "\nStrings to: " << ar << "\n";
153 MathInset * singleItem(MathArray & ar)
155 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
159 void extractMatrices(MathArray & ar)
161 //lyxerr << "\nMatrices from: " << ar << "\n";
162 // first pass for explicitly delimited stuff
163 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
164 MathDelimInset * del = (*it)->asDelimInset();
167 MathInset * arr = singleItem(del->cell(0));
168 if (!arr || !arr->asGridInset())
170 *it = MathAtom(new MathMatrixInset(*(arr->asGridInset())));
173 // second pass for AMS "pmatrix" etc
174 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
175 MathAMSArrayInset * ams = (*it)->asAMSArrayInset();
178 *it = MathAtom(new MathMatrixInset(*ams));
180 //lyxerr << "\nMatrices to: " << ar << "\n";
184 // convert this inset somehow to a string
185 bool extractString(MathInset * p, string & str)
190 str = string(1, p->getChar());
193 if (p->asStringInset()) {
194 str = p->asStringInset()->str();
201 // convert this inset somehow to a number
202 bool extractNumber(MathArray const & ar, int & i)
205 charSequence(ar.begin(), ar.end(), s);
206 istringstream is(s.c_str());
212 bool extractNumber(MathArray const & ar, double & i)
215 charSequence(ar.begin(), ar.end(), s);
216 istringstream is(s.c_str());
222 bool testString(MathInset * p, const string & str)
225 return extractString(p, s) && str == s;
229 // search end of nested sequence
230 MathArray::iterator endNestSearch(
231 MathArray::iterator it,
232 MathArray::iterator last,
233 TestItemFunc testOpen,
234 TestItemFunc testClose
237 for (int level = 0; it != last; ++it) {
238 if (testOpen(it->nucleus()))
240 if (testClose(it->nucleus()))
249 // replace nested sequences by a real Insets
252 TestItemFunc testOpen,
253 TestItemFunc testClose,
254 ReplaceArgumentFunc replaceArg
257 // use indices rather than iterators for the loop because we are going
258 // to modify the array.
259 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
260 // check whether this is the begin of the sequence
261 MathArray::iterator it = ar.begin() + i;
262 if (!testOpen(it->nucleus()))
265 // search end of sequence
266 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
270 // create a proper inset as replacement
271 MathInset * p = replaceArg(MathArray(it + 1, jt));
273 // replace the original stuff by the new inset
274 ar.erase(it + 1, jt + 1);
282 // split scripts into seperate super- and subscript insets. sub goes in
286 void splitScripts(MathArray & ar)
288 //lyxerr << "\nScripts from: " << ar << "\n";
289 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
290 MathArray::iterator it = ar.begin() + i;
292 // is this script inset?
293 MathScriptInset * p = (*it)->asScriptInset();
297 // no problem if we don't have both...
298 if (!p->hasUp() || !p->hasDown())
301 // create extra script inset and move superscript over
302 MathScriptInset * q = new MathScriptInset;
304 q->up().data_.swap(p->up().data_);
305 p->removeScript(true);
307 // insert new inset behind
309 ar.insert(i, MathAtom(q));
311 //lyxerr << "\nScripts to: " << ar << "\n";
319 void extractExps(MathArray & ar)
321 //lyxerr << "\nExps from: " << ar << "\n";
323 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
324 MathArray::iterator it = ar.begin() + i;
327 MathCharInset const * p = (*it)->asCharInset();
328 if (!p || p->getChar() != 'e')
331 // we need an exponent but no subscript
332 MathScriptInset * sup = (*(it + 1))->asScriptInset();
333 if (!sup || sup->hasDown())
336 // create a proper exp-inset as replacement
337 MathExFuncInset * func = new MathExFuncInset("exp");
338 func->cell(0) = sup->cell(1);
344 //lyxerr << "\nExps to: " << ar << "\n";
349 // search deliminiters
352 bool testOpenParan(MathInset * p)
354 return testString(p, "(");
358 bool testCloseParan(MathInset * p)
360 return testString(p, ")");
364 MathInset * replaceDelims(const MathArray & ar)
366 MathDelimInset * del = new MathDelimInset("(", ")");
372 // replace '('...')' sequences by a real MathDelimInset
373 void extractDelims(MathArray & ar)
375 //lyxerr << "\nDelims from: " << ar << "\n";
376 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
377 //lyxerr << "\nDelims to: " << ar << "\n";
383 // search well-known functions
387 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
388 // assume 'extractDelims' ran before
389 void extractFunctions(MathArray & ar)
391 // we need at least two items...
395 //lyxerr << "\nFunctions from: " << ar << "\n";
396 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
397 MathArray::iterator it = ar.begin() + i;
398 MathArray::iterator jt = it + 1;
402 if ((*it)->asUnknownInset()) {
403 // it certainly is if it is well known...
404 name = (*it)->asUnknownInset()->name();
406 // is this a user defined function?
407 // it it probably not, if it doesn't have a name.
408 if (!extractString((*it).nucleus(), name))
410 // it is not if it has no argument
413 // guess so, if this is followed by
414 // a DelimInset with a single item in the cell
415 MathDelimInset * del = (*jt)->asDelimInset();
416 if (!del || del->cell(0).size() != 1)
418 // fall trough into main branch
421 // do we have an exponent like in
422 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
424 extractScript(exp, jt, ar.end());
426 // create a proper inset as replacement
427 MathExFuncInset * p = new MathExFuncInset(name);
429 // jt points to the "argument". Get hold of this.
430 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
432 // replace the function name by a real function inset
435 // remove the source of the argument from the array
436 ar.erase(it + 1, st);
438 // re-insert exponent
439 ar.insert(i + 1, exp);
440 //lyxerr << "\nFunctions to: " << ar << "\n";
449 bool testSymbol(MathInset * p, string const & name)
451 return p->asSymbolInset() && p->asSymbolInset()->name() == name;
455 bool testIntSymbol(MathInset * p)
457 return testSymbol(p, "int");
461 bool testIntDiff(MathInset * p)
463 return testString(p, "d");
467 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
468 // assume 'extractDelims' ran before
469 void extractIntegrals(MathArray & ar)
471 // we need at least three items...
475 //lyxerr << "\nIntegrals from: " << ar << "\n";
476 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
477 MathArray::iterator it = ar.begin() + i;
479 // is this a integral name?
480 if (!testIntSymbol(it->nucleus()))
484 MathArray::iterator jt =
485 endNestSearch(it, ar.end(), testIntSymbol, testIntDiff);
487 // something sensible found?
491 // create a proper inset as replacement
492 MathExIntInset * p = new MathExIntInset("int");
494 // collect subscript if any
495 MathArray::iterator st = it + 1;
497 if (MathScriptInset * sub = (*st)->asScriptInset())
498 if (sub->hasDown()) {
499 p->cell(2) = sub->down().data_;
503 // collect superscript if any
505 if (MathScriptInset * sup = (*st)->asScriptInset())
507 p->cell(3) = sup->up().data_;
511 // core ist part from behind the scripts to the 'd'
512 p->cell(0) = MathArray(st, jt);
514 // use the "thing" behind the 'd' as differential
515 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
518 ar.erase(it + 1, tt);
521 //lyxerr << "\nIntegrals to: " << ar << "\n";
529 bool testSumSymbol(MathInset * p)
531 return testSymbol(p, "sum");
535 bool testEqualSign(MathAtom const & at)
537 return testString(at.nucleus(), "=");
542 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
543 // assume 'extractDelims' ran before
544 void extractSums(MathArray & ar)
546 // we need at least two items...
550 //lyxerr << "\nSums from: " << ar << "\n";
551 for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) {
552 MathArray::iterator it = ar.begin() + i;
554 // is this a sum name?
555 if (!testSumSymbol(it->nucleus()))
558 // create a proper inset as replacement
559 MathExIntInset * p = new MathExIntInset("sum");
561 // collect lower bound and summation index
562 MathArray::iterator st = it + 1;
564 if (MathScriptInset * sub = (*st)->asScriptInset())
565 if (sub->hasDown()) {
566 // try to figure out the summation index from the subscript
567 MathArray & ar = sub->down().data_;
568 MathArray::iterator it =
569 find_if(ar.begin(), ar.end(), &testEqualSign);
570 if (it != ar.end()) {
571 // we found a '=', use everything in front of that as index,
572 // and everything behind as lower index
573 p->cell(1) = MathArray(ar.begin(), it);
574 p->cell(2) = MathArray(it + 1, ar.end());
576 // use everything as summation index, don't use scripts.
582 // collect upper bound
584 if (MathScriptInset * sup = (*st)->asScriptInset())
586 p->cell(3) = sup->up().data_;
590 // use some behind the script as core
591 MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
594 ar.erase(it + 1, tt);
597 //lyxerr << "\nSums to: " << ar << "\n";
602 // search differential stuff
605 // tests for 'd' or '\partial'
606 bool testDiffItem(MathAtom const & at)
608 return testString(at.nucleus(), "d");
612 bool testDiffArray(MathArray const & ar)
614 return ar.size() && testDiffItem(ar.front());
618 bool testDiffFrac(MathInset * p)
620 MathFracInset * f = p->asFracInset();
621 return f && testDiffArray(f->cell(0)) && testDiffArray(f->cell(1));
625 // is this something like ^number?
626 bool extractDiffExponent(MathArray::iterator it, int & i)
628 if (!(*it)->asScriptInset())
632 if (!extractString((*it).nucleus(), s))
634 istringstream is(s.c_str());
640 void extractDiff(MathArray & ar)
642 //lyxerr << "\nDiffs from: " << ar << "\n";
643 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
644 MathArray::iterator it = ar.begin() + i;
646 // is this a "differential fraction"?
647 if (!testDiffFrac(it->nucleus()))
650 MathFracInset * f = (*it)->asFracInset();
652 lyxerr << "should not happen\n";
656 // create a proper diff inset
657 MathDiffInset * diff = new MathDiffInset;
659 // collect function, let jt point behind last used item
660 MathArray::iterator jt = it + 1;
662 MathArray & numer = f->cell(0);
663 if (numer.size() > 1 && numer.at(1)->asScriptInset()) {
664 // this is something like d^n f(x) / d... or d^n / d...
667 if (numer.size() > 2)
668 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
670 jt = extractArgument(diff->cell(0), jt, ar.end());
672 // simply d f(x) / d... or d/d...
673 if (numer.size() > 1)
674 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
676 jt = extractArgument(diff->cell(0), jt, ar.end());
679 // collect denominator parts
680 MathArray & denom = f->cell(1);
681 for (MathArray::iterator dt = denom.begin(); dt != denom.end();) {
683 MathArray::iterator et = find_if(dt + 1, denom.end(), &testDiffItem);
686 MathArray::iterator st = et - 1;
687 MathScriptInset * script = (*st)->asScriptInset();
688 if (script && script->hasUp()) {
689 // things like d.../dx^n
691 if (extractNumber(script->up().data_, mult)) {
692 //lyxerr << "mult: " << mult << endl;
693 for (int i = 0; i < mult; ++i)
694 diff->addDer(MathArray(dt + 1, st));
698 diff->addDer(MathArray(dt + 1, et));
704 ar.erase(it + 1, jt);
707 //lyxerr << "\nDiffs to: " << ar << "\n";
716 void extractStructure(MathArray & ar)
721 extractFunctions(ar);
722 extractIntegrals(ar);
730 void write(MathArray const & dat, WriteStream & wi)
734 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
735 wi.firstitem() = (it == ar.begin());
736 MathInset const * p = it->nucleus();
737 if (it + 1 != ar.end()) {
738 if (MathScriptInset const * q = asScript(it)) {
749 void normalize(MathArray const & ar, NormalStream & os)
751 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
752 (*it)->normalize(os);
756 void octavize(MathArray const & dat, OctaveStream & os)
759 extractStructure(ar);
760 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
761 MathInset const * p = it->nucleus();
762 if (it + 1 != ar.end()) {
763 if (MathScriptInset const * q = asScript(it)) {
774 void maplize(MathArray const & dat, MapleStream & os)
777 extractStructure(ar);
778 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
779 MathInset const * p = it->nucleus();
780 if (it + 1 != ar.end()) {
781 if (MathScriptInset const * q = asScript(it)) {
792 void mathmlize(MathArray const & dat, MathMLStream & os)
795 extractStructure(ar);
798 else if (ar.size() == 1)
799 os << ar.begin()->nucleus();
802 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
803 MathInset const * p = it->nucleus();
804 if (it + 1 != ar.end()) {
805 if (MathScriptInset const * q = asScript(it)) {
806 q->mathmlize2(p, os);