]> git.lyx.org Git - lyx.git/blob - src/mathed/math_textinset.C
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / math_textinset.C
1
2 #include "math_textinset.h"
3 #include "math_metricsinfo.h"
4 #include "debug.h"
5
6
7 MathTextInset::MathTextInset()
8         : MathNestInset(1)
9 {}
10
11
12 MathInset * MathTextInset::clone() const
13 {
14         return new MathTextInset(*this);
15 }
16
17
18 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
19 {
20         for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
21                 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
22                         return r;
23         lyxerr << "illegal row for pos " << pos << "\n";
24         return 0;
25 }
26
27
28 void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const
29 {
30         idx_type const i = pos2row(pos);
31         pos_type const p = pos - cache_.cellinfo_[i].begin_;
32         cache_.getPos(i, p, x, y);
33 }
34
35
36 bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up,
37         int /*targetx*/) const
38 {
39         // try to move only one screen row up or down if possible
40         idx_type i = pos2row(pos);
41         MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
42         int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_);
43         if (up) {
44                 if (i == 0)
45                         return false;
46                 --i;
47         } else {
48                 ++i;
49                 if (i == cache_.nargs())
50                         return false;
51         }
52         MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
53         pos = cell(0).x2pos(cell2.begin_, x, cell2.glue_);
54         return true;
55 }
56
57
58 void MathTextInset::metrics(MathMetricsInfo & mi) const
59 {
60         cell(0).metrics(mi);
61
62         // we do our own metrics fiddling
63         // delete old cache
64         cache_ = MathGridInset(1, 0);
65
66         int spaces  = 0;
67         int safe    = 0;
68         int curr    = 0;
69         int begin   = 0;
70         int safepos = 0;
71         for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
72                 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
73                 //      << " curr: " << curr << " safe: " << safe
74                 //      << " spaces: " << spaces << endl;
75
76                 //   0      1      2      3       4      5      6
77                 // <char> <char> <char> <space> <char> <char> <char>
78                 // ................... <safe>
79                 //                      ..........................<curr>
80                 // ....................<safepos>
81
82                 // Special handling of spaces. We reached a safe position for breaking.
83                 char const c = cell(0)[i]->getChar();
84                 if (c == ' ') {
85                         //lyxerr << "reached safe pos\n";
86                         // we don't count the space into the safe pos
87                         safe += curr;
88                         // we reset to this safepos if the next chunk does not fit
89                         safepos = i;
90                         ++spaces;
91                         // restart chunk with size of the space
92                         curr = cell(0)[i]->width();
93                         continue;
94                 }
95
96                 if (c != '\n') {
97                         // This is a regular char. Go on if we either don't care for
98                         // the width limit or have not reached that limit.
99                         curr += cell(0)[i]->width();
100                         if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
101                                 continue;
102                 }
103
104                 // We passed the limit. Create a row entry.
105                 //lyxerr << "passed limit\n";
106                 cache_.appendRow();
107                 MathArray & ar = cache_.cell(cache_.nargs() - 1);
108                 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
109                 if (c == '\n') {
110                         // we are here because we hit a hard newline
111                         row.begin_ = begin;
112                         row.end_   = i + 1;
113                         begin      = i + 1;    // next chunk starts after the newline
114                         spaces     = 0;
115                 } else if (spaces) {
116                         // but we had a space break before this position.
117                         // so retreat to this position
118                         //lyxerr << "... but had safe pos.\n";
119                         row.begin_ = begin;
120                         row.end_   = safepos;  // this is position of the safe space
121                         i          = safepos;  // i gets incremented at end of loop
122                         begin      = i + 1;    // next chunk starts after the space
123                         spaces     = 0;
124                 } else {
125                         // This item is too large and it is the only one.
126                         // We have no choice but to produce an overfull box.
127                         lyxerr << "... without safe pos\n";
128                         row.begin_ = begin;
129                         row.end_   = i + 1;
130                         begin      = i + 1;
131                 }
132                 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
133                 //lyxerr << "line: " << ar << "\n";
134                 // in any case, start the new row with empty boxes
135                 curr = 0;
136                 safe = 0;
137         }
138         // last row: put in everything else
139         cache_.appendRow();
140         MathArray & ar = cache_.cell(cache_.nargs() - 1);
141         MathGridInset::CellInfo & row = cache_.cellinfo_.back();
142         row.begin_ = begin;
143         row.end_   = cell(0).size();
144         ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
145         //lyxerr << "last line: " << ar.data() << "\n";
146
147         // what to report?
148         cache_.metrics(mi);
149         dim_ = cache_.dimensions();
150         //lyxerr << "outer dim: " << dim_ << endl;
151 }
152
153
154 void MathTextInset::draw(MathPainterInfo & pi, int x, int y) const
155 {
156         cache_.draw(pi, x + 1, y);
157 }
158
159
160 void MathTextInset::drawSelection(MathPainterInfo & pi,
161                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
162 {
163         cache_.drawSelection(pi, idx1, pos1, idx2, pos2);
164 }