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, MathClass mc)
40 : type(t), mclass(mc), before(0), after(0), inset(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(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(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(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 // this vector remembers the stack of macro nesting values
126 vector<int> macro_nesting;
127 macro_nesting.push_back(mi.base.macro_nesting);
128 CoordCache & coords = mi.base.bv->coordCache();
129 for (Element const & e : elements_) {
130 mi.base.macro_nesting = macro_nesting.back();
136 e.inset->metrics(mi, d);
137 d.wid += e.before + e.after;
138 coords.insets().add(e.inset, d);
141 macro_nesting.push_back(e.macro->nesting());
142 e.macro->macro()->lock();
143 // Add a macro to current list
144 dim_macros[e.macro] = Dimension();
147 LATTEST(dim_macros.find(e.macro) != dim_macros.end());
148 macro_nesting.pop_back();
149 e.macro->macro()->unlock();
150 // Cache the dimension of the macro and remove it from
152 coords.insets().add(e.macro, dim_macros[e.macro]);
153 dim_macros.erase(e.macro);
155 // This is basically like macros
158 macro_nesting.push_back(e.macro->nesting());
159 e.macro->macro()->unlock();
161 dim_arrays[e.ar] = Dimension();
164 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
166 macro_nesting.pop_back();
167 e.macro->macro()->lock();
169 coords.arrays().add(e.ar, dim_arrays[e.ar]);
170 dim_arrays.erase(e.ar);
173 d = theFontMetrics(mi.base.font).dimension('I');
174 d.wid += e.before + e.after;
180 // Now add the dimension to current macros and arguments.
181 for (auto & dim_macro : dim_macros)
182 dim_macro.second += d;
183 for (auto & dim_array : dim_arrays)
184 dim_array.second += d;
187 if (e.compl_text.empty())
189 FontInfo font = mi.base.font;
190 augmentFont(font, "mathnormal");
191 dim.wid += mathed_string_width(font, e.compl_text);
193 LATTEST(dim_macros.empty() && dim_arrays.empty());
197 void MathRow::draw(PainterInfo & pi, int x, int const y) const
199 CoordCache & coords = pi.base.bv->coordCache();
200 for (Element const & e : elements_) {
203 // This is hackish: the math inset does not know that space
204 // has been added before and after it; we alter its dimension
205 // while it is drawing, because it relies on this value.
206 Dimension const d = coords.insets().dim(e.inset);
208 d2.wid -= e.before + e.after;
209 coords.insets().add(e.inset, d2);
210 e.inset->drawSelection(pi, x + e.before, y);
211 e.inset->draw(pi, x + e.before, y);
212 coords.insets().add(e.inset, x, y);
213 coords.insets().add(e.inset, d);
218 coords.insets().add(e.macro, x, y);
221 coords.arrays().add(e.ar, x, y);
222 // if the macro is being edited, then the painter is in
224 if (e.macro->editMetrics(pi.base.bv))
225 pi.pain.leaveMonochromeMode();
228 if (e.macro->editMetrics(pi.base.bv))
229 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
232 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
233 // the box is not visible in non-editable context (except for grey macro boxes).
234 if (e.color != Color_none)
235 pi.pain.rectangle(x + e.before, y - d.ascent(),
236 d.width(), d.height(), e.color);
237 x += d.wid + e.before + e.after;
245 if (e.compl_text.empty())
247 FontInfo f = pi.base.font;
248 augmentFont(f, "mathnormal");
250 // draw the unique and the non-unique completion part
251 // Note: this is not time-critical as it is
252 // only done once per screen.
253 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
254 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
257 f.setColor(Color_inlinecompletion);
258 pi.pain.text(x, y, s1, f);
259 x += mathed_string_width(f, s1);
262 f.setColor(Color_nonunique_inlinecompletion);
263 pi.pain.text(x, y, s2, f);
264 x += mathed_string_width(f, s2);
270 int MathRow::kerning(BufferView const * bv) const
272 if (elements_.empty())
274 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
275 return inset ? inset->kerning(bv) : 0;
279 ostream & operator<<(ostream & os, MathRow::Element const & e)
283 os << (e.mclass == MC_OPEN ? "{" : "}");
286 os << "<" << e.before << "-"
287 << to_utf8(class_to_string(e.mclass))
288 << "-" << e.after << ">";
290 case MathRow::BEG_MACRO:
291 os << "\\" << to_utf8(e.macro->name())
292 << "^" << e.macro->nesting() << "[";
294 case MathRow::END_MACRO:
297 case MathRow::BEG_ARG:
300 case MathRow::END_ARG:
304 os << "<" << e.before << "-[]-" << e.after << ">";
311 ostream & operator<<(ostream & os, MathRow const & mrow)
313 for (MathRow::Element const & e : mrow)