]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
Set correctly the spacing between atoms in MathData
[lyx.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/Painter.h"
26
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/lassert.h"
30
31 #include <ostream>
32
33 using namespace std;
34
35 namespace lyx {
36
37
38 MathRow::Element::Element(Type t, MathClass const mc)
39         : type(t),
40           inset(0), mclass(mc), before(0), after(0), compl_unique_to(0),
41           macro(0)
42 {}
43
44
45 MathRow::MathRow(MetricsInfo const & mi, MathData const * ar)
46 {
47         if (ar->empty())
48                 return;
49
50         // First there is a dummy element of type "open"
51         push_back(Element(BEGIN, MC_OPEN));
52
53         // Then insert the MathData argument
54         ar->addToMathRow(*this, mi);
55
56         // Finally there is a dummy element of type "close"
57         push_back(Element(END, MC_CLOSE));
58
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.
61          */
62         if (!isMathFont(mi.base.fontname))
63                 return;
64
65         // update classes
66         for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
67                 if (elements_[i].type != INSET)
68                         continue;
69                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
70                              elements_[after(i)].mclass);
71         }
72
73         // set spacing
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)
77                         continue;
78                 Element & bef = elements_[before(i)];
79                 int spc = class_spacing(bef.mclass, elements_[i].mclass, mi.base);
80                 bef.after = spc / 2;
81                 // this is better than spc / 2 to avoid rounding problems
82                 elements_[i].before = spc - spc / 2;
83         }
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;
88         }
89 }
90
91
92 int MathRow::before(int i) const
93 {
94         do
95                 --i;
96         while (elements_[i].type != BEGIN
97                    && elements_[i].type != INSET);
98
99         return i;
100 }
101
102
103 int MathRow::after(int i) const
104 {
105         do
106                 ++i;
107         while (elements_[i].type != END
108                    && elements_[i].type != INSET);
109
110         return i;
111 }
112
113
114 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
115 {
116         dim.asc = 0;
117         dim.wid = 0;
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();
123
124         for (Element const & e : elements_) {
125                 Dimension d;
126                 switch (e.type) {
127                 case BEGIN:
128                 case END:
129                         break;
130                 case INSET:
131                         e.inset->metrics(mi, d);
132                         d.wid += e.before + e.after;
133                         coords.insets().add(e.inset, d);
134                         dim += 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;
140                         break;
141                 case BEG_MACRO:
142                         e.macro->macro()->lock();
143                         // Add a macro to current list
144                         dim_macros[e.macro] = Dimension();
145                         break;
146                 case END_MACRO:
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
150                         // tracking map.
151                         coords.insets().add(e.macro, dim_macros[e.macro]);
152                         dim_macros.erase(e.macro);
153                         break;
154                         // This is basically like macros
155                 case BEG_ARG:
156                         if (e.macro)
157                                 e.macro->macro()->unlock();
158                         dim_arrays[e.ar] = Dimension();
159                         break;
160                 case END_ARG:
161                         LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
162                         if (e.macro)
163                                 e.macro->macro()->lock();
164                         coords.arrays().add(e.ar, dim_arrays[e.ar]);
165                         dim_arrays.erase(e.ar);
166                         break;
167                 }
168
169                 if (e.compl_text.empty())
170                         continue;
171                 FontInfo font = mi.base.font;
172                 augmentFont(font, "mathnormal");
173                 dim.wid += mathed_string_width(font, e.compl_text);
174         }
175         LATTEST(dim_macros.empty() && dim_arrays.empty());
176 }
177
178
179 void MathRow::draw(PainterInfo & pi, int x, int const y) const
180 {
181         CoordCache & coords = pi.base.bv->coordCache();
182         for (Element const & e : elements_) {
183                 Dimension d;
184                 switch (e.type) {
185                 case INSET: {
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);
190                         Dimension d2 = d;
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);
197                         x += d.wid;
198                         break;
199                 }
200                 case BEG_MACRO:
201                         coords.insets().add(e.macro, x, y);
202                         break;
203                 case BEG_ARG:
204                         coords.arrays().add(e.ar, x, y);
205                         // if the macro is being edited, then the painter is in
206                         // monochrome mode.
207                         if (e.macro->editMetrics(pi.base.bv))
208                                 pi.pain.leaveMonochromeMode();
209                         break;
210                 case END_ARG:
211                         if (e.macro->editMetrics(pi.base.bv))
212                                 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
213                         break;
214                 case BEGIN:
215                 case END:
216                 case END_MACRO:
217                         break;
218                 }
219
220                 if (e.compl_text.empty())
221                         continue;
222                 FontInfo f = pi.base.font;
223                 augmentFont(f, "mathnormal");
224
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);
230
231                 if (!s1.empty()) {
232                         f.setColor(Color_inlinecompletion);
233                         pi.pain.text(x, y, s1, f);
234                         x += mathed_string_width(f, s1);
235                 }
236                 if (!s2.empty()) {
237                         f.setColor(Color_nonunique_inlinecompletion);
238                         pi.pain.text(x, y, s2, f);
239                         x += mathed_string_width(f, s2);
240                 }
241         }
242 }
243
244
245 int MathRow::kerning(BufferView const * bv) const
246 {
247         if (elements_.empty())
248                 return 0;
249         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
250         return inset ? inset->kerning(bv) : 0;
251 }
252
253
254 ostream & operator<<(ostream & os, MathRow::Element const & e)
255 {
256         switch (e.type) {
257         case MathRow::BEGIN:
258                 os << "{";
259                 break;
260         case MathRow::END:
261                 os << "}";
262                 break;
263         case MathRow::INSET:
264                 os << "<" << e.before << "-"
265                    << to_utf8(class_to_string(e.mclass))
266                    << "-" << e.after << ">";
267                 break;
268         case MathRow::BEG_MACRO:
269                 os << "\\" << to_utf8(e.macro->name()) << "[";
270                 break;
271         case MathRow::END_MACRO:
272                 os << "]";
273                 break;
274         case MathRow::BEG_ARG:
275                 os << "#(";
276                 break;
277         case MathRow::END_ARG:
278                 os << ")";
279                 break;
280         }
281         return os;
282 }
283
284
285 ostream & operator<<(ostream & os, MathRow const & mrow)
286 {
287         for (MathRow::Element const & e : mrow)
288                 os << e << "  ";
289         return os;
290 }
291
292 } // namespace lyx