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"
21 #include "MetricsInfo.h"
23 #include "frontends/FontMetrics.h"
24 #include "frontends/Painter.h"
26 #include "support/debug.h"
27 #include "support/docstring.h"
28 #include "support/lassert.h"
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(nullptr), compl_unique_to(0), ar(nullptr),
47 // Helper functions for markers
49 int markerMargin(MathRow::Element const & e)
52 case InsetMath::MARKER:
53 case InsetMath::MARKER2:
54 case InsetMath::BOX_MARKER:
56 case InsetMath::NO_MARKER:
64 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
67 // handle vertical space for markers
69 case InsetMath::NO_MARKER:
71 case InsetMath::MARKER:
74 case InsetMath::MARKER2:
78 case InsetMath::BOX_MARKER:
80 font.setSize(TINY_SIZE);
82 mathed_string_dim(font, e.inset->name(), namedim);
83 int const namewid = 1 + namedim.wid + 1;
85 if (namewid > dim.wid)
86 e.after += namewid - dim.wid;
88 dim.des += 3 + namedim.height();
93 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
94 int const x, int const y)
96 if (e.marker == InsetMath::NO_MARKER)
99 CoordCache const & coords = pi.base.bv->coordCache();
100 Dimension const dim = coords.getInsets().dim(e.inset);
102 // the marker is before/after the inset. Necessary space has been reserved already.
103 int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
104 int const r = x + dim.width() - e.after;
107 if (e.marker == InsetMath::BOX_MARKER) {
108 // draw header and rectangle around
110 font.setSize(TINY_SIZE);
111 font.setColor(Color_mathmacrolabel);
113 mathed_string_dim(font, e.inset->name(), namedim);
114 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
115 dim.wid, namedim.height() + 2, Color_mathmacrobg);
116 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
120 bool const highlight = e.inset->mouseHovered(pi.base.bv)
121 || e.inset->editing(pi.base.bv);
122 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
123 // If the corners have the same color as the background, do not paint them.
124 if (lcolor.getX11HexName(Color_mathbg) == lcolor.getX11HexName(pen_color))
127 // Lower corners in all cases
128 int const d = y + dim.descent();
129 pi.pain.line(l, d - 3, l, d, pen_color);
130 pi.pain.line(r, d - 3, r, d, pen_color);
131 pi.pain.line(l, d, l + 3, d, pen_color);
132 pi.pain.line(r - 3, d, r, d, pen_color);
135 if (e.marker == InsetMath::BOX_MARKER
136 || e.marker == InsetMath::MARKER2) {
137 int const a = y - dim.ascent();
138 pi.pain.line(l, a + 3, l, a, pen_color);
139 pi.pain.line(r, a + 3, r, a, pen_color);
140 pi.pain.line(l, a, l + 3, a, pen_color);
141 pi.pain.line(r - 3, a, r, a, pen_color);
148 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
150 // First there is a dummy element of type "open"
151 push_back(Element(mi, DUMMY, MC_OPEN));
153 // Then insert the MathData argument
154 bool const has_contents = ar->addToMathRow(*this, mi);
156 // A MathRow should not be completely empty
158 Element e(mi, BOX, MC_ORD);
159 // empty arrays are visible when they are editable
160 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
164 // Finally there is a dummy element of type "close"
165 push_back(Element(mi, DUMMY, MC_CLOSE));
167 /* Do spacing only in math mode. This test is a bit clumsy,
168 * but it is used in other places for guessing the current mode.
170 bool const dospacing = isMathFont(mi.base.fontname);
174 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
175 if (elements_[i].mclass != MC_UNKNOWN)
176 update_class(elements_[i].mclass, elements_[before(i)].mclass,
177 elements_[after(i)].mclass);
182 // We go to the end to handle spacing at the end of equation
183 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
184 Element & e = elements_[i];
186 Element & bef = elements_[before(i)];
187 if (dospacing && e.mclass != MC_UNKNOWN) {
188 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
189 bef.after += spc / 2;
190 // this is better than spc / 2 to avoid rounding problems
191 e.before += spc - spc / 2;
194 // finally reserve space for markers
195 bef.after = max(bef.after, markerMargin(bef));
196 if (e.mclass != MC_UNKNOWN)
197 e.before = max(e.before, markerMargin(e));
198 // for linearized insets (macros...) too
200 bef.after = max(bef.after, markerMargin(e));
201 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
202 Element & aft = elements_[after(i)];
203 aft.before = max(aft.before, markerMargin(e));
207 // Do not lose spacing allocated to extremities
208 if (!elements_.empty()) {
209 elements_[after(0)].before += elements_.front().after;
210 elements_[before(elements_.size() - 1)].after += elements_.back().before;
215 int MathRow::before(int i) const
219 while (elements_[i].mclass == MC_UNKNOWN);
225 int MathRow::after(int i) const
229 while (elements_[i].mclass == MC_UNKNOWN);
235 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
238 // In order to compute the dimension of macros and their
239 // arguments, it is necessary to keep track of them.
240 vector<pair<InsetMath const *, Dimension>> dim_insets;
241 vector<pair<MathData const *, Dimension>> dim_arrays;
242 CoordCache & coords = mi.base.bv->coordCache();
243 for (Element & e : elements_) {
244 mi.base.macro_nesting = e.macro_nesting;
250 e.inset->metrics(mi, d);
251 d.wid += e.before + e.after;
252 coords.insets().add(e.inset, d);
256 dim_insets.push_back(make_pair(e.inset, Dimension()));
257 dim_insets.back().second.wid += e.before + e.after;
258 d.wid = e.before + e.after;
259 e.inset->beforeMetrics();
262 dim_arrays.push_back(make_pair(e.ar, Dimension()));
266 e.inset->afterMetrics();
267 LATTEST(dim_insets.back().first == e.inset);
268 d = dim_insets.back().second;
269 afterMetricsMarkers(mi, e, d);
270 d.wid += e.before + e.after;
271 coords.insets().add(e.inset, d);
272 dim_insets.pop_back();
273 // We do not want to count the width again, but the
274 // padding and the vertical dimension are meaningful.
275 d.wid = e.before + e.after;
278 LATTEST(dim_arrays.back().first == e.ar);
279 coords.arrays().add(e.ar, dim_arrays.back().second);
280 dim_arrays.pop_back();
284 d = theFontMetrics(mi.base.font).dimension('I');
285 if (e.color != Color_none) {
286 // allow for one pixel before/after the box.
287 d.wid += e.before + e.after + 2;
289 // hide the box, but keep its height
297 // Now add the dimension to current macros and arguments.
298 for (auto & dim_macro : dim_insets)
299 dim_macro.second += d;
300 for (auto & dim_array : dim_arrays)
301 dim_array.second += d;
304 if (e.compl_text.empty())
306 FontInfo font = mi.base.font;
307 augmentFont(font, "mathnormal");
308 dim.wid += mathed_string_width(font, e.compl_text);
310 LATTEST(dim_insets.empty() && dim_arrays.empty());
314 void MathRow::draw(PainterInfo & pi, int x, int const y) const
316 CoordCache & coords = pi.base.bv->coordCache();
317 for (Element const & e : elements_) {
320 // This is hackish: the math inset does not know that space
321 // has been added before and after it; we alter its dimension
322 // while it is drawing, because it relies on this value.
323 Dimension const d = coords.insets().dim(e.inset);
325 d2.wid -= e.before + e.after;
326 coords.insets().add(e.inset, d2);
327 if (pi.pain.develMode() && !e.inset->isBufferValid())
328 pi.pain.fillRectangle(x + e.before, y - d2.ascent(),
329 d2.width(), d2.height(), Color_error);
330 e.inset->draw(pi, x + e.before, y);
331 coords.insets().add(e.inset, x, y);
332 coords.insets().add(e.inset, d);
333 drawMarkers(pi, e, x, y);
339 coords.arrays().add(e.ar, x, y);
340 e.ar->drawSelection(pi, x, y);
343 coords.insets().add(e.inset, x, y);
344 drawMarkers(pi, e, x, y);
345 e.inset->beforeDraw(pi);
347 x += e.before + e.after;
351 e.inset->afterDraw(pi);
352 x += e.before + e.after;
355 if (e.color == Color_none)
357 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
358 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
359 d.width() - 1, d.height() - 1, e.color);
360 x += d.wid + 2 + e.before + e.after;
367 if (e.compl_text.empty())
369 FontInfo f = pi.base.font;
370 augmentFont(f, "mathnormal");
372 // draw the unique and the non-unique completion part
373 // Note: this is not time-critical as it is
374 // only done once per screen.
375 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
376 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
379 f.setColor(Color_inlinecompletion);
380 // offset the text by e.after to make sure that the
381 // spacing is after the completion, not before.
382 pi.pain.text(x - e.after, y, s1, f);
383 x += mathed_string_width(f, s1);
386 f.setColor(Color_nonunique_inlinecompletion);
387 pi.pain.text(x - e.after, y, s2, f);
388 x += mathed_string_width(f, s2);
394 int MathRow::kerning(BufferView const * bv) const
396 if (elements_.empty())
398 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
399 return inset ? inset->kerning(bv) : 0;
403 ostream & operator<<(ostream & os, MathRow::Element const & e)
407 os << (e.mclass == MC_OPEN ? "{" : "}");
410 os << "<" << e.before << "-"
411 << to_utf8(class_to_string(e.mclass))
412 << "-" << e.after << ">";
416 os << "\\" << to_utf8(e.inset->name())
417 << "^" << e.macro_nesting << "[";
428 os << "<" << e.before << "-[]-" << e.after << ">";
435 ostream & operator<<(ostream & os, MathRow const & mrow)
437 for (MathRow::Element const & e : mrow)