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);
125 // Lower corners in all cases
126 int const d = y + dim.descent();
127 pi.pain.line(l, d - 3, l, d, pen_color);
128 pi.pain.line(r, d - 3, r, d, pen_color);
129 pi.pain.line(l, d, l + 3, d, pen_color);
130 pi.pain.line(r - 3, d, r, d, pen_color);
133 if (e.marker == InsetMath::BOX_MARKER
134 || e.marker == InsetMath::MARKER2) {
135 int const a = y - dim.ascent();
136 pi.pain.line(l, a + 3, l, a, pen_color);
137 pi.pain.line(r, a + 3, r, a, pen_color);
138 pi.pain.line(l, a, l + 3, a, pen_color);
139 pi.pain.line(r - 3, a, r, a, pen_color);
146 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
148 // First there is a dummy element of type "open"
149 push_back(Element(mi, DUMMY, MC_OPEN));
151 // Then insert the MathData argument
152 bool const has_contents = ar->addToMathRow(*this, mi);
154 // A MathRow should not be completely empty
156 Element e(mi, BOX, MC_ORD);
157 // empty arrays are visible when they are editable
158 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
162 // Finally there is a dummy element of type "close"
163 push_back(Element(mi, DUMMY, MC_CLOSE));
165 /* Do spacing only in math mode. This test is a bit clumsy,
166 * but it is used in other places for guessing the current mode.
168 bool const dospacing = isMathFont(mi.base.fontname);
172 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
173 if (elements_[i].mclass != MC_UNKNOWN)
174 update_class(elements_[i].mclass, elements_[before(i)].mclass,
175 elements_[after(i)].mclass);
180 // We go to the end to handle spacing at the end of equation
181 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
182 Element & e = elements_[i];
184 Element & bef = elements_[before(i)];
185 if (dospacing && e.mclass != MC_UNKNOWN) {
186 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
187 bef.after += spc / 2;
188 // this is better than spc / 2 to avoid rounding problems
189 e.before += spc - spc / 2;
192 // finally reserve space for markers
193 bef.after = max(bef.after, markerMargin(bef));
194 if (e.mclass != MC_UNKNOWN)
195 e.before = max(e.before, markerMargin(e));
196 // for linearized insets (macros...) too
198 bef.after = max(bef.after, markerMargin(e));
199 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
200 Element & aft = elements_[after(i)];
201 aft.before = max(aft.before, markerMargin(e));
205 // Do not lose spacing allocated to extremities
206 if (!elements_.empty()) {
207 elements_[after(0)].before += elements_.front().after;
208 elements_[before(elements_.size() - 1)].after += elements_.back().before;
213 int MathRow::before(int i) const
217 while (elements_[i].mclass == MC_UNKNOWN);
223 int MathRow::after(int i) const
227 while (elements_[i].mclass == MC_UNKNOWN);
233 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
237 // In order to compute the dimension of macros and their
238 // arguments, it is necessary to keep track of them.
239 vector<pair<InsetMath const *, Dimension>> dim_insets;
240 vector<pair<MathData const *, Dimension>> dim_arrays;
241 CoordCache & coords = mi.base.bv->coordCache();
242 for (Element & e : elements_) {
243 mi.base.macro_nesting = e.macro_nesting;
249 e.inset->metrics(mi, d);
250 d.wid += e.before + e.after;
251 coords.insets().add(e.inset, d);
255 dim_insets.push_back(make_pair(e.inset, Dimension()));
256 dim_insets.back().second.wid += e.before + e.after;
257 d.wid = e.before + e.after;
258 e.inset->beforeMetrics();
261 dim_arrays.push_back(make_pair(e.ar, Dimension()));
265 e.inset->afterMetrics();
266 LATTEST(dim_insets.back().first == e.inset);
267 d = dim_insets.back().second;
268 afterMetricsMarkers(mi, e, d);
269 d.wid += e.before + e.after;
270 coords.insets().add(e.inset, d);
271 dim_insets.pop_back();
272 // We do not want to count the width again, but the
273 // padding and the vertical dimension are meaningful.
274 d.wid = e.before + e.after;
277 LATTEST(dim_arrays.back().first == e.ar);
278 coords.arrays().add(e.ar, dim_arrays.back().second);
279 dim_arrays.pop_back();
283 d = theFontMetrics(mi.base.font).dimension('I');
284 if (e.color != Color_none) {
285 // allow for one pixel before/after the box.
286 d.wid += e.before + e.after + 2;
288 // hide the box, but keep its height
296 // Now add the dimension to current macros and arguments.
297 for (auto & dim_macro : dim_insets)
298 dim_macro.second += d;
299 for (auto & dim_array : dim_arrays)
300 dim_array.second += d;
303 if (e.compl_text.empty())
305 FontInfo font = mi.base.font;
306 augmentFont(font, "mathnormal");
307 dim.wid += mathed_string_width(font, e.compl_text);
309 LATTEST(dim_insets.empty() && dim_arrays.empty());
313 void MathRow::draw(PainterInfo & pi, int x, int const y) const
315 CoordCache & coords = pi.base.bv->coordCache();
316 for (Element const & e : elements_) {
319 // This is hackish: the math inset does not know that space
320 // has been added before and after it; we alter its dimension
321 // while it is drawing, because it relies on this value.
322 Dimension const d = coords.insets().dim(e.inset);
324 d2.wid -= e.before + e.after;
325 coords.insets().add(e.inset, d2);
326 e.inset->draw(pi, x + e.before, y);
327 coords.insets().add(e.inset, x, y);
328 coords.insets().add(e.inset, d);
329 drawMarkers(pi, e, x, y);
335 coords.arrays().add(e.ar, x, y);
336 e.ar->drawSelection(pi, x, y);
339 coords.insets().add(e.inset, x, y);
340 drawMarkers(pi, e, x, y);
341 e.inset->beforeDraw(pi);
343 x += e.before + e.after;
347 e.inset->afterDraw(pi);
348 x += e.before + e.after;
351 if (e.color == Color_none)
353 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
354 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
355 d.width() - 1, d.height() - 1, e.color);
356 x += d.wid + 2 + e.before + e.after;
363 if (e.compl_text.empty())
365 FontInfo f = pi.base.font;
366 augmentFont(f, "mathnormal");
368 // draw the unique and the non-unique completion part
369 // Note: this is not time-critical as it is
370 // only done once per screen.
371 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
372 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
375 f.setColor(Color_inlinecompletion);
376 pi.pain.text(x, y, s1, f);
377 x += mathed_string_width(f, s1);
380 f.setColor(Color_nonunique_inlinecompletion);
381 pi.pain.text(x, y, s2, f);
382 x += mathed_string_width(f, s2);
388 int MathRow::kerning(BufferView const * bv) const
390 if (elements_.empty())
392 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
393 return inset ? inset->kerning(bv) : 0;
397 ostream & operator<<(ostream & os, MathRow::Element const & e)
401 os << (e.mclass == MC_OPEN ? "{" : "}");
404 os << "<" << e.before << "-"
405 << to_utf8(class_to_string(e.mclass))
406 << "-" << e.after << ">";
410 os << "\\" << to_utf8(e.inset->name())
411 << "^" << e.macro_nesting << "[";
422 os << "<" << e.before << "-[]-" << e.after << ">";
429 ostream & operator<<(ostream & os, MathRow const & mrow)
431 for (MathRow::Element const & e : mrow)