]> git.lyx.org Git - features.git/blob - src/mathed/MathRow.cpp
Some clean-up to markers handling code.
[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 "MathSupport.h"
19
20 #include "BufferView.h"
21 #include "CoordCache.h"
22 #include "MetricsInfo.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/lassert.h"
30
31 #include <ostream>
32
33 using namespace std;
34
35 namespace lyx {
36
37
38 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
39         : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
40           marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
41           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(mi, DUMMY, MC_OPEN));
49
50         // Then insert the MathData argument
51         bool const has_contents = ar->addToMathRow(*this, mi);
52
53         // A MathRow should not be completely empty
54         if (!has_contents) {
55                 Element e(mi, BOX, MC_ORD);
56                 // empty arrays are visible when they are editable
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(mi, 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         bool const dospacing = isMathFont(mi.base.fontname);
68
69         // update classes
70         if (dospacing) {
71                 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
72                         if (elements_[i].mclass != MC_UNKNOWN)
73                                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
74                                                          elements_[after(i)].mclass);
75                 }
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                 Element & e = elements_[i];
82
83                 Element & bef = elements_[before(i)];
84                 if (dospacing && e.mclass != MC_UNKNOWN) {
85                         int spc = class_spacing(bef.mclass, e.mclass, mi.base);
86                         bef.after += spc / 2;
87                         // this is better than spc / 2 to avoid rounding problems
88                         e.before += spc - spc / 2;
89                 }
90
91                 // finally reserve space for markers
92                 if (bef.marker != Inset::NO_MARKER)
93                         bef.after = max(bef.after, 1);
94                 if (e.mclass != MC_UNKNOWN && e.marker != Inset::NO_MARKER)
95                         e.before = max(e.before, 1);
96                 // for linearized insets (macros...) too
97                 if (e.type == BEGIN && e.marker != Inset::NO_MARKER)
98                         bef.after = max(bef.after, 1);
99                 if (e.type == END && e.marker != Inset::NO_MARKER) {
100                         Element & aft = elements_[after(i)];
101                         aft.before = max(aft.before, 1);
102                 }
103         }
104
105         // Do not lose spacing allocated to extremities
106         if (!elements_.empty()) {
107                 elements_[after(0)].before += elements_.front().after;
108                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
109         }
110 }
111
112
113 int MathRow::before(int i) const
114 {
115         do
116                 --i;
117         while (elements_[i].mclass == MC_UNKNOWN);
118
119         return i;
120 }
121
122
123 int MathRow::after(int i) const
124 {
125         do
126                 ++i;
127         while (elements_[i].mclass == MC_UNKNOWN);
128
129         return i;
130 }
131
132
133 namespace {
134
135 void metricsMarkersVertical(MetricsInfo const & , MathRow::Element const & e,
136                             Dimension & dim)
137 {
138         // handle vertical space for markers
139         switch(e.marker) {
140         case InsetMath::NO_MARKER:
141                 break;
142         case InsetMath::MARKER:
143                 ++dim.des;
144                 break;
145         case InsetMath::MARKER2:
146                 ++dim.asc;
147                 ++dim.des;
148         }
149 }
150
151 }
152
153
154 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
155 {
156         dim.asc = 0;
157         dim.wid = 0;
158         // In order to compute the dimension of macros and their
159         // arguments, it is necessary to keep track of them.
160         vector<pair<InsetMath const *, Dimension>> dim_insets;
161         vector<pair<MathData const *, Dimension>> dim_arrays;
162         CoordCache & coords = mi.base.bv->coordCache();
163         for (Element const & e : elements_) {
164                 mi.base.macro_nesting = e.macro_nesting;
165                 Dimension d;
166                 switch (e.type) {
167                 case DUMMY:
168                         break;
169                 case INSET:
170                         e.inset->metrics(mi, d);
171                         d.wid += e.before + e.after;
172                         coords.insets().add(e.inset, d);
173                         break;
174                 case BEGIN:
175                         if (e.inset) {
176                                 dim_insets.push_back(make_pair(e.inset, Dimension()));
177                                 dim_insets.back().second.wid += e.before + e.after;
178                                 e.inset->beforeMetrics();
179                         }
180                         if (e.ar)
181                                 dim_arrays.push_back(make_pair(e.ar, Dimension()));
182                         break;
183                 case END:
184                         if (e.inset) {
185                                 e.inset->afterMetrics();
186                                 LATTEST(dim_insets.back().first == e.inset);
187                                 Dimension & idim = dim_insets.back().second;
188                                 metricsMarkersVertical(mi, e, idim);
189                                 idim.wid += e.before + e.after;
190                                 coords.insets().add(e.inset, idim);
191                                 dim_insets.pop_back();
192                         }
193                         if (e.ar) {
194                                 LATTEST(dim_arrays.back().first == e.ar);
195                                 coords.arrays().add(e.ar, dim_arrays.back().second);
196                                 dim_arrays.pop_back();
197                         }
198                         break;
199                 case BOX:
200                         d = theFontMetrics(mi.base.font).dimension('I');
201                         if (e.color != Color_none) {
202                                 // allow for one pixel before/after the box.
203                                 d.wid += e.before + e.after + 2;
204                         } else {
205                                 // hide the box, but give it some height
206                                 d.wid = 0;
207                         }
208                         break;
209                 }
210
211                 if (!d.empty()) {
212                         dim += d;
213                         // Now add the dimension to current macros and arguments.
214                         for (auto & dim_macro : dim_insets)
215                                 dim_macro.second += d;
216                         for (auto & dim_array : dim_arrays)
217                                 dim_array.second += d;
218                 }
219
220                 if (e.compl_text.empty())
221                         continue;
222                 FontInfo font = mi.base.font;
223                 augmentFont(font, "mathnormal");
224                 dim.wid += mathed_string_width(font, e.compl_text);
225         }
226         LATTEST(dim_insets.empty() && dim_arrays.empty());
227 }
228
229
230 namespace {
231
232 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
233 {
234         if (e.marker == InsetMath::NO_MARKER)
235                 return;
236
237         CoordCache const & coords = pi.base.bv->coordCache();
238         Dimension const dim = coords.getInsets().dim(e.inset);
239
240         // the marker is before/after the inset. Normally some space has been reserved already.
241         int const l = x + e.before - 1;
242         int const r = x + dim.width() - e.after;
243
244         // Duplicated from Inset.cpp and adapted. It is believed that the
245         // Inset version should die eventually
246         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
247                 Color_mathframe : Color_mathcorners;
248
249         int const d = y + dim.descent();
250         pi.pain.line(l, d - 3, l, d, pen_color);
251         pi.pain.line(r, d - 3, r, d, pen_color);
252         pi.pain.line(l, d, l + 3, d, pen_color);
253         pi.pain.line(r - 3, d, r, d, pen_color);
254
255         if (e.marker == InsetMath::MARKER)
256                 return;
257
258         int const a = y - dim.ascent();
259         pi.pain.line(l, a + 3, l, a, pen_color);
260         pi.pain.line(r, a + 3, r, a, pen_color);
261         pi.pain.line(l, a, l + 3, a, pen_color);
262         pi.pain.line(r - 3, a, r, a, pen_color);
263 }
264
265 }
266
267 void MathRow::draw(PainterInfo & pi, int x, int const y) const
268 {
269         CoordCache & coords = pi.base.bv->coordCache();
270         for (Element const & e : elements_) {
271                 switch (e.type) {
272                 case INSET: {
273                         // This is hackish: the math inset does not know that space
274                         // has been added before and after it; we alter its dimension
275                         // while it is drawing, because it relies on this value.
276                         Dimension const d = coords.insets().dim(e.inset);
277                         Dimension d2 = d;
278                         d2.wid -= e.before + e.after;
279                         coords.insets().add(e.inset, d2);
280                         e.inset->drawSelection(pi, x + e.before, y);
281                         e.inset->draw(pi, x + e.before, y);
282                         coords.insets().add(e.inset, x, y);
283                         coords.insets().add(e.inset, d);
284                         drawMarkers(pi, e, x, y);
285                         x += d.wid;
286                         break;
287                 }
288                 case BEGIN:
289                         if (e.inset) {
290                                 coords.insets().add(e.inset, x, y);
291                                 drawMarkers(pi, e, x, y);
292                                 e.inset->beforeDraw(pi);
293                         }
294                         x += e.before + e.after;
295                         if (e.ar)
296                                 coords.arrays().add(e.ar, x, y);
297                         break;
298                 case END:
299                         if (e.inset)
300                                 e.inset->afterDraw(pi);
301                         x += e.before + e.after;
302                         break;
303                 case BOX: {
304                         if (e.color == Color_none)
305                                 break;
306                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
307                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
308                                           d.width() - 1, d.height() - 1, e.color);
309                         x += d.wid + 2 + e.before + e.after;
310                         break;
311                 }
312                 case DUMMY:
313                         break;
314                 }
315
316                 if (e.compl_text.empty())
317                         continue;
318                 FontInfo f = pi.base.font;
319                 augmentFont(f, "mathnormal");
320
321                 // draw the unique and the non-unique completion part
322                 // Note: this is not time-critical as it is
323                 // only done once per screen.
324                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
325                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
326
327                 if (!s1.empty()) {
328                         f.setColor(Color_inlinecompletion);
329                         pi.pain.text(x, y, s1, f);
330                         x += mathed_string_width(f, s1);
331                 }
332                 if (!s2.empty()) {
333                         f.setColor(Color_nonunique_inlinecompletion);
334                         pi.pain.text(x, y, s2, f);
335                         x += mathed_string_width(f, s2);
336                 }
337         }
338 }
339
340
341 int MathRow::kerning(BufferView const * bv) const
342 {
343         if (elements_.empty())
344                 return 0;
345         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
346         return inset ? inset->kerning(bv) : 0;
347 }
348
349
350 ostream & operator<<(ostream & os, MathRow::Element const & e)
351 {
352         switch (e.type) {
353         case MathRow::DUMMY:
354                 os << (e.mclass == MC_OPEN ? "{" : "}");
355                 break;
356         case MathRow::INSET:
357                 os << "<" << e.before << "-"
358                    << to_utf8(class_to_string(e.mclass))
359                    << "-" << e.after << ">";
360                 break;
361         case MathRow::BEGIN:
362                 if (e.inset)
363                         os << "\\" << to_utf8(e.inset->name())
364                            << "^" << e.macro_nesting << "[";
365                 if (e.ar)
366                         os << "(";
367                 break;
368         case MathRow::END:
369                 if (e.ar)
370                         os << ")";
371                 if (e.inset)
372                         os << "]";
373                 break;
374         case MathRow::BOX:
375                 os << "<" << e.before << "-[]-" << e.after << ">";
376                 break;
377         }
378         return os;
379 }
380
381
382 ostream & operator<<(ostream & os, MathRow const & mrow)
383 {
384         for (MathRow::Element const & e : mrow)
385                 os << e << "  ";
386         return os;
387 }
388
389 } // namespace lyx