]> git.lyx.org Git - features.git/blob - src/mathed/MathRow.cpp
730d7a4a18a9e8835407f53ad5d6dc71d323961a
[features.git] / src / mathed / MathRow.cpp
1 /**
2  * \file MathRow.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathRow.h"
14
15 #include "InsetMath.h"
16 #include "MathClass.h"
17 #include "MathData.h"
18 #include "MathMacro.h"
19 #include "MathSupport.h"
20
21 #include "BufferView.h"
22 #include "CoordCache.h"
23 #include "MetricsInfo.h"
24
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/debug.h"
29 #include "support/docstring.h"
30 #include "support/lassert.h"
31
32 #include <ostream>
33
34 using namespace std;
35
36 namespace lyx {
37
38
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)
42 {}
43
44
45 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
46 {
47         // First there is a dummy element of type "open"
48         push_back(Element(mi, DUMMY, MC_OPEN));
49
50         // Then insert the MathData argument
51         bool const has_contents = ar->addToMathRow(*this, mi);
52
53         // empty arrays are visible when they are editable
54         // we reserve the necessary space anyway (even if nothing gets drawn)
55         if (!has_contents) {
56                 Element e(mi, BOX, MC_ORD);
57                 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
58                 push_back(e);
59         }
60
61         // Finally there is a dummy element of type "close"
62         push_back(Element(mi, DUMMY, MC_CLOSE));
63
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.
66          */
67         if (!isMathFont(mi.base.fontname))
68                 return;
69
70         // update classes
71         for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
72                 if (elements_[i].mclass == MC_UNKNOWN)
73                         continue;
74                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
75                              elements_[after(i)].mclass);
76         }
77
78         // set spacing
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)
82                         continue;
83                 Element & bef = elements_[before(i)];
84                 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
85                 bef.after = spc / 2;
86                 // this is better than spc / 2 to avoid rounding problems
87                 elements_[i].before = spc - spc / 2;
88         }
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;
93         }
94 }
95
96
97 int MathRow::before(int i) const
98 {
99         do
100                 --i;
101         while (elements_[i].mclass == MC_UNKNOWN);
102
103         return i;
104 }
105
106
107 int MathRow::after(int i) const
108 {
109         do
110                 ++i;
111         while (elements_[i].mclass == MC_UNKNOWN);
112
113         return i;
114 }
115
116
117 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
118 {
119         dim.asc = 0;
120         dim.wid = 0;
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;
128                 Dimension d;
129                 switch (e.type) {
130                 case DUMMY:
131                         break;
132                 case INSET:
133                         e.inset->metrics(mi, d);
134                         d.wid += e.before + e.after;
135                         coords.insets().add(e.inset, d);
136                         break;
137                 case BEG_MACRO:
138                         e.macro->macro()->lock();
139                         // Add a macro to current list
140                         dim_macros[e.macro] = Dimension();
141                         break;
142                 case END_MACRO:
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
146                         // tracking map.
147                         coords.insets().add(e.macro, dim_macros[e.macro]);
148                         dim_macros.erase(e.macro);
149                         break;
150                         // This is basically like macros
151                 case BEG_ARG:
152                         if (e.macro)
153                                 e.macro->macro()->unlock();
154                         dim_arrays[e.ar] = Dimension();
155                         break;
156                 case END_ARG:
157                         LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
158                         if (e.macro)
159                                 e.macro->macro()->lock();
160                         coords.arrays().add(e.ar, dim_arrays[e.ar]);
161                         dim_arrays.erase(e.ar);
162                         break;
163                 case BOX:
164                         d = theFontMetrics(mi.base.font).dimension('I');
165                         d.wid += e.before + e.after;
166                         break;
167                 }
168
169                 if (!d.empty()) {
170                         dim += d;
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;
176                 }
177
178                 if (e.compl_text.empty())
179                         continue;
180                 FontInfo font = mi.base.font;
181                 augmentFont(font, "mathnormal");
182                 dim.wid += mathed_string_width(font, e.compl_text);
183         }
184         LATTEST(dim_macros.empty() && dim_arrays.empty());
185 }
186
187
188 void MathRow::draw(PainterInfo & pi, int x, int const y) const
189 {
190         CoordCache & coords = pi.base.bv->coordCache();
191         for (Element const & e : elements_) {
192                 switch (e.type) {
193                 case INSET: {
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);
198                         Dimension d2 = d;
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);
205                         x += d.wid;
206                         break;
207                 }
208                 case BEG_MACRO:
209                         coords.insets().add(e.macro, x, y);
210                         break;
211                 case BEG_ARG:
212                         coords.arrays().add(e.ar, x, y);
213                         // if the macro is being edited, then the painter is in
214                         // monochrome mode.
215                         if (e.macro->editMetrics(pi.base.bv))
216                                 pi.pain.leaveMonochromeMode();
217                         break;
218                 case END_ARG:
219                         if (e.macro->editMetrics(pi.base.bv))
220                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
221                         break;
222                 case BOX: {
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;
229                         break;
230                 }
231                 case DUMMY:
232                 case END_MACRO:
233                         break;
234                 }
235
236                 if (e.compl_text.empty())
237                         continue;
238                 FontInfo f = pi.base.font;
239                 augmentFont(f, "mathnormal");
240
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);
246
247                 if (!s1.empty()) {
248                         f.setColor(Color_inlinecompletion);
249                         pi.pain.text(x, y, s1, f);
250                         x += mathed_string_width(f, s1);
251                 }
252                 if (!s2.empty()) {
253                         f.setColor(Color_nonunique_inlinecompletion);
254                         pi.pain.text(x, y, s2, f);
255                         x += mathed_string_width(f, s2);
256                 }
257         }
258 }
259
260
261 int MathRow::kerning(BufferView const * bv) const
262 {
263         if (elements_.empty())
264                 return 0;
265         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
266         return inset ? inset->kerning(bv) : 0;
267 }
268
269
270 ostream & operator<<(ostream & os, MathRow::Element const & e)
271 {
272         switch (e.type) {
273         case MathRow::DUMMY:
274                 os << (e.mclass == MC_OPEN ? "{" : "}");
275                 break;
276         case MathRow::INSET:
277                 os << "<" << e.before << "-"
278                    << to_utf8(class_to_string(e.mclass))
279                    << "-" << e.after << ">";
280                 break;
281         case MathRow::BEG_MACRO:
282                 os << "\\" << to_utf8(e.macro->name())
283                    << "^" << e.macro->nesting() << "[";
284                 break;
285         case MathRow::END_MACRO:
286                 os << "]";
287                 break;
288         case MathRow::BEG_ARG:
289                 os << "#(";
290                 break;
291         case MathRow::END_ARG:
292                 os << ")";
293                 break;
294         case MathRow::BOX:
295                 os << "<" << e.before << "-[]-" << e.after << ">";
296                 break;
297         }
298         return os;
299 }
300
301
302 ostream & operator<<(ostream & os, MathRow const & mrow)
303 {
304         for (MathRow::Element const & e : mrow)
305                 os << e << "  ";
306         return os;
307 }
308
309 } // namespace lyx