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