]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
Make MathRow tokens completely generic
[lyx.git] / src / mathed / MathRow.cpp
1 /**
2  * \file MathRow.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathRow.h"
14
15 #include "InsetMath.h"
16 #include "MathClass.h"
17 #include "MathData.h"
18 #include "MathSupport.h"
19
20 #include "BufferView.h"
21 #include "CoordCache.h"
22 #include "MetricsInfo.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/lassert.h"
30
31 #include <ostream>
32
33 using namespace std;
34
35 namespace lyx {
36
37
38 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
39         : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
40           marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
41           color(Color_red)
42 {}
43
44
45 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
46 {
47         // First there is a dummy element of type "open"
48         push_back(Element(mi, DUMMY, MC_OPEN));
49
50         // Then insert the MathData argument
51         bool const has_contents = ar->addToMathRow(*this, mi);
52
53         // A MathRow should not be completely empty
54         if (!has_contents) {
55                 Element e(mi, BOX, MC_ORD);
56                 // empty arrays are visible when they are editable
57                 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
58                 push_back(e);
59         }
60
61         // Finally there is a dummy element of type "close"
62         push_back(Element(mi, DUMMY, MC_CLOSE));
63
64         /* Do spacing only in math mode. This test is a bit clumsy,
65          * but it is used in other places for guessing the current mode.
66          */
67         bool const dospacing = isMathFont(mi.base.fontname);
68
69         // update classes
70         if (dospacing) {
71                 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
72                         if (elements_[i].mclass != MC_UNKNOWN)
73                                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
74                                                          elements_[after(i)].mclass);
75                 }
76         }
77
78         // set spacing
79         // We go to the end to handle spacing at the end of equation
80         for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
81                 Element & e = elements_[i];
82
83                 if (e.mclass == MC_UNKNOWN)
84                         continue;
85
86                 Element & bef = elements_[before(i)];
87                 if (dospacing) {
88                         int spc = class_spacing(bef.mclass, e.mclass, mi.base);
89                         bef.after += spc / 2;
90                         // this is better than spc / 2 to avoid rounding problems
91                         e.before += spc - spc / 2;
92                 }
93
94                 // finally reserve space for markers
95                 if (bef.marker != Inset::NO_MARKER)
96                         bef.after = max(bef.after, 1);
97                 if (e.marker != Inset::NO_MARKER)
98                         e.before = max(e.before, 1);
99         }
100
101         // Do not lose spacing allocated to extremities
102         if (!elements_.empty()) {
103                 elements_[after(0)].before += elements_.front().after;
104                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
105         }
106 }
107
108
109 int MathRow::before(int i) const
110 {
111         do
112                 --i;
113         while (elements_[i].mclass == MC_UNKNOWN);
114
115         return i;
116 }
117
118
119 int MathRow::after(int i) const
120 {
121         do
122                 ++i;
123         while (elements_[i].mclass == MC_UNKNOWN);
124
125         return i;
126 }
127
128
129 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
130 {
131         dim.asc = 0;
132         dim.wid = 0;
133         // In order to compute the dimension of macros and their
134         // arguments, it is necessary to keep track of them.
135         vector<pair<InsetMath const *, Dimension>> dim_insets;
136         vector<pair<MathData const *, Dimension>> dim_arrays;
137         CoordCache & coords = mi.base.bv->coordCache();
138         for (Element const & e : elements_) {
139                 mi.base.macro_nesting = e.macro_nesting;
140                 Dimension d;
141                 switch (e.type) {
142                 case DUMMY:
143                         break;
144                 case INSET:
145                         e.inset->metrics(mi, d);
146                         d.wid += e.before + e.after;
147                         coords.insets().add(e.inset, d);
148                         break;
149                 case BEGIN:
150                         if (e.inset) {
151                                 dim_insets.push_back(make_pair(e.inset, Dimension()));
152                                 e.inset->beforeMetrics();
153                         }
154                         if (e.ar)
155                                 dim_arrays.push_back(make_pair(e.ar, Dimension()));
156                         break;
157                 case END:
158                         if (e.inset) {
159                                 e.inset->afterMetrics();
160                                 LATTEST(dim_insets.back().first == e.inset);
161                                 coords.insets().add(e.inset, dim_insets.back().second);
162                                 dim_insets.pop_back();
163                         }
164                         if (e.ar) {
165                                 LATTEST(dim_arrays.back().first == e.ar);
166                                 coords.arrays().add(e.ar, dim_arrays.back().second);
167                                 dim_arrays.pop_back();
168                         }
169                         break;
170                 case BOX:
171                         d = theFontMetrics(mi.base.font).dimension('I');
172                         if (e.color != Color_none) {
173                                 // allow for one pixel before/after the box.
174                                 d.wid += e.before + e.after + 2;
175                         } else {
176                                 // hide the box, but give it some height
177                                 d.wid = 0;
178                         }
179                         break;
180                 }
181
182                 // handle vertical space for markers
183                 switch(e.marker) {
184                 case InsetMath::NO_MARKER:
185                         break;
186                 case InsetMath::MARKER:
187                         ++d.des;
188                         break;
189                 case InsetMath::MARKER2:
190                         ++d.asc;
191                         ++d.des;
192                 }
193
194                 if (!d.empty()) {
195                         dim += d;
196                         // Now add the dimension to current macros and arguments.
197                         for (auto & dim_macro : dim_insets)
198                                 dim_macro.second += d;
199                         for (auto & dim_array : dim_arrays)
200                                 dim_array.second += d;
201                 }
202
203                 if (e.compl_text.empty())
204                         continue;
205                 FontInfo font = mi.base.font;
206                 augmentFont(font, "mathnormal");
207                 dim.wid += mathed_string_width(font, e.compl_text);
208         }
209         LATTEST(dim_insets.empty() && dim_arrays.empty());
210 }
211
212
213 namespace {
214
215 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
216 {
217         if (e.marker == InsetMath::NO_MARKER)
218                 return;
219
220         CoordCache const & coords = pi.base.bv->coordCache();
221         Dimension const dim = coords.getInsets().dim(e.inset);
222
223         // the marker is before/after the inset. Normally some space has been reserved already.
224         int const l = x + e.before - 1;
225         int const r = x + dim.width() - e.after;
226
227         // Duplicated from Inset.cpp and adapted. It is believed that the
228         // Inset version should die eventually
229         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
230                 Color_mathframe : Color_mathcorners;
231
232         int const d = y + dim.descent();
233         pi.pain.line(l, d - 3, l, d, pen_color);
234         pi.pain.line(r, d - 3, r, d, pen_color);
235         pi.pain.line(l, d, l + 3, d, pen_color);
236         pi.pain.line(r - 3, d, r, d, pen_color);
237
238         if (e.marker == InsetMath::MARKER)
239                 return;
240
241         int const a = y - dim.ascent();
242         pi.pain.line(l, a + 3, l, a, pen_color);
243         pi.pain.line(r, a + 3, r, a, pen_color);
244         pi.pain.line(l, a, l + 3, a, pen_color);
245         pi.pain.line(r - 3, a, r, a, pen_color);
246 }
247
248 }
249
250 void MathRow::draw(PainterInfo & pi, int x, int const y) const
251 {
252         CoordCache & coords = pi.base.bv->coordCache();
253         for (Element const & e : elements_) {
254                 switch (e.type) {
255                 case INSET: {
256                         // This is hackish: the math inset does not know that space
257                         // has been added before and after it; we alter its dimension
258                         // while it is drawing, because it relies on this value.
259                         Dimension const d = coords.insets().dim(e.inset);
260                         Dimension d2 = d;
261                         d2.wid -= e.before + e.after;
262                         coords.insets().add(e.inset, d2);
263                         e.inset->drawSelection(pi, x + e.before, y);
264                         e.inset->draw(pi, x + e.before, y);
265                         coords.insets().add(e.inset, x, y);
266                         coords.insets().add(e.inset, d);
267                         drawMarkers(pi, e, x, y);
268                         x += d.wid;
269                         break;
270                 }
271                 case BEGIN:
272                         if (e.inset) {
273                                 coords.insets().add(e.inset, x, y);
274                                 drawMarkers(pi, e, x, y);
275                                 e.inset->beforeDraw(pi);
276                         }
277                         if (e.ar)
278                                 coords.arrays().add(e.ar, x, y);
279                         break;
280                 case END:
281                         if (e.inset)
282                                 e.inset->afterDraw(pi);
283                         break;
284                 case BOX: {
285                         if (e.color == Color_none)
286                                 break;
287                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
288                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
289                                           d.width() - 1, d.height() - 1, e.color);
290                         x += d.wid + 2 + e.before + e.after;
291                         break;
292                 }
293                 case DUMMY:
294                         break;
295                 }
296
297                 if (e.compl_text.empty())
298                         continue;
299                 FontInfo f = pi.base.font;
300                 augmentFont(f, "mathnormal");
301
302                 // draw the unique and the non-unique completion part
303                 // Note: this is not time-critical as it is
304                 // only done once per screen.
305                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
306                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
307
308                 if (!s1.empty()) {
309                         f.setColor(Color_inlinecompletion);
310                         pi.pain.text(x, y, s1, f);
311                         x += mathed_string_width(f, s1);
312                 }
313                 if (!s2.empty()) {
314                         f.setColor(Color_nonunique_inlinecompletion);
315                         pi.pain.text(x, y, s2, f);
316                         x += mathed_string_width(f, s2);
317                 }
318         }
319 }
320
321
322 int MathRow::kerning(BufferView const * bv) const
323 {
324         if (elements_.empty())
325                 return 0;
326         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
327         return inset ? inset->kerning(bv) : 0;
328 }
329
330
331 ostream & operator<<(ostream & os, MathRow::Element const & e)
332 {
333         switch (e.type) {
334         case MathRow::DUMMY:
335                 os << (e.mclass == MC_OPEN ? "{" : "}");
336                 break;
337         case MathRow::INSET:
338                 os << "<" << e.before << "-"
339                    << to_utf8(class_to_string(e.mclass))
340                    << "-" << e.after << ">";
341                 break;
342         case MathRow::BEGIN:
343                 if (e.inset)
344                         os << "\\" << to_utf8(e.inset->name())
345                            << "^" << e.macro_nesting << "[";
346                 if (e.ar)
347                         os << "(";
348                 break;
349         case MathRow::END:
350                 if (e.ar)
351                         os << ")";
352                 if (e.inset)
353                         os << "]";
354                 break;
355         case MathRow::BOX:
356                 os << "<" << e.before << "-[]-" << e.after << ">";
357                 break;
358         }
359         return os;
360 }
361
362
363 ostream & operator<<(ostream & os, MathRow const & mrow)
364 {
365         for (MathRow::Element const & e : mrow)
366                 os << e << "  ";
367         return os;
368 }
369
370 } // namespace lyx