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 MathInset * intReplacement(const MathArray & ar)
274 MathDelimInset * del = new MathDelimInset("(", ")");
280 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
281 // assume 'extractDelims' ran before
282 void extractIntegrals(MathArray & ar)
284 // we need at least three items...
288 lyxerr << "\nIntegrals from: " << ar << "\n";
289 for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
290 MathArray::iterator it = ar.begin() + i;
292 // is this a integral name?
293 if (!intSymbolTest(it->nucleus()))
297 MathArray::iterator jt =
298 endNestSearch(it, ar.end(), intSymbolTest, differentialTest);
300 // create a proper inset as replacement
301 MathInset * p = intReplacement(MathArray(it + 1, jt));
303 // replace the original stuff by the new inset
304 ar.erase(it + 1, jt + 1);
310 void extractStructure(MathArray & ar)
315 extractFunctions(ar);
316 extractIntegrals(ar);
320 void write(MathArray const & dat, WriteStream & wi)
324 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
325 MathInset const * p = it->nucleus();
326 if (it + 1 != ar.end()) {
327 if (MathScriptInset const * q = asScript(it)) {
338 void normalize(MathArray const & ar, NormalStream & os)
340 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
341 (*it)->normalize(os);
345 void octavize(MathArray const & dat, OctaveStream & os)
348 extractStructure(ar);
349 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
350 MathInset const * p = it->nucleus();
351 if (it + 1 != ar.end()) {
352 if (MathScriptInset const * q = asScript(it)) {
363 void maplize(MathArray const & dat, MapleStream & os)
366 extractStructure(ar);
367 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
368 MathInset const * p = it->nucleus();
369 if (it + 1 != ar.end()) {
370 if (MathScriptInset const * q = asScript(it)) {
381 void mathmlize(MathArray const & dat, MathMLStream & os)
384 extractStructure(ar);
387 else if (ar.size() == 1)
388 os << ar.begin()->nucleus();
391 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
392 MathInset const * p = it->nucleus();
393 if (it + 1 != ar.end()) {
394 if (MathScriptInset const * q = asScript(it)) {