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 if (e.mclass == MC_UNKNOWN)
86 Element & bef = elements_[before(i)];
88 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
90 // this is better than spc / 2 to avoid rounding problems
91 e.before += spc - spc / 2;
94 // finally reserve space for markers
95 if (bef.marker != Inset::NO_MARKER)
96 bef.after = max(bef.after, 1);
97 if (e.marker != Inset::NO_MARKER)
98 e.before = max(e.before, 1);
101 // Do not lose spacing allocated to extremities
102 if (!elements_.empty()) {
103 elements_[after(0)].before += elements_.front().after;
104 elements_[before(elements_.size() - 1)].after += elements_.back().before;
109 int MathRow::before(int i) const
113 while (elements_[i].mclass == MC_UNKNOWN);
119 int MathRow::after(int i) const
123 while (elements_[i].mclass == MC_UNKNOWN);
129 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
133 // In order to compute the dimension of macros and their
134 // arguments, it is necessary to keep track of them.
135 vector<pair<InsetMath const *, Dimension>> dim_insets;
136 vector<pair<MathData const *, Dimension>> dim_arrays;
137 CoordCache & coords = mi.base.bv->coordCache();
138 for (Element const & e : elements_) {
139 mi.base.macro_nesting = e.macro_nesting;
145 e.inset->metrics(mi, d);
146 d.wid += e.before + e.after;
147 coords.insets().add(e.inset, d);
151 dim_insets.push_back(make_pair(e.inset, Dimension()));
152 e.inset->beforeMetrics();
155 dim_arrays.push_back(make_pair(e.ar, Dimension()));
159 e.inset->afterMetrics();
160 LATTEST(dim_insets.back().first == e.inset);
161 coords.insets().add(e.inset, dim_insets.back().second);
162 dim_insets.pop_back();
165 LATTEST(dim_arrays.back().first == e.ar);
166 coords.arrays().add(e.ar, dim_arrays.back().second);
167 dim_arrays.pop_back();
171 d = theFontMetrics(mi.base.font).dimension('I');
172 if (e.color != Color_none) {
173 // allow for one pixel before/after the box.
174 d.wid += e.before + e.after + 2;
176 // hide the box, but give it some height
182 // handle vertical space for markers
184 case InsetMath::NO_MARKER:
186 case InsetMath::MARKER:
189 case InsetMath::MARKER2:
196 // Now add the dimension to current macros and arguments.
197 for (auto & dim_macro : dim_insets)
198 dim_macro.second += d;
199 for (auto & dim_array : dim_arrays)
200 dim_array.second += d;
203 if (e.compl_text.empty())
205 FontInfo font = mi.base.font;
206 augmentFont(font, "mathnormal");
207 dim.wid += mathed_string_width(font, e.compl_text);
209 LATTEST(dim_insets.empty() && dim_arrays.empty());
215 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
217 if (e.marker == InsetMath::NO_MARKER)
220 CoordCache const & coords = pi.base.bv->coordCache();
221 Dimension const dim = coords.getInsets().dim(e.inset);
223 // the marker is before/after the inset. Normally some space has been reserved already.
224 int const l = x + e.before - 1;
225 int const r = x + dim.width() - e.after;
227 // Duplicated from Inset.cpp and adapted. It is believed that the
228 // Inset version should die eventually
229 ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
230 Color_mathframe : Color_mathcorners;
232 int const d = y + dim.descent();
233 pi.pain.line(l, d - 3, l, d, pen_color);
234 pi.pain.line(r, d - 3, r, d, pen_color);
235 pi.pain.line(l, d, l + 3, d, pen_color);
236 pi.pain.line(r - 3, d, r, d, pen_color);
238 if (e.marker == InsetMath::MARKER)
241 int const a = y - dim.ascent();
242 pi.pain.line(l, a + 3, l, a, pen_color);
243 pi.pain.line(r, a + 3, r, a, pen_color);
244 pi.pain.line(l, a, l + 3, a, pen_color);
245 pi.pain.line(r - 3, a, r, a, pen_color);
250 void MathRow::draw(PainterInfo & pi, int x, int const y) const
252 CoordCache & coords = pi.base.bv->coordCache();
253 for (Element const & e : elements_) {
256 // This is hackish: the math inset does not know that space
257 // has been added before and after it; we alter its dimension
258 // while it is drawing, because it relies on this value.
259 Dimension const d = coords.insets().dim(e.inset);
261 d2.wid -= e.before + e.after;
262 coords.insets().add(e.inset, d2);
263 e.inset->drawSelection(pi, x + e.before, y);
264 e.inset->draw(pi, x + e.before, y);
265 coords.insets().add(e.inset, x, y);
266 coords.insets().add(e.inset, d);
267 drawMarkers(pi, e, x, y);
273 coords.insets().add(e.inset, x, y);
274 drawMarkers(pi, e, x, y);
275 e.inset->beforeDraw(pi);
278 coords.arrays().add(e.ar, x, y);
282 e.inset->afterDraw(pi);
285 if (e.color == Color_none)
287 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
288 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
289 d.width() - 1, d.height() - 1, e.color);
290 x += d.wid + 2 + e.before + e.after;
297 if (e.compl_text.empty())
299 FontInfo f = pi.base.font;
300 augmentFont(f, "mathnormal");
302 // draw the unique and the non-unique completion part
303 // Note: this is not time-critical as it is
304 // only done once per screen.
305 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
306 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
309 f.setColor(Color_inlinecompletion);
310 pi.pain.text(x, y, s1, f);
311 x += mathed_string_width(f, s1);
314 f.setColor(Color_nonunique_inlinecompletion);
315 pi.pain.text(x, y, s2, f);
316 x += mathed_string_width(f, s2);
322 int MathRow::kerning(BufferView const * bv) const
324 if (elements_.empty())
326 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
327 return inset ? inset->kerning(bv) : 0;
331 ostream & operator<<(ostream & os, MathRow::Element const & e)
335 os << (e.mclass == MC_OPEN ? "{" : "}");
338 os << "<" << e.before << "-"
339 << to_utf8(class_to_string(e.mclass))
340 << "-" << e.after << ">";
344 os << "\\" << to_utf8(e.inset->name())
345 << "^" << e.macro_nesting << "[";
356 os << "<" << e.before << "-[]-" << e.after << ">";
363 ostream & operator<<(ostream & os, MathRow const & mrow)
365 for (MathRow::Element const & e : mrow)