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),
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 map<InsetMath const *, Dimension> dim_insets;
136 map<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);
150 e.inset->beforeMetrics();
151 // Add a macro to current list
152 dim_insets[e.inset] = Dimension();
155 LATTEST(dim_insets.find(e.inset) != dim_insets.end());
156 e.inset->afterMetrics();
157 // Cache the dimension of the macro and remove it from
159 coords.insets().add(e.inset, dim_insets[e.inset]);
160 dim_insets.erase(e.inset);
162 // This is basically like macros
164 e.inset->beforeMetrics();
165 dim_arrays[e.ar] = Dimension();
168 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
169 e.inset->afterMetrics();
170 coords.arrays().add(e.ar, dim_arrays[e.ar]);
171 dim_arrays.erase(e.ar);
174 d = theFontMetrics(mi.base.font).dimension('I');
175 if (e.color != Color_none) {
176 // allow for one pixel before/after the box.
177 d.wid += e.before + e.after + 2;
179 // hide the box, but give it some height
185 // handle vertical space for markers
187 case InsetMath::NO_MARKER:
189 case InsetMath::MARKER:
192 case InsetMath::MARKER2:
199 // Now add the dimension to current macros and arguments.
200 for (auto & dim_macro : dim_insets)
201 dim_macro.second += d;
202 for (auto & dim_array : dim_arrays)
203 dim_array.second += d;
206 if (e.compl_text.empty())
208 FontInfo font = mi.base.font;
209 augmentFont(font, "mathnormal");
210 dim.wid += mathed_string_width(font, e.compl_text);
212 LATTEST(dim_insets.empty() && dim_arrays.empty());
218 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
220 if (e.marker == InsetMath::NO_MARKER)
223 CoordCache const & coords = pi.base.bv->coordCache();
224 Dimension const dim = coords.getInsets().dim(e.inset);
226 // the marker is before/after the inset. Normally some space has been reserved already.
227 int const l = x + e.before - 1;
228 int const r = x + dim.width() - e.after;
230 // Duplicated from Inset.cpp and adapted. It is believed that the
231 // Inset version should die eventually
232 ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
233 Color_mathframe : Color_mathcorners;
235 int const d = y + dim.descent();
236 pi.pain.line(l, d - 3, l, d, pen_color);
237 pi.pain.line(r, d - 3, r, d, pen_color);
238 pi.pain.line(l, d, l + 3, d, pen_color);
239 pi.pain.line(r - 3, d, r, d, pen_color);
241 if (e.marker == InsetMath::MARKER)
244 int const a = y - dim.ascent();
245 pi.pain.line(l, a + 3, l, a, pen_color);
246 pi.pain.line(r, a + 3, r, a, pen_color);
247 pi.pain.line(l, a, l + 3, a, pen_color);
248 pi.pain.line(r - 3, a, r, a, pen_color);
253 void MathRow::draw(PainterInfo & pi, int x, int const y) const
255 CoordCache & coords = pi.base.bv->coordCache();
256 for (Element const & e : elements_) {
259 // This is hackish: the math inset does not know that space
260 // has been added before and after it; we alter its dimension
261 // while it is drawing, because it relies on this value.
262 Dimension const d = coords.insets().dim(e.inset);
264 d2.wid -= e.before + e.after;
265 coords.insets().add(e.inset, d2);
266 e.inset->drawSelection(pi, x + e.before, y);
267 e.inset->draw(pi, x + e.before, y);
268 coords.insets().add(e.inset, x, y);
269 coords.insets().add(e.inset, d);
270 drawMarkers(pi, e, x, y);
275 coords.insets().add(e.inset, x, y);
276 drawMarkers(pi, e, x, y);
277 e.inset->beforeDraw(pi);
280 e.inset->afterDraw(pi);
283 coords.arrays().add(e.ar, x, y);
284 e.inset->beforeDraw(pi);
287 e.inset->afterDraw(pi);
290 if (e.color == Color_none)
292 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
293 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
294 d.width() - 1, d.height() - 1, e.color);
295 x += d.wid + 2 + e.before + e.after;
302 if (e.compl_text.empty())
304 FontInfo f = pi.base.font;
305 augmentFont(f, "mathnormal");
307 // draw the unique and the non-unique completion part
308 // Note: this is not time-critical as it is
309 // only done once per screen.
310 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
311 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
314 f.setColor(Color_inlinecompletion);
315 pi.pain.text(x, y, s1, f);
316 x += mathed_string_width(f, s1);
319 f.setColor(Color_nonunique_inlinecompletion);
320 pi.pain.text(x, y, s2, f);
321 x += mathed_string_width(f, s2);
327 int MathRow::kerning(BufferView const * bv) const
329 if (elements_.empty())
331 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
332 return inset ? inset->kerning(bv) : 0;
336 ostream & operator<<(ostream & os, MathRow::Element const & e)
340 os << (e.mclass == MC_OPEN ? "{" : "}");
343 os << "<" << e.before << "-"
344 << to_utf8(class_to_string(e.mclass))
345 << "-" << e.after << ">";
347 case MathRow::BEG_MACRO:
348 os << "\\" << to_utf8(e.inset->name())
349 << "^" << e.macro_nesting << "[";
351 case MathRow::END_MACRO:
354 case MathRow::BEG_ARG:
357 case MathRow::END_ARG:
361 os << "<" << e.before << "-[]-" << e.after << ">";
368 ostream & operator<<(ostream & os, MathRow const & mrow)
370 for (MathRow::Element const & e : mrow)