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"
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), ar(0),
48 // Helper functions for markers
50 int markerMargin(MathRow::Element const & e)
52 return e.marker == InsetMath::MARKER
53 || e.marker == InsetMath::MARKER2;
57 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
60 // handle vertical space for markers
62 case InsetMath::NO_MARKER:
64 case InsetMath::MARKER:
67 case InsetMath::MARKER2:
71 case InsetMath::BOX_MARKER:
73 font.setSize(FONT_SIZE_TINY);
75 mathed_string_dim(font, e.inset->name(), namedim);
76 int const namewid = 1 + namedim.wid + 1;
78 if (namewid > dim.wid)
79 e.after += namewid - dim.wid;
80 dim.des += 3 + namedim.height();
85 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
86 int const x, int const y)
88 if (e.marker == InsetMath::NO_MARKER)
91 CoordCache const & coords = pi.base.bv->coordCache();
92 Dimension const dim = coords.getInsets().dim(e.inset);
94 // the marker is before/after the inset. Necessary space has been reserved already.
95 int const l = x + e.before - markerMargin(e);
96 int const r = x + dim.width() - e.after;
98 if (e.marker == InsetMath::BOX_MARKER) {
99 // draw header and rectangle around
101 font.setSize(FONT_SIZE_TINY);
102 font.setColor(Color_mathmacrolabel);
104 mathed_string_dim(font, e.inset->name(), namedim);
105 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
106 dim.wid, namedim.height() + 2, Color_mathmacrobg);
107 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
111 // Now markers with corners
112 bool const highlight = e.inset->mouseHovered(pi.base.bv)
113 || e.inset->editing(pi.base.bv);
114 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
116 int const d = y + dim.descent();
117 pi.pain.line(l, d - 3, l, d, pen_color);
118 pi.pain.line(r, d - 3, r, d, pen_color);
119 pi.pain.line(l, d, l + 3, d, pen_color);
120 pi.pain.line(r - 3, d, r, d, pen_color);
122 if (e.marker == InsetMath::MARKER)
125 int const a = y - dim.ascent();
126 pi.pain.line(l, a + 3, l, a, pen_color);
127 pi.pain.line(r, a + 3, r, a, pen_color);
128 pi.pain.line(l, a, l + 3, a, pen_color);
129 pi.pain.line(r - 3, a, r, a, pen_color);
135 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
137 // First there is a dummy element of type "open"
138 push_back(Element(mi, DUMMY, MC_OPEN));
140 // Then insert the MathData argument
141 bool const has_contents = ar->addToMathRow(*this, mi);
143 // A MathRow should not be completely empty
145 Element e(mi, BOX, MC_ORD);
146 // empty arrays are visible when they are editable
147 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
151 // Finally there is a dummy element of type "close"
152 push_back(Element(mi, DUMMY, MC_CLOSE));
154 /* Do spacing only in math mode. This test is a bit clumsy,
155 * but it is used in other places for guessing the current mode.
157 bool const dospacing = isMathFont(mi.base.fontname);
161 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
162 if (elements_[i].mclass != MC_UNKNOWN)
163 update_class(elements_[i].mclass, elements_[before(i)].mclass,
164 elements_[after(i)].mclass);
169 // We go to the end to handle spacing at the end of equation
170 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
171 Element & e = elements_[i];
173 Element & bef = elements_[before(i)];
174 if (dospacing && e.mclass != MC_UNKNOWN) {
175 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
176 bef.after += spc / 2;
177 // this is better than spc / 2 to avoid rounding problems
178 e.before += spc - spc / 2;
181 // finally reserve space for markers
182 bef.after = max(bef.after, markerMargin(bef));
183 if (e.mclass != MC_UNKNOWN)
184 e.before = max(e.before, markerMargin(e));
185 // for linearized insets (macros...) too
187 bef.after = max(bef.after, markerMargin(e));
188 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
189 Element & aft = elements_[after(i)];
190 aft.before = max(aft.before, markerMargin(e));
194 // Do not lose spacing allocated to extremities
195 if (!elements_.empty()) {
196 elements_[after(0)].before += elements_.front().after;
197 elements_[before(elements_.size() - 1)].after += elements_.back().before;
202 int MathRow::before(int i) const
206 while (elements_[i].mclass == MC_UNKNOWN);
212 int MathRow::after(int i) const
216 while (elements_[i].mclass == MC_UNKNOWN);
222 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
226 // In order to compute the dimension of macros and their
227 // arguments, it is necessary to keep track of them.
228 vector<pair<InsetMath const *, Dimension>> dim_insets;
229 vector<pair<MathData const *, Dimension>> dim_arrays;
230 CoordCache & coords = mi.base.bv->coordCache();
231 for (Element & e : elements_) {
232 mi.base.macro_nesting = e.macro_nesting;
238 e.inset->metrics(mi, d);
239 d.wid += e.before + e.after;
240 coords.insets().add(e.inset, d);
244 dim_insets.push_back(make_pair(e.inset, Dimension()));
245 dim_insets.back().second.wid += e.before + e.after;
246 d.wid = e.before + e.after;
247 e.inset->beforeMetrics();
250 dim_arrays.push_back(make_pair(e.ar, Dimension()));
254 e.inset->afterMetrics();
255 LATTEST(dim_insets.back().first == e.inset);
256 d = dim_insets.back().second;
257 afterMetricsMarkers(mi, e, d);
258 d.wid += e.before + e.after;
259 coords.insets().add(e.inset, d);
260 dim_insets.pop_back();
261 // We do not want to count the width again, but the
262 // padding and the vertical dimension are meaningful.
263 d.wid = e.before + e.after;
266 LATTEST(dim_arrays.back().first == e.ar);
267 coords.arrays().add(e.ar, dim_arrays.back().second);
268 dim_arrays.pop_back();
272 d = theFontMetrics(mi.base.font).dimension('I');
273 if (e.color != Color_none) {
274 // allow for one pixel before/after the box.
275 d.wid += e.before + e.after + 2;
277 // hide the box, but keep its height
285 // Now add the dimension to current macros and arguments.
286 for (auto & dim_macro : dim_insets)
287 dim_macro.second += d;
288 for (auto & dim_array : dim_arrays)
289 dim_array.second += d;
292 if (e.compl_text.empty())
294 FontInfo font = mi.base.font;
295 augmentFont(font, "mathnormal");
296 dim.wid += mathed_string_width(font, e.compl_text);
298 LATTEST(dim_insets.empty() && dim_arrays.empty());
302 void MathRow::draw(PainterInfo & pi, int x, int const y) const
304 CoordCache & coords = pi.base.bv->coordCache();
305 for (Element const & e : elements_) {
308 // This is hackish: the math inset does not know that space
309 // has been added before and after it; we alter its dimension
310 // while it is drawing, because it relies on this value.
311 Dimension const d = coords.insets().dim(e.inset);
313 d2.wid -= e.before + e.after;
314 coords.insets().add(e.inset, d2);
315 e.inset->draw(pi, x + e.before, y);
316 coords.insets().add(e.inset, x, y);
317 coords.insets().add(e.inset, d);
318 drawMarkers(pi, e, x, y);
324 coords.arrays().add(e.ar, x, y);
325 e.ar->drawSelection(pi, x, y);
328 coords.insets().add(e.inset, x, y);
329 drawMarkers(pi, e, x, y);
330 e.inset->beforeDraw(pi);
332 x += e.before + e.after;
336 e.inset->afterDraw(pi);
337 x += e.before + e.after;
340 if (e.color == Color_none)
342 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
343 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
344 d.width() - 1, d.height() - 1, e.color);
345 x += d.wid + 2 + e.before + e.after;
352 if (e.compl_text.empty())
354 FontInfo f = pi.base.font;
355 augmentFont(f, "mathnormal");
357 // draw the unique and the non-unique completion part
358 // Note: this is not time-critical as it is
359 // only done once per screen.
360 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
361 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
364 f.setColor(Color_inlinecompletion);
365 pi.pain.text(x, y, s1, f);
366 x += mathed_string_width(f, s1);
369 f.setColor(Color_nonunique_inlinecompletion);
370 pi.pain.text(x, y, s2, f);
371 x += mathed_string_width(f, s2);
377 int MathRow::kerning(BufferView const * bv) const
379 if (elements_.empty())
381 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
382 return inset ? inset->kerning(bv) : 0;
386 ostream & operator<<(ostream & os, MathRow::Element const & e)
390 os << (e.mclass == MC_OPEN ? "{" : "}");
393 os << "<" << e.before << "-"
394 << to_utf8(class_to_string(e.mclass))
395 << "-" << e.after << ">";
399 os << "\\" << to_utf8(e.inset->name())
400 << "^" << e.macro_nesting << "[";
411 os << "<" << e.before << "-[]-" << e.after << ">";
418 ostream & operator<<(ostream & os, MathRow const & mrow)
420 for (MathRow::Element const & e : mrow)