]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
Make the height of a formula at least the height of an 'I'
[lyx.git] / src / mathed / xarray.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "xarray.h"
8 #include "math_inset.h"
9 #include "mathed/support.h"
10 #include "math_defs.h"
11 #include "Painter.h"
12 #include "debug.h"
13
14 using std::max;
15 using std::min;
16
17
18 MathXArray::MathXArray()
19         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), style_(LM_ST_TEXT)
20 {}
21
22
23 void MathXArray::metrics(MathStyles st) const
24 {
25         style_   = st;
26         mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_);
27
28         if (data_.empty()) 
29                 return;
30         
31         width_   = 0;
32
33         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
34         for (int pos = 0; pos < data_.size(); ++pos) {
35                 MathInset const * p = data_.nextInset(pos);
36                 p->metrics(st);
37                 int asc  = p->ascent();
38                 int des  = p->descent();
39                 int wid  = p->width();
40                 ascent_  = max(ascent_, asc);
41                 descent_ = max(descent_, des);
42                 width_   += wid;
43         }
44 }
45
46
47 void MathXArray::draw(Painter & pain, int x, int y) const
48 {
49         xo_ = x;
50         yo_ = y;
51
52         if (data_.empty()) {
53                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
54                 return;
55         }
56
57         for (int pos = 0; pos < data_.size(); ++pos) {
58                 MathInset const * p = data_.nextInset(pos);
59                 p->draw(pain, x, y);
60                 x += p->width();
61         }
62 }
63
64
65 int MathXArray::pos2x(int targetpos) const
66 {
67         int x = 0;
68         targetpos = min(targetpos, data_.size());
69         for (int pos = 0; pos < targetpos; ++pos) 
70                 x += width(pos);
71         return x;
72 }
73
74
75 int MathXArray::x2pos(int targetx) const
76 {
77         int pos   = 0;
78         int lastx = 0;
79         int currx = 0;
80         for ( ; currx < targetx && pos < data_.size(); ++pos) {
81                 lastx = currx;
82                 currx += width(pos);
83         }
84         if (abs(lastx - targetx) < abs(currx - targetx) && pos > 0)
85                 --pos;
86         return pos;
87 }
88
89
90 int MathXArray::width(int pos) const
91 {
92         if (pos >= data_.size())
93                 return 0;
94         return data_.nextInset(pos)->width();
95 }
96
97
98 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
99 {
100         os << ar.data_;
101         return os;
102 }
103