]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
365e802cec1746f889cc362322918c267e47593e
[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
126         if (e.marker == InsetMath::MARKER
127             || e.marker == InsetMath::MARKER2) {
128                 int const d = y + dim.descent();
129                 pi.pain.line(l, d - 3, l, d, pen_color);
130                 pi.pain.line(r, d - 3, r, d, pen_color);
131                 pi.pain.line(l, d, l + 3, d, pen_color);
132                 pi.pain.line(r - 3, d, r, d, pen_color);
133         }
134
135         // Upper corners
136         if (e.marker == InsetMath::BOX_MARKER
137             || e.marker == InsetMath::MARKER2) {
138                 int const a = y - dim.ascent();
139                 pi.pain.line(l, a + 3, l, a, pen_color);
140                 pi.pain.line(r, a + 3, r, a, pen_color);
141                 pi.pain.line(l, a, l + 3, a, pen_color);
142                 pi.pain.line(r - 3, a, r, a, pen_color);
143         }
144 }
145
146 }
147
148
149 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
150 {
151         // First there is a dummy element of type "open"
152         push_back(Element(mi, DUMMY, MC_OPEN));
153
154         // Then insert the MathData argument
155         bool const has_contents = ar->addToMathRow(*this, mi);
156
157         // A MathRow should not be completely empty
158         if (!has_contents) {
159                 Element e(mi, BOX, MC_ORD);
160                 // empty arrays are visible when they are editable
161                 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
162                 push_back(e);
163         }
164
165         // Finally there is a dummy element of type "close"
166         push_back(Element(mi, DUMMY, MC_CLOSE));
167
168         /* Do spacing only in math mode. This test is a bit clumsy,
169          * but it is used in other places for guessing the current mode.
170          */
171         bool const dospacing = isMathFont(mi.base.fontname);
172
173         // update classes
174         if (dospacing) {
175                 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
176                         if (elements_[i].mclass != MC_UNKNOWN)
177                                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
178                                                          elements_[after(i)].mclass);
179                 }
180         }
181
182         // set spacing
183         // We go to the end to handle spacing at the end of equation
184         for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
185                 Element & e = elements_[i];
186
187                 Element & bef = elements_[before(i)];
188                 if (dospacing && e.mclass != MC_UNKNOWN) {
189                         int spc = class_spacing(bef.mclass, e.mclass, mi.base);
190                         bef.after += spc / 2;
191                         // this is better than spc / 2 to avoid rounding problems
192                         e.before += spc - spc / 2;
193                 }
194
195                 // finally reserve space for markers
196                 bef.after = max(bef.after, markerMargin(bef));
197                 if (e.mclass != MC_UNKNOWN)
198                         e.before = max(e.before, markerMargin(e));
199                 // for linearized insets (macros...) too
200                 if (e.type == BEGIN)
201                         bef.after = max(bef.after, markerMargin(e));
202                 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
203                         Element & aft = elements_[after(i)];
204                         aft.before = max(aft.before, markerMargin(e));
205                 }
206         }
207
208         // Do not lose spacing allocated to extremities
209         if (!elements_.empty()) {
210                 elements_[after(0)].before += elements_.front().after;
211                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
212         }
213 }
214
215
216 int MathRow::before(int i) const
217 {
218         do
219                 --i;
220         while (elements_[i].mclass == MC_UNKNOWN);
221
222         return i;
223 }
224
225
226 int MathRow::after(int i) const
227 {
228         do
229                 ++i;
230         while (elements_[i].mclass == MC_UNKNOWN);
231
232         return i;
233 }
234
235
236 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
237 {
238         dim.asc = 0;
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                         Dimension const d = coords.insets().dim(e.inset);
326                         Dimension d2 = d;
327                         d2.wid -= e.before + e.after;
328                         coords.insets().add(e.inset, d2);
329                         e.inset->draw(pi, x + e.before, y);
330                         coords.insets().add(e.inset, x, y);
331                         coords.insets().add(e.inset, d);
332                         drawMarkers(pi, e, x, y);
333                         x += d.wid;
334                         break;
335                 }
336                 case BEGIN:
337                         if (e.ar) {
338                                 coords.arrays().add(e.ar, x, y);
339                                 e.ar->drawSelection(pi, x, y);
340                         }
341                         if (e.inset) {
342                                 coords.insets().add(e.inset, x, y);
343                                 drawMarkers(pi, e, x, y);
344                                 e.inset->beforeDraw(pi);
345                         }
346                         x += e.before + e.after;
347                         break;
348                 case END:
349                         if (e.inset)
350                                 e.inset->afterDraw(pi);
351                         x += e.before + e.after;
352                         break;
353                 case BOX: {
354                         if (e.color == Color_none)
355                                 break;
356                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
357                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
358                                           d.width() - 1, d.height() - 1, e.color);
359                         x += d.wid + 2 + e.before + e.after;
360                         break;
361                 }
362                 case DUMMY:
363                         break;
364                 }
365
366                 if (e.compl_text.empty())
367                         continue;
368                 FontInfo f = pi.base.font;
369                 augmentFont(f, "mathnormal");
370
371                 // draw the unique and the non-unique completion part
372                 // Note: this is not time-critical as it is
373                 // only done once per screen.
374                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
375                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
376
377                 if (!s1.empty()) {
378                         f.setColor(Color_inlinecompletion);
379                         pi.pain.text(x, y, s1, f);
380                         x += mathed_string_width(f, s1);
381                 }
382                 if (!s2.empty()) {
383                         f.setColor(Color_nonunique_inlinecompletion);
384                         pi.pain.text(x, y, s2, f);
385                         x += mathed_string_width(f, s2);
386                 }
387         }
388 }
389
390
391 int MathRow::kerning(BufferView const * bv) const
392 {
393         if (elements_.empty())
394                 return 0;
395         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
396         return inset ? inset->kerning(bv) : 0;
397 }
398
399
400 ostream & operator<<(ostream & os, MathRow::Element const & e)
401 {
402         switch (e.type) {
403         case MathRow::DUMMY:
404                 os << (e.mclass == MC_OPEN ? "{" : "}");
405                 break;
406         case MathRow::INSET:
407                 os << "<" << e.before << "-"
408                    << to_utf8(class_to_string(e.mclass))
409                    << "-" << e.after << ">";
410                 break;
411         case MathRow::BEGIN:
412                 if (e.inset)
413                         os << "\\" << to_utf8(e.inset->name())
414                            << "^" << e.macro_nesting << "[";
415                 if (e.ar)
416                         os << "(";
417                 break;
418         case MathRow::END:
419                 if (e.ar)
420                         os << ")";
421                 if (e.inset)
422                         os << "]";
423                 break;
424         case MathRow::BOX:
425                 os << "<" << e.before << "-[]-" << e.after << ">";
426                 break;
427         }
428         return os;
429 }
430
431
432 ostream & operator<<(ostream & os, MathRow const & mrow)
433 {
434         for (MathRow::Element const & e : mrow)
435                 os << e << "  ";
436         return os;
437 }
438
439 } // namespace lyx