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