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