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/Painter.h"
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/lassert.h"
38 MathRow::Element::Element(Type t, MathClass const mc)
40 inset(0), mclass(mc), before(0), after(0), compl_unique_to(0),
45 MathRow::MathRow(MetricsInfo const & mi, MathData const * ar)
50 // First there is a dummy element of type "open"
51 push_back(Element(BEGIN, MC_OPEN));
53 // Then insert the MathData argument
54 ar->addToMathRow(*this, mi);
56 // Finally there is a dummy element of type "close"
57 push_back(Element(END, MC_CLOSE));
59 /* Do spacing only in math mode. This test is a bit clumsy,
60 * but it is used in other places for guessing the current mode.
62 if (!isMathFont(mi.base.fontname))
66 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
67 if (elements_[i].type != INSET)
69 update_class(elements_[i].mclass, elements_[before(i)].mclass,
70 elements_[after(i)].mclass);
74 // We go to the end to handle spacing at the end of equation
75 for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
76 if (elements_[i].type != INSET)
78 Element & bef = elements_[before(i)];
79 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
81 // this is better than spc / 2 to avoid rounding problems
82 elements_[i].before = spc - spc / 2;
84 // Do not lose spacing allocated to extremities
85 if (!elements_.empty()) {
86 elements_[after(0)].before += elements_.front().after;
87 elements_[before(elements_.size() - 1)].after += elements_.back().before;
92 int MathRow::before(int i) const
96 while (elements_[i].type != BEGIN
97 && elements_[i].type != INSET);
103 int MathRow::after(int i) const
107 while (elements_[i].type != END
108 && elements_[i].type != INSET);
114 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
118 // In order to compute the dimension of macros and their
119 // arguments, it is necessary to keep track of them.
120 map<MathMacro const *, Dimension> dim_macros;
121 map<MathData const *, Dimension> dim_arrays;
122 CoordCache & coords = mi.base.bv->coordCache();
124 for (Element const & e : elements_) {
131 e.inset->metrics(mi, d);
132 d.wid += e.before + e.after;
133 coords.insets().add(e.inset, d);
135 // Now add the dimension to current macros and arguments.
136 for (auto & dim_macro : dim_macros)
137 dim_macro.second += d;
138 for (auto & dim_array : dim_arrays)
139 dim_array.second += d;
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 e.macro->macro()->unlock();
149 // Cache the dimension of the macro and remove it from
151 coords.insets().add(e.macro, dim_macros[e.macro]);
152 dim_macros.erase(e.macro);
154 // This is basically like macros
157 e.macro->macro()->unlock();
158 dim_arrays[e.ar] = Dimension();
161 LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
163 e.macro->macro()->lock();
164 coords.arrays().add(e.ar, dim_arrays[e.ar]);
165 dim_arrays.erase(e.ar);
169 if (e.compl_text.empty())
171 FontInfo font = mi.base.font;
172 augmentFont(font, "mathnormal");
173 dim.wid += mathed_string_width(font, e.compl_text);
175 LATTEST(dim_macros.empty() && dim_arrays.empty());
179 void MathRow::draw(PainterInfo & pi, int x, int const y) const
181 CoordCache & coords = pi.base.bv->coordCache();
182 for (Element const & e : elements_) {
186 // This is hackish: the math inset does not know that space
187 // has been added before and after it; we alter its dimension
188 // while it is drawing, because it relies on this value.
189 Dimension const d = coords.insets().dim(e.inset);
191 d2.wid -= e.before + e.after;
192 coords.insets().add(e.inset, d2);
193 e.inset->drawSelection(pi, x + e.before, y);
194 e.inset->draw(pi, x + e.before, y);
195 coords.insets().add(e.inset, x, y);
196 coords.insets().add(e.inset, d);
201 coords.insets().add(e.macro, x, y);
204 coords.arrays().add(e.ar, x, y);
205 // if the macro is being edited, then the painter is in
207 if (e.macro->editMetrics(pi.base.bv))
208 pi.pain.leaveMonochromeMode();
211 if (e.macro->editMetrics(pi.base.bv))
212 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
220 if (e.compl_text.empty())
222 FontInfo f = pi.base.font;
223 augmentFont(f, "mathnormal");
225 // draw the unique and the non-unique completion part
226 // Note: this is not time-critical as it is
227 // only done once per screen.
228 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
229 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
232 f.setColor(Color_inlinecompletion);
233 pi.pain.text(x, y, s1, f);
234 x += mathed_string_width(f, s1);
237 f.setColor(Color_nonunique_inlinecompletion);
238 pi.pain.text(x, y, s2, f);
239 x += mathed_string_width(f, s2);
245 int MathRow::kerning(BufferView const * bv) const
247 if (elements_.empty())
249 InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
250 return inset ? inset->kerning(bv) : 0;
254 ostream & operator<<(ostream & os, MathRow::Element const & e)
264 os << "<" << e.before << "-"
265 << to_utf8(class_to_string(e.mclass))
266 << "-" << e.after << ">";
268 case MathRow::BEG_MACRO:
269 os << "\\" << to_utf8(e.macro->name()) << "[";
271 case MathRow::END_MACRO:
274 case MathRow::BEG_ARG:
277 case MathRow::END_ARG:
285 ostream & operator<<(ostream & os, MathRow const & mrow)
287 for (MathRow::Element const & e : mrow)