]> git.lyx.org Git - features.git/blob - src/mathed/MathData.C
Account for real width of nucleus when positioning superscripts in mathed
[features.git] / src / mathed / MathData.C
1 /**
2  * \file MathData.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathData.h"
14 #include "InsetMathFont.h"
15 #include "InsetMathScript.h"
16 #include "InsetMathMacro.h"
17 #include "MathMacroTable.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MathReplace.h"
21
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "cursor.h"
25 #include "debug.h"
26 #include "LColor.h"
27
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
30
31 #include <boost/assert.hpp>
32
33
34 namespace lyx {
35
36 using std::abs;
37 using std::endl;
38 using std::min;
39 using std::ostringstream;
40 using std::string;
41 using std::vector;
42
43
44 MathArray::MathArray(const_iterator from, const_iterator to)
45         : base_type(from, to)
46 {}
47
48
49 MathAtom & MathArray::operator[](pos_type pos)
50 {
51         BOOST_ASSERT(pos < size());
52         return base_type::operator[](pos);
53 }
54
55
56 MathAtom const & MathArray::operator[](pos_type pos) const
57 {
58         BOOST_ASSERT(pos < size());
59         return base_type::operator[](pos);
60 }
61
62
63 void MathArray::insert(size_type pos, MathAtom const & t)
64 {
65         base_type::insert(begin() + pos, t);
66 }
67
68
69 void MathArray::insert(size_type pos, MathArray const & ar)
70 {
71         BOOST_ASSERT(pos <= size());
72         base_type::insert(begin() + pos, ar.begin(), ar.end());
73 }
74
75
76 void MathArray::append(MathArray const & ar)
77 {
78         insert(size(), ar);
79 }
80
81
82 void MathArray::erase(size_type pos)
83 {
84         if (pos < size())
85                 erase(pos, pos + 1);
86 }
87
88
89 void MathArray::erase(iterator pos1, iterator pos2)
90 {
91         base_type::erase(pos1, pos2);
92 }
93
94
95 void MathArray::erase(iterator pos)
96 {
97         base_type::erase(pos);
98 }
99
100
101 void MathArray::erase(size_type pos1, size_type pos2)
102 {
103         base_type::erase(begin() + pos1, begin() + pos2);
104 }
105
106
107 void MathArray::dump2() const
108 {
109         odocstringstream os;
110         NormalStream ns(os);
111         for (const_iterator it = begin(); it != end(); ++it)
112                 ns << *it << ' ';
113         lyxerr << to_utf8(os.str());
114 }
115
116
117 void MathArray::dump() const
118 {
119         odocstringstream os;
120         NormalStream ns(os);
121         for (const_iterator it = begin(); it != end(); ++it)
122                 ns << '<' << *it << '>';
123         lyxerr << to_utf8(os.str());
124 }
125
126
127 void MathArray::validate(LaTeXFeatures & features) const
128 {
129         for (const_iterator it = begin(); it != end(); ++it)
130                 (*it)->validate(features);
131 }
132
133
134 bool MathArray::match(MathArray const & ar) const
135 {
136         return size() == ar.size() && matchpart(ar, 0);
137 }
138
139
140 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
141 {
142         if (size() < ar.size() + pos)
143                 return false;
144         const_iterator it = begin() + pos;
145         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
146                 if (asString(*it) != asString(*jt))
147                         return false;
148         return true;
149 }
150
151
152 void MathArray::replace(ReplaceData & rep)
153 {
154         for (size_type i = 0; i < size(); ++i) {
155                 if (find1(rep.from, i)) {
156                         // match found
157                         lyxerr << "match found!" << endl;
158                         erase(i, i + rep.from.size());
159                         insert(i, rep.to);
160                 }
161         }
162
163 #ifdef WITH_WARNINGS
164 #warning temporarily disabled
165         // for (const_iterator it = begin(); it != end(); ++it)
166         //      it->nucleus()->replace(rep);
167 #endif
168 }
169
170
171 bool MathArray::find1(MathArray const & ar, size_type pos) const
172 {
173         lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
174         for (size_type i = 0, n = ar.size(); i < n; ++i)
175                 if (asString(operator[](pos + i)) != asString(ar[i]))
176                         return false;
177         return true;
178 }
179
180
181 MathArray::size_type MathArray::find(MathArray const & ar) const
182 {
183         for (int i = 0, last = size() - ar.size(); i < last; ++i)
184                 if (find1(ar, i))
185                         return i;
186         return size();
187 }
188
189
190 MathArray::size_type MathArray::find_last(MathArray const & ar) const
191 {
192         for (int i = size() - ar.size(); i >= 0; --i)
193                 if (find1(ar, i))
194                         return i;
195         return size();
196 }
197
198
199 bool MathArray::contains(MathArray const & ar) const
200 {
201         if (find(ar) != size())
202                 return true;
203         for (const_iterator it = begin(); it != end(); ++it)
204                 if ((*it)->contains(ar))
205                         return true;
206         return false;
207 }
208
209
210 void MathArray::touch() const
211 {
212 }
213
214
215 bool MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
216 {
217         dim = dim_;
218         metrics(mi);
219         if (dim_ == dim)
220                 return false;
221         dim = dim_;
222         return true;
223 }
224
225
226 namespace {
227
228 bool isInside(DocIterator const & it, MathArray const & ar,
229         pos_type p1, pos_type p2)
230 {
231         for (size_t i = 0; i != it.depth(); ++i) {
232                 CursorSlice const & sl = it[i];
233                 if (sl.inset().inMathed() && &sl.cell() == &ar)
234                         return p1 <= sl.pos() && sl.pos() < p2;
235         }
236         return false;
237 }
238
239 }
240
241
242
243 void MathArray::metrics(MetricsInfo & mi) const
244 {
245         dim_ = theFontMetrics(mi.base.font).dimension('I');
246         int xascent = theFontMetrics(mi.base.font).dimension('x').ascent();
247         minasc_ = xascent;
248         mindes_ = (3 * xascent) / 4;
249         slevel_ = (4 * xascent) / 5;
250         sshift_ = xascent / 4;
251
252         if (empty())
253                 return;
254
255         dim_.asc = 0;
256         dim_.wid = 0;
257         Dimension d;
258         //BufferView & bv  = *mi.base.bv;
259         //Buffer const & buf = *bv.buffer();
260         for (size_t i = 0, n = size(); i != n; ++i) {
261                 MathAtom const & at = operator[](i);
262 #if 0
263                 MathMacro const * mac = at->asMacro();
264                 if (mac && buf.hasMacro(mac->name())) {
265                         MacroData const & tmpl = buf.getMacro(mac->name());
266                         int numargs = tmpl.numargs();
267                         if (i + numargs > n)
268                                 numargs = n - i - 1;
269                         lyxerr << "metrics:found macro: " << mac->name()
270                                 << " numargs: " << numargs << endl;
271                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
272                                 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
273                                 MathArray exp;
274                                 tmpl.expand(args, exp);
275                                 mac->setExpansion(exp, args);
276                                 mac->metricsExpanded(mi, d);
277                                 dim_.wid += mac->widthExpanded();
278                                 i += numargs;
279                                 continue;
280                         }
281                 }
282 #endif
283                 at->metrics(mi, d);
284                 dim_ += d;
285                 if (i == n - 1)
286                         kerning_ = at->kerning();
287         }
288 }
289
290
291 void MathArray::draw(PainterInfo & pi, int x, int y) const
292 {
293         //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
294         BufferView & bv  = *pi.base.bv;
295         setXY(bv, x, y);
296
297         if (empty()) {
298                 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
299                 return;
300         }
301
302         // don't draw outside the workarea
303         if (y + descent() <= 0
304                 || y - ascent() >= bv.workHeight()
305                 || x + width() <= 0
306                 || x >= bv. workWidth())
307                 return;
308
309         for (size_t i = 0, n = size(); i != n; ++i) {
310                 MathAtom const & at = operator[](i);
311 #if 0
312         Buffer const & buf = bv.buffer();
313                 // special macro handling
314                 MathMacro const * mac = at->asMacro();
315                 if (mac && buf.hasMacro(mac->name())) {
316                         MacroData const & tmpl = buf.getMacro(mac->name());
317                         int numargs = tmpl.numargs();
318                         if (i + numargs > n)
319                                 numargs = n - i - 1;
320                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
321                                 mac->drawExpanded(pi, x, y);
322                                 x += mac->widthExpanded();
323                                 i += numargs;
324                                 continue;
325                         }
326                 }
327 #endif
328                 bv.coordCache().insets().add(at.nucleus(), x, y);
329                 at->drawSelection(pi, x, y);
330                 at->draw(pi, x, y);
331                 x += at->width();
332         }
333 }
334
335
336 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
337 {
338         dim.clear();
339         Dimension d;
340         for (const_iterator it = begin(); it != end(); ++it) {
341                 (*it)->metricsT(mi, d);
342                 dim += d;
343         }
344 }
345
346
347 void MathArray::drawT(TextPainter & pain, int x, int y) const
348 {
349         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
350
351         // FIXME: Abdel 16/10/2006
352         // This drawT() method is never used, this is dead code.
353
354         for (const_iterator it = begin(), et = end(); it != et; ++it) {
355                 (*it)->drawT(pain, x, y);
356                 //x += (*it)->width_;
357                 x += 2;
358         }
359 }
360
361
362 int MathArray::pos2x(size_type pos) const
363 {
364         return pos2x(pos, 0);
365 }
366
367
368 int MathArray::pos2x(size_type pos, int glue) const
369 {
370         int x = 0;
371         size_type target = min(pos, size());
372         for (size_type i = 0; i < target; ++i) {
373                 const_iterator it = begin() + i;
374                 if ((*it)->getChar() == ' ')
375                         x += glue;
376                 //lyxerr << "char: " << (*it)->getChar()
377                 //      << "width: " << (*it)->width() << std::endl;
378                 x += (*it)->width();
379         }
380         return x;
381 }
382
383
384 MathArray::size_type MathArray::x2pos(int targetx) const
385 {
386         return x2pos(targetx, 0);
387 }
388
389
390 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
391 {
392         const_iterator it = begin();
393         int lastx = 0;
394         int currx = 0;
395         // find first position after targetx
396         for (; currx < targetx && it < end(); ++it) {
397                 lastx = currx;
398                 if ((*it)->getChar() == ' ')
399                         currx += glue;
400                 currx += (*it)->width();
401         }
402
403         /**
404          * If we are not at the beginning of the array, go to the left
405          * of the inset if one of the following two condition holds:
406          * - the current inset is editable (so that the cursor tip is
407          *   deeper than us): in this case, we want all intermediate
408          *   cursor slices to be before insets;
409          * - the mouse is closer to the left side of the inset than to
410          *   the right one.
411          * See bug 1918 for details.
412          **/
413         if (it != begin() && currx >= targetx
414             && ((*boost::prior(it))->asNestInset()
415                 || abs(lastx - targetx) < abs(currx - targetx))) {
416                 --it;
417         }
418
419         return it - begin();
420 }
421
422
423 int MathArray::dist(BufferView const & bv, int x, int y) const
424 {
425         int xx = 0;
426         int yy = 0;
427
428         const int xo_ = xo(bv);
429         const int yo_ = yo(bv);
430
431         if (x < xo_)
432                 xx = xo_ - x;
433         else if (x > xo_ + width())
434                 xx = x - xo_ - width();
435
436         if (y < yo_ - ascent())
437                 yy = yo_ - ascent() - y;
438         else if (y > yo_ + descent())
439                 yy = y - yo_ - descent();
440
441         return xx + yy;
442 }
443
444
445 void MathArray::setXY(BufferView & bv, int x, int y) const
446 {
447         //lyxerr << "setting position cache for MathArray " << this << std::endl;
448         bv.coordCache().arrays().add(this, x, y);
449 }
450
451
452 int MathArray::xo(BufferView const & bv) const
453 {
454         return bv.coordCache().getArrays().x(this);
455 }
456
457
458 int MathArray::yo(BufferView const & bv) const
459 {
460         return bv.coordCache().getArrays().y(this);
461 }
462
463
464 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
465 {
466         odocstringstream oss;
467         NormalStream ns(oss);
468         ns << ar;
469         return os << to_utf8(oss.str());
470 }
471
472
473 odocstream & operator<<(odocstream & os, MathArray const & ar)
474 {
475         NormalStream ns(os);
476         ns << ar;
477         return os;
478 }
479
480
481 } // namespace lyx