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 d.wid += e.before + e.after;
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_macros)
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_macros.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);
272 coords.insets().add(e.macro, x, y);
274 drawMarkers(pi, e, x, y);
277 coords.arrays().add(e.ar, x, y);
278 // if the macro is being edited, then the painter is in
280 if (e.macro->editMetrics(pi.base.bv))
281 pi.pain.leaveMonochromeMode();
284 if (e.macro->editMetrics(pi.base.bv))
285 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
288 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
289 // the box is not visible in non-editable context (except for grey macro boxes).
290 if (e.color != Color_none)
291 pi.pain.rectangle(x + e.before, y - d.ascent(),
292 d.width() - 1, d.height() - 1, e.color);
293 x += d.wid + e.before + e.after;
301 if (e.compl_text.empty())
303 FontInfo f = pi.base.font;
304 augmentFont(f, "mathnormal");
306 // draw the unique and the non-unique completion part
307 // Note: this is not time-critical as it is
308 // only done once per screen.
309 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
310 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
313 f.setColor(Color_inlinecompletion);
314 pi.pain.text(x, y, s1, f);
315 x += mathed_string_width(f, s1);
318 f.setColor(Color_nonunique_inlinecompletion);
319 pi.pain.text(x, y, s2, f);
320 x += mathed_string_width(f, s2);
326 int MathRow::kerning(BufferView const * bv) const
328 if (elements_.empty())
330 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
331 return inset ? inset->kerning(bv) : 0;
335 ostream & operator<<(ostream & os, MathRow::Element const & e)
339 os << (e.mclass == MC_OPEN ? "{" : "}");
342 os << "<" << e.before << "-"
343 << to_utf8(class_to_string(e.mclass))
344 << "-" << e.after << ">";
346 case MathRow::BEG_MACRO:
347 os << "\\" << to_utf8(e.macro->name())
348 << "^" << e.macro->nesting() << "[";
350 case MathRow::END_MACRO:
353 case MathRow::BEG_ARG:
356 case MathRow::END_ARG:
360 os << "<" << e.before << "-[]-" << e.after << ">";
367 ostream & operator<<(ostream & os, MathRow const & mrow)
369 for (MathRow::Element const & e : mrow)