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"
22 #include "MetricsInfo.h"
24 #include "mathed/InsetMath.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/lassert.h"
41 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
42 : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
43 marker(marker_type::NO_MARKER), inset(nullptr), compl_unique_to(0), ar(nullptr),
50 // Helper functions for markers
52 int markerMargin(MathRow::Element const & e)
55 case marker_type::MARKER:
56 case marker_type::MARKER2:
57 case marker_type::BOX_MARKER:
59 case marker_type::NO_MARKER:
67 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
70 // handle vertical space for markers
72 case marker_type::NO_MARKER:
74 case marker_type::MARKER:
77 case marker_type::MARKER2:
81 case marker_type::BOX_MARKER:
83 font.setSize(TINY_SIZE);
85 mathed_string_dim(font, e.inset->name(), namedim);
86 int const namewid = 1 + namedim.wid + 1;
88 if (namewid > dim.wid)
89 e.after += namewid - dim.wid;
91 dim.des += 3 + namedim.height();
96 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
97 int const x, int const y)
99 if (e.marker == marker_type::NO_MARKER)
102 CoordCache const & coords = pi.base.bv->coordCache();
103 Dimension const dim = coords.getInsets().dim(e.inset);
105 // the marker is before/after the inset. Necessary space has been reserved already.
106 int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
107 int const r = x + dim.width() - e.after;
110 if (e.marker == marker_type::BOX_MARKER) {
111 // draw header and rectangle around
113 font.setSize(TINY_SIZE);
114 font.setColor(Color_mathmacrolabel);
116 mathed_string_dim(font, e.inset->name(), namedim);
117 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
118 dim.wid, namedim.height() + 2, Color_mathmacrobg);
119 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
123 bool const highlight = e.inset->mouseHovered(pi.base.bv)
124 || e.inset->editing(pi.base.bv);
125 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
126 // If the corners have the same color as the background, do not paint them.
127 if (lcolor.getX11HexName(Color_mathbg) == lcolor.getX11HexName(pen_color))
130 // Lower corners in all cases
131 int const d = y + dim.descent();
132 pi.pain.line(l, d - 3, l, d, pen_color);
133 pi.pain.line(r, d - 3, r, d, pen_color);
134 pi.pain.line(l, d, l + 3, d, pen_color);
135 pi.pain.line(r - 3, d, r, d, pen_color);
138 if (e.marker == marker_type::BOX_MARKER
139 || e.marker == marker_type::MARKER2) {
140 int const a = y - dim.ascent();
141 pi.pain.line(l, a + 3, l, a, pen_color);
142 pi.pain.line(r, a + 3, r, a, pen_color);
143 pi.pain.line(l, a, l + 3, a, pen_color);
144 pi.pain.line(r - 3, a, r, a, pen_color);
151 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
153 // First there is a dummy element of type "open"
154 push_back(Element(mi, DUMMY, MC_OPEN));
156 // Then insert the MathData argument
157 bool const has_contents = ar->addToMathRow(*this, mi);
159 // A MathRow should not be completely empty
161 Element e(mi, BOX, MC_ORD);
162 // empty arrays are visible when they are editable
163 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
167 // Finally there is a dummy element of type "close"
168 push_back(Element(mi, DUMMY, MC_CLOSE));
170 /* Do spacing only in math mode. This test is a bit clumsy,
171 * but it is used in other places for guessing the current mode.
173 bool const dospacing = isMathFont(mi.base.fontname);
177 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
178 if (elements_[i].mclass != MC_UNKNOWN)
179 update_class(elements_[i].mclass, elements_[before(i)].mclass,
180 elements_[after(i)].mclass);
185 // We go to the end to handle spacing at the end of equation
186 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
187 Element & e = elements_[i];
189 Element & bef = elements_[before(i)];
190 if (dospacing && e.mclass != MC_UNKNOWN) {
191 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
192 bef.after += spc / 2;
193 // this is better than spc / 2 to avoid rounding problems
194 e.before += spc - spc / 2;
197 // finally reserve space for markers
198 bef.after = max(bef.after, markerMargin(bef));
199 if (e.mclass != MC_UNKNOWN)
200 e.before = max(e.before, markerMargin(e));
201 // for linearized insets (macros...) too
203 bef.after = max(bef.after, markerMargin(e));
204 if (e.type == END && e.marker != marker_type::NO_MARKER) {
205 Element & aft = elements_[after(i)];
206 aft.before = max(aft.before, markerMargin(e));
210 // Do not lose spacing allocated to extremities
211 if (!elements_.empty()) {
212 elements_[after(0)].before += elements_.front().after;
213 elements_[before(elements_.size() - 1)].after += elements_.back().before;
218 int MathRow::before(int i) const
222 while (elements_[i].mclass == MC_UNKNOWN);
228 int MathRow::after(int i) const
232 while (elements_[i].mclass == MC_UNKNOWN);
238 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
241 // In order to compute the dimension of macros and their
242 // arguments, it is necessary to keep track of them.
243 vector<pair<InsetMath const *, Dimension>> dim_insets;
244 vector<pair<MathData const *, Dimension>> dim_arrays;
245 CoordCache & coords = mi.base.bv->coordCache();
246 for (Element & e : elements_) {
247 mi.base.macro_nesting = e.macro_nesting;
255 e.inset->metrics(mi, d);
256 d.wid += e.before + e.after;
257 coords.insets().add(e.inset, d);
261 dim_insets.push_back(make_pair(e.inset, Dimension()));
262 dim_insets.back().second.wid += e.before + e.after;
263 d.wid = e.before + e.after;
264 e.inset->beforeMetrics();
267 dim_arrays.push_back(make_pair(e.ar, Dimension()));
271 e.inset->afterMetrics();
272 LATTEST(dim_insets.back().first == e.inset);
273 d = dim_insets.back().second;
274 afterMetricsMarkers(mi, e, d);
275 d.wid += e.before + e.after;
276 coords.insets().add(e.inset, d);
277 dim_insets.pop_back();
278 // We do not want to count the width again, but the
279 // padding and the vertical dimension are meaningful.
280 d.wid = e.before + e.after;
283 LATTEST(dim_arrays.back().first == e.ar);
284 coords.arrays().add(e.ar, dim_arrays.back().second);
285 dim_arrays.pop_back();
289 d = theFontMetrics(mi.base.font).dimension('I');
290 if (e.color != Color_none) {
291 // allow for one pixel before/after the box.
292 d.wid += e.before + e.after + 2;
294 // hide the box, but keep its height
302 // Now add the dimension to current macros and arguments.
303 for (auto & dim_macro : dim_insets)
304 dim_macro.second += d;
305 for (auto & dim_array : dim_arrays)
306 dim_array.second += d;
309 if (e.compl_text.empty())
311 FontInfo font = mi.base.font;
312 augmentFont(font, "mathnormal");
313 dim.wid += mathed_string_width(font, e.compl_text);
315 LATTEST(dim_insets.empty() && dim_arrays.empty());
319 void MathRow::draw(PainterInfo & pi, int x, int const y) const
321 Changer change_color;
322 CoordCache & coords = pi.base.bv->coordCache();
323 for (Element const & e : elements_) {
326 // This is hackish: the math inset does not know that space
327 // has been added before and after it; we alter its dimension
328 // while it is drawing, because it relies on this value.
329 Geometry & g = coords.insets().geometry(e.inset);
330 g.dim.wid -= e.before + e.after;
331 if (pi.pain.develMode() && !e.inset->isBufferValid())
332 pi.pain.fillRectangle(x + e.before, y - g.dim.ascent(),
333 g.dim.width(), g.dim.height(), Color_error);
334 e.inset->draw(pi, x + e.before, y);
336 g.dim.wid += e.before + e.after;
337 drawMarkers(pi, e, x, y);
343 coords.arrays().add(e.ar, x, y);
344 e.ar->drawSelection(pi, x, y);
347 coords.insets().add(e.inset, x, y);
348 drawMarkers(pi, e, x, y);
349 e.inset->beforeDraw(pi);
351 x += e.before + e.after;
355 e.inset->afterDraw(pi);
356 x += e.before + e.after;
359 if (lyxrc.use_system_colors)
360 change_color = pi.base.font.changeColor(Color_selectiontext);
363 change_color = noChange();
366 if (e.color == Color_none)
368 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
369 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
370 d.width() - 1, d.height() - 1, e.color);
371 x += d.wid + 2 + e.before + e.after;
378 if (e.compl_text.empty())
380 FontInfo f = pi.base.font;
381 augmentFont(f, "mathnormal");
383 // draw the unique and the non-unique completion part
384 // Note: this is not time-critical as it is
385 // only done once per screen.
386 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
387 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
390 f.setColor(Color_inlinecompletion);
391 // offset the text by e.after to make sure that the
392 // spacing is after the completion, not before.
393 pi.pain.text(x - e.after, y, s1, f);
394 x += mathed_string_width(f, s1);
397 f.setColor(Color_nonunique_inlinecompletion);
398 pi.pain.text(x - e.after, y, s2, f);
399 x += mathed_string_width(f, s2);
405 int MathRow::kerning(BufferView const * bv) const
407 if (elements_.empty())
409 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
410 return inset ? inset->kerning(bv) : 0;
414 ostream & operator<<(ostream & os, MathRow::Element const & e)
418 os << (e.mclass == MC_OPEN ? "{" : "}");
421 os << "<" << e.before << "-"
422 << to_utf8(class_to_string(e.mclass))
423 << "-" << e.after << ">";
427 os << "\\" << to_utf8(e.inset->name())
428 << "^" << e.macro_nesting << "[";
438 case MathRow::BEGIN_SEL:
441 case MathRow::END_SEL:
445 os << "<" << e.before << "-[]-" << e.after << ">";
452 ostream & operator<<(ostream & os, MathRow const & mrow)
454 for (MathRow::Element const & e : mrow)