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),
46 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
48 // First there is a dummy element of type "open"
49 push_back(Element(mi, DUMMY, MC_OPEN));
51 // Then insert the MathData argument
52 bool const has_contents = ar->addToMathRow(*this, mi);
54 // A MathRow should not be completely empty
56 Element e(mi, BOX, MC_ORD);
57 // empty arrays are visible when they are editable
58 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
62 // Finally there is a dummy element of type "close"
63 push_back(Element(mi, DUMMY, MC_CLOSE));
65 /* Do spacing only in math mode. This test is a bit clumsy,
66 * but it is used in other places for guessing the current mode.
68 bool const dospacing = isMathFont(mi.base.fontname);
72 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
73 if (elements_[i].mclass != MC_UNKNOWN)
74 update_class(elements_[i].mclass, elements_[before(i)].mclass,
75 elements_[after(i)].mclass);
80 // We go to the end to handle spacing at the end of equation
81 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
82 Element & e = elements_[i];
84 Element & bef = elements_[before(i)];
85 if (dospacing && e.mclass != MC_UNKNOWN) {
86 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
88 // this is better than spc / 2 to avoid rounding problems
89 e.before += spc - spc / 2;
92 // finally reserve space for markers
93 if (bef.marker != Inset::NO_MARKER)
94 bef.after = max(bef.after, 1);
95 if (e.mclass != MC_UNKNOWN && e.marker != Inset::NO_MARKER)
96 e.before = max(e.before, 1);
97 // for linearized insets (macros...) too
98 if (e.type == BEGIN && e.marker != Inset::NO_MARKER)
99 bef.after = max(bef.after, 1);
100 if (e.type == END && e.marker != Inset::NO_MARKER) {
101 Element & aft = elements_[after(i)];
102 aft.before = max(aft.before, 1);
106 // Do not lose spacing allocated to extremities
107 if (!elements_.empty()) {
108 elements_[after(0)].before += elements_.front().after;
109 elements_[before(elements_.size() - 1)].after += elements_.back().before;
114 int MathRow::before(int i) const
118 while (elements_[i].mclass == MC_UNKNOWN);
124 int MathRow::after(int i) const
128 while (elements_[i].mclass == MC_UNKNOWN);
136 void metricsMarkersVertical(MetricsInfo const & , MathRow::Element const & e,
139 // handle vertical space for markers
141 case InsetMath::NO_MARKER:
143 case InsetMath::MARKER:
146 case InsetMath::MARKER2:
155 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
159 // In order to compute the dimension of macros and their
160 // arguments, it is necessary to keep track of them.
161 vector<pair<InsetMath const *, Dimension>> dim_insets;
162 vector<pair<MathData const *, Dimension>> dim_arrays;
163 CoordCache & coords = mi.base.bv->coordCache();
164 for (Element const & e : elements_) {
165 mi.base.macro_nesting = e.macro_nesting;
171 e.inset->metrics(mi, d);
172 d.wid += e.before + e.after;
173 coords.insets().add(e.inset, d);
177 dim_insets.push_back(make_pair(e.inset, Dimension()));
178 dim_insets.back().second.wid += e.before + e.after;
179 e.inset->beforeMetrics();
182 dim_arrays.push_back(make_pair(e.ar, Dimension()));
186 e.inset->afterMetrics();
187 LATTEST(dim_insets.back().first == e.inset);
188 Dimension & idim = dim_insets.back().second;
189 metricsMarkersVertical(mi, e, idim);
190 idim.wid += e.before + e.after;
191 coords.insets().add(e.inset, idim);
192 dim_insets.pop_back();
195 LATTEST(dim_arrays.back().first == e.ar);
196 coords.arrays().add(e.ar, dim_arrays.back().second);
197 dim_arrays.pop_back();
201 d = theFontMetrics(mi.base.font).dimension('I');
202 if (e.color != Color_none) {
203 // allow for one pixel before/after the box.
204 d.wid += e.before + e.after + 2;
206 // hide the box, but give it some height
214 // Now add the dimension to current macros and arguments.
215 for (auto & dim_macro : dim_insets)
216 dim_macro.second += d;
217 for (auto & dim_array : dim_arrays)
218 dim_array.second += d;
221 if (e.compl_text.empty())
223 FontInfo font = mi.base.font;
224 augmentFont(font, "mathnormal");
225 dim.wid += mathed_string_width(font, e.compl_text);
227 LATTEST(dim_insets.empty() && dim_arrays.empty());
233 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
235 if (e.marker == InsetMath::NO_MARKER)
238 CoordCache const & coords = pi.base.bv->coordCache();
239 Dimension const dim = coords.getInsets().dim(e.inset);
241 // the marker is before/after the inset. Normally some space has been reserved already.
242 int const l = x + e.before - 1;
243 int const r = x + dim.width() - e.after;
245 // Duplicated from Inset.cpp and adapted. It is believed that the
246 // Inset version should die eventually
247 ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
248 Color_mathframe : Color_mathcorners;
250 int const d = y + dim.descent();
251 pi.pain.line(l, d - 3, l, d, pen_color);
252 pi.pain.line(r, d - 3, r, d, pen_color);
253 pi.pain.line(l, d, l + 3, d, pen_color);
254 pi.pain.line(r - 3, d, r, d, pen_color);
256 if (e.marker == InsetMath::MARKER)
259 int const a = y - dim.ascent();
260 pi.pain.line(l, a + 3, l, a, pen_color);
261 pi.pain.line(r, a + 3, r, a, pen_color);
262 pi.pain.line(l, a, l + 3, a, pen_color);
263 pi.pain.line(r - 3, a, r, a, pen_color);
268 void MathRow::draw(PainterInfo & pi, int x, int const y) const
270 CoordCache & coords = pi.base.bv->coordCache();
271 for (Element const & e : elements_) {
274 // This is hackish: the math inset does not know that space
275 // has been added before and after it; we alter its dimension
276 // while it is drawing, because it relies on this value.
277 Dimension const d = coords.insets().dim(e.inset);
279 d2.wid -= e.before + e.after;
280 coords.insets().add(e.inset, d2);
281 e.inset->drawSelection(pi, x + e.before, y);
282 e.inset->draw(pi, x + e.before, y);
283 coords.insets().add(e.inset, x, y);
284 coords.insets().add(e.inset, d);
285 drawMarkers(pi, e, x, y);
291 coords.insets().add(e.inset, x, y);
292 drawMarkers(pi, e, x, y);
293 e.inset->beforeDraw(pi);
295 x += e.before + e.after;
297 coords.arrays().add(e.ar, x, y);
301 e.inset->afterDraw(pi);
302 x += e.before + e.after;
305 if (e.color == Color_none)
307 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
308 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
309 d.width() - 1, d.height() - 1, e.color);
310 x += d.wid + 2 + e.before + e.after;
317 if (e.compl_text.empty())
319 FontInfo f = pi.base.font;
320 augmentFont(f, "mathnormal");
322 // draw the unique and the non-unique completion part
323 // Note: this is not time-critical as it is
324 // only done once per screen.
325 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
326 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
329 f.setColor(Color_inlinecompletion);
330 pi.pain.text(x, y, s1, f);
331 x += mathed_string_width(f, s1);
334 f.setColor(Color_nonunique_inlinecompletion);
335 pi.pain.text(x, y, s2, f);
336 x += mathed_string_width(f, s2);
342 int MathRow::kerning(BufferView const * bv) const
344 if (elements_.empty())
346 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
347 return inset ? inset->kerning(bv) : 0;
351 ostream & operator<<(ostream & os, MathRow::Element const & e)
355 os << (e.mclass == MC_OPEN ? "{" : "}");
358 os << "<" << e.before << "-"
359 << to_utf8(class_to_string(e.mclass))
360 << "-" << e.after << ">";
364 os << "\\" << to_utf8(e.inset->name())
365 << "^" << e.macro_nesting << "[";
376 os << "<" << e.before << "-[]-" << e.after << ">";
383 ostream & operator<<(ostream & os, MathRow const & mrow)
385 for (MathRow::Element const & e : mrow)