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, MetricsInfo &mi)
40 : type(t), macro_nesting(mi.base.macro_nesting),
41 inset(0), mclass(MC_ORD), before(0), after(0), compl_unique_to(0),
42 macro(0), color(Color_red)
46 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
48 // First there is a dummy element of type "open"
49 push_back(Element(BEGIN, mi));
50 back().mclass = MC_OPEN;
52 // Then insert the MathData argument
53 bool const has_contents = ar->addToMathRow(*this, mi);
55 // empty arrays are visible when they are editable
56 // we reserve the necessary space anyway (even if nothing gets drawn)
59 e.color = Color_mathline;
63 // Finally there is a dummy element of type "close"
64 push_back(Element(END, mi));
65 back().mclass = MC_CLOSE;
67 /* Do spacing only in math mode. This test is a bit clumsy,
68 * but it is used in other places for guessing the current mode.
70 if (!isMathFont(mi.base.fontname))
74 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
75 if (elements_[i].type != INSET)
77 update_class(elements_[i].mclass, elements_[before(i)].mclass,
78 elements_[after(i)].mclass);
82 // We go to the end to handle spacing at the end of equation
83 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
84 if (elements_[i].type != INSET)
86 Element & bef = elements_[before(i)];
87 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
89 // this is better than spc / 2 to avoid rounding problems
90 elements_[i].before = spc - spc / 2;
92 // Do not lose spacing allocated to extremities
93 if (!elements_.empty()) {
94 elements_[after(0)].before += elements_.front().after;
95 elements_[before(elements_.size() - 1)].after += elements_.back().before;
100 int MathRow::before(int i) const
104 while (elements_[i].type != BEGIN
105 && elements_[i].type != INSET);
111 int MathRow::after(int i) const
115 while (elements_[i].type != END
116 && elements_[i].type != INSET);
122 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
126 // In order to compute the dimension of macros and their
127 // arguments, it is necessary to keep track of them.
128 map<MathMacro const *, Dimension> dim_macros;
129 map<MathData const *, Dimension> dim_arrays;
130 CoordCache & coords = mi.base.bv->coordCache();
131 for (Element const & e : elements_) {
133 mi.base.macro_nesting = e.macro_nesting;
139 e.inset->metrics(mi, d);
140 d.wid += e.before + e.after;
141 coords.insets().add(e.inset, d);
144 e.macro->macro()->lock();
145 // Add a macro to current list
146 dim_macros[e.macro] = Dimension();
149 LATTEST(dim_macros.find(e.macro) != dim_macros.end());
150 e.macro->macro()->unlock();
151 // Cache the dimension of the macro and remove it from
153 coords.insets().add(e.macro, dim_macros[e.macro]);
154 dim_macros.erase(e.macro);
156 // This is basically like macros
159 e.macro->macro()->unlock();
160 dim_arrays[e.ar] = Dimension();
163 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
165 e.macro->macro()->lock();
166 coords.arrays().add(e.ar, dim_arrays[e.ar]);
167 dim_arrays.erase(e.ar);
170 d = theFontMetrics(mi.base.font).dimension('I');
171 d.wid += e.before + e.after;
177 // Now add the dimension to current macros and arguments.
178 for (auto & dim_macro : dim_macros)
179 dim_macro.second += d;
180 for (auto & dim_array : dim_arrays)
181 dim_array.second += d;
184 if (e.compl_text.empty())
186 FontInfo font = mi.base.font;
187 augmentFont(font, "mathnormal");
188 dim.wid += mathed_string_width(font, e.compl_text);
190 LATTEST(dim_macros.empty() && dim_arrays.empty());
194 void MathRow::draw(PainterInfo & pi, int x, int const y) const
196 CoordCache & coords = pi.base.bv->coordCache();
197 for (Element const & e : elements_) {
198 pi.base.macro_nesting = e.macro_nesting;
201 // This is hackish: the math inset does not know that space
202 // has been added before and after it; we alter its dimension
203 // while it is drawing, because it relies on this value.
204 Dimension const d = coords.insets().dim(e.inset);
206 d2.wid -= e.before + e.after;
207 coords.insets().add(e.inset, d2);
208 e.inset->drawSelection(pi, x + e.before, y);
209 e.inset->draw(pi, x + e.before, y);
210 coords.insets().add(e.inset, x, y);
211 coords.insets().add(e.inset, d);
216 coords.insets().add(e.macro, x, y);
219 coords.arrays().add(e.ar, x, y);
220 // if the macro is being edited, then the painter is in
222 if (e.macro->editMetrics(pi.base.bv))
223 pi.pain.leaveMonochromeMode();
226 if (e.macro->editMetrics(pi.base.bv))
227 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
230 Dimension const d = theFontMetrics(pi.base.font).dimension('I');
231 // the box is not visible in non-editable context (except for grey macro boxes).
232 if (e.macro_nesting == 0 || e.color == Color_mathmacroblend)
233 pi.pain.rectangle(x + e.before, y - d.ascent(),
234 d.width(), d.height(), e.color);
244 if (e.compl_text.empty())
246 FontInfo f = pi.base.font;
247 augmentFont(f, "mathnormal");
249 // draw the unique and the non-unique completion part
250 // Note: this is not time-critical as it is
251 // only done once per screen.
252 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
253 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
256 f.setColor(Color_inlinecompletion);
257 pi.pain.text(x, y, s1, f);
258 x += mathed_string_width(f, s1);
261 f.setColor(Color_nonunique_inlinecompletion);
262 pi.pain.text(x, y, s2, f);
263 x += mathed_string_width(f, s2);
269 int MathRow::kerning(BufferView const * bv) const
271 if (elements_.empty())
273 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
274 return inset ? inset->kerning(bv) : 0;
278 ostream & operator<<(ostream & os, MathRow::Element const & e)
288 os << "<" << e.before << "-"
289 << to_utf8(class_to_string(e.mclass))
290 << "-" << e.after << ">";
292 case MathRow::BEG_MACRO:
293 os << "\\" << to_utf8(e.macro->name()) << "[";
295 case MathRow::END_MACRO:
298 case MathRow::BEG_ARG:
301 case MathRow::END_ARG:
312 ostream & operator<<(ostream & os, MathRow const & mrow)
314 for (MathRow::Element const & e : mrow)