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 inset(0), compl_unique_to(0), macro(0), color(Color_red)
45 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
47 // First there is a dummy element of type "open"
48 push_back(Element(mi, DUMMY, MC_OPEN));
50 // Then insert the MathData argument
51 bool const has_contents = ar->addToMathRow(*this, mi);
53 // empty arrays are visible when they are editable
54 // we reserve the necessary space anyway (even if nothing gets drawn)
56 Element e(mi, BOX, MC_ORD);
57 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
61 // Finally there is a dummy element of type "close"
62 push_back(Element(mi, DUMMY, MC_CLOSE));
64 /* Do spacing only in math mode. This test is a bit clumsy,
65 * but it is used in other places for guessing the current mode.
67 if (!isMathFont(mi.base.fontname))
71 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
72 if (elements_[i].mclass == MC_UNKNOWN)
74 update_class(elements_[i].mclass, elements_[before(i)].mclass,
75 elements_[after(i)].mclass);
79 // We go to the end to handle spacing at the end of equation
80 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
81 if (elements_[i].mclass == MC_UNKNOWN)
83 Element & bef = elements_[before(i)];
84 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
86 // this is better than spc / 2 to avoid rounding problems
87 elements_[i].before = spc - spc / 2;
89 // Do not lose spacing allocated to extremities
90 if (!elements_.empty()) {
91 elements_[after(0)].before += elements_.front().after;
92 elements_[before(elements_.size() - 1)].after += elements_.back().before;
97 int MathRow::before(int i) const
101 while (elements_[i].mclass == MC_UNKNOWN);
107 int MathRow::after(int i) const
111 while (elements_[i].mclass == MC_UNKNOWN);
117 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
121 // In order to compute the dimension of macros and their
122 // arguments, it is necessary to keep track of them.
123 map<MathMacro const *, Dimension> dim_macros;
124 map<MathData const *, Dimension> dim_arrays;
125 CoordCache & coords = mi.base.bv->coordCache();
126 for (Element const & e : elements_) {
127 mi.base.macro_nesting = e.macro_nesting;
133 e.inset->metrics(mi, d);
134 d.wid += e.before + e.after;
135 coords.insets().add(e.inset, d);
138 e.macro->macro()->lock();
139 // Add a macro to current list
140 dim_macros[e.macro] = Dimension();
143 LATTEST(dim_macros.find(e.macro) != dim_macros.end());
144 e.macro->macro()->unlock();
145 // Cache the dimension of the macro and remove it from
147 coords.insets().add(e.macro, dim_macros[e.macro]);
148 dim_macros.erase(e.macro);
150 // This is basically like macros
153 e.macro->macro()->unlock();
154 dim_arrays[e.ar] = Dimension();
157 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
159 e.macro->macro()->lock();
160 coords.arrays().add(e.ar, dim_arrays[e.ar]);
161 dim_arrays.erase(e.ar);
164 d = theFontMetrics(mi.base.font).dimension('I');
165 d.wid += e.before + e.after;
171 // Now add the dimension to current macros and arguments.
172 for (auto & dim_macro : dim_macros)
173 dim_macro.second += d;
174 for (auto & dim_array : dim_arrays)
175 dim_array.second += d;
178 if (e.compl_text.empty())
180 FontInfo font = mi.base.font;
181 augmentFont(font, "mathnormal");
182 dim.wid += mathed_string_width(font, e.compl_text);
184 LATTEST(dim_macros.empty() && dim_arrays.empty());
188 void MathRow::draw(PainterInfo & pi, int x, int const y) const
190 CoordCache & coords = pi.base.bv->coordCache();
191 for (Element const & e : elements_) {
194 // This is hackish: the math inset does not know that space
195 // has been added before and after it; we alter its dimension
196 // while it is drawing, because it relies on this value.
197 Dimension const d = coords.insets().dim(e.inset);
199 d2.wid -= e.before + e.after;
200 coords.insets().add(e.inset, d2);
201 e.inset->drawSelection(pi, x + e.before, y);
202 e.inset->draw(pi, x + e.before, y);
203 coords.insets().add(e.inset, x, y);
204 coords.insets().add(e.inset, d);
209 coords.insets().add(e.macro, x, y);
212 coords.arrays().add(e.ar, x, y);
213 // if the macro is being edited, then the painter is in
215 if (e.macro->editMetrics(pi.base.bv))
216 pi.pain.leaveMonochromeMode();
219 if (e.macro->editMetrics(pi.base.bv))
220 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
223 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
224 // the box is not visible in non-editable context (except for grey macro boxes).
225 if (e.color != Color_none)
226 pi.pain.rectangle(x + e.before, y - d.ascent(),
227 d.width(), d.height(), e.color);
228 x += d.wid + e.before + e.after;
236 if (e.compl_text.empty())
238 FontInfo f = pi.base.font;
239 augmentFont(f, "mathnormal");
241 // draw the unique and the non-unique completion part
242 // Note: this is not time-critical as it is
243 // only done once per screen.
244 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
245 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
248 f.setColor(Color_inlinecompletion);
249 pi.pain.text(x, y, s1, f);
250 x += mathed_string_width(f, s1);
253 f.setColor(Color_nonunique_inlinecompletion);
254 pi.pain.text(x, y, s2, f);
255 x += mathed_string_width(f, s2);
261 int MathRow::kerning(BufferView const * bv) const
263 if (elements_.empty())
265 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
266 return inset ? inset->kerning(bv) : 0;
270 ostream & operator<<(ostream & os, MathRow::Element const & e)
274 os << (e.mclass == MC_OPEN ? "{" : "}");
277 os << "<" << e.before << "-"
278 << to_utf8(class_to_string(e.mclass))
279 << "-" << e.after << ">";
281 case MathRow::BEG_MACRO:
282 os << "\\" << to_utf8(e.macro->name())
283 << "^" << e.macro->nesting() << "[";
285 case MathRow::END_MACRO:
288 case MathRow::BEG_ARG:
291 case MathRow::END_ARG:
295 os << "<" << e.before << "-[]-" << e.after << ">";
302 ostream & operator<<(ostream & os, MathRow const & mrow)
304 for (MathRow::Element const & e : mrow)