]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathPhantom.cpp
Allow automatic text direction in Painter::text()
[lyx.git] / src / mathed / InsetMathPhantom.cpp
1 /**
2  * \file InsetMathPhantom.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Georg Baum
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathPhantom.h"
14
15 #include "LaTeXFeatures.h"
16 #include "MathStream.h"
17 #include "frontends/Painter.h"
18
19 #include <algorithm>
20 #include <ostream>
21
22 namespace lyx {
23
24
25 InsetMathPhantom::InsetMathPhantom(Buffer * buf, Kind k)
26         : InsetMathNest(buf, 1), kind_(k)
27 {}
28
29
30 Inset * InsetMathPhantom::clone() const
31 {
32         return new InsetMathPhantom(*this);
33 }
34
35
36 void InsetMathPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
37 {
38         cell(0).metrics(mi, dim);
39         metricsMarkers(dim);
40 }
41
42
43 void InsetMathPhantom::draw(PainterInfo & pi, int x, int y) const
44 {
45         static int const arrow_size = 4;
46
47         // We first draw the text and then an arrow
48         ColorCode const origcol = pi.base.font.color();
49         if (visibleContents())
50                 pi.base.font.setColor(Color_special);
51         cell(0).draw(pi, x + 1, y);
52         if (visibleContents())
53                 pi.base.font.setColor(origcol);
54         Dimension const dim = dimension(*pi.base.bv);
55
56         if (kind_ == phantom || kind_ == vphantom) {
57                 // y1---------
58                 //           / \.
59                 // y2-----  / | \.
60                 //            |
61                 //            |
62                 // y3-----  \ | /
63                 //           \ /
64                 // y4---------
65                 //          | | |
66                 //         /  |  \.
67                 //        x1  x2 x3
68
69                 int const x2 = x + dim.wid / 2;
70                 int const x1 = x2 - arrow_size;
71                 int const x3 = x2 + arrow_size;
72
73                 int const y1 = y - dim.asc;
74                 int const y2 = y1 + arrow_size;
75                 int const y4 = y + dim.des;
76                 int const y3 = y4 - arrow_size;
77
78                 // top arrow
79                 pi.pain.line(x2, y1, x1, y2, Color_added_space);
80                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
81
82                 // bottom arrow
83                 pi.pain.line(x2, y4, x1, y3, Color_added_space);
84                 pi.pain.line(x2, y4, x3, y3, Color_added_space);
85
86                 // joining line
87                 pi.pain.line(x2, y1, x2, y4, Color_added_space);
88         }
89
90         if (kind_ == phantom || kind_ == hphantom) {
91                 // y1----  /            \.
92                 //        /              \.
93                 // y2--- <---------------->
94                 //        \              /
95                 // y3----  \            /
96                 //       |  |          |  |
97                 //      x1 x2         x3 x4
98
99                 int const x1 = x;
100                 int const x2 = x + arrow_size;
101                 int const x4 = x + dim.wid;
102                 int const x3 = x4 - arrow_size;
103
104                 int const y2 = y + (dim.des - dim.asc) / 2;
105                 int const y1 = y2 - arrow_size;
106                 int const y3 = y2 + arrow_size;
107
108                 // left arrow
109                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
110                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
111
112                 // right arrow
113                 pi.pain.line(x4, y2, x3, y3, Color_added_space);
114                 pi.pain.line(x4, y2, x3, y1, Color_added_space);
115
116                 // joining line
117                 pi.pain.line(x1, y2, x4, y2, Color_added_space);
118         }
119
120         else if (kind_ == mathclap) {
121                 // y1----      \     /
122                 //              \   /
123                 // y2--- -------->-<--------
124                 //              /   \.
125                 // y3----      /     \.
126                 //       |    |   |   |    |
127                 //      x1   x2  x3  x4   x5
128
129                 int const x1 = x;
130                 int const x5 = x + dim.wid;
131                 int const x3 = x + dim.wid / 2;
132                 int const x2 = std::max(x1, x3 - arrow_size);
133                 int const x4 = std::min(x5, x3 + arrow_size);
134
135                 int const y2 = y + (dim.des - dim.asc) / 2;
136                 int const y1 = y2 - arrow_size;
137                 int const y3 = y2 + arrow_size;
138
139                 // left arrow
140                 pi.pain.line(x2, y3, x3, y2, Color_added_space);
141                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
142
143                 // right arrow
144                 pi.pain.line(x4, y3, x3, y2, Color_added_space);
145                 pi.pain.line(x4, y1, x3, y2, Color_added_space);
146
147                 // joining line
148                 pi.pain.line(x1, y2, x5, y2, Color_added_space);
149         }
150
151         else if (kind_ == mathllap) {
152                 // y1----                \.
153                 //                        \.
154                 // y2--- ------------------>
155                 //                        /
156                 // y3----                /
157                 //       |              |  |
158                 //      x1             x2 x3
159
160                 int const x1 = x;
161                 int const x3 = x + dim.wid;
162                 int const x2 = std::max(x1, x3 - arrow_size);
163
164                 int const y2 = y + (dim.des - dim.asc) / 2;
165                 int const y1 = y2 - arrow_size;
166                 int const y3 = y2 + arrow_size;
167
168                 // right arrow
169                 pi.pain.line(x3, y2, x2, y3, Color_added_space);
170                 pi.pain.line(x3, y2, x2, y1, Color_added_space);
171
172                 // joining line
173                 pi.pain.line(x1, y2, x3, y2, Color_added_space);
174         }
175
176         else if (kind_ == mathrlap) {
177                 // y1----  /
178                 //        /
179                 // y2--- <------------------
180                 //        \.
181                 // y3----  \.
182                 //       |  |              |
183                 //      x1 x2             x3
184
185                 int const x1 = x;
186                 int const x3 = x + dim.wid;
187                 int const x2 = std::min(x3, x + arrow_size);
188
189                 int const y2 = y + (dim.des - dim.asc) / 2;
190                 int const y1 = y2 - arrow_size;
191                 int const y3 = y2 + arrow_size;
192
193                 // left arrow
194                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
195                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
196
197                 // joining line
198                 pi.pain.line(x1, y2, x3, y2, Color_added_space);
199         }
200
201         else if (kind_ == smash || kind_ == smasht || kind_ == smashb) {
202                 // y1---------
203                 //            |
204                 // y2-----  \ | /
205                 //           \ /
206                 // y3-------- |
207                 //           / \.
208                 // y4-----  / | \.
209                 //            |
210                 // y5---------
211                 //          | | |
212                 //         /  |  \.
213                 //        x1  x2 x3
214
215                 int const x2 = x + dim.wid / 2;
216                 int const x1 = x2 - arrow_size;
217                 int const x3 = x2 + arrow_size;
218
219                 int const y1 = y - dim.asc;
220                 int const y5 = y + dim.des;
221                 int const y3 = y;
222                 int const y2 = std::max(y1, y3 - arrow_size);
223                 int const y4 = std::min(y5, y3 + arrow_size);
224
225                 // top arrow
226                 if (kind_ != smashb) {
227                         pi.pain.line(x1, y2, x2, y3, Color_added_space);
228                         pi.pain.line(x3, y2, x2, y3, Color_added_space);
229                 }
230
231                 // bottom arrow
232                 if (kind_ != smasht) {
233                         pi.pain.line(x1, y4, x2, y3, Color_added_space);
234                         pi.pain.line(x3, y4, x2, y3, Color_added_space);
235                 }
236
237                 // joining line
238                 if (kind_ == smasht)
239                         pi.pain.line(x2, y1, x2, y3, Color_added_space);
240                 else if (kind_ == smashb)
241                         pi.pain.line(x2, y3, x2, y5, Color_added_space);
242                 else
243                         pi.pain.line(x2, y1, x2, y5, Color_added_space);
244         }
245
246         drawMarkers(pi, x, y);
247 }
248
249
250 void InsetMathPhantom::write(WriteStream & os) const
251 {
252         MathEnsurer ensurer(os);
253         if (os.fragile())
254                 os << "\\protect";
255         switch (kind_) {
256         case phantom:
257                 os << "\\phantom{";
258                 break;
259         case vphantom:
260                 os << "\\vphantom{";
261                 break;
262         case hphantom:
263                 os << "\\hphantom{";
264                 break;
265         case smash:
266                 os << "\\smash{";
267                 break;
268         case smasht:
269                 os << "\\smash[t]{";
270                 break;
271         case smashb:
272                 os << "\\smash[b]{";
273                 break;
274         case mathclap:
275                 os << "\\mathclap{";
276                 break;
277         case mathllap:
278                 os << "\\mathllap{";
279                 break;
280         case mathrlap:
281                 os << "\\mathrlap{";
282                 break;
283         }
284         os << cell(0) << '}';
285 }
286
287
288 void InsetMathPhantom::normalize(NormalStream & os) const
289 {
290         switch (kind_) {
291         case phantom:
292                 os << "[phantom ";
293                 break;
294         case vphantom:
295                 os << "[vphantom ";
296                 break;
297         case hphantom:
298                 os << "[hphantom ";
299                 break;
300         case smash:
301                 os << "[smash ";
302                 break;
303         case smasht:
304                 os << "[smasht ";
305                 break;
306         case smashb:
307                 os << "[smashb ";
308                 break;
309         case mathclap:
310                 os << "[mathclap ";
311                 break;
312         case mathllap:
313                 os << "[mathllap ";
314                 break;
315         case mathrlap:
316                 os << "[mathrlap ";
317                 break;
318         }
319         os << cell(0) << ']';
320 }
321
322
323 void InsetMathPhantom::infoize(odocstream & os) const
324 {
325         switch (kind_) {
326         case phantom:
327                 os << "Phantom";
328                 break;
329         case vphantom:
330                 os << "Vphantom";
331                 break;
332         case hphantom:
333                 os << "Hphantom";
334                 break;
335         case smash:
336                 os << "Smash";
337                 break;
338         case smasht:
339                 os << "Smashtop";
340                 break;
341         case smashb:
342                 os << "Smashbottom";
343                 break;
344         case mathllap:
345                 os << "Mathllap";
346                 break;
347         case mathclap:
348                 os << "Mathclap";
349                 break;
350         case mathrlap:
351                 os << "Mathrlap";
352                 break;
353         }
354 }
355
356
357 void InsetMathPhantom::validate(LaTeXFeatures & features) const
358 {
359         InsetMathNest::validate(features);
360         switch (kind_) {
361         case phantom:
362         case vphantom:
363         case hphantom:
364         case smash:
365                 break;
366         case smasht:
367         case smashb:
368                 features.require("amsmath");
369                 break;
370         case mathclap:
371         case mathllap:
372         case mathrlap:
373                 features.require("mathtools");
374                 break;
375         }
376 }
377
378
379 bool InsetMathPhantom::visibleContents() const 
380
381         return kind_ == phantom || kind_ == vphantom || kind_ == hphantom;
382 }
383
384
385 } // namespace lyx