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