]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
Linearize macros also when editing them
[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 "MathMacro.h"
19 #include "MathSupport.h"
20
21 #include "BufferView.h"
22 #include "CoordCache.h"
23 #include "MetricsInfo.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 <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),
42           macro(0), 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                 if (e.mclass == MC_UNKNOWN)
85                         continue;
86
87                 Element & bef = elements_[before(i)];
88                 if (dospacing) {
89                         int spc = class_spacing(bef.mclass, e.mclass, mi.base);
90                         bef.after += spc / 2;
91                         // this is better than spc / 2 to avoid rounding problems
92                         e.before += spc - spc / 2;
93                 }
94
95                 // finally reserve space for markers
96                 if (bef.marker != Inset::NO_MARKER)
97                         bef.after = max(bef.after, 1);
98                 if (e.marker != Inset::NO_MARKER)
99                         e.before = max(e.before, 1);
100         }
101
102         // Do not lose spacing allocated to extremities
103         if (!elements_.empty()) {
104                 elements_[after(0)].before += elements_.front().after;
105                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
106         }
107 }
108
109
110 int MathRow::before(int i) const
111 {
112         do
113                 --i;
114         while (elements_[i].mclass == MC_UNKNOWN);
115
116         return i;
117 }
118
119
120 int MathRow::after(int i) const
121 {
122         do
123                 ++i;
124         while (elements_[i].mclass == MC_UNKNOWN);
125
126         return i;
127 }
128
129
130 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         dim.asc = 0;
133         dim.wid = 0;
134         // In order to compute the dimension of macros and their
135         // arguments, it is necessary to keep track of them.
136         map<MathMacro const *, Dimension> dim_macros;
137         map<MathData const *, Dimension> dim_arrays;
138         CoordCache & coords = mi.base.bv->coordCache();
139         for (Element const & e : elements_) {
140                 mi.base.macro_nesting = e.macro_nesting;
141                 Dimension d;
142                 switch (e.type) {
143                 case DUMMY:
144                         break;
145                 case INSET:
146                         e.inset->metrics(mi, d);
147                         d.wid += e.before + e.after;
148                         coords.insets().add(e.inset, d);
149                         break;
150                 case BEG_MACRO:
151                         e.macro->macro()->lock();
152                         // Add a macro to current list
153                         dim_macros[e.macro] = Dimension();
154                         break;
155                 case END_MACRO:
156                         LATTEST(dim_macros.find(e.macro) != dim_macros.end());
157                         e.macro->macro()->unlock();
158                         // Cache the dimension of the macro and remove it from
159                         // tracking map.
160                         coords.insets().add(e.macro, dim_macros[e.macro]);
161                         dim_macros.erase(e.macro);
162                         break;
163                         // This is basically like macros
164                 case BEG_ARG:
165                         if (e.macro)
166                                 e.macro->macro()->unlock();
167                         dim_arrays[e.ar] = Dimension();
168                         break;
169                 case END_ARG:
170                         LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
171                         if (e.macro)
172                                 e.macro->macro()->lock();
173                         coords.arrays().add(e.ar, dim_arrays[e.ar]);
174                         dim_arrays.erase(e.ar);
175                         break;
176                 case BOX:
177                         d = theFontMetrics(mi.base.font).dimension('I');
178                         if (e.color != Color_none) {
179                                 // allow for one pixel before/after the box.
180                                 d.wid += e.before + e.after + 2;
181                         } else {
182                                 // hide the box, but give it some height
183                                 d.wid = 0;
184                         }
185                         break;
186                 }
187
188                 // handle vertical space for markers
189                 switch(e.marker) {
190                 case InsetMath::NO_MARKER:
191                         break;
192                 case InsetMath::MARKER:
193                         ++d.des;
194                         break;
195                 case InsetMath::MARKER2:
196                         ++d.asc;
197                         ++d.des;
198                 }
199
200                 if (!d.empty()) {
201                         dim += d;
202                         // Now add the dimension to current macros and arguments.
203                         for (auto & dim_macro : dim_macros)
204                                 dim_macro.second += d;
205                         for (auto & dim_array : dim_arrays)
206                                 dim_array.second += d;
207                 }
208
209                 if (e.compl_text.empty())
210                         continue;
211                 FontInfo font = mi.base.font;
212                 augmentFont(font, "mathnormal");
213                 dim.wid += mathed_string_width(font, e.compl_text);
214         }
215         LATTEST(dim_macros.empty() && dim_arrays.empty());
216 }
217
218
219 namespace {
220
221 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
222 {
223         if (e.marker == InsetMath::NO_MARKER)
224                 return;
225
226         CoordCache const & coords = pi.base.bv->coordCache();
227         Dimension const dim = coords.getInsets().dim(e.inset);
228
229         // the marker is before/after the inset. Normally some space has been reserved already.
230         int const l = x + e.before - 1;
231         int const r = x + dim.width() - e.after;
232
233         // Duplicated from Inset.cpp and adapted. It is believed that the
234         // Inset version should die eventually
235         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
236                 Color_mathframe : Color_mathcorners;
237
238         int const d = y + dim.descent();
239         pi.pain.line(l, d - 3, l, d, pen_color);
240         pi.pain.line(r, d - 3, r, d, pen_color);
241         pi.pain.line(l, d, l + 3, d, pen_color);
242         pi.pain.line(r - 3, d, r, d, pen_color);
243
244         if (e.marker == InsetMath::MARKER)
245                 return;
246
247         int const a = y - dim.ascent();
248         pi.pain.line(l, a + 3, l, a, pen_color);
249         pi.pain.line(r, a + 3, r, a, pen_color);
250         pi.pain.line(l, a, l + 3, a, pen_color);
251         pi.pain.line(r - 3, a, r, a, pen_color);
252 }
253
254 }
255
256 void MathRow::draw(PainterInfo & pi, int x, int const y) const
257 {
258         CoordCache & coords = pi.base.bv->coordCache();
259         for (Element const & e : elements_) {
260                 switch (e.type) {
261                 case INSET: {
262                         // This is hackish: the math inset does not know that space
263                         // has been added before and after it; we alter its dimension
264                         // while it is drawing, because it relies on this value.
265                         Dimension const d = coords.insets().dim(e.inset);
266                         Dimension d2 = d;
267                         d2.wid -= e.before + e.after;
268                         coords.insets().add(e.inset, d2);
269                         e.inset->drawSelection(pi, x + e.before, y);
270                         e.inset->draw(pi, x + e.before, y);
271                         coords.insets().add(e.inset, x, y);
272                         coords.insets().add(e.inset, d);
273                         drawMarkers(pi, e, x, y);
274                         x += d.wid;
275                         break;
276                 }
277                 case BEG_MACRO:
278                         coords.insets().add(e.macro, x, y);
279                         drawMarkers(pi, e, x, y);
280                         if (e.macro->editMetrics(pi.base.bv))
281                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
282                         break;
283                 case END_MACRO:
284                         if (e.macro->editMetrics(pi.base.bv))
285                                 pi.pain.leaveMonochromeMode();
286                         break;
287                 case BEG_ARG:
288                         coords.arrays().add(e.ar, x, y);
289                         // if the macro is being edited, then the painter is in
290                         // monochrome mode.
291                         if (e.macro->editMetrics(pi.base.bv))
292                                 pi.pain.leaveMonochromeMode();
293                         break;
294                 case END_ARG:
295                         if (e.macro->editMetrics(pi.base.bv))
296                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
297                         break;
298                 case BOX: {
299                         if (e.color == Color_none)
300                                 break;
301                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
302                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
303                                           d.width() - 1, d.height() - 1, e.color);
304                         x += d.wid + 2 + e.before + e.after;
305                         break;
306                 }
307                 case DUMMY:
308                         break;
309                 }
310
311                 if (e.compl_text.empty())
312                         continue;
313                 FontInfo f = pi.base.font;
314                 augmentFont(f, "mathnormal");
315
316                 // draw the unique and the non-unique completion part
317                 // Note: this is not time-critical as it is
318                 // only done once per screen.
319                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
320                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
321
322                 if (!s1.empty()) {
323                         f.setColor(Color_inlinecompletion);
324                         pi.pain.text(x, y, s1, f);
325                         x += mathed_string_width(f, s1);
326                 }
327                 if (!s2.empty()) {
328                         f.setColor(Color_nonunique_inlinecompletion);
329                         pi.pain.text(x, y, s2, f);
330                         x += mathed_string_width(f, s2);
331                 }
332         }
333 }
334
335
336 int MathRow::kerning(BufferView const * bv) const
337 {
338         if (elements_.empty())
339                 return 0;
340         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
341         return inset ? inset->kerning(bv) : 0;
342 }
343
344
345 ostream & operator<<(ostream & os, MathRow::Element const & e)
346 {
347         switch (e.type) {
348         case MathRow::DUMMY:
349                 os << (e.mclass == MC_OPEN ? "{" : "}");
350                 break;
351         case MathRow::INSET:
352                 os << "<" << e.before << "-"
353                    << to_utf8(class_to_string(e.mclass))
354                    << "-" << e.after << ">";
355                 break;
356         case MathRow::BEG_MACRO:
357                 os << "\\" << to_utf8(e.macro->name())
358                    << "^" << e.macro->nesting() << "[";
359                 break;
360         case MathRow::END_MACRO:
361                 os << "]";
362                 break;
363         case MathRow::BEG_ARG:
364                 os << "#(";
365                 break;
366         case MathRow::END_ARG:
367                 os << ")";
368                 break;
369         case MathRow::BOX:
370                 os << "<" << e.before << "-[]-" << e.after << ">";
371                 break;
372         }
373         return os;
374 }
375
376
377 ostream & operator<<(ostream & os, MathRow const & mrow)
378 {
379         for (MathRow::Element const & e : mrow)
380                 os << e << "  ";
381         return os;
382 }
383
384 } // namespace lyx