]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
Rewrite handling of macro nesting in math rows
[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(Type t)
40         : type(t), inset(0), mclass(MC_ORD), before(0), after(0),
41           compl_unique_to(0), macro(0), 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(BEGIN));
49         back().mclass = MC_OPEN;
50
51         // Then insert the MathData argument
52         bool const has_contents = ar->addToMathRow(*this, mi);
53
54         // empty arrays are visible when they are editable
55         // we reserve the necessary space anyway (even if nothing gets drawn)
56         if (!has_contents) {
57                 Element e(BOX);
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(END));
64         back().mclass = MC_CLOSE;
65
66         /* Do spacing only in math mode. This test is a bit clumsy,
67          * but it is used in other places for guessing the current mode.
68          */
69         if (!isMathFont(mi.base.fontname))
70                 return;
71
72         // update classes
73         for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
74                 if (elements_[i].type != INSET)
75                         continue;
76                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
77                              elements_[after(i)].mclass);
78         }
79
80         // set spacing
81         // We go to the end to handle spacing at the end of equation
82         for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
83                 if (elements_[i].type != INSET)
84                         continue;
85                 Element & bef = elements_[before(i)];
86                 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
87                 bef.after = spc / 2;
88                 // this is better than spc / 2 to avoid rounding problems
89                 elements_[i].before = spc - spc / 2;
90         }
91         // Do not lose spacing allocated to extremities
92         if (!elements_.empty()) {
93                 elements_[after(0)].before += elements_.front().after;
94                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
95         }
96 }
97
98
99 int MathRow::before(int i) const
100 {
101         do
102                 --i;
103         while (elements_[i].type != BEGIN
104                    && elements_[i].type != INSET);
105
106         return i;
107 }
108
109
110 int MathRow::after(int i) const
111 {
112         do
113                 ++i;
114         while (elements_[i].type != END
115                    && elements_[i].type != INSET);
116
117         return i;
118 }
119
120
121 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
122 {
123         dim.asc = 0;
124         dim.wid = 0;
125         // In order to compute the dimension of macros and their
126         // arguments, it is necessary to keep track of them.
127         map<MathMacro const *, Dimension> dim_macros;
128         map<MathData const *, Dimension> dim_arrays;
129         // this vector remembers the stack of macro nesting values
130         vector<int> macro_nesting;
131         macro_nesting.push_back(mi.base.macro_nesting);
132         CoordCache & coords = mi.base.bv->coordCache();
133         for (Element const & e : elements_) {
134                 mi.base.macro_nesting = macro_nesting.back();
135                 Dimension d;
136                 switch (e.type) {
137                 case BEGIN:
138                 case END:
139                         break;
140                 case INSET:
141                         e.inset->metrics(mi, d);
142                         d.wid += e.before + e.after;
143                         coords.insets().add(e.inset, d);
144                         break;
145                 case BEG_MACRO:
146                         macro_nesting.push_back(e.macro->nesting());
147                         e.macro->macro()->lock();
148                         // Add a macro to current list
149                         dim_macros[e.macro] = Dimension();
150                         break;
151                 case END_MACRO:
152                         LATTEST(dim_macros.find(e.macro) != dim_macros.end());
153                         macro_nesting.pop_back();
154                         e.macro->macro()->unlock();
155                         // Cache the dimension of the macro and remove it from
156                         // tracking map.
157                         coords.insets().add(e.macro, dim_macros[e.macro]);
158                         dim_macros.erase(e.macro);
159                         break;
160                         // This is basically like macros
161                 case BEG_ARG:
162                         if (e.macro) {
163                                 macro_nesting.push_back(e.macro->nesting());
164                                 e.macro->macro()->unlock();
165                         }
166                         dim_arrays[e.ar] = Dimension();
167                         break;
168                 case END_ARG:
169                         LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
170                         if (e.macro) {
171                                 macro_nesting.pop_back();
172                                 e.macro->macro()->lock();
173                         }
174                         coords.arrays().add(e.ar, dim_arrays[e.ar]);
175                         dim_arrays.erase(e.ar);
176                         break;
177                 case BOX:
178                         d = theFontMetrics(mi.base.font).dimension('I');
179                         d.wid += e.before + e.after;
180                         break;
181                 }
182
183                 if (!d.empty()) {
184                         dim += d;
185                         // Now add the dimension to current macros and arguments.
186                         for (auto & dim_macro : dim_macros)
187                                 dim_macro.second += d;
188                         for (auto & dim_array : dim_arrays)
189                                 dim_array.second += d;
190                 }
191
192                 if (e.compl_text.empty())
193                         continue;
194                 FontInfo font = mi.base.font;
195                 augmentFont(font, "mathnormal");
196                 dim.wid += mathed_string_width(font, e.compl_text);
197         }
198         LATTEST(dim_macros.empty() && dim_arrays.empty());
199 }
200
201
202 void MathRow::draw(PainterInfo & pi, int x, int const y) const
203 {
204         CoordCache & coords = pi.base.bv->coordCache();
205         for (Element const & e : elements_) {
206                 switch (e.type) {
207                 case INSET: {
208                         // This is hackish: the math inset does not know that space
209                         // has been added before and after it; we alter its dimension
210                         // while it is drawing, because it relies on this value.
211                         Dimension const d = coords.insets().dim(e.inset);
212                         Dimension d2 = d;
213                         d2.wid -= e.before + e.after;
214                         coords.insets().add(e.inset, d2);
215                         e.inset->drawSelection(pi, x + e.before, y);
216                         e.inset->draw(pi, x + e.before, y);
217                         coords.insets().add(e.inset, x, y);
218                         coords.insets().add(e.inset, d);
219                         x += d.wid;
220                         break;
221                 }
222                 case BEG_MACRO:
223                         coords.insets().add(e.macro, x, y);
224                         break;
225                 case BEG_ARG:
226                         coords.arrays().add(e.ar, x, y);
227                         // if the macro is being edited, then the painter is in
228                         // monochrome mode.
229                         if (e.macro->editMetrics(pi.base.bv))
230                                 pi.pain.leaveMonochromeMode();
231                         break;
232                 case END_ARG:
233                         if (e.macro->editMetrics(pi.base.bv))
234                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
235                         break;
236                 case BOX: {
237                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
238                         // the box is not visible in non-editable context (except for grey macro boxes).
239                         if (e.color != Color_none)
240                                 pi.pain.rectangle(x + e.before, y - d.ascent(),
241                                                   d.width(), d.height(), e.color);
242                         x += d.wid;
243                         break;
244                 }
245                 case BEGIN:
246                 case END:
247                 case END_MACRO:
248                         break;
249                 }
250
251                 if (e.compl_text.empty())
252                         continue;
253                 FontInfo f = pi.base.font;
254                 augmentFont(f, "mathnormal");
255
256                 // draw the unique and the non-unique completion part
257                 // Note: this is not time-critical as it is
258                 // only done once per screen.
259                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
260                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
261
262                 if (!s1.empty()) {
263                         f.setColor(Color_inlinecompletion);
264                         pi.pain.text(x, y, s1, f);
265                         x += mathed_string_width(f, s1);
266                 }
267                 if (!s2.empty()) {
268                         f.setColor(Color_nonunique_inlinecompletion);
269                         pi.pain.text(x, y, s2, f);
270                         x += mathed_string_width(f, s2);
271                 }
272         }
273 }
274
275
276 int MathRow::kerning(BufferView const * bv) const
277 {
278         if (elements_.empty())
279                 return 0;
280         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
281         return inset ? inset->kerning(bv) : 0;
282 }
283
284
285 ostream & operator<<(ostream & os, MathRow::Element const & e)
286 {
287         switch (e.type) {
288         case MathRow::BEGIN:
289                 os << "{";
290                 break;
291         case MathRow::END:
292                 os << "}";
293                 break;
294         case MathRow::INSET:
295                 os << "<" << e.before << "-"
296                    << to_utf8(class_to_string(e.mclass))
297                    << "-" << e.after << ">";
298                 break;
299         case MathRow::BEG_MACRO:
300                 os << "\\" << to_utf8(e.macro->name())
301                    << "^" << e.macro->nesting() << "[";
302                 break;
303         case MathRow::END_MACRO:
304                 os << "]";
305                 break;
306         case MathRow::BEG_ARG:
307                 os << "#(";
308                 break;
309         case MathRow::END_ARG:
310                 os << ")";
311                 break;
312         case MathRow::BOX:
313                 os << "@";
314                 break;
315         }
316         return os;
317 }
318
319
320 ostream & operator<<(ostream & os, MathRow const & mrow)
321 {
322         for (MathRow::Element const & e : mrow)
323                 os << e << "  ";
324         return os;
325 }
326
327 } // namespace lyx