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 "MathMacro.h"
19 #include "MathSupport.h"
21 #include "BufferView.h"
22 #include "CoordCache.h"
23 #include "MetricsInfo.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/debug.h"
29 #include "support/docstring.h"
30 #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),
42 macro(0), color(Color_red)
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 // empty arrays are visible when they are editable
55 // we reserve the necessary space anyway (even if nothing gets drawn)
57 Element e(mi, BOX, MC_ORD);
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 if (e.mclass == MC_UNKNOWN)
87 Element & bef = elements_[before(i)];
89 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
91 // this is better than spc / 2 to avoid rounding problems
92 e.before += spc - spc / 2;
95 // finally reserve space for markers
96 if (bef.marker != Inset::NO_MARKER)
97 bef.after = max(bef.after, 1);
98 if (e.marker != Inset::NO_MARKER)
99 e.before = max(e.before, 1);
102 // Do not lose spacing allocated to extremities
103 if (!elements_.empty()) {
104 elements_[after(0)].before += elements_.front().after;
105 elements_[before(elements_.size() - 1)].after += elements_.back().before;
110 int MathRow::before(int i) const
114 while (elements_[i].mclass == MC_UNKNOWN);
120 int MathRow::after(int i) const
124 while (elements_[i].mclass == MC_UNKNOWN);
130 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
134 // In order to compute the dimension of macros and their
135 // arguments, it is necessary to keep track of them.
136 map<MathMacro const *, Dimension> dim_macros;
137 map<MathData const *, Dimension> dim_arrays;
138 CoordCache & coords = mi.base.bv->coordCache();
139 for (Element const & e : elements_) {
140 mi.base.macro_nesting = e.macro_nesting;
146 e.inset->metrics(mi, d);
147 d.wid += e.before + e.after;
148 coords.insets().add(e.inset, d);
151 e.macro->macro()->lock();
152 // Add a macro to current list
153 dim_macros[e.macro] = Dimension();
156 LATTEST(dim_macros.find(e.macro) != dim_macros.end());
157 e.macro->macro()->unlock();
158 // Cache the dimension of the macro and remove it from
160 coords.insets().add(e.macro, dim_macros[e.macro]);
161 dim_macros.erase(e.macro);
163 // This is basically like macros
166 e.macro->macro()->unlock();
167 dim_arrays[e.ar] = Dimension();
170 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
172 e.macro->macro()->lock();
173 coords.arrays().add(e.ar, dim_arrays[e.ar]);
174 dim_arrays.erase(e.ar);
177 d = theFontMetrics(mi.base.font).dimension('I');
178 // allow for one pixel in front of the box.
179 d.wid += e.before + e.after + 1;
183 // handle vertical space for markers
185 case InsetMath::NO_MARKER:
187 case InsetMath::MARKER:
190 case InsetMath::MARKER2:
197 // Now add the dimension to current macros and arguments.
198 for (auto & dim_macro : dim_macros)
199 dim_macro.second += d;
200 for (auto & dim_array : dim_arrays)
201 dim_array.second += d;
204 if (e.compl_text.empty())
206 FontInfo font = mi.base.font;
207 augmentFont(font, "mathnormal");
208 dim.wid += mathed_string_width(font, e.compl_text);
210 LATTEST(dim_macros.empty() && dim_arrays.empty());
216 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
218 if (e.marker == InsetMath::NO_MARKER)
221 CoordCache const & coords = pi.base.bv->coordCache();
222 Dimension const dim = coords.getInsets().dim(e.inset);
224 // the marker is before/after the inset. Normally some space has been reserved already.
225 int const l = x + e.before - 1;
226 int const r = x + dim.width() - e.after;
228 // Duplicated from Inset.cpp and adapted. It is believed that the
229 // Inset version should die eventually
230 ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
231 Color_mathframe : Color_mathcorners;
233 int const d = y + dim.descent();
234 pi.pain.line(l, d - 3, l, d, pen_color);
235 pi.pain.line(r, d - 3, r, d, pen_color);
236 pi.pain.line(l, d, l + 3, d, pen_color);
237 pi.pain.line(r - 3, d, r, d, pen_color);
239 if (e.marker == InsetMath::MARKER)
242 int const a = y - dim.ascent();
243 pi.pain.line(l, a + 3, l, a, pen_color);
244 pi.pain.line(r, a + 3, r, a, pen_color);
245 pi.pain.line(l, a, l + 3, a, pen_color);
246 pi.pain.line(r - 3, a, r, a, pen_color);
251 void MathRow::draw(PainterInfo & pi, int x, int const y) const
253 CoordCache & coords = pi.base.bv->coordCache();
254 for (Element const & e : elements_) {
257 // This is hackish: the math inset does not know that space
258 // has been added before and after it; we alter its dimension
259 // while it is drawing, because it relies on this value.
260 Dimension const d = coords.insets().dim(e.inset);
262 d2.wid -= e.before + e.after;
263 coords.insets().add(e.inset, d2);
264 e.inset->drawSelection(pi, x + e.before, y);
265 e.inset->draw(pi, x + e.before, y);
266 coords.insets().add(e.inset, x, y);
267 coords.insets().add(e.inset, d);
268 drawMarkers(pi, e, x, y);
273 coords.insets().add(e.macro, x, y);
275 drawMarkers(pi, e, x, y);
278 coords.arrays().add(e.ar, x, y);
279 // if the macro is being edited, then the painter is in
281 if (e.macro->editMetrics(pi.base.bv))
282 pi.pain.leaveMonochromeMode();
285 if (e.macro->editMetrics(pi.base.bv))
286 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
289 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
290 // the box is not visible in non-editable context (except for grey macro boxes).
291 if (e.color != Color_none)
292 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
293 d.width() - 1, d.height() - 1, e.color);
294 x += d.wid + 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.macro->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)