]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
cfb41642d476d6506cb30f797fa4d7cf6403c8e1
[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         // 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(mi, BOX, MC_ORD);
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                         d.wid += e.before + e.after;
179                         break;
180                 }
181
182                 // handle vertical space for markers
183                 switch(e.marker) {
184                 case InsetMath::NO_MARKER:
185                         break;
186                 case InsetMath::MARKER:
187                         ++d.des;
188                         break;
189                 case InsetMath::MARKER2:
190                         ++d.asc;
191                         ++d.des;
192                 }
193
194                 if (!d.empty()) {
195                         dim += d;
196                         // Now add the dimension to current macros and arguments.
197                         for (auto & dim_macro : dim_macros)
198                                 dim_macro.second += d;
199                         for (auto & dim_array : dim_arrays)
200                                 dim_array.second += d;
201                 }
202
203                 if (e.compl_text.empty())
204                         continue;
205                 FontInfo font = mi.base.font;
206                 augmentFont(font, "mathnormal");
207                 dim.wid += mathed_string_width(font, e.compl_text);
208         }
209         LATTEST(dim_macros.empty() && dim_arrays.empty());
210 }
211
212
213 namespace {
214
215 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
216 {
217         if (e.marker == InsetMath::NO_MARKER)
218                 return;
219
220         CoordCache const & coords = pi.base.bv->coordCache();
221         Dimension const dim = coords.getInsets().dim(e.inset);
222
223         // the marker is before/after the inset. Normally some space has been reserved already.
224         int const l = x + e.before - 1;
225         int const r = x + dim.width() - e.after;
226
227         // Duplicated from Inset.cpp and adapted. It is believed that the
228         // Inset version should die eventually
229         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
230                 Color_mathframe : Color_mathcorners;
231
232         int const d = y + dim.descent();
233         pi.pain.line(l, d - 3, l, d, pen_color);
234         pi.pain.line(r, d - 3, r, d, pen_color);
235         pi.pain.line(l, d, l + 3, d, pen_color);
236         pi.pain.line(r - 3, d, r, d, pen_color);
237
238         if (e.marker == InsetMath::MARKER)
239                 return;
240
241         int const a = y - dim.ascent();
242         pi.pain.line(l, a + 3, l, a, pen_color);
243         pi.pain.line(r, a + 3, r, a, pen_color);
244         pi.pain.line(l, a, l + 3, a, pen_color);
245         pi.pain.line(r - 3, a, r, a, pen_color);
246 }
247
248 }
249
250 void MathRow::draw(PainterInfo & pi, int x, int const y) const
251 {
252         CoordCache & coords = pi.base.bv->coordCache();
253         for (Element const & e : elements_) {
254                 switch (e.type) {
255                 case INSET: {
256                         // This is hackish: the math inset does not know that space
257                         // has been added before and after it; we alter its dimension
258                         // while it is drawing, because it relies on this value.
259                         Dimension const d = coords.insets().dim(e.inset);
260                         Dimension d2 = d;
261                         d2.wid -= e.before + e.after;
262                         coords.insets().add(e.inset, d2);
263                         e.inset->drawSelection(pi, x + e.before, y);
264                         e.inset->draw(pi, x + e.before, y);
265                         coords.insets().add(e.inset, x, y);
266                         coords.insets().add(e.inset, d);
267                         drawMarkers(pi, e, x, y);
268                         x += d.wid;
269                         break;
270                 }
271                 case BEG_MACRO:
272                         coords.insets().add(e.macro, x, y);
273
274                         drawMarkers(pi, e, x, y);
275                         break;
276                 case BEG_ARG:
277                         coords.arrays().add(e.ar, x, y);
278                         // if the macro is being edited, then the painter is in
279                         // monochrome mode.
280                         if (e.macro->editMetrics(pi.base.bv))
281                                 pi.pain.leaveMonochromeMode();
282                         break;
283                 case END_ARG:
284                         if (e.macro->editMetrics(pi.base.bv))
285                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
286                         break;
287                 case BOX: {
288                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
289                         // the box is not visible in non-editable context (except for grey macro boxes).
290                         if (e.color != Color_none)
291                                 pi.pain.rectangle(x + e.before, y - d.ascent(),
292                                                   d.width(), d.height(), e.color);
293                         x += d.wid + e.before + e.after;
294                         break;
295                 }
296                 case DUMMY:
297                 case END_MACRO:
298                         break;
299                 }
300
301                 if (e.compl_text.empty())
302                         continue;
303                 FontInfo f = pi.base.font;
304                 augmentFont(f, "mathnormal");
305
306                 // draw the unique and the non-unique completion part
307                 // Note: this is not time-critical as it is
308                 // only done once per screen.
309                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
310                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
311
312                 if (!s1.empty()) {
313                         f.setColor(Color_inlinecompletion);
314                         pi.pain.text(x, y, s1, f);
315                         x += mathed_string_width(f, s1);
316                 }
317                 if (!s2.empty()) {
318                         f.setColor(Color_nonunique_inlinecompletion);
319                         pi.pain.text(x, y, s2, f);
320                         x += mathed_string_width(f, s2);
321                 }
322         }
323 }
324
325
326 int MathRow::kerning(BufferView const * bv) const
327 {
328         if (elements_.empty())
329                 return 0;
330         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
331         return inset ? inset->kerning(bv) : 0;
332 }
333
334
335 ostream & operator<<(ostream & os, MathRow::Element const & e)
336 {
337         switch (e.type) {
338         case MathRow::DUMMY:
339                 os << (e.mclass == MC_OPEN ? "{" : "}");
340                 break;
341         case MathRow::INSET:
342                 os << "<" << e.before << "-"
343                    << to_utf8(class_to_string(e.mclass))
344                    << "-" << e.after << ">";
345                 break;
346         case MathRow::BEG_MACRO:
347                 os << "\\" << to_utf8(e.macro->name())
348                    << "^" << e.macro->nesting() << "[";
349                 break;
350         case MathRow::END_MACRO:
351                 os << "]";
352                 break;
353         case MathRow::BEG_ARG:
354                 os << "#(";
355                 break;
356         case MathRow::END_ARG:
357                 os << ")";
358                 break;
359         case MathRow::BOX:
360                 os << "<" << e.before << "-[]-" << e.after << ">";
361                 break;
362         }
363         return os;
364 }
365
366
367 ostream & operator<<(ostream & os, MathRow const & mrow)
368 {
369         for (MathRow::Element const & e : mrow)
370                 os << e << "  ";
371         return os;
372 }
373
374 } // namespace lyx