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"
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)
109 if (it->nucleus()->asScriptInset())
114 return it->nucleus()->asScriptInset();
119 // returns sequence of char with same code starting at it up to end
120 // it might be less, though...
121 MathArray::const_iterator charSequence(MathArray::const_iterator it,
122 MathArray::const_iterator end, string & s, MathTextCodes & c)
124 MathCharInset const * p = (*it)->asCharInset();
126 for (; it != end; ++it) {
127 p = (*it)->asCharInset();
128 if (!p || p->code() != c)
136 void extractStrings(MathArray & ar)
138 //lyxerr << "\nStrings from: " << ar << "\n";
139 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
140 MathArray::iterator it = ar.begin() + i;
141 if (!(*it)->asCharInset())
144 // create proper string inset
145 MathStringInset * p = new MathStringInset;
146 MathArray::const_iterator
147 jt = charSequence(it, ar.end(), p->str_, p->code_);
151 ar.erase(i + 1, jt - ar.begin());
153 //lyxerr << "\nStrings to: " << ar << "\n";
157 MathInset * singleItem(MathArray & ar)
159 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
163 void extractMatrices(MathArray & ar)
165 //lyxerr << "\nMatrices from: " << ar << "\n";
166 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
167 MathDelimInset * del = (*it)->asDelimInset();
170 MathInset * arr = singleItem(del->cell(0));
171 if (!arr || !arr->asArrayInset())
173 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
175 //lyxerr << "\nMatrices to: " << ar << "\n";
179 // convert this inset somehow to a string
180 bool extractString(MathInset * p, string & str)
185 str = string(1, p->getChar());
188 if (p->asStringInset()) {
189 str = p->asStringInset()->str();
196 // convert this inset somehow to a number
197 bool extractNumber(MathArray const & ar, int & i)
201 charSequence(ar.begin(), ar.end(), s, c);
202 istringstream is(s.c_str());
208 bool testString(MathInset * p, const string & str)
211 return extractString(p, s) && str == s;
215 // search end of nested sequence
216 MathArray::iterator endNestSearch(
217 MathArray::iterator it,
218 MathArray::iterator last,
219 TestItemFunc testOpen,
220 TestItemFunc testClose
223 for (int level = 0; it != last; ++it) {
224 if (testOpen(it->nucleus()))
226 if (testClose(it->nucleus()))
235 // replace nested sequences by a real Insets
238 TestItemFunc testOpen,
239 TestItemFunc testClose,
240 ReplaceArgumentFunc replaceArg
243 // use indices rather than iterators for the loop because we are going
244 // to modify the array.
245 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
246 // check whether this is the begin of the sequence
247 MathArray::iterator it = ar.begin() + i;
248 if (!testOpen(it->nucleus()))
251 // search end of sequence
252 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
256 // create a proper inset as replacement
257 MathInset * p = replaceArg(MathArray(it + 1, jt));
259 // replace the original stuff by the new inset
260 ar.erase(it + 1, jt + 1);
268 // split scripts into seperate super- and subscript insets. sub goes in
272 void splitScripts(MathArray & ar)
274 //lyxerr << "\nScripts from: " << ar << "\n";
275 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
276 MathArray::iterator it = ar.begin() + i;
278 // is this script inset?
279 MathScriptInset * p = (*it)->asScriptInset();
283 // no problem if we don't have both...
284 if (!p->hasUp() || !p->hasDown())
287 // create extra script inset and move superscript over
288 MathScriptInset * q = new MathScriptInset;
290 q->up().data_.swap(p->up().data_);
291 p->removeScript(true);
293 // insert new inset behind
295 ar.insert(i, MathAtom(q));
297 //lyxerr << "\nScripts to: " << ar << "\n";
305 void extractExps(MathArray & ar)
307 //lyxerr << "\nExps from: " << ar << "\n";
309 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
310 MathArray::iterator it = ar.begin() + i;
313 MathCharInset const * p = (*it)->asCharInset();
314 if (!p || p->getChar() != 'e')
317 // we need an exponent but no subscript
318 MathScriptInset * sup = (*(it + 1))->asScriptInset();
319 if (!sup || sup->hasDown())
322 // create a proper exp-inset as replacement
323 MathExFuncInset * func = new MathExFuncInset("exp");
324 func->cell(0) = sup->cell(1);
330 //lyxerr << "\nExps to: " << ar << "\n";
335 // search deliminiters
338 bool testOpenParan(MathInset * p)
340 return testString(p, "(");
344 bool testCloseParan(MathInset * p)
346 return testString(p, ")");
350 MathInset * replaceDelims(const MathArray & ar)
352 MathDelimInset * del = new MathDelimInset("(", ")");
358 // replace '('...')' sequences by a real MathDelimInset
359 void extractDelims(MathArray & ar)
361 //lyxerr << "\nDelims from: " << ar << "\n";
362 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
363 //lyxerr << "\nDelims to: " << ar << "\n";
369 // search well-known functions
373 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
374 // assume 'extractDelims' ran before
375 void extractFunctions(MathArray & ar)
377 // we need at least two items...
381 //lyxerr << "\nFunctions from: " << ar << "\n";
382 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
383 MathArray::iterator it = ar.begin() + i;
384 MathArray::iterator jt = it + 1;
388 if ((*it)->asFuncInset()) {
389 // it certainly is if it is well known...
390 name = (*it)->asFuncInset()->name();
392 // is this a user defined function?
393 // it it probably not, if it doesn't have a name.
394 if (!extractString((*it).nucleus(), name))
396 // it is not if it has no argument
399 // guess so, if this is followed by
400 // a DelimInset with a single item in the cell
401 MathDelimInset * del = (*jt)->asDelimInset();
402 if (!del || del->cell(0).size() != 1)
404 // fall trough into main branch
407 // do we have an exponent like in
408 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
410 extractScript(exp, jt, ar.end());
412 // create a proper inset as replacement
413 MathExFuncInset * p = new MathExFuncInset(name);
415 // jt points to the "argument". Get hold of this.
416 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
418 // replace the function name by a real function inset
421 // remove the source of the argument from the array
422 ar.erase(it + 1, st);
424 // re-insert exponent
425 ar.insert(i + 1, exp);
426 //lyxerr << "\nFunctions to: " << ar << "\n";
435 bool testSymbol(MathInset * p, string const & name)
437 return p->asSymbolInset() && p->asSymbolInset()->name() == name;
441 bool testIntSymbol(MathInset * p)
443 return testSymbol(p, "int");
447 bool testIntDiff(MathInset * p)
449 return testString(p, "d");
453 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
454 // assume 'extractDelims' ran before
455 void extractIntegrals(MathArray & ar)
457 // we need at least three items...
461 //lyxerr << "\nIntegrals from: " << ar << "\n";
462 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
463 MathArray::iterator it = ar.begin() + i;
465 // is this a integral name?
466 if (!testIntSymbol(it->nucleus()))
470 MathArray::iterator jt =
471 endNestSearch(it, ar.end(), testIntSymbol, testIntDiff);
473 // something sensible found?
477 // create a proper inset as replacement
478 MathExIntInset * p = new MathExIntInset("int");
480 // collect subscript if any
481 MathArray::iterator st = it + 1;
483 if (MathScriptInset * sub = (*st)->asScriptInset())
484 if (sub->hasDown()) {
485 p->cell(2) = sub->down().data_;
489 // collect superscript if any
491 if (MathScriptInset * sup = (*st)->asScriptInset())
493 p->cell(3) = sup->up().data_;
497 // core ist part from behind the scripts to the 'd'
498 p->cell(0) = MathArray(st, jt);
500 // use the "thing" behind the 'd' as differential
501 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
504 ar.erase(it + 1, tt);
507 //lyxerr << "\nIntegrals to: " << ar << "\n";
515 bool testSumSymbol(MathInset * p)
517 return testSymbol(p, "sum");
521 bool testEqualSign(MathAtom const & at)
523 return testString(at.nucleus(), "=");
528 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
529 // assume 'extractDelims' ran before
530 void extractSums(MathArray & ar)
532 // we need at least two items...
536 //lyxerr << "\nSums from: " << ar << "\n";
537 for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) {
538 MathArray::iterator it = ar.begin() + i;
540 // is this a sum name?
541 if (!testSumSymbol(it->nucleus()))
544 // create a proper inset as replacement
545 MathExIntInset * p = new MathExIntInset("sum");
547 // collect lower bound and summation index
548 MathArray::iterator st = it + 1;
550 if (MathScriptInset * sub = (*st)->asScriptInset())
551 if (sub->hasDown()) {
552 // try to figure out the summation index from the subscript
553 MathArray & ar = sub->down().data_;
554 MathArray::iterator it =
555 find_if(ar.begin(), ar.end(), &testEqualSign);
556 if (it != ar.end()) {
557 // we found a '=', use everything in front of that as index,
558 // and everything behind as lower index
559 p->cell(1) = MathArray(ar.begin(), it);
560 p->cell(2) = MathArray(it + 1, ar.end());
562 // use everything as summation index, don't use scripts.
568 // collect upper bound
570 if (MathScriptInset * sup = (*st)->asScriptInset())
572 p->cell(3) = sup->up().data_;
576 // use some behind the script as core
577 MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
580 ar.erase(it + 1, tt);
583 //lyxerr << "\nSums to: " << ar << "\n";
588 // search differential stuff
591 // tests for 'd' or '\partial'
592 bool testDiffItem(MathAtom const & at)
594 return testString(at.nucleus(), "d");
598 bool testDiffArray(MathArray const & ar)
600 return ar.size() && testDiffItem(ar.front());
604 bool testDiffFrac(MathInset * p)
606 MathFracInset * f = p->asFracInset();
607 return f && testDiffArray(f->cell(0)) && testDiffArray(f->cell(1));
611 // is this something like ^number?
612 bool extractDiffExponent(MathArray::iterator it, int & i)
614 if (!(*it)->asScriptInset())
618 if (!extractString((*it).nucleus(), s))
620 istringstream is(s.c_str());
626 void extractDiff(MathArray & ar)
628 //lyxerr << "\nDiffs from: " << ar << "\n";
629 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
630 MathArray::iterator it = ar.begin() + i;
632 // is this a "differential fraction"?
633 if (!testDiffFrac(it->nucleus()))
636 MathFracInset * f = (*it)->asFracInset();
638 lyxerr << "should not happen\n";
642 // create a proper diff inset
643 MathDiffInset * diff = new MathDiffInset;
645 // collect function, let jt point behind last used item
646 MathArray::iterator jt = it + 1;
648 MathArray & numer = f->cell(0);
649 if (numer.size() > 1 && numer.at(1)->asScriptInset()) {
650 // this is something like d^n f(x) / d... or d^n / d...
653 if (numer.size() > 2)
654 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
656 jt = extractArgument(diff->cell(0), jt, ar.end());
658 // simply d f(x) / d... or d/d...
659 if (numer.size() > 1)
660 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
662 jt = extractArgument(diff->cell(0), jt, ar.end());
665 // collect denominator parts
666 MathArray & denom = f->cell(1);
667 for (MathArray::iterator dt = denom.begin(); dt != denom.end();) {
669 MathArray::iterator et = find_if(dt + 1, denom.end(), &testDiffItem);
672 MathArray::iterator st = et - 1;
673 MathScriptInset * script = (*st)->asScriptInset();
674 if (script && script->hasUp()) {
675 // things like d.../dx^n
677 if (extractNumber(script->up().data_, mult)) {
678 //lyxerr << "mult: " << mult << endl;
679 for (int i = 0; i < mult; ++i)
680 diff->addDer(MathArray(dt + 1, st));
684 diff->addDer(MathArray(dt + 1, et));
690 ar.erase(it + 1, jt);
693 //lyxerr << "\nDiffs to: " << ar << "\n";
702 void extractStructure(MathArray & ar)
707 extractFunctions(ar);
708 extractIntegrals(ar);
716 void write(MathArray const & dat, WriteStream & wi)
720 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
721 wi.firstitem() = (it == ar.begin());
722 MathInset const * p = it->nucleus();
723 if (it + 1 != ar.end()) {
724 if (MathScriptInset const * q = asScript(it)) {
735 void normalize(MathArray const & ar, NormalStream & os)
737 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
738 (*it)->normalize(os);
742 void octavize(MathArray const & dat, OctaveStream & os)
745 extractStructure(ar);
746 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
747 MathInset const * p = it->nucleus();
748 if (it + 1 != ar.end()) {
749 if (MathScriptInset const * q = asScript(it)) {
760 void maplize(MathArray const & dat, MapleStream & os)
763 extractStructure(ar);
764 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
765 MathInset const * p = it->nucleus();
766 if (it + 1 != ar.end()) {
767 if (MathScriptInset const * q = asScript(it)) {
778 void mathmlize(MathArray const & dat, MathMLStream & os)
781 extractStructure(ar);
784 else if (ar.size() == 1)
785 os << ar.begin()->nucleus();
788 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
789 MathInset const * p = it->nucleus();
790 if (it + 1 != ar.end()) {
791 if (MathScriptInset const * q = asScript(it)) {
792 q->mathmlize2(p, os);