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)
110 if (it->nucleus()->asScriptInset())
115 return it->nucleus()->asScriptInset();
120 // returns sequence of char with same code starting at it up to end
121 // it might be less, though...
122 MathArray::const_iterator charSequence(MathArray::const_iterator it,
123 MathArray::const_iterator end, string & s, MathTextCodes & c)
125 MathCharInset const * p = (*it)->asCharInset();
127 for (; it != end; ++it) {
128 p = (*it)->asCharInset();
129 if (!p || p->code() != c)
137 void extractStrings(MathArray & ar)
139 //lyxerr << "\nStrings from: " << ar << "\n";
140 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
141 MathArray::iterator it = ar.begin() + i;
142 if (!(*it)->asCharInset())
145 // create proper string inset
146 MathStringInset * p = new MathStringInset;
147 MathArray::const_iterator
148 jt = charSequence(it, ar.end(), p->str_, p->code_);
152 ar.erase(i + 1, jt - ar.begin());
154 //lyxerr << "\nStrings to: " << ar << "\n";
158 MathInset * singleItem(MathArray & ar)
160 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
164 void extractMatrices(MathArray & ar)
166 //lyxerr << "\nMatrices from: " << ar << "\n";
167 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
168 MathDelimInset * del = (*it)->asDelimInset();
171 MathInset * arr = singleItem(del->cell(0));
172 if (!arr || !arr->asArrayInset())
174 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
176 //lyxerr << "\nMatrices to: " << ar << "\n";
180 // convert this inset somehow to a string
181 bool extractString(MathInset * p, string & str)
186 str = string(1, p->getChar());
189 if (p->asStringInset()) {
190 str = p->asStringInset()->str();
197 // convert this inset somehow to a number
198 bool extractNumber(MathArray const & ar, int & i)
202 charSequence(ar.begin(), ar.end(), s, c);
203 istringstream is(s.c_str());
209 bool extractNumber(MathArray const & ar, double & i)
213 charSequence(ar.begin(), ar.end(), s, c);
214 istringstream is(s.c_str());
220 bool testString(MathInset * p, const string & str)
223 return extractString(p, s) && str == s;
227 // search end of nested sequence
228 MathArray::iterator endNestSearch(
229 MathArray::iterator it,
230 MathArray::iterator last,
231 TestItemFunc testOpen,
232 TestItemFunc testClose
235 for (int level = 0; it != last; ++it) {
236 if (testOpen(it->nucleus()))
238 if (testClose(it->nucleus()))
247 // replace nested sequences by a real Insets
250 TestItemFunc testOpen,
251 TestItemFunc testClose,
252 ReplaceArgumentFunc replaceArg
255 // use indices rather than iterators for the loop because we are going
256 // to modify the array.
257 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
258 // check whether this is the begin of the sequence
259 MathArray::iterator it = ar.begin() + i;
260 if (!testOpen(it->nucleus()))
263 // search end of sequence
264 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
268 // create a proper inset as replacement
269 MathInset * p = replaceArg(MathArray(it + 1, jt));
271 // replace the original stuff by the new inset
272 ar.erase(it + 1, jt + 1);
280 // split scripts into seperate super- and subscript insets. sub goes in
284 void splitScripts(MathArray & ar)
286 //lyxerr << "\nScripts from: " << ar << "\n";
287 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
288 MathArray::iterator it = ar.begin() + i;
290 // is this script inset?
291 MathScriptInset * p = (*it)->asScriptInset();
295 // no problem if we don't have both...
296 if (!p->hasUp() || !p->hasDown())
299 // create extra script inset and move superscript over
300 MathScriptInset * q = new MathScriptInset;
302 q->up().data_.swap(p->up().data_);
303 p->removeScript(true);
305 // insert new inset behind
307 ar.insert(i, MathAtom(q));
309 //lyxerr << "\nScripts to: " << ar << "\n";
317 void extractExps(MathArray & ar)
319 //lyxerr << "\nExps from: " << ar << "\n";
321 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
322 MathArray::iterator it = ar.begin() + i;
325 MathCharInset const * p = (*it)->asCharInset();
326 if (!p || p->getChar() != 'e')
329 // we need an exponent but no subscript
330 MathScriptInset * sup = (*(it + 1))->asScriptInset();
331 if (!sup || sup->hasDown())
334 // create a proper exp-inset as replacement
335 MathExFuncInset * func = new MathExFuncInset("exp");
336 func->cell(0) = sup->cell(1);
342 //lyxerr << "\nExps to: " << ar << "\n";
347 // search deliminiters
350 bool testOpenParan(MathInset * p)
352 return testString(p, "(");
356 bool testCloseParan(MathInset * p)
358 return testString(p, ")");
362 MathInset * replaceDelims(const MathArray & ar)
364 MathDelimInset * del = new MathDelimInset("(", ")");
370 // replace '('...')' sequences by a real MathDelimInset
371 void extractDelims(MathArray & ar)
373 //lyxerr << "\nDelims from: " << ar << "\n";
374 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
375 //lyxerr << "\nDelims to: " << ar << "\n";
381 // search well-known functions
385 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
386 // assume 'extractDelims' ran before
387 void extractFunctions(MathArray & ar)
389 // we need at least two items...
393 //lyxerr << "\nFunctions from: " << ar << "\n";
394 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
395 MathArray::iterator it = ar.begin() + i;
396 MathArray::iterator jt = it + 1;
400 if ((*it)->asFuncInset()) {
401 // it certainly is if it is well known...
402 name = (*it)->asFuncInset()->name();
404 // is this a user defined function?
405 // it it probably not, if it doesn't have a name.
406 if (!extractString((*it).nucleus(), name))
408 // it is not if it has no argument
411 // guess so, if this is followed by
412 // a DelimInset with a single item in the cell
413 MathDelimInset * del = (*jt)->asDelimInset();
414 if (!del || del->cell(0).size() != 1)
416 // fall trough into main branch
419 // do we have an exponent like in
420 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
422 extractScript(exp, jt, ar.end());
424 // create a proper inset as replacement
425 MathExFuncInset * p = new MathExFuncInset(name);
427 // jt points to the "argument". Get hold of this.
428 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
430 // replace the function name by a real function inset
433 // remove the source of the argument from the array
434 ar.erase(it + 1, st);
436 // re-insert exponent
437 ar.insert(i + 1, exp);
438 //lyxerr << "\nFunctions to: " << ar << "\n";
447 bool testSymbol(MathInset * p, string const & name)
449 return p->asSymbolInset() && p->asSymbolInset()->name() == name;
453 bool testIntSymbol(MathInset * p)
455 return testSymbol(p, "int");
459 bool testIntDiff(MathInset * p)
461 return testString(p, "d");
465 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
466 // assume 'extractDelims' ran before
467 void extractIntegrals(MathArray & ar)
469 // we need at least three items...
473 //lyxerr << "\nIntegrals from: " << ar << "\n";
474 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
475 MathArray::iterator it = ar.begin() + i;
477 // is this a integral name?
478 if (!testIntSymbol(it->nucleus()))
482 MathArray::iterator jt =
483 endNestSearch(it, ar.end(), testIntSymbol, testIntDiff);
485 // something sensible found?
489 // create a proper inset as replacement
490 MathExIntInset * p = new MathExIntInset("int");
492 // collect subscript if any
493 MathArray::iterator st = it + 1;
495 if (MathScriptInset * sub = (*st)->asScriptInset())
496 if (sub->hasDown()) {
497 p->cell(2) = sub->down().data_;
501 // collect superscript if any
503 if (MathScriptInset * sup = (*st)->asScriptInset())
505 p->cell(3) = sup->up().data_;
509 // core ist part from behind the scripts to the 'd'
510 p->cell(0) = MathArray(st, jt);
512 // use the "thing" behind the 'd' as differential
513 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
516 ar.erase(it + 1, tt);
519 //lyxerr << "\nIntegrals to: " << ar << "\n";
527 bool testSumSymbol(MathInset * p)
529 return testSymbol(p, "sum");
533 bool testEqualSign(MathAtom const & at)
535 return testString(at.nucleus(), "=");
540 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
541 // assume 'extractDelims' ran before
542 void extractSums(MathArray & ar)
544 // we need at least two items...
548 //lyxerr << "\nSums from: " << ar << "\n";
549 for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) {
550 MathArray::iterator it = ar.begin() + i;
552 // is this a sum name?
553 if (!testSumSymbol(it->nucleus()))
556 // create a proper inset as replacement
557 MathExIntInset * p = new MathExIntInset("sum");
559 // collect lower bound and summation index
560 MathArray::iterator st = it + 1;
562 if (MathScriptInset * sub = (*st)->asScriptInset())
563 if (sub->hasDown()) {
564 // try to figure out the summation index from the subscript
565 MathArray & ar = sub->down().data_;
566 MathArray::iterator it =
567 find_if(ar.begin(), ar.end(), &testEqualSign);
568 if (it != ar.end()) {
569 // we found a '=', use everything in front of that as index,
570 // and everything behind as lower index
571 p->cell(1) = MathArray(ar.begin(), it);
572 p->cell(2) = MathArray(it + 1, ar.end());
574 // use everything as summation index, don't use scripts.
580 // collect upper bound
582 if (MathScriptInset * sup = (*st)->asScriptInset())
584 p->cell(3) = sup->up().data_;
588 // use some behind the script as core
589 MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
592 ar.erase(it + 1, tt);
595 //lyxerr << "\nSums to: " << ar << "\n";
600 // search differential stuff
603 // tests for 'd' or '\partial'
604 bool testDiffItem(MathAtom const & at)
606 return testString(at.nucleus(), "d");
610 bool testDiffArray(MathArray const & ar)
612 return ar.size() && testDiffItem(ar.front());
616 bool testDiffFrac(MathInset * p)
618 MathFracInset * f = p->asFracInset();
619 return f && testDiffArray(f->cell(0)) && testDiffArray(f->cell(1));
623 // is this something like ^number?
624 bool extractDiffExponent(MathArray::iterator it, int & i)
626 if (!(*it)->asScriptInset())
630 if (!extractString((*it).nucleus(), s))
632 istringstream is(s.c_str());
638 void extractDiff(MathArray & ar)
640 //lyxerr << "\nDiffs from: " << ar << "\n";
641 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
642 MathArray::iterator it = ar.begin() + i;
644 // is this a "differential fraction"?
645 if (!testDiffFrac(it->nucleus()))
648 MathFracInset * f = (*it)->asFracInset();
650 lyxerr << "should not happen\n";
654 // create a proper diff inset
655 MathDiffInset * diff = new MathDiffInset;
657 // collect function, let jt point behind last used item
658 MathArray::iterator jt = it + 1;
660 MathArray & numer = f->cell(0);
661 if (numer.size() > 1 && numer.at(1)->asScriptInset()) {
662 // this is something like d^n f(x) / d... or d^n / d...
665 if (numer.size() > 2)
666 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
668 jt = extractArgument(diff->cell(0), jt, ar.end());
670 // simply d f(x) / d... or d/d...
671 if (numer.size() > 1)
672 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
674 jt = extractArgument(diff->cell(0), jt, ar.end());
677 // collect denominator parts
678 MathArray & denom = f->cell(1);
679 for (MathArray::iterator dt = denom.begin(); dt != denom.end();) {
681 MathArray::iterator et = find_if(dt + 1, denom.end(), &testDiffItem);
684 MathArray::iterator st = et - 1;
685 MathScriptInset * script = (*st)->asScriptInset();
686 if (script && script->hasUp()) {
687 // things like d.../dx^n
689 if (extractNumber(script->up().data_, mult)) {
690 //lyxerr << "mult: " << mult << endl;
691 for (int i = 0; i < mult; ++i)
692 diff->addDer(MathArray(dt + 1, st));
696 diff->addDer(MathArray(dt + 1, et));
702 ar.erase(it + 1, jt);
705 //lyxerr << "\nDiffs to: " << ar << "\n";
714 void extractStructure(MathArray & ar)
719 extractFunctions(ar);
720 extractIntegrals(ar);
728 void write(MathArray const & dat, WriteStream & wi)
732 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
733 wi.firstitem() = (it == ar.begin());
734 MathInset const * p = it->nucleus();
735 if (it + 1 != ar.end()) {
736 if (MathScriptInset const * q = asScript(it)) {
747 void normalize(MathArray const & ar, NormalStream & os)
749 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
750 (*it)->normalize(os);
754 void octavize(MathArray const & dat, OctaveStream & os)
757 extractStructure(ar);
758 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
759 MathInset const * p = it->nucleus();
760 if (it + 1 != ar.end()) {
761 if (MathScriptInset const * q = asScript(it)) {
772 void maplize(MathArray const & dat, MapleStream & os)
775 extractStructure(ar);
776 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
777 MathInset const * p = it->nucleus();
778 if (it + 1 != ar.end()) {
779 if (MathScriptInset const * q = asScript(it)) {
790 void mathmlize(MathArray const & dat, MathMLStream & os)
793 extractStructure(ar);
796 else if (ar.size() == 1)
797 os << ar.begin()->nucleus();
800 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
801 MathInset const * p = it->nucleus();
802 if (it + 1 != ar.end()) {
803 if (MathScriptInset const * q = asScript(it)) {
804 q->mathmlize2(p, os);