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