]> git.lyx.org Git - features.git/blob - src/mathed/MathRow.cpp
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 != InsetMath::NO_MARKER)
94                         bef.after = max(bef.after, 1);
95                 if (e.mclass != MC_UNKNOWN && e.marker != InsetMath::NO_MARKER)
96                         e.before = max(e.before, 1);
97                 // for linearized insets (macros...) too
98                 if (e.type == BEGIN && e.marker != InsetMath::NO_MARKER)
99                         bef.after = max(bef.after, 1);
100                 if (e.type == END && e.marker != InsetMath::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 afterMetricsMarkers(MetricsInfo const & mi, MathRow::Element & 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                 break;
150         case InsetMath::BOX_MARKER:
151                 FontInfo font = mi.base.font;
152                 augmentFont(font, "lyxtex");
153                 font.setSize(FONT_SIZE_TINY);
154                 Dimension namedim;
155                 mathed_string_dim(font, e.inset->name(), namedim);
156                 int const namewid = 1 + namedim.wid + 1;
157                 dim.wid += 2;
158                 if (namewid > dim.wid)
159                         e.after += namewid - dim.wid;
160                 dim.asc += 2;
161                 dim.des += 2 + namedim.height();
162         }
163 }
164
165 }
166
167
168 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
169 {
170         dim.asc = 0;
171         dim.wid = 0;
172         // In order to compute the dimension of macros and their
173         // arguments, it is necessary to keep track of them.
174         vector<pair<InsetMath const *, Dimension>> dim_insets;
175         vector<pair<MathData const *, Dimension>> dim_arrays;
176         CoordCache & coords = mi.base.bv->coordCache();
177         for (Element & e : elements_) {
178                 mi.base.macro_nesting = e.macro_nesting;
179                 Dimension d;
180                 switch (e.type) {
181                 case DUMMY:
182                         break;
183                 case INSET:
184                         e.inset->metrics(mi, d);
185                         d.wid += e.before + e.after;
186                         coords.insets().add(e.inset, d);
187                         break;
188                 case BEGIN:
189                         if (e.inset) {
190                                 dim_insets.push_back(make_pair(e.inset, Dimension()));
191                                 dim_insets.back().second.wid += e.before + e.after;
192                                 d.wid = e.before + e.after;
193                                 e.inset->beforeMetrics();
194                         }
195                         if (e.ar)
196                                 dim_arrays.push_back(make_pair(e.ar, Dimension()));
197                         break;
198                 case END:
199                         if (e.inset) {
200                                 e.inset->afterMetrics();
201                                 LATTEST(dim_insets.back().first == e.inset);
202                                 d = dim_insets.back().second;
203                                 afterMetricsMarkers(mi, e, d);
204                                 d.wid += e.before + e.after;
205                                 coords.insets().add(e.inset, d);
206                                 dim_insets.pop_back();
207                                 // We do not want to count the width again, but the
208                                 // padding and the vertical dimension are meaningful.
209                                 d.wid = e.before + e.after;
210                         }
211                         if (e.ar) {
212                                 LATTEST(dim_arrays.back().first == e.ar);
213                                 coords.arrays().add(e.ar, dim_arrays.back().second);
214                                 dim_arrays.pop_back();
215                         }
216                         break;
217                 case BOX:
218                         d = theFontMetrics(mi.base.font).dimension('I');
219                         if (e.color != Color_none) {
220                                 // allow for one pixel before/after the box.
221                                 d.wid += e.before + e.after + 2;
222                         } else {
223                                 // hide the box, but keep its height
224                                 d.wid = 0;
225                         }
226                         break;
227                 }
228
229                 if (!d.empty()) {
230                         dim += d;
231                         // Now add the dimension to current macros and arguments.
232                         for (auto & dim_macro : dim_insets)
233                                 dim_macro.second += d;
234                         for (auto & dim_array : dim_arrays)
235                                 dim_array.second += d;
236                 }
237
238                 if (e.compl_text.empty())
239                         continue;
240                 FontInfo font = mi.base.font;
241                 augmentFont(font, "mathnormal");
242                 dim.wid += mathed_string_width(font, e.compl_text);
243         }
244         LATTEST(dim_insets.empty() && dim_arrays.empty());
245 }
246
247
248 namespace {
249
250 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
251 {
252         if (e.marker == InsetMath::NO_MARKER)
253                 return;
254
255         CoordCache const & coords = pi.base.bv->coordCache();
256         Dimension const dim = coords.getInsets().dim(e.inset);
257
258         // the marker is before/after the inset. Normally some space has been reserved already.
259         int const l = x + e.before - 1;
260         int const r = x + dim.width() - e.after;
261
262         if (e.marker == InsetMath::BOX_MARKER) {
263                 // draw header and rectangle around
264                 FontInfo font = pi.base.font;
265                 augmentFont(font, "lyxtex");
266                 font.setSize(FONT_SIZE_TINY);
267                 font.setColor(Color_mathmacrolabel);
268                 Dimension namedim;
269                 mathed_string_dim(font, e.inset->name(), namedim);
270                 pi.pain.rectangle(l, y - dim.asc, dim.wid,
271                                   dim.height(), Color_mathmacroframe);
272                 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
273                                       dim.wid, namedim.height() + 2, Color_mathmacrobg);
274                 pi.pain.text(l + 1, y + dim.des - namedim.des - 1, e.inset->name(), font);
275                 return;
276         }
277
278         // Duplicated from Inset.cpp and adapted. It is believed that the
279         // Inset version should die eventually
280         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
281                 Color_mathframe : Color_mathcorners;
282
283         int const d = y + dim.descent();
284         pi.pain.line(l, d - 3, l, d, pen_color);
285         pi.pain.line(r, d - 3, r, d, pen_color);
286         pi.pain.line(l, d, l + 3, d, pen_color);
287         pi.pain.line(r - 3, d, r, d, pen_color);
288
289         if (e.marker == InsetMath::MARKER)
290                 return;
291
292         int const a = y - dim.ascent();
293         pi.pain.line(l, a + 3, l, a, pen_color);
294         pi.pain.line(r, a + 3, r, a, pen_color);
295         pi.pain.line(l, a, l + 3, a, pen_color);
296         pi.pain.line(r - 3, a, r, a, pen_color);
297 }
298
299 }
300
301 void MathRow::draw(PainterInfo & pi, int x, int const y) const
302 {
303         CoordCache & coords = pi.base.bv->coordCache();
304         for (Element const & e : elements_) {
305                 switch (e.type) {
306                 case INSET: {
307                         // This is hackish: the math inset does not know that space
308                         // has been added before and after it; we alter its dimension
309                         // while it is drawing, because it relies on this value.
310                         Dimension const d = coords.insets().dim(e.inset);
311                         Dimension d2 = d;
312                         d2.wid -= e.before + e.after;
313                         coords.insets().add(e.inset, d2);
314                         e.inset->drawSelection(pi, x + e.before, y);
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.inset) {
324                                 coords.insets().add(e.inset, x, y);
325                                 drawMarkers(pi, e, x, y);
326                                 e.inset->beforeDraw(pi);
327                         }
328                         x += e.before + e.after;
329                         if (e.ar)
330                                 coords.arrays().add(e.ar, x, y);
331                         break;
332                 case END:
333                         if (e.inset)
334                                 e.inset->afterDraw(pi);
335                         x += e.before + e.after;
336                         break;
337                 case BOX: {
338                         if (e.color == Color_none)
339                                 break;
340                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
341                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
342                                           d.width() - 1, d.height() - 1, e.color);
343                         x += d.wid + 2 + e.before + e.after;
344                         break;
345                 }
346                 case DUMMY:
347                         break;
348                 }
349
350                 if (e.compl_text.empty())
351                         continue;
352                 FontInfo f = pi.base.font;
353                 augmentFont(f, "mathnormal");
354
355                 // draw the unique and the non-unique completion part
356                 // Note: this is not time-critical as it is
357                 // only done once per screen.
358                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
359                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
360
361                 if (!s1.empty()) {
362                         f.setColor(Color_inlinecompletion);
363                         pi.pain.text(x, y, s1, f);
364                         x += mathed_string_width(f, s1);
365                 }
366                 if (!s2.empty()) {
367                         f.setColor(Color_nonunique_inlinecompletion);
368                         pi.pain.text(x, y, s2, f);
369                         x += mathed_string_width(f, s2);
370                 }
371         }
372 }
373
374
375 int MathRow::kerning(BufferView const * bv) const
376 {
377         if (elements_.empty())
378                 return 0;
379         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
380         return inset ? inset->kerning(bv) : 0;
381 }
382
383
384 ostream & operator<<(ostream & os, MathRow::Element const & e)
385 {
386         switch (e.type) {
387         case MathRow::DUMMY:
388                 os << (e.mclass == MC_OPEN ? "{" : "}");
389                 break;
390         case MathRow::INSET:
391                 os << "<" << e.before << "-"
392                    << to_utf8(class_to_string(e.mclass))
393                    << "-" << e.after << ">";
394                 break;
395         case MathRow::BEGIN:
396                 if (e.inset)
397                         os << "\\" << to_utf8(e.inset->name())
398                            << "^" << e.macro_nesting << "[";
399                 if (e.ar)
400                         os << "(";
401                 break;
402         case MathRow::END:
403                 if (e.ar)
404                         os << ")";
405                 if (e.inset)
406                         os << "]";
407                 break;
408         case MathRow::BOX:
409                 os << "<" << e.before << "-[]-" << e.after << ">";
410                 break;
411         }
412         return os;
413 }
414
415
416 ostream & operator<<(ostream & os, MathRow const & mrow)
417 {
418         for (MathRow::Element const & e : mrow)
419                 os << e << "  ";
420         return os;
421 }
422
423 } // namespace lyx