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.
15 #include "InsetMath.h"
16 #include "MathClass.h"
18 #include "MathSupport.h"
20 #include "BufferView.h"
22 #include "CoordCache.h"
23 #include "MetricsInfo.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/debug.h"
29 #include "support/docstring.h"
30 #include "support/lassert.h"
40 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
41 : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
42 marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
49 // Helper functions for markers
51 int markerMargin(MathRow::Element const & e)
54 case InsetMath::MARKER:
55 case InsetMath::MARKER2:
56 case InsetMath::BOX_MARKER:
58 case InsetMath::NO_MARKER:
66 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
69 // handle vertical space for markers
71 case InsetMath::NO_MARKER:
73 case InsetMath::MARKER:
76 case InsetMath::MARKER2:
80 case InsetMath::BOX_MARKER:
82 font.setSize(FONT_SIZE_TINY);
84 mathed_string_dim(font, e.inset->name(), namedim);
85 int const namewid = 1 + namedim.wid + 1;
87 if (namewid > dim.wid)
88 e.after += namewid - dim.wid;
90 dim.des += 3 + namedim.height();
95 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
96 int const x, int const y)
98 if (e.marker == InsetMath::NO_MARKER)
101 CoordCache const & coords = pi.base.bv->coordCache();
102 Dimension const dim = coords.getInsets().dim(e.inset);
104 // the marker is before/after the inset. Necessary space has been reserved already.
105 int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
106 int const r = x + dim.width() - e.after;
109 if (e.marker == InsetMath::BOX_MARKER) {
110 // draw header and rectangle around
112 font.setSize(FONT_SIZE_TINY);
113 font.setColor(Color_mathmacrolabel);
115 mathed_string_dim(font, e.inset->name(), namedim);
116 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
117 dim.wid, namedim.height() + 2, Color_mathmacrobg);
118 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
122 bool const highlight = e.inset->mouseHovered(pi.base.bv)
123 || e.inset->editing(pi.base.bv);
124 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
125 // If the corners have the same color as the background, do not paint them.
126 if (lcolor.getX11Name(Color_mathbg) == lcolor.getX11Name(pen_color))
129 // Lower corners in all cases
130 int const d = y + dim.descent();
131 pi.pain.line(l, d - 3, l, d, pen_color);
132 pi.pain.line(r, d - 3, r, d, pen_color);
133 pi.pain.line(l, d, l + 3, d, pen_color);
134 pi.pain.line(r - 3, d, r, d, pen_color);
137 if (e.marker == InsetMath::BOX_MARKER
138 || e.marker == InsetMath::MARKER2) {
139 int const a = y - dim.ascent();
140 pi.pain.line(l, a + 3, l, a, pen_color);
141 pi.pain.line(r, a + 3, r, a, pen_color);
142 pi.pain.line(l, a, l + 3, a, pen_color);
143 pi.pain.line(r - 3, a, r, a, pen_color);
150 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
152 // First there is a dummy element of type "open"
153 push_back(Element(mi, DUMMY, MC_OPEN));
155 // Then insert the MathData argument
156 bool const has_contents = ar->addToMathRow(*this, mi);
158 // A MathRow should not be completely empty
160 Element e(mi, BOX, MC_ORD);
161 // empty arrays are visible when they are editable
162 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
166 // Finally there is a dummy element of type "close"
167 push_back(Element(mi, DUMMY, MC_CLOSE));
169 /* Do spacing only in math mode. This test is a bit clumsy,
170 * but it is used in other places for guessing the current mode.
172 bool const dospacing = isMathFont(mi.base.fontname);
176 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
177 if (elements_[i].mclass != MC_UNKNOWN)
178 update_class(elements_[i].mclass, elements_[before(i)].mclass,
179 elements_[after(i)].mclass);
184 // We go to the end to handle spacing at the end of equation
185 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
186 Element & e = elements_[i];
188 Element & bef = elements_[before(i)];
189 if (dospacing && e.mclass != MC_UNKNOWN) {
190 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
191 bef.after += spc / 2;
192 // this is better than spc / 2 to avoid rounding problems
193 e.before += spc - spc / 2;
196 // finally reserve space for markers
197 bef.after = max(bef.after, markerMargin(bef));
198 if (e.mclass != MC_UNKNOWN)
199 e.before = max(e.before, markerMargin(e));
200 // for linearized insets (macros...) too
202 bef.after = max(bef.after, markerMargin(e));
203 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
204 Element & aft = elements_[after(i)];
205 aft.before = max(aft.before, markerMargin(e));
209 // Do not lose spacing allocated to extremities
210 if (!elements_.empty()) {
211 elements_[after(0)].before += elements_.front().after;
212 elements_[before(elements_.size() - 1)].after += elements_.back().before;
217 int MathRow::before(int i) const
221 while (elements_[i].mclass == MC_UNKNOWN);
227 int MathRow::after(int i) const
231 while (elements_[i].mclass == MC_UNKNOWN);
237 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
239 // In order to compute the dimension of macros and their
240 // arguments, it is necessary to keep track of them.
241 vector<pair<InsetMath const *, Dimension>> dim_insets;
242 vector<pair<MathData const *, Dimension>> dim_arrays;
243 CoordCache & coords = mi.base.bv->coordCache();
244 for (Element & e : elements_) {
245 mi.base.macro_nesting = e.macro_nesting;
251 e.inset->metrics(mi, d);
252 d.wid += e.before + e.after;
253 coords.insets().add(e.inset, d);
257 dim_insets.push_back(make_pair(e.inset, Dimension()));
258 dim_insets.back().second.wid += e.before + e.after;
259 d.wid = e.before + e.after;
260 e.inset->beforeMetrics();
263 dim_arrays.push_back(make_pair(e.ar, Dimension()));
267 e.inset->afterMetrics();
268 LATTEST(dim_insets.back().first == e.inset);
269 d = dim_insets.back().second;
270 afterMetricsMarkers(mi, e, d);
271 d.wid += e.before + e.after;
272 coords.insets().add(e.inset, d);
273 dim_insets.pop_back();
274 // We do not want to count the width again, but the
275 // padding and the vertical dimension are meaningful.
276 d.wid = e.before + e.after;
279 LATTEST(dim_arrays.back().first == e.ar);
280 coords.arrays().add(e.ar, dim_arrays.back().second);
281 dim_arrays.pop_back();
285 d = theFontMetrics(mi.base.font).dimension('I');
286 if (e.color != Color_none) {
287 // allow for one pixel before/after the box.
288 d.wid += e.before + e.after + 2;
290 // hide the box, but keep its height
298 // Now add the dimension to current macros and arguments.
299 for (auto & dim_macro : dim_insets)
300 dim_macro.second += d;
301 for (auto & dim_array : dim_arrays)
302 dim_array.second += d;
305 if (e.compl_text.empty())
307 FontInfo font = mi.base.font;
308 augmentFont(font, "mathnormal");
309 dim.wid += mathed_string_width(font, e.compl_text);
311 LATTEST(dim_insets.empty() && dim_arrays.empty());
315 void MathRow::draw(PainterInfo & pi, int x, int const y) const
317 CoordCache & coords = pi.base.bv->coordCache();
318 for (Element const & e : elements_) {
321 // This is hackish: the math inset does not know that space
322 // has been added before and after it; we alter its dimension
323 // while it is drawing, because it relies on this value.
324 Dimension const d = coords.insets().dim(e.inset);
326 d2.wid -= e.before + e.after;
327 coords.insets().add(e.inset, d2);
328 e.inset->draw(pi, x + e.before, y);
329 coords.insets().add(e.inset, x, y);
330 coords.insets().add(e.inset, d);
331 drawMarkers(pi, e, x, y);
337 coords.arrays().add(e.ar, x, y);
338 e.ar->drawSelection(pi, x, y);
341 coords.insets().add(e.inset, x, y);
342 drawMarkers(pi, e, x, y);
343 e.inset->beforeDraw(pi);
345 x += e.before + e.after;
349 e.inset->afterDraw(pi);
350 x += e.before + e.after;
353 if (e.color == Color_none)
355 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
356 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
357 d.width() - 1, d.height() - 1, e.color);
358 x += d.wid + 2 + e.before + e.after;
365 if (e.compl_text.empty())
367 FontInfo f = pi.base.font;
368 augmentFont(f, "mathnormal");
370 // draw the unique and the non-unique completion part
371 // Note: this is not time-critical as it is
372 // only done once per screen.
373 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
374 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
377 f.setColor(Color_inlinecompletion);
378 pi.pain.text(x, y, s1, f);
379 x += mathed_string_width(f, s1);
382 f.setColor(Color_nonunique_inlinecompletion);
383 pi.pain.text(x, y, s2, f);
384 x += mathed_string_width(f, s2);
390 int MathRow::kerning(BufferView const * bv) const
392 if (elements_.empty())
394 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
395 return inset ? inset->kerning(bv) : 0;
399 ostream & operator<<(ostream & os, MathRow::Element const & e)
403 os << (e.mclass == MC_OPEN ? "{" : "}");
406 os << "<" << e.before << "-"
407 << to_utf8(class_to_string(e.mclass))
408 << "-" << e.after << ">";
412 os << "\\" << to_utf8(e.inset->name())
413 << "^" << e.macro_nesting << "[";
424 os << "<" << e.before << "-[]-" << e.after << ">";
431 ostream & operator<<(ostream & os, MathRow const & mrow)
433 for (MathRow::Element const & e : mrow)