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"
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(0), compl_unique_to(0), ar(0),
45 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
47 // First there is a dummy element of type "open"
48 push_back(Element(mi, DUMMY, MC_OPEN));
50 // Then insert the MathData argument
51 bool const has_contents = ar->addToMathRow(*this, mi);
53 // A MathRow should not be completely empty
55 Element e(mi, BOX, MC_ORD);
56 // empty arrays are visible when they are editable
57 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
61 // Finally there is a dummy element of type "close"
62 push_back(Element(mi, DUMMY, MC_CLOSE));
64 /* Do spacing only in math mode. This test is a bit clumsy,
65 * but it is used in other places for guessing the current mode.
67 bool const dospacing = isMathFont(mi.base.fontname);
71 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
72 if (elements_[i].mclass != MC_UNKNOWN)
73 update_class(elements_[i].mclass, elements_[before(i)].mclass,
74 elements_[after(i)].mclass);
79 // We go to the end to handle spacing at the end of equation
80 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
81 Element & e = elements_[i];
83 Element & bef = elements_[before(i)];
84 if (dospacing && e.mclass != MC_UNKNOWN) {
85 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
87 // this is better than spc / 2 to avoid rounding problems
88 e.before += spc - spc / 2;
91 // finally reserve space for markers
92 if (bef.marker != Inset::NO_MARKER)
93 bef.after = max(bef.after, 1);
94 if (e.mclass != MC_UNKNOWN && e.marker != Inset::NO_MARKER)
95 e.before = max(e.before, 1);
96 // for linearized insets (macros...) too
97 if (e.type == BEGIN && e.marker != Inset::NO_MARKER)
98 bef.after = max(bef.after, 1);
99 if (e.type == END && e.marker != Inset::NO_MARKER) {
100 Element & aft = elements_[after(i)];
101 aft.before = max(aft.before, 1);
105 // Do not lose spacing allocated to extremities
106 if (!elements_.empty()) {
107 elements_[after(0)].before += elements_.front().after;
108 elements_[before(elements_.size() - 1)].after += elements_.back().before;
113 int MathRow::before(int i) const
117 while (elements_[i].mclass == MC_UNKNOWN);
123 int MathRow::after(int i) const
127 while (elements_[i].mclass == MC_UNKNOWN);
135 void metricsMarkersVertical(MetricsInfo const & , MathRow::Element const & e,
138 // handle vertical space for markers
140 case InsetMath::NO_MARKER:
142 case InsetMath::MARKER:
145 case InsetMath::MARKER2:
154 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
158 // In order to compute the dimension of macros and their
159 // arguments, it is necessary to keep track of them.
160 vector<pair<InsetMath const *, Dimension>> dim_insets;
161 vector<pair<MathData const *, Dimension>> dim_arrays;
162 CoordCache & coords = mi.base.bv->coordCache();
163 for (Element const & e : elements_) {
164 mi.base.macro_nesting = e.macro_nesting;
170 e.inset->metrics(mi, d);
171 d.wid += e.before + e.after;
172 coords.insets().add(e.inset, d);
176 dim_insets.push_back(make_pair(e.inset, Dimension()));
177 dim_insets.back().second.wid += e.before + e.after;
178 e.inset->beforeMetrics();
181 dim_arrays.push_back(make_pair(e.ar, Dimension()));
185 e.inset->afterMetrics();
186 LATTEST(dim_insets.back().first == e.inset);
187 Dimension & idim = dim_insets.back().second;
188 metricsMarkersVertical(mi, e, idim);
189 idim.wid += e.before + e.after;
190 coords.insets().add(e.inset, idim);
191 dim_insets.pop_back();
194 LATTEST(dim_arrays.back().first == e.ar);
195 coords.arrays().add(e.ar, dim_arrays.back().second);
196 dim_arrays.pop_back();
200 d = theFontMetrics(mi.base.font).dimension('I');
201 if (e.color != Color_none) {
202 // allow for one pixel before/after the box.
203 d.wid += e.before + e.after + 2;
205 // hide the box, but give it some height
213 // Now add the dimension to current macros and arguments.
214 for (auto & dim_macro : dim_insets)
215 dim_macro.second += d;
216 for (auto & dim_array : dim_arrays)
217 dim_array.second += d;
220 if (e.compl_text.empty())
222 FontInfo font = mi.base.font;
223 augmentFont(font, "mathnormal");
224 dim.wid += mathed_string_width(font, e.compl_text);
226 LATTEST(dim_insets.empty() && dim_arrays.empty());
232 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
234 if (e.marker == InsetMath::NO_MARKER)
237 CoordCache const & coords = pi.base.bv->coordCache();
238 Dimension const dim = coords.getInsets().dim(e.inset);
240 // the marker is before/after the inset. Normally some space has been reserved already.
241 int const l = x + e.before - 1;
242 int const r = x + dim.width() - e.after;
244 // Duplicated from Inset.cpp and adapted. It is believed that the
245 // Inset version should die eventually
246 ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
247 Color_mathframe : Color_mathcorners;
249 int const d = y + dim.descent();
250 pi.pain.line(l, d - 3, l, d, pen_color);
251 pi.pain.line(r, d - 3, r, d, pen_color);
252 pi.pain.line(l, d, l + 3, d, pen_color);
253 pi.pain.line(r - 3, d, r, d, pen_color);
255 if (e.marker == InsetMath::MARKER)
258 int const a = y - dim.ascent();
259 pi.pain.line(l, a + 3, l, a, pen_color);
260 pi.pain.line(r, a + 3, r, a, pen_color);
261 pi.pain.line(l, a, l + 3, a, pen_color);
262 pi.pain.line(r - 3, a, r, a, pen_color);
267 void MathRow::draw(PainterInfo & pi, int x, int const y) const
269 CoordCache & coords = pi.base.bv->coordCache();
270 for (Element const & e : elements_) {
273 // This is hackish: the math inset does not know that space
274 // has been added before and after it; we alter its dimension
275 // while it is drawing, because it relies on this value.
276 Dimension const d = coords.insets().dim(e.inset);
278 d2.wid -= e.before + e.after;
279 coords.insets().add(e.inset, d2);
280 e.inset->drawSelection(pi, x + e.before, y);
281 e.inset->draw(pi, x + e.before, y);
282 coords.insets().add(e.inset, x, y);
283 coords.insets().add(e.inset, d);
284 drawMarkers(pi, e, x, y);
290 coords.insets().add(e.inset, x, y);
291 drawMarkers(pi, e, x, y);
292 e.inset->beforeDraw(pi);
294 x += e.before + e.after;
296 coords.arrays().add(e.ar, x, y);
300 e.inset->afterDraw(pi);
301 x += e.before + e.after;
304 if (e.color == Color_none)
306 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
307 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
308 d.width() - 1, d.height() - 1, e.color);
309 x += d.wid + 2 + e.before + e.after;
316 if (e.compl_text.empty())
318 FontInfo f = pi.base.font;
319 augmentFont(f, "mathnormal");
321 // draw the unique and the non-unique completion part
322 // Note: this is not time-critical as it is
323 // only done once per screen.
324 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
325 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
328 f.setColor(Color_inlinecompletion);
329 pi.pain.text(x, y, s1, f);
330 x += mathed_string_width(f, s1);
333 f.setColor(Color_nonunique_inlinecompletion);
334 pi.pain.text(x, y, s2, f);
335 x += mathed_string_width(f, s2);
341 int MathRow::kerning(BufferView const * bv) const
343 if (elements_.empty())
345 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
346 return inset ? inset->kerning(bv) : 0;
350 ostream & operator<<(ostream & os, MathRow::Element const & e)
354 os << (e.mclass == MC_OPEN ? "{" : "}");
357 os << "<" << e.before << "-"
358 << to_utf8(class_to_string(e.mclass))
359 << "-" << e.after << ">";
363 os << "\\" << to_utf8(e.inset->name())
364 << "^" << e.macro_nesting << "[";
375 os << "<" << e.before << "-[]-" << e.after << ">";
382 ostream & operator<<(ostream & os, MathRow const & mrow)
384 for (MathRow::Element const & e : mrow)