2 // This file contains most of the magic that extracts "context
3 // information" from the unstructered layout-oriented stuff in an
7 #include "math_charinset.h"
8 #include "math_deliminset.h"
9 #include "math_exfuncinset.h"
10 #include "math_exintinset.h"
11 #include "math_funcinset.h"
12 #include "math_matrixinset.h"
13 #include "math_mathmlstream.h"
14 #include "math_scriptinset.h"
15 #include "math_stringinset.h"
16 #include "math_symbolinset.h"
20 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
28 MathScriptInset const * asScript(MathArray::const_iterator it)
30 if (it->nucleus()->asScriptInset())
35 return it->nucleus()->asScriptInset();
40 // returns sequence of char with same code starting at it up to end
41 // it might be less, though...
42 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
45 MathCharInset const * p = it->nucleus()->asCharInset();
47 for (MathTextCodes c = p->code(); it != end; ++it) {
48 p = it->nucleus()->asCharInset();
49 if (!p || p->code() != c)
58 void extractStrings(MathArray & dat)
60 //lyxerr << "\nStrings from: " << ar << "\n";
62 MathArray::const_iterator it = dat.begin();
63 while (it != dat.end()) {
64 if (it->nucleus() && it->nucleus()->asCharInset()) {
65 string s = charSequence(it, dat.end());
66 MathTextCodes c = it->nucleus()->asCharInset()->code();
67 ar.push_back(MathAtom(new MathStringInset(s, c)));
75 //lyxerr << "\nStrings to: " << ar << "\n";
79 MathInset * singleItem(MathArray & ar)
81 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
85 void extractMatrices(MathArray & ar)
87 lyxerr << "\nMatrices from: " << ar << "\n";
88 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
89 MathDelimInset * del = (*it)->asDelimInset();
92 MathInset * arr = singleItem(del->cell(0));
93 if (!arr || !arr->asArrayInset())
95 *it = MathAtom(new MathMatrixInset(*(arr->asArrayInset())));
96 lyxerr << "\nMatrices to: " << ar << "\n";
100 // convert this inset somehow to a string
101 string extractString(MathInset * p)
103 if (p && p->getChar())
104 return string(1, p->getChar());
105 if (p && p->asStringInset())
106 return p->asStringInset()->str();
111 // define a function for tests
112 typedef bool TestItemFunc(MathInset *);
114 // define a function for replacing pa
115 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
117 // search end of nested sequence
118 MathArray::iterator endNestSearch(
119 MathArray::iterator it,
120 MathArray::iterator last,
121 TestItemFunc testOpen,
122 TestItemFunc testClose
125 for (int level = 0; it != last; ++it) {
126 if (testOpen(it->nucleus()))
128 if (testClose(it->nucleus()))
137 // replace nested sequences by a real Insets
140 TestItemFunc testOpen,
141 TestItemFunc testClose,
142 ReplaceArgumentFunc replaceArg
145 // use indices rather than iterators for the loop because we are going
146 // to modify the array.
147 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
148 // check whether this is the begin of the sequence
149 MathArray::iterator it = ar.begin() + i;
150 if (!testOpen(it->nucleus()))
153 // search end of sequence
154 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
158 // create a proper inset as replacement
159 MathInset * p = replaceArg(MathArray(it + 1, jt));
161 // replace the original stuff by the new inset
162 ar.erase(it + 1, jt + 1);
169 // search deliminiters
172 bool openParanTest(MathInset * p)
174 return extractString(p) == "(";
178 bool closeParanTest(MathInset * p)
180 return extractString(p) == ")";
184 MathInset * delimReplacement(const MathArray & ar)
186 MathDelimInset * del = new MathDelimInset("(", ")");
192 // replace '('...')' sequences by a real MathDelimInset
193 void extractDelims(MathArray & ar) {
194 lyxerr << "\nDelims from: " << ar << "\n";
195 replaceNested(ar, openParanTest, closeParanTest, delimReplacement);
196 lyxerr << "\nDelims to: " << ar << "\n";
202 // search well-known functions
206 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
207 // assume 'extractDelims' ran before
208 void extractFunctions(MathArray & ar)
210 // we need at least two items...
214 lyxerr << "\nFunctions from: " << ar << "\n";
215 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
216 MathArray::iterator it = ar.begin() + i;
218 // is this a function name?
219 MathFuncInset * func = (*it)->asFuncInset();
223 // do we have an exponent?
224 // simply skippping the postion does the right thing:
225 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
226 MathArray::iterator jt = it + 1;
227 if (MathScriptInset * script = (*jt)->asScriptInset()) {
228 // allow superscripts only
229 if (script->hasDown())
236 // jt points now to the "argument". Since we had run "extractDelims"
237 // before, this could be a single argument only. Get hold of this.
239 MathDelimInset * del = (*jt)->asDelimInset();
240 if (del && del->isParanthesis())
245 // replace the function name by a real function inset
246 (*it).reset(new MathExFuncInset(func->name(), arg));
248 // remove the source of the argument from the array
250 lyxerr << "\nFunctions to: " << ar << "\n";
259 bool intSymbolTest(MathInset * p)
261 return p->asSymbolInset() && p->asSymbolInset()->name() == "int";
265 bool differentialTest(MathInset * p)
267 string s = extractString(p);
268 return s.size() && s[0] == 'd';
272 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
273 // assume 'extractDelims' ran before
274 void extractIntegrals(MathArray & ar)
276 // we need at least three items...
280 lyxerr << "\nIntegrals from: " << ar << "\n";
281 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
282 MathArray::iterator it = ar.begin() + i;
284 // is this a integral name?
285 if (!intSymbolTest(it->nucleus()))
289 MathArray::iterator jt =
290 endNestSearch(it, ar.end(), intSymbolTest, differentialTest);
292 // create a proper inset as replacement
293 MathExIntInset * p = new MathExIntInset;
296 MathArray::iterator st = it + 1;
297 if ((*st)->asScriptInset()) {
299 p->core(MathArray(st + 1, jt));
301 p->core(MathArray(st, jt));
304 // is the differential just 'd' oder 'dsomething'?
305 // and replace the original stuff by the new inset
306 string s = extractString(jt->nucleus());
308 if (s.size() == 1 && jt + 1 != ar.end()) {
309 // use the next atom as differential
310 diff.push_back(*(jt + 1));
311 ar.erase(it + 1, jt + 2);
313 diff.push_back(MathAtom(new MathStringInset(s.substr(1))));
314 ar.erase(it + 1, jt + 1);
316 p->differential(diff);
319 lyxerr << "\nIntegrals to: " << ar << "\n";
323 void extractStructure(MathArray & ar)
328 extractFunctions(ar);
329 extractIntegrals(ar);
333 void write(MathArray const & dat, WriteStream & wi)
337 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
338 MathInset const * p = it->nucleus();
339 if (it + 1 != ar.end()) {
340 if (MathScriptInset const * q = asScript(it)) {
351 void normalize(MathArray const & ar, NormalStream & os)
353 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
354 (*it)->normalize(os);
358 void octavize(MathArray const & dat, OctaveStream & os)
361 extractStructure(ar);
362 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
363 MathInset const * p = it->nucleus();
364 if (it + 1 != ar.end()) {
365 if (MathScriptInset const * q = asScript(it)) {
376 void maplize(MathArray const & dat, MapleStream & os)
379 extractStructure(ar);
380 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
381 MathInset const * p = it->nucleus();
382 if (it + 1 != ar.end()) {
383 if (MathScriptInset const * q = asScript(it)) {
394 void mathmlize(MathArray const & dat, MathMLStream & os)
397 extractStructure(ar);
400 else if (ar.size() == 1)
401 os << ar.begin()->nucleus();
404 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
405 MathInset const * p = it->nucleus();
406 if (it + 1 != ar.end()) {
407 if (MathScriptInset const * q = asScript(it)) {