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(Type t)
40 : type(t), inset(0), mclass(MC_ORD), before(0), after(0),
41 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(BEGIN));
49 back().mclass = 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)
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(END));
64 back().mclass = MC_CLOSE;
66 /* Do spacing only in math mode. This test is a bit clumsy,
67 * but it is used in other places for guessing the current mode.
69 if (!isMathFont(mi.base.fontname))
73 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
74 if (elements_[i].type != INSET)
76 update_class(elements_[i].mclass, elements_[before(i)].mclass,
77 elements_[after(i)].mclass);
81 // We go to the end to handle spacing at the end of equation
82 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
83 if (elements_[i].type != INSET)
85 Element & bef = elements_[before(i)];
86 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
88 // this is better than spc / 2 to avoid rounding problems
89 elements_[i].before = spc - spc / 2;
91 // Do not lose spacing allocated to extremities
92 if (!elements_.empty()) {
93 elements_[after(0)].before += elements_.front().after;
94 elements_[before(elements_.size() - 1)].after += elements_.back().before;
99 int MathRow::before(int i) const
103 while (elements_[i].type != BEGIN
104 && elements_[i].type != INSET);
110 int MathRow::after(int i) const
114 while (elements_[i].type != END
115 && elements_[i].type != INSET);
121 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
125 // In order to compute the dimension of macros and their
126 // arguments, it is necessary to keep track of them.
127 map<MathMacro const *, Dimension> dim_macros;
128 map<MathData const *, Dimension> dim_arrays;
129 // this vector remembers the stack of macro nesting values
130 vector<int> macro_nesting;
131 macro_nesting.push_back(mi.base.macro_nesting);
132 CoordCache & coords = mi.base.bv->coordCache();
133 for (Element const & e : elements_) {
134 mi.base.macro_nesting = macro_nesting.back();
141 e.inset->metrics(mi, d);
142 d.wid += e.before + e.after;
143 coords.insets().add(e.inset, d);
146 macro_nesting.push_back(e.macro->nesting());
147 e.macro->macro()->lock();
148 // Add a macro to current list
149 dim_macros[e.macro] = Dimension();
152 LATTEST(dim_macros.find(e.macro) != dim_macros.end());
153 macro_nesting.pop_back();
154 e.macro->macro()->unlock();
155 // Cache the dimension of the macro and remove it from
157 coords.insets().add(e.macro, dim_macros[e.macro]);
158 dim_macros.erase(e.macro);
160 // This is basically like macros
163 macro_nesting.push_back(e.macro->nesting());
164 e.macro->macro()->unlock();
166 dim_arrays[e.ar] = Dimension();
169 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
171 macro_nesting.pop_back();
172 e.macro->macro()->lock();
174 coords.arrays().add(e.ar, dim_arrays[e.ar]);
175 dim_arrays.erase(e.ar);
178 d = theFontMetrics(mi.base.font).dimension('I');
179 d.wid += e.before + e.after;
185 // Now add the dimension to current macros and arguments.
186 for (auto & dim_macro : dim_macros)
187 dim_macro.second += d;
188 for (auto & dim_array : dim_arrays)
189 dim_array.second += d;
192 if (e.compl_text.empty())
194 FontInfo font = mi.base.font;
195 augmentFont(font, "mathnormal");
196 dim.wid += mathed_string_width(font, e.compl_text);
198 LATTEST(dim_macros.empty() && dim_arrays.empty());
202 void MathRow::draw(PainterInfo & pi, int x, int const y) const
204 CoordCache & coords = pi.base.bv->coordCache();
205 for (Element const & e : elements_) {
208 // This is hackish: the math inset does not know that space
209 // has been added before and after it; we alter its dimension
210 // while it is drawing, because it relies on this value.
211 Dimension const d = coords.insets().dim(e.inset);
213 d2.wid -= e.before + e.after;
214 coords.insets().add(e.inset, d2);
215 e.inset->drawSelection(pi, x + e.before, y);
216 e.inset->draw(pi, x + e.before, y);
217 coords.insets().add(e.inset, x, y);
218 coords.insets().add(e.inset, d);
223 coords.insets().add(e.macro, x, y);
226 coords.arrays().add(e.ar, x, y);
227 // if the macro is being edited, then the painter is in
229 if (e.macro->editMetrics(pi.base.bv))
230 pi.pain.leaveMonochromeMode();
233 if (e.macro->editMetrics(pi.base.bv))
234 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
237 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
238 // the box is not visible in non-editable context (except for grey macro boxes).
239 if (e.color != Color_none)
240 pi.pain.rectangle(x + e.before, y - d.ascent(),
241 d.width(), d.height(), e.color);
251 if (e.compl_text.empty())
253 FontInfo f = pi.base.font;
254 augmentFont(f, "mathnormal");
256 // draw the unique and the non-unique completion part
257 // Note: this is not time-critical as it is
258 // only done once per screen.
259 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
260 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
263 f.setColor(Color_inlinecompletion);
264 pi.pain.text(x, y, s1, f);
265 x += mathed_string_width(f, s1);
268 f.setColor(Color_nonunique_inlinecompletion);
269 pi.pain.text(x, y, s2, f);
270 x += mathed_string_width(f, s2);
276 int MathRow::kerning(BufferView const * bv) const
278 if (elements_.empty())
280 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
281 return inset ? inset->kerning(bv) : 0;
285 ostream & operator<<(ostream & os, MathRow::Element const & e)
295 os << "<" << e.before << "-"
296 << to_utf8(class_to_string(e.mclass))
297 << "-" << e.after << ">";
299 case MathRow::BEG_MACRO:
300 os << "\\" << to_utf8(e.macro->name())
301 << "^" << e.macro->nesting() << "[";
303 case MathRow::END_MACRO:
306 case MathRow::BEG_ARG:
309 case MathRow::END_ARG:
320 ostream & operator<<(ostream & os, MathRow const & mrow)
322 for (MathRow::Element const & e : mrow)