]> git.lyx.org Git - features.git/blob - src/mathed/MathRow.cpp
BOX tweak
[features.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                         // allow for one pixel in front of the box.
179                         d.wid += e.before + e.after + 1;
180                         break;
181                 }
182
183                 // handle vertical space for markers
184                 switch(e.marker) {
185                 case InsetMath::NO_MARKER:
186                         break;
187                 case InsetMath::MARKER:
188                         ++d.des;
189                         break;
190                 case InsetMath::MARKER2:
191                         ++d.asc;
192                         ++d.des;
193                 }
194
195                 if (!d.empty()) {
196                         dim += d;
197                         // Now add the dimension to current macros and arguments.
198                         for (auto & dim_macro : dim_macros)
199                                 dim_macro.second += d;
200                         for (auto & dim_array : dim_arrays)
201                                 dim_array.second += d;
202                 }
203
204                 if (e.compl_text.empty())
205                         continue;
206                 FontInfo font = mi.base.font;
207                 augmentFont(font, "mathnormal");
208                 dim.wid += mathed_string_width(font, e.compl_text);
209         }
210         LATTEST(dim_macros.empty() && dim_arrays.empty());
211 }
212
213
214 namespace {
215
216 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
217 {
218         if (e.marker == InsetMath::NO_MARKER)
219                 return;
220
221         CoordCache const & coords = pi.base.bv->coordCache();
222         Dimension const dim = coords.getInsets().dim(e.inset);
223
224         // the marker is before/after the inset. Normally some space has been reserved already.
225         int const l = x + e.before - 1;
226         int const r = x + dim.width() - e.after;
227
228         // Duplicated from Inset.cpp and adapted. It is believed that the
229         // Inset version should die eventually
230         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
231                 Color_mathframe : Color_mathcorners;
232
233         int const d = y + dim.descent();
234         pi.pain.line(l, d - 3, l, d, pen_color);
235         pi.pain.line(r, d - 3, r, d, pen_color);
236         pi.pain.line(l, d, l + 3, d, pen_color);
237         pi.pain.line(r - 3, d, r, d, pen_color);
238
239         if (e.marker == InsetMath::MARKER)
240                 return;
241
242         int const a = y - dim.ascent();
243         pi.pain.line(l, a + 3, l, a, pen_color);
244         pi.pain.line(r, a + 3, r, a, pen_color);
245         pi.pain.line(l, a, l + 3, a, pen_color);
246         pi.pain.line(r - 3, a, r, a, pen_color);
247 }
248
249 }
250
251 void MathRow::draw(PainterInfo & pi, int x, int const y) const
252 {
253         CoordCache & coords = pi.base.bv->coordCache();
254         for (Element const & e : elements_) {
255                 switch (e.type) {
256                 case INSET: {
257                         // This is hackish: the math inset does not know that space
258                         // has been added before and after it; we alter its dimension
259                         // while it is drawing, because it relies on this value.
260                         Dimension const d = coords.insets().dim(e.inset);
261                         Dimension d2 = d;
262                         d2.wid -= e.before + e.after;
263                         coords.insets().add(e.inset, d2);
264                         e.inset->drawSelection(pi, x + e.before, y);
265                         e.inset->draw(pi, x + e.before, y);
266                         coords.insets().add(e.inset, x, y);
267                         coords.insets().add(e.inset, d);
268                         drawMarkers(pi, e, x, y);
269                         x += d.wid;
270                         break;
271                 }
272                 case BEG_MACRO:
273                         coords.insets().add(e.macro, x, y);
274
275                         drawMarkers(pi, e, x, y);
276                         break;
277                 case BEG_ARG:
278                         coords.arrays().add(e.ar, x, y);
279                         // if the macro is being edited, then the painter is in
280                         // monochrome mode.
281                         if (e.macro->editMetrics(pi.base.bv))
282                                 pi.pain.leaveMonochromeMode();
283                         break;
284                 case END_ARG:
285                         if (e.macro->editMetrics(pi.base.bv))
286                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
287                         break;
288                 case BOX: {
289                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
290                         // the box is not visible in non-editable context (except for grey macro boxes).
291                         if (e.color != Color_none)
292                                 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
293                                                   d.width() - 1, d.height() - 1, e.color);
294                         x += d.wid + e.before + e.after;
295                         break;
296                 }
297                 case DUMMY:
298                 case END_MACRO:
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.macro->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