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"
24 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
32 // define a function for tests
33 typedef bool TestItemFunc(MathInset *);
35 // define a function for replacing subexpressions
36 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
40 // try to extract a super/subscript
41 // modify iterator position to point behind the thing
42 bool extractScript(MathArray & ar,
43 MathArray::iterator & pos, MathArray::iterator last)
45 // nothing to get here
49 // is this a scriptinset?
50 if (!(*pos)->asScriptInset())
53 // it is a scriptinset, use it.
60 // try to extract an "argument" to some function.
61 // returns position behind the argument
62 MathArray::iterator extractArgument(MathArray & ar,
63 MathArray::iterator pos, MathArray::iterator last, string const & = "")
65 // nothing to get here
69 // something deliminited _is_ an argument
70 if ((*pos)->asDelimInset()) {
75 // always take the first thing, no matter what it is
78 // go ahead if possible
83 // if the next item is a subscript, it most certainly belongs to the
85 extractScript(ar, pos, last);
89 // but it might be more than that.
90 // FIXME: not implemented
91 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
92 // // always take the first thing, no matter
102 MathScriptInset const * asScript(MathArray::const_iterator it)
104 if (it->nucleus()->asScriptInset())
109 return it->nucleus()->asScriptInset();
114 // returns sequence of char with same code starting at it up to end
115 // it might be less, though...
116 MathArray::const_iterator charSequence(MathArray::const_iterator it,
117 MathArray::const_iterator end, string & s, MathTextCodes & c)
119 MathCharInset const * p = (*it)->asCharInset();
121 for (; it != end; ++it) {
122 p = (*it)->asCharInset();
123 if (!p || p->code() != c)
131 void extractStrings(MathArray & ar)
133 //lyxerr << "\nStrings from: " << ar << "\n";
134 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
135 MathArray::iterator it = ar.begin() + i;
136 if (!(*it)->asCharInset())
139 // create proper string inset
140 MathStringInset * p = new MathStringInset;
141 MathArray::const_iterator
142 jt = charSequence(it, ar.end(), p->str_, p->code_);
146 ar.erase(i + 1, jt - ar.begin());
148 //lyxerr << "\nStrings to: " << ar << "\n";
152 MathInset * singleItem(MathArray & ar)
154 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
158 void extractMatrices(MathArray & ar)
160 lyxerr << "\nMatrices from: " << ar << "\n";
161 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
162 MathDelimInset * del = (*it)->asDelimInset();
165 MathInset * arr = singleItem(del->cell(0));
166 if (!arr || !arr->asArrayInset())
168 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
170 lyxerr << "\nMatrices to: " << ar << "\n";
174 // convert this inset somehow to a string
175 bool extractString(MathInset * p, string & str)
180 str = string(1, p->getChar());
183 if (p->asStringInset()) {
184 str = p->asStringInset()->str();
191 // convert this inset somehow to a number
192 bool extractNumber(MathArray const & ar, int & i)
196 charSequence(ar.begin(), ar.end(), s, c);
197 std::istringstream is(s);
203 bool testString(MathInset * p, const string & str)
206 return extractString(p, s) && str == s;
210 // search end of nested sequence
211 MathArray::iterator endNestSearch(
212 MathArray::iterator it,
213 MathArray::iterator last,
214 TestItemFunc testOpen,
215 TestItemFunc testClose
218 for (int level = 0; it != last; ++it) {
219 if (testOpen(it->nucleus()))
221 if (testClose(it->nucleus()))
230 // replace nested sequences by a real Insets
233 TestItemFunc testOpen,
234 TestItemFunc testClose,
235 ReplaceArgumentFunc replaceArg
238 // use indices rather than iterators for the loop because we are going
239 // to modify the array.
240 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
241 // check whether this is the begin of the sequence
242 MathArray::iterator it = ar.begin() + i;
243 if (!testOpen(it->nucleus()))
246 // search end of sequence
247 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
251 // create a proper inset as replacement
252 MathInset * p = replaceArg(MathArray(it + 1, jt));
254 // replace the original stuff by the new inset
255 ar.erase(it + 1, jt + 1);
263 // split scripts into seperate super- and subscript insets. sub goes in
267 void splitScripts(MathArray & ar)
269 lyxerr << "\nScripts from: " << ar << "\n";
270 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
271 MathArray::iterator it = ar.begin() + i;
273 // is this script inset?
274 MathScriptInset * p = (*it)->asScriptInset();
278 // no problem if we don't have both...
279 if (!p->hasUp() || !p->hasDown())
282 // create extra script inset and move superscript over
283 MathScriptInset * q = new MathScriptInset;
285 q->up().data_.swap(p->up().data_);
286 p->removeScript(true);
288 // insert new inset behind
290 ar.insert(i, MathAtom(q));
292 lyxerr << "\nScripts to: " << ar << "\n";
298 // search deliminiters
301 bool testOpenParan(MathInset * p)
303 return testString(p, "(");
307 bool testCloseParan(MathInset * p)
309 return testString(p, ")");
313 MathInset * replaceDelims(const MathArray & ar)
315 MathDelimInset * del = new MathDelimInset("(", ")");
321 // replace '('...')' sequences by a real MathDelimInset
322 void extractDelims(MathArray & ar)
324 lyxerr << "\nDelims from: " << ar << "\n";
325 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
326 lyxerr << "\nDelims to: " << ar << "\n";
332 // search well-known functions
336 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
337 // assume 'extractDelims' ran before
338 void extractFunctions(MathArray & ar)
340 // we need at least two items...
344 lyxerr << "\nFunctions from: " << ar << "\n";
345 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
346 MathArray::iterator it = ar.begin() + i;
347 MathArray::iterator jt = it + 1;
351 if ((*it)->asFuncInset()) {
352 // it certainly is if it is well known...
353 name = (*it)->asFuncInset()->name();
355 // is this a user defined function?
356 // it it probably not, if it doesn't have a name.
357 if (!extractString((*it).nucleus(), name))
359 // it is not if it has no argument
362 // guess so, if this is followed by
363 // a DelimInset with a single item in the cell
364 MathDelimInset * del = (*jt)->asDelimInset();
365 if (!del || del->cell(0).size() != 1)
367 // fall trough into main branch
370 // do we have an exponent like in
371 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
373 extractScript(exp, jt, ar.end());
375 // create a proper inset as replacement
376 MathExFuncInset * p = new MathExFuncInset(name);
378 // jt points to the "argument". Get hold of this.
379 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
381 // replace the function name by a real function inset
384 // remove the source of the argument from the array
385 ar.erase(it + 1, st);
387 // re-insert exponent
388 ar.insert(i + 1, exp);
389 lyxerr << "\nFunctions to: " << ar << "\n";
398 bool testSymbol(MathInset * p, string const & name)
400 return p->asSymbolInset() && p->asSymbolInset()->name() == name;
404 bool testIntSymbol(MathInset * p)
406 return testSymbol(p, "int");
410 bool testIntDiff(MathInset * p)
412 return testString(p, "d");
416 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
417 // assume 'extractDelims' ran before
418 void extractIntegrals(MathArray & ar)
420 // we need at least three items...
424 lyxerr << "\nIntegrals from: " << ar << "\n";
425 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
426 MathArray::iterator it = ar.begin() + i;
428 // is this a integral name?
429 if (!testIntSymbol(it->nucleus()))
433 MathArray::iterator jt =
434 endNestSearch(it, ar.end(), testIntSymbol, testIntDiff);
436 // something sensible found?
440 // create a proper inset as replacement
441 MathExIntInset * p = new MathExIntInset("int");
443 // collect subscript if any
444 MathArray::iterator st = it + 1;
446 if (MathScriptInset * sub = (*st)->asScriptInset())
447 if (sub->hasDown()) {
448 p->cell(2) = sub->down().data_;
452 // collect superscript if any
454 if (MathScriptInset * sup = (*st)->asScriptInset())
456 p->cell(3) = sup->up().data_;
460 // core ist part from behind the scripts to the 'd'
461 p->cell(0) = MathArray(st, jt);
463 // use the "thing" behind the 'd' as differential
464 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
467 ar.erase(it + 1, tt);
470 lyxerr << "\nIntegrals to: " << ar << "\n";
478 bool testSumSymbol(MathInset * p)
480 return testSymbol(p, "sum");
484 bool testEqualSign(MathAtom const & at)
486 return testString(at.nucleus(), "=");
491 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
492 // assume 'extractDelims' ran before
493 void extractSums(MathArray & ar)
495 // we need at least two items...
499 lyxerr << "\nSums from: " << ar << "\n";
500 for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) {
501 MathArray::iterator it = ar.begin() + i;
503 // is this a sum name?
504 if (!testSumSymbol(it->nucleus()))
507 // create a proper inset as replacement
508 MathExIntInset * p = new MathExIntInset("sum");
510 // collect lower bound and summation index
511 MathArray::iterator st = it + 1;
513 if (MathScriptInset * sub = (*st)->asScriptInset())
514 if (sub->hasDown()) {
515 // try to figure out the summation index from the subscript
516 MathArray & ar = sub->down().data_;
517 MathArray::iterator it =
518 std::find_if(ar.begin(), ar.end(), &testEqualSign);
519 if (it != ar.end()) {
520 // we found a '=', use everything in front of that as index,
521 // and everything behind as lower index
522 p->cell(1) = MathArray(ar.begin(), it);
523 p->cell(2) = MathArray(it + 1, ar.end());
525 // use everything as summation index, don't use scripts.
531 // collect upper bound
533 if (MathScriptInset * sup = (*st)->asScriptInset())
535 p->cell(3) = sup->up().data_;
539 // use some behind the script as core
540 MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
543 ar.erase(it + 1, tt);
546 lyxerr << "\nSums to: " << ar << "\n";
551 // search differential stuff
554 // tests for 'd' or '\partial'
555 bool testDiffItem(MathAtom const & at)
557 return testString(at.nucleus(), "d");
561 bool testDiffArray(MathArray const & ar)
563 return ar.size() && testDiffItem(ar.front());
567 bool testDiffFrac(MathInset * p)
569 MathFracInset * f = p->asFracInset();
570 return f && testDiffArray(f->cell(0)) && testDiffArray(f->cell(1));
574 // is this something like ^number?
575 bool extractDiffExponent(MathArray::iterator it, int & i)
577 if (!(*it)->asScriptInset())
581 if (!extractString((*it).nucleus(), s))
583 std::istringstream is(s);
589 void extractDiff(MathArray & ar)
591 lyxerr << "\nDiffs from: " << ar << "\n";
592 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
593 MathArray::iterator it = ar.begin() + i;
595 // is this a "differential fraction"?
596 if (!testDiffFrac(it->nucleus()))
599 MathFracInset * f = (*it)->asFracInset();
601 lyxerr << "should not happen\n";
605 // create a proper diff inset
606 MathDiffInset * diff = new MathDiffInset;
608 // collect function, let jt point behind last used item
609 MathArray::iterator jt = it + 1;
611 MathArray & numer = f->cell(0);
612 if (numer.size() > 1 && numer.at(1)->asScriptInset()) {
613 // this is something like d^n f(x) / d... or d^n / d...
616 if (numer.size() > 2)
617 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
619 jt = extractArgument(diff->cell(0), jt, ar.end());
621 // simply d f(x) / d... or d/d...
622 if (numer.size() > 1)
623 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
625 jt = extractArgument(diff->cell(0), jt, ar.end());
628 // collect denominator parts
629 MathArray & denom = f->cell(1);
630 for (MathArray::iterator dt = denom.begin(); dt != denom.end(); ) {
632 MathArray::iterator et = std::find_if(dt + 1, denom.end(), &testDiffItem);
635 MathArray::iterator st = et - 1;
636 MathScriptInset * script = (*st)->asScriptInset();
637 if (script && script->hasUp()) {
638 // things like d.../dx^n
640 if (extractNumber(script->up().data_, mult)) {
641 lyxerr << "mult: " << mult << endl;
642 for (int i = 0; i < mult; ++i)
643 diff->addDer(MathArray(dt + 1, st));
647 diff->addDer(MathArray(dt + 1, et));
653 ar.erase(it + 1, jt);
656 lyxerr << "\nDiffs to: " << ar << "\n";
663 void extractStructure(MathArray & ar)
668 extractFunctions(ar);
669 extractIntegrals(ar);
676 void write(MathArray const & dat, WriteStream & wi)
680 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
681 wi.firstitem = (it == ar.begin());
682 MathInset const * p = it->nucleus();
683 if (it + 1 != ar.end()) {
684 if (MathScriptInset const * q = asScript(it)) {
695 void normalize(MathArray const & ar, NormalStream & os)
697 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
698 (*it)->normalize(os);
702 void octavize(MathArray const & dat, OctaveStream & os)
705 extractStructure(ar);
706 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
707 MathInset const * p = it->nucleus();
708 if (it + 1 != ar.end()) {
709 if (MathScriptInset const * q = asScript(it)) {
720 void maplize(MathArray const & dat, MapleStream & os)
723 extractStructure(ar);
724 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
725 MathInset const * p = it->nucleus();
726 if (it + 1 != ar.end()) {
727 if (MathScriptInset const * q = asScript(it)) {
738 void mathmlize(MathArray const & dat, MathMLStream & os)
741 extractStructure(ar);
744 else if (ar.size() == 1)
745 os << ar.begin()->nucleus();
748 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
749 MathInset const * p = it->nucleus();
750 if (it + 1 != ar.end()) {
751 if (MathScriptInset const * q = asScript(it)) {