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),
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 // A MathRow should not be completely empty
56 Element e(mi, BOX, MC_ORD);
57 // empty arrays are visible when they are editable
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 Element & bef = elements_[before(i)];
85 if (dospacing && e.mclass != MC_UNKNOWN) {
86 int spc = class_spacing(bef.mclass, e.mclass, mi.base);
88 // this is better than spc / 2 to avoid rounding problems
89 e.before += spc - spc / 2;
92 // finally reserve space for markers
93 if (bef.marker != InsetMath::NO_MARKER)
94 bef.after = max(bef.after, 1);
95 if (e.mclass != MC_UNKNOWN && e.marker != InsetMath::NO_MARKER)
96 e.before = max(e.before, 1);
97 // for linearized insets (macros...) too
98 if (e.type == BEGIN && e.marker != InsetMath::NO_MARKER)
99 bef.after = max(bef.after, 1);
100 if (e.type == END && e.marker != InsetMath::NO_MARKER) {
101 Element & aft = elements_[after(i)];
102 aft.before = max(aft.before, 1);
106 // Do not lose spacing allocated to extremities
107 if (!elements_.empty()) {
108 elements_[after(0)].before += elements_.front().after;
109 elements_[before(elements_.size() - 1)].after += elements_.back().before;
114 int MathRow::before(int i) const
118 while (elements_[i].mclass == MC_UNKNOWN);
124 int MathRow::after(int i) const
128 while (elements_[i].mclass == MC_UNKNOWN);
136 void afterMetricsMarkers(MetricsInfo const & mi, MathRow::Element & e,
139 // handle vertical space for markers
141 case InsetMath::NO_MARKER:
143 case InsetMath::MARKER:
146 case InsetMath::MARKER2:
150 case InsetMath::BOX_MARKER:
151 FontInfo font = mi.base.font;
152 augmentFont(font, "lyxtex");
153 font.setSize(FONT_SIZE_TINY);
155 mathed_string_dim(font, e.inset->name(), namedim);
156 int const namewid = 1 + namedim.wid + 1;
158 if (namewid > dim.wid)
159 e.after += namewid - dim.wid;
161 dim.des += 2 + namedim.height();
168 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
172 // In order to compute the dimension of macros and their
173 // arguments, it is necessary to keep track of them.
174 vector<pair<InsetMath const *, Dimension>> dim_insets;
175 vector<pair<MathData const *, Dimension>> dim_arrays;
176 CoordCache & coords = mi.base.bv->coordCache();
177 for (Element & e : elements_) {
178 mi.base.macro_nesting = e.macro_nesting;
184 e.inset->metrics(mi, d);
185 d.wid += e.before + e.after;
186 coords.insets().add(e.inset, d);
190 dim_insets.push_back(make_pair(e.inset, Dimension()));
191 dim_insets.back().second.wid += e.before + e.after;
192 d.wid = e.before + e.after;
193 e.inset->beforeMetrics();
196 dim_arrays.push_back(make_pair(e.ar, Dimension()));
200 e.inset->afterMetrics();
201 LATTEST(dim_insets.back().first == e.inset);
202 d = dim_insets.back().second;
203 afterMetricsMarkers(mi, e, d);
204 d.wid += e.before + e.after;
205 coords.insets().add(e.inset, d);
206 dim_insets.pop_back();
207 // We do not want to count the width again, but the
208 // padding and the vertical dimension are meaningful.
209 d.wid = e.before + e.after;
212 LATTEST(dim_arrays.back().first == e.ar);
213 coords.arrays().add(e.ar, dim_arrays.back().second);
214 dim_arrays.pop_back();
218 d = theFontMetrics(mi.base.font).dimension('I');
219 if (e.color != Color_none) {
220 // allow for one pixel before/after the box.
221 d.wid += e.before + e.after + 2;
223 // hide the box, but keep its height
231 // Now add the dimension to current macros and arguments.
232 for (auto & dim_macro : dim_insets)
233 dim_macro.second += d;
234 for (auto & dim_array : dim_arrays)
235 dim_array.second += d;
238 if (e.compl_text.empty())
240 FontInfo font = mi.base.font;
241 augmentFont(font, "mathnormal");
242 dim.wid += mathed_string_width(font, e.compl_text);
244 LATTEST(dim_insets.empty() && dim_arrays.empty());
250 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
252 if (e.marker == InsetMath::NO_MARKER)
255 CoordCache const & coords = pi.base.bv->coordCache();
256 Dimension const dim = coords.getInsets().dim(e.inset);
258 // the marker is before/after the inset. Normally some space has been reserved already.
259 int const l = x + e.before - 1;
260 int const r = x + dim.width() - e.after;
262 if (e.marker == InsetMath::BOX_MARKER) {
263 // draw header and rectangle around
264 FontInfo font = pi.base.font;
265 augmentFont(font, "lyxtex");
266 font.setSize(FONT_SIZE_TINY);
267 font.setColor(Color_mathmacrolabel);
269 mathed_string_dim(font, e.inset->name(), namedim);
270 pi.pain.rectangle(l, y - dim.asc, dim.wid,
271 dim.height(), Color_mathmacroframe);
272 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
273 dim.wid, namedim.height() + 2, Color_mathmacrobg);
274 pi.pain.text(l + 1, y + dim.des - namedim.des - 1, e.inset->name(), font);
278 // Duplicated from Inset.cpp and adapted. It is believed that the
279 // Inset version should die eventually
280 ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
281 Color_mathframe : Color_mathcorners;
283 int const d = y + dim.descent();
284 pi.pain.line(l, d - 3, l, d, pen_color);
285 pi.pain.line(r, d - 3, r, d, pen_color);
286 pi.pain.line(l, d, l + 3, d, pen_color);
287 pi.pain.line(r - 3, d, r, d, pen_color);
289 if (e.marker == InsetMath::MARKER)
292 int const a = y - dim.ascent();
293 pi.pain.line(l, a + 3, l, a, pen_color);
294 pi.pain.line(r, a + 3, r, a, pen_color);
295 pi.pain.line(l, a, l + 3, a, pen_color);
296 pi.pain.line(r - 3, a, r, a, pen_color);
301 void MathRow::draw(PainterInfo & pi, int x, int const y) const
303 CoordCache & coords = pi.base.bv->coordCache();
304 for (Element const & e : elements_) {
307 // This is hackish: the math inset does not know that space
308 // has been added before and after it; we alter its dimension
309 // while it is drawing, because it relies on this value.
310 Dimension const d = coords.insets().dim(e.inset);
312 d2.wid -= e.before + e.after;
313 coords.insets().add(e.inset, d2);
314 e.inset->drawSelection(pi, x + e.before, y);
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.insets().add(e.inset, x, y);
325 drawMarkers(pi, e, x, y);
326 e.inset->beforeDraw(pi);
328 x += e.before + e.after;
330 coords.arrays().add(e.ar, x, y);
334 e.inset->afterDraw(pi);
335 x += e.before + e.after;
338 if (e.color == Color_none)
340 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
341 pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
342 d.width() - 1, d.height() - 1, e.color);
343 x += d.wid + 2 + e.before + e.after;
350 if (e.compl_text.empty())
352 FontInfo f = pi.base.font;
353 augmentFont(f, "mathnormal");
355 // draw the unique and the non-unique completion part
356 // Note: this is not time-critical as it is
357 // only done once per screen.
358 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
359 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
362 f.setColor(Color_inlinecompletion);
363 pi.pain.text(x, y, s1, f);
364 x += mathed_string_width(f, s1);
367 f.setColor(Color_nonunique_inlinecompletion);
368 pi.pain.text(x, y, s2, f);
369 x += mathed_string_width(f, s2);
375 int MathRow::kerning(BufferView const * bv) const
377 if (elements_.empty())
379 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
380 return inset ? inset->kerning(bv) : 0;
384 ostream & operator<<(ostream & os, MathRow::Element const & e)
388 os << (e.mclass == MC_OPEN ? "{" : "}");
391 os << "<" << e.before << "-"
392 << to_utf8(class_to_string(e.mclass))
393 << "-" << e.after << ">";
397 os << "\\" << to_utf8(e.inset->name())
398 << "^" << e.macro_nesting << "[";
409 os << "<" << e.before << "-[]-" << e.after << ">";
416 ostream & operator<<(ostream & os, MathRow const & mrow)
418 for (MathRow::Element const & e : mrow)