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)
53 case InsetMath::MARKER:
54 case InsetMath::MARKER2:
55 case InsetMath::BOX_MARKER:
57 case InsetMath::NO_MARKER:
65 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
68 // handle vertical space for markers
70 case InsetMath::NO_MARKER:
72 case InsetMath::MARKER:
75 case InsetMath::MARKER2:
79 case InsetMath::BOX_MARKER:
81 font.setSize(FONT_SIZE_TINY);
83 mathed_string_dim(font, e.inset->name(), namedim);
84 int const namewid = 1 + namedim.wid + 1;
86 if (namewid > dim.wid)
87 e.after += namewid - dim.wid;
88 dim.des += 3 + namedim.height();
93 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
94 int const x, int const y)
96 if (e.marker == InsetMath::NO_MARKER)
99 CoordCache const & coords = pi.base.bv->coordCache();
100 Dimension const dim = coords.getInsets().dim(e.inset);
102 // the marker is before/after the inset. Necessary space has been reserved already.
103 int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
104 int const r = x + dim.width() - e.after;
106 if (e.marker == InsetMath::BOX_MARKER) {
107 // draw header and rectangle around
109 font.setSize(FONT_SIZE_TINY);
110 font.setColor(Color_mathmacrolabel);
112 mathed_string_dim(font, e.inset->name(), namedim);
113 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
114 dim.wid, namedim.height() + 2, Color_mathmacrobg);
115 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
119 // Now markers with corners
120 bool const highlight = e.inset->mouseHovered(pi.base.bv)
121 || e.inset->editing(pi.base.bv);
122 ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
124 int const d = y + dim.descent();
125 pi.pain.line(l, d - 3, l, d, pen_color);
126 pi.pain.line(r, d - 3, r, d, pen_color);
127 pi.pain.line(l, d, l + 3, d, pen_color);
128 pi.pain.line(r - 3, d, r, d, pen_color);
130 if (e.marker == InsetMath::MARKER)
133 int const a = y - dim.ascent();
134 pi.pain.line(l, a + 3, l, a, pen_color);
135 pi.pain.line(r, a + 3, r, a, pen_color);
136 pi.pain.line(l, a, l + 3, a, pen_color);
137 pi.pain.line(r - 3, a, r, a, pen_color);
143 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
145 // First there is a dummy element of type "open"
146 push_back(Element(mi, DUMMY, MC_OPEN));
148 // Then insert the MathData argument
149 bool const has_contents = ar->addToMathRow(*this, mi);
151 // A MathRow should not be completely empty
153 Element e(mi, BOX, MC_ORD);
154 // empty arrays are visible when they are editable
155 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
159 // Finally there is a dummy element of type "close"
160 push_back(Element(mi, DUMMY, MC_CLOSE));
162 /* Do spacing only in math mode. This test is a bit clumsy,
163 * but it is used in other places for guessing the current mode.
165 bool const dospacing = isMathFont(mi.base.fontname);
169 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
170 if (elements_[i].mclass != MC_UNKNOWN)
171 update_class(elements_[i].mclass, elements_[before(i)].mclass,
172 elements_[after(i)].mclass);
177 // We go to the end to handle spacing at the end of equation
178 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
179 Element & e = elements_[i];
181 Element & bef = elements_[before(i)];
182 if (dospacing && e.mclass != MC_UNKNOWN) {
183 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
184 bef.after += spc / 2;
185 // this is better than spc / 2 to avoid rounding problems
186 e.before += spc - spc / 2;
189 // finally reserve space for markers
190 bef.after = max(bef.after, markerMargin(bef));
191 if (e.mclass != MC_UNKNOWN)
192 e.before = max(e.before, markerMargin(e));
193 // for linearized insets (macros...) too
195 bef.after = max(bef.after, markerMargin(e));
196 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
197 Element & aft = elements_[after(i)];
198 aft.before = max(aft.before, markerMargin(e));
202 // Do not lose spacing allocated to extremities
203 if (!elements_.empty()) {
204 elements_[after(0)].before += elements_.front().after;
205 elements_[before(elements_.size() - 1)].after += elements_.back().before;
210 int MathRow::before(int i) const
214 while (elements_[i].mclass == MC_UNKNOWN);
220 int MathRow::after(int i) const
224 while (elements_[i].mclass == MC_UNKNOWN);
230 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
234 // In order to compute the dimension of macros and their
235 // arguments, it is necessary to keep track of them.
236 vector<pair<InsetMath const *, Dimension>> dim_insets;
237 vector<pair<MathData const *, Dimension>> dim_arrays;
238 CoordCache & coords = mi.base.bv->coordCache();
239 for (Element & e : elements_) {
240 mi.base.macro_nesting = e.macro_nesting;
246 e.inset->metrics(mi, d);
247 d.wid += e.before + e.after;
248 coords.insets().add(e.inset, d);
252 dim_insets.push_back(make_pair(e.inset, Dimension()));
253 dim_insets.back().second.wid += e.before + e.after;
254 d.wid = e.before + e.after;
255 e.inset->beforeMetrics();
258 dim_arrays.push_back(make_pair(e.ar, Dimension()));
262 e.inset->afterMetrics();
263 LATTEST(dim_insets.back().first == e.inset);
264 d = dim_insets.back().second;
265 afterMetricsMarkers(mi, e, d);
266 d.wid += e.before + e.after;
267 coords.insets().add(e.inset, d);
268 dim_insets.pop_back();
269 // We do not want to count the width again, but the
270 // padding and the vertical dimension are meaningful.
271 d.wid = e.before + e.after;
274 LATTEST(dim_arrays.back().first == e.ar);
275 coords.arrays().add(e.ar, dim_arrays.back().second);
276 dim_arrays.pop_back();
280 d = theFontMetrics(mi.base.font).dimension('I');
281 if (e.color != Color_none) {
282 // allow for one pixel before/after the box.
283 d.wid += e.before + e.after + 2;
285 // hide the box, but keep its height
293 // Now add the dimension to current macros and arguments.
294 for (auto & dim_macro : dim_insets)
295 dim_macro.second += d;
296 for (auto & dim_array : dim_arrays)
297 dim_array.second += d;
300 if (e.compl_text.empty())
302 FontInfo font = mi.base.font;
303 augmentFont(font, "mathnormal");
304 dim.wid += mathed_string_width(font, e.compl_text);
306 LATTEST(dim_insets.empty() && dim_arrays.empty());
310 void MathRow::draw(PainterInfo & pi, int x, int const y) const
312 CoordCache & coords = pi.base.bv->coordCache();
313 for (Element const & e : elements_) {
316 // This is hackish: the math inset does not know that space
317 // has been added before and after it; we alter its dimension
318 // while it is drawing, because it relies on this value.
319 Dimension const d = coords.insets().dim(e.inset);
321 d2.wid -= e.before + e.after;
322 coords.insets().add(e.inset, d2);
323 e.inset->draw(pi, x + e.before, y);
324 coords.insets().add(e.inset, x, y);
325 coords.insets().add(e.inset, d);
326 drawMarkers(pi, e, x, y);
332 coords.arrays().add(e.ar, x, y);
333 e.ar->drawSelection(pi, x, y);
336 coords.insets().add(e.inset, x, y);
337 drawMarkers(pi, e, x, y);
338 e.inset->beforeDraw(pi);
340 x += e.before + e.after;
344 e.inset->afterDraw(pi);
345 x += e.before + e.after;
348 if (e.color == Color_none)
350 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
351 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
352 d.width() - 1, d.height() - 1, e.color);
353 x += d.wid + 2 + e.before + e.after;
360 if (e.compl_text.empty())
362 FontInfo f = pi.base.font;
363 augmentFont(f, "mathnormal");
365 // draw the unique and the non-unique completion part
366 // Note: this is not time-critical as it is
367 // only done once per screen.
368 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
369 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
372 f.setColor(Color_inlinecompletion);
373 pi.pain.text(x, y, s1, f);
374 x += mathed_string_width(f, s1);
377 f.setColor(Color_nonunique_inlinecompletion);
378 pi.pain.text(x, y, s2, f);
379 x += mathed_string_width(f, s2);
385 int MathRow::kerning(BufferView const * bv) const
387 if (elements_.empty())
389 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
390 return inset ? inset->kerning(bv) : 0;
394 ostream & operator<<(ostream & os, MathRow::Element const & e)
398 os << (e.mclass == MC_OPEN ? "{" : "}");
401 os << "<" << e.before << "-"
402 << to_utf8(class_to_string(e.mclass))
403 << "-" << e.after << ">";
407 os << "\\" << to_utf8(e.inset->name())
408 << "^" << e.macro_nesting << "[";
419 os << "<" << e.before << "-[]-" << e.after << ">";
426 ostream & operator<<(ostream & os, MathRow const & mrow)
428 for (MathRow::Element const & e : mrow)