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