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