]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
884a03b5adfd2772b791bdbc86b7db96a8ababa6
[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
13 using std::max;
14 using std::min;
15
16
17 MathXArray::MathXArray()
18         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), style_(LM_ST_TEXT)
19 {}
20
21
22 void MathXArray::metrics(MathStyles st)
23 {
24         if (data_.empty()) {
25                 mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_); 
26                 return;
27         }
28         
29         ascent_  = 0;
30         descent_ = 0;
31         width_   = 0;
32         style_   = st;
33
34         for (int pos = 0; pos < data_.size(); data_.next(pos)) {
35                 int asc;
36                 int des;
37                 int wid;
38                 MathInset * p = data_.nextInset(pos);
39                 if (p) {
40                         p->metrics(st);
41                         asc = p->ascent();
42                         des = p->descent();
43                         wid = p->width();
44                 } else {
45                         char cx = data_.getChar(pos); 
46                         MathTextCodes fc = data_.getCode(pos); 
47                         mathed_char_dim(fc, style_, cx, asc, des, wid);
48                 }
49                 ascent_  = max(ascent_, asc);
50                 descent_ = max(descent_, des);
51                 width_   += wid;
52         }
53 }
54
55
56 void MathXArray::draw(Painter & pain, int x, int y)
57 {
58         xo_ = x;
59         yo_ = y;
60
61         if (data_.empty()) {
62                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
63                 return;
64         }
65
66         for (int pos = 0; pos < data_.size(); data_.next(pos)) {
67                 MathInset * p = data_.nextInset(pos);
68                 if (p) {
69                         p->draw(pain, x, y);
70                         x += p->width();
71                 } else {
72                         char cx = data_.getChar(pos);
73                         MathTextCodes fc = data_.getCode(pos);
74                         string s;
75                         s += cx;
76                         drawStr(pain, fc, style_, x, y, s);
77                         x += mathed_char_width(fc, style_, cx);
78                 }
79         }
80 }
81
82
83 int MathXArray::pos2x(int targetpos) const
84 {
85         int x = 0;
86         targetpos = min(targetpos, data_.size());
87         for (int pos = 0; pos < targetpos; data_.next(pos)) 
88                 x += width(pos);
89         return x;
90 }
91
92
93 int MathXArray::x2pos(int targetx) const
94 {
95         int pos   = 0;
96         int lastx = 0;
97         int currx = 0;
98         while (currx < targetx && pos < data_.size()) {
99                 lastx = currx;
100                 currx += width(pos);
101                 data_.next(pos);
102         }
103         if (abs(lastx - targetx) < abs(currx - targetx))
104                 data_.prev(pos);
105         return pos;
106 }
107
108 int MathXArray::width(int pos) const
109 {
110         if (pos >= data_.size())
111                 return 0;
112
113         if (data_.isInset(pos)) 
114                 return data_.nextInset(pos)->width();
115         else 
116                 return mathed_char_width(data_.getCode(pos), style_, data_.getChar(pos));
117 }
118
119 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
120 {
121         os << ar.data_;
122         return os;
123 }
124