3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
8 * Full author contact details are available in file CREDITS.
16 #include "MathSupport.h"
18 #include "BufferView.h"
20 #include "CoordCache.h"
23 #include "MetricsInfo.h"
25 #include "mathed/InsetMath.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstring.h"
32 #include "support/lassert.h"
42 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
43 : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
44 marker(marker_type::NO_MARKER), inset(nullptr), compl_unique_to(0), ar(nullptr),
51 // Helper functions for markers
53 int markerMargin(MathRow::Element const & e)
56 case marker_type::MARKER:
57 case marker_type::MARKER2:
58 case marker_type::BOX_MARKER:
60 case marker_type::NO_MARKER:
68 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
71 // handle vertical space for markers
73 case marker_type::NO_MARKER:
75 case marker_type::MARKER:
78 case marker_type::MARKER2:
82 case marker_type::BOX_MARKER:
84 font.setSize(TINY_SIZE);
86 mathed_string_dim(font, e.inset->name(), namedim);
87 int const namewid = 1 + namedim.wid + 1;
89 if (namewid > dim.wid)
90 e.after += namewid - dim.wid;
92 dim.des += 3 + namedim.height();
97 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
98 int const x, int const y)
100 if (e.marker == marker_type::NO_MARKER)
103 CoordCache const & coords = pi.base.bv->coordCache();
104 Dimension const dim = coords.getInsets().dim(e.inset);
106 // the marker is before/after the inset. Necessary space has been reserved already.
107 int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
108 int const r = x + dim.width() - e.after;
111 if (e.marker == marker_type::BOX_MARKER) {
112 // draw header and rectangle around
114 font.setSize(TINY_SIZE);
115 font.setColor(Color_mathmacrolabel);
117 mathed_string_dim(font, e.inset->name(), namedim);
118 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
119 dim.wid, namedim.height() + 2, Color_mathmacrobg);
120 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
124 bool const highlight = e.inset->mouseHovered(pi.base.bv)
125 || e.inset->editing(pi.base.bv);
126 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
127 // If the corners have the same color as the background, do not paint them.
128 if (lcolor.getX11HexName(Color_mathbg) == lcolor.getX11HexName(pen_color))
131 // Lower corners in all cases
132 int const d = y + dim.descent();
133 pi.pain.line(l, d - 3, l, d, pen_color);
134 pi.pain.line(r, d - 3, r, d, pen_color);
135 pi.pain.line(l, d, l + 3, d, pen_color);
136 pi.pain.line(r - 3, d, r, d, pen_color);
139 if (e.marker == marker_type::BOX_MARKER
140 || e.marker == marker_type::MARKER2) {
141 int const a = y - dim.ascent();
142 pi.pain.line(l, a + 3, l, a, pen_color);
143 pi.pain.line(r, a + 3, r, a, pen_color);
144 pi.pain.line(l, a, l + 3, a, pen_color);
145 pi.pain.line(r - 3, a, r, a, pen_color);
152 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
154 // First there is a dummy element of type "open"
155 push_back(Element(mi, DUMMY, MC_OPEN));
157 // Then insert the MathData argument
158 bool const has_contents = ar->addToMathRow(*this, mi);
160 // A MathRow should not be completely empty
162 Element e(mi, BOX, MC_ORD);
163 // empty arrays are visible when they are editable
164 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
168 // Finally there is a dummy element of type "close"
169 push_back(Element(mi, DUMMY, MC_CLOSE));
171 /* Do spacing only in math mode. This test is a bit clumsy,
172 * but it is used in other places for guessing the current mode.
174 bool const dospacing = isMathFont(mi.base.fontname);
178 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
179 if (elements_[i].mclass != MC_UNKNOWN)
180 update_class(elements_[i].mclass, elements_[before(i)].mclass,
181 elements_[after(i)].mclass);
186 // We go to the end to handle spacing at the end of equation
187 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
188 Element & e = elements_[i];
189 Element & bef = elements_[before(i)];
191 if (dospacing && e.mclass != MC_UNKNOWN) {
192 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
193 bef.after += spc / 2;
194 // this is better than spc / 2 to avoid rounding problems
195 e.before += spc - spc / 2;
198 // finally reserve space for markers
199 /* FIXME : the test below avoids that the spacing grows grows
200 * when a BEGIN/END_SEL element is added (typically start or
201 * end a selection after a fraction). I would think that the
202 * code should just go under the e.mclass != MC_UNKNOWN branch
203 * below, but I am not sure why it has not been done before.
204 * Therefore, until we double check this, be very conservative
206 if (e.type != BEGIN_SEL && e.type != END_SEL)
207 bef.after = max(bef.after, markerMargin(bef));
208 if (e.mclass != MC_UNKNOWN)
209 e.before = max(e.before, markerMargin(e));
210 // for linearized insets (macros...) too
212 bef.after = max(bef.after, markerMargin(e));
213 if (e.type == END && e.marker != marker_type::NO_MARKER) {
214 Element & aft = elements_[after(i)];
215 aft.before = max(aft.before, markerMargin(e));
219 // Do not lose spacing allocated to extremities
220 if (!elements_.empty()) {
221 elements_[after(0)].before += elements_.front().after;
222 elements_[before(elements_.size() - 1)].after += elements_.back().before;
227 int MathRow::before(int i) const
231 while (elements_[i].mclass == MC_UNKNOWN);
237 int MathRow::after(int i) const
241 while (elements_[i].mclass == MC_UNKNOWN);
247 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
250 // In order to compute the dimension of macros and their
251 // arguments, it is necessary to keep track of them.
252 vector<pair<InsetMath const *, Dimension>> dim_insets;
253 vector<pair<MathData const *, Dimension>> dim_arrays;
254 CoordCache & coords = mi.base.bv->coordCache();
255 for (Element & e : elements_) {
256 mi.base.macro_nesting = e.macro_nesting;
264 e.inset->metrics(mi, d);
265 d.wid += e.before + e.after;
266 coords.insets().add(e.inset, d);
270 dim_insets.push_back(make_pair(e.inset, Dimension()));
271 dim_insets.back().second.wid += e.before + e.after;
272 d.wid = e.before + e.after;
273 e.inset->beforeMetrics();
276 dim_arrays.push_back(make_pair(e.ar, Dimension()));
280 e.inset->afterMetrics();
281 LATTEST(dim_insets.back().first == e.inset);
282 d = dim_insets.back().second;
283 afterMetricsMarkers(mi, e, d);
284 d.wid += e.before + e.after;
285 coords.insets().add(e.inset, d);
286 dim_insets.pop_back();
287 // We do not want to count the width again, but the
288 // padding and the vertical dimension are meaningful.
289 d.wid = e.before + e.after;
292 LATTEST(dim_arrays.back().first == e.ar);
293 coords.arrays().add(e.ar, dim_arrays.back().second);
294 dim_arrays.pop_back();
298 d = theFontMetrics(mi.base.font).dimension('I');
299 if (e.color != Color_none) {
300 // allow for one pixel before/after the box.
301 d.wid += e.before + e.after + 2;
303 // hide the box, but keep its height
311 // Now add the dimension to current macros and arguments.
312 for (auto & dim_macro : dim_insets)
313 dim_macro.second += d;
314 for (auto & dim_array : dim_arrays)
315 dim_array.second += d;
318 if (e.compl_text.empty())
320 FontInfo font = mi.base.font;
321 augmentFont(font, "mathnormal");
322 dim.wid += mathed_string_width(font, e.compl_text);
324 LATTEST(dim_insets.empty() && dim_arrays.empty());
328 void MathRow::draw(PainterInfo & pi, int x, int const y) const
330 Changer change_color;
331 CoordCache & coords = pi.base.bv->coordCache();
332 for (Element const & e : elements_) {
335 // This is hackish: the math inset does not know that space
336 // has been added before and after it; we alter its dimension
337 // while it is drawing, because it relies on this value.
338 Geometry & g = coords.insets().geometry(e.inset);
339 g.dim.wid -= e.before + e.after;
340 if (pi.pain.develMode() && !e.inset->isBufferValid()) {
341 pi.pain.fillRectangle(x + e.before, y - g.dim.ascent(),
342 g.dim.width(), g.dim.height(), Color_error);
343 LYXERR0("Unset Buffer memeber in " << insetName(e.inset->lyxCode()));
345 e.inset->draw(pi, x + e.before, y);
347 g.dim.wid += e.before + e.after;
348 drawMarkers(pi, e, x, y);
354 coords.arrays().add(e.ar, x, y);
355 e.ar->drawSelection(pi, x, y);
358 coords.insets().add(e.inset, x, y);
359 drawMarkers(pi, e, x, y);
360 e.inset->beforeDraw(pi);
362 x += e.before + e.after;
366 e.inset->afterDraw(pi);
367 x += e.before + e.after;
370 // Change the color if the selection is indeed active
371 // FIXME: it would be better to make sure that metrics are
372 // computed again when selection status changes.
373 if (pi.base.bv->cursor().selection())
374 change_color = pi.base.font.changeColor(Color_selectionmath);
377 change_color = noChange();
380 if (e.color == Color_none)
382 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
383 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
384 d.width() - 1, d.height() - 1, e.color);
385 x += d.wid + 2 + e.before + e.after;
392 if (e.compl_text.empty())
394 FontInfo f = pi.base.font;
395 augmentFont(f, "mathnormal");
397 // draw the unique and the non-unique completion part
398 // Note: this is not time-critical as it is
399 // only done once per screen.
400 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
401 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
404 f.setColor(Color_inlinecompletion);
405 // offset the text by e.after to make sure that the
406 // spacing is after the completion, not before.
407 pi.pain.text(x - e.after, y, s1, f);
408 x += mathed_string_width(f, s1);
411 f.setColor(Color_nonunique_inlinecompletion);
412 pi.pain.text(x - e.after, y, s2, f);
413 x += mathed_string_width(f, s2);
419 int MathRow::kerning(BufferView const * bv) const
421 if (elements_.empty())
423 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
424 return inset ? inset->kerning(bv) : 0;
428 ostream & operator<<(ostream & os, MathRow::Element const & e)
432 os << (e.mclass == MC_OPEN ? "{" : "}");
435 os << "<" << e.before << "-"
436 << to_utf8(class_to_string(e.mclass))
437 << "-" << e.after << ">";
441 os << "\\" << to_utf8(e.inset->name())
442 << "^" << e.macro_nesting << "[";
452 case MathRow::BEGIN_SEL:
455 case MathRow::END_SEL:
459 os << "<" << e.before << "-[]-" << e.after << ">";
466 ostream & operator<<(ostream & os, MathRow const & mrow)
468 for (MathRow::Element const & e : mrow)