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"
21 #include "CoordCache.h"
22 #include "MetricsInfo.h"
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/lassert.h"
39 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
40 : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
41 marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
48 // Helper functions for markers
50 int markerMargin(MathRow::Element const & e)
53 case InsetMath::MARKER:
54 case InsetMath::MARKER2:
55 case InsetMath::BOX_MARKER:
57 case InsetMath::NO_MARKER:
65 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
68 // handle vertical space for markers
70 case InsetMath::NO_MARKER:
72 case InsetMath::MARKER:
75 case InsetMath::MARKER2:
79 case InsetMath::BOX_MARKER:
81 font.setSize(FONT_SIZE_TINY);
83 mathed_string_dim(font, e.inset->name(), namedim);
84 int const namewid = 1 + namedim.wid + 1;
86 if (namewid > dim.wid)
87 e.after += namewid - dim.wid;
89 dim.des += 3 + namedim.height();
94 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
95 int const x, int const y)
97 if (e.marker == InsetMath::NO_MARKER)
101 bool const highlight = e.inset->mouseHovered(pi.base.bv)
102 || e.inset->editing(pi.base.bv);
103 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
105 CoordCache const & coords = pi.base.bv->coordCache();
106 Dimension const dim = coords.getInsets().dim(e.inset);
108 // the marker is before/after the inset. Necessary space has been reserved already.
109 int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
110 int const r = x + dim.width() - e.after;
113 if (e.marker == InsetMath::BOX_MARKER) {
114 // draw header and rectangle around
116 font.setSize(FONT_SIZE_TINY);
117 font.setColor(Color_mathmacrolabel);
119 mathed_string_dim(font, e.inset->name(), namedim);
120 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
121 dim.wid, namedim.height() + 2, Color_mathmacrobg);
122 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
126 if (e.marker == InsetMath::MARKER
127 || e.marker == InsetMath::MARKER2) {
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);
136 if (e.marker == InsetMath::BOX_MARKER
137 || e.marker == InsetMath::MARKER2) {
138 int const a = y - dim.ascent();
139 pi.pain.line(l, a + 3, l, a, pen_color);
140 pi.pain.line(r, a + 3, r, a, pen_color);
141 pi.pain.line(l, a, l + 3, a, pen_color);
142 pi.pain.line(r - 3, a, r, a, pen_color);
149 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
151 // First there is a dummy element of type "open"
152 push_back(Element(mi, DUMMY, MC_OPEN));
154 // Then insert the MathData argument
155 bool const has_contents = ar->addToMathRow(*this, mi);
157 // A MathRow should not be completely empty
159 Element e(mi, BOX, MC_ORD);
160 // empty arrays are visible when they are editable
161 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
165 // Finally there is a dummy element of type "close"
166 push_back(Element(mi, DUMMY, MC_CLOSE));
168 /* Do spacing only in math mode. This test is a bit clumsy,
169 * but it is used in other places for guessing the current mode.
171 bool const dospacing = isMathFont(mi.base.fontname);
175 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
176 if (elements_[i].mclass != MC_UNKNOWN)
177 update_class(elements_[i].mclass, elements_[before(i)].mclass,
178 elements_[after(i)].mclass);
183 // We go to the end to handle spacing at the end of equation
184 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
185 Element & e = elements_[i];
187 Element & bef = elements_[before(i)];
188 if (dospacing && e.mclass != MC_UNKNOWN) {
189 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
190 bef.after += spc / 2;
191 // this is better than spc / 2 to avoid rounding problems
192 e.before += spc - spc / 2;
195 // finally reserve space for markers
196 bef.after = max(bef.after, markerMargin(bef));
197 if (e.mclass != MC_UNKNOWN)
198 e.before = max(e.before, markerMargin(e));
199 // for linearized insets (macros...) too
201 bef.after = max(bef.after, markerMargin(e));
202 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
203 Element & aft = elements_[after(i)];
204 aft.before = max(aft.before, markerMargin(e));
208 // Do not lose spacing allocated to extremities
209 if (!elements_.empty()) {
210 elements_[after(0)].before += elements_.front().after;
211 elements_[before(elements_.size() - 1)].after += elements_.back().before;
216 int MathRow::before(int i) const
220 while (elements_[i].mclass == MC_UNKNOWN);
226 int MathRow::after(int i) const
230 while (elements_[i].mclass == MC_UNKNOWN);
236 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
240 // In order to compute the dimension of macros and their
241 // arguments, it is necessary to keep track of them.
242 vector<pair<InsetMath const *, Dimension>> dim_insets;
243 vector<pair<MathData const *, Dimension>> dim_arrays;
244 CoordCache & coords = mi.base.bv->coordCache();
245 for (Element & e : elements_) {
246 mi.base.macro_nesting = e.macro_nesting;
252 e.inset->metrics(mi, d);
253 d.wid += e.before + e.after;
254 coords.insets().add(e.inset, d);
258 dim_insets.push_back(make_pair(e.inset, Dimension()));
259 dim_insets.back().second.wid += e.before + e.after;
260 d.wid = e.before + e.after;
261 e.inset->beforeMetrics();
264 dim_arrays.push_back(make_pair(e.ar, Dimension()));
268 e.inset->afterMetrics();
269 LATTEST(dim_insets.back().first == e.inset);
270 d = dim_insets.back().second;
271 afterMetricsMarkers(mi, e, d);
272 d.wid += e.before + e.after;
273 coords.insets().add(e.inset, d);
274 dim_insets.pop_back();
275 // We do not want to count the width again, but the
276 // padding and the vertical dimension are meaningful.
277 d.wid = e.before + e.after;
280 LATTEST(dim_arrays.back().first == e.ar);
281 coords.arrays().add(e.ar, dim_arrays.back().second);
282 dim_arrays.pop_back();
286 d = theFontMetrics(mi.base.font).dimension('I');
287 if (e.color != Color_none) {
288 // allow for one pixel before/after the box.
289 d.wid += e.before + e.after + 2;
291 // hide the box, but keep its height
299 // Now add the dimension to current macros and arguments.
300 for (auto & dim_macro : dim_insets)
301 dim_macro.second += d;
302 for (auto & dim_array : dim_arrays)
303 dim_array.second += d;
306 if (e.compl_text.empty())
308 FontInfo font = mi.base.font;
309 augmentFont(font, "mathnormal");
310 dim.wid += mathed_string_width(font, e.compl_text);
312 LATTEST(dim_insets.empty() && dim_arrays.empty());
316 void MathRow::draw(PainterInfo & pi, int x, int const y) const
318 CoordCache & coords = pi.base.bv->coordCache();
319 for (Element const & e : elements_) {
322 // This is hackish: the math inset does not know that space
323 // has been added before and after it; we alter its dimension
324 // while it is drawing, because it relies on this value.
325 Dimension const d = coords.insets().dim(e.inset);
327 d2.wid -= e.before + e.after;
328 coords.insets().add(e.inset, d2);
329 e.inset->draw(pi, x + e.before, y);
330 coords.insets().add(e.inset, x, y);
331 coords.insets().add(e.inset, d);
332 drawMarkers(pi, e, x, y);
338 coords.arrays().add(e.ar, x, y);
339 e.ar->drawSelection(pi, x, y);
342 coords.insets().add(e.inset, x, y);
343 drawMarkers(pi, e, x, y);
344 e.inset->beforeDraw(pi);
346 x += e.before + e.after;
350 e.inset->afterDraw(pi);
351 x += e.before + e.after;
354 if (e.color == Color_none)
356 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
357 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
358 d.width() - 1, d.height() - 1, e.color);
359 x += d.wid + 2 + e.before + e.after;
366 if (e.compl_text.empty())
368 FontInfo f = pi.base.font;
369 augmentFont(f, "mathnormal");
371 // draw the unique and the non-unique completion part
372 // Note: this is not time-critical as it is
373 // only done once per screen.
374 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
375 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
378 f.setColor(Color_inlinecompletion);
379 pi.pain.text(x, y, s1, f);
380 x += mathed_string_width(f, s1);
383 f.setColor(Color_nonunique_inlinecompletion);
384 pi.pain.text(x, y, s2, f);
385 x += mathed_string_width(f, s2);
391 int MathRow::kerning(BufferView const * bv) const
393 if (elements_.empty())
395 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
396 return inset ? inset->kerning(bv) : 0;
400 ostream & operator<<(ostream & os, MathRow::Element const & e)
404 os << (e.mclass == MC_OPEN ? "{" : "}");
407 os << "<" << e.before << "-"
408 << to_utf8(class_to_string(e.mclass))
409 << "-" << e.after << ">";
413 os << "\\" << to_utf8(e.inset->name())
414 << "^" << e.macro_nesting << "[";
425 os << "<" << e.before << "-[]-" << e.after << ">";
432 ostream & operator<<(ostream & os, MathRow const & mrow)
434 for (MathRow::Element const & e : mrow)