]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / insets / insetspace.C
1 /**
2  * \file insetspace.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Jean-Marc Lasgouttes
8  * \author Lars Gullik Bjønnes
9  * \author Juergen Spitzmueller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "insetspace.h"
17
18 #include "debug.h"
19 #include "dimension.h"
20 #include "LaTeXFeatures.h"
21 #include "latexrunparams.h"
22 #include "BufferView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
25 #include "lyxlex.h"
26 #include "lyxfont.h"
27 #include "metricsinfo.h"
28
29 using std::ostream;
30 using std::max;
31 using std::auto_ptr;
32
33
34 InsetSpace::InsetSpace()
35 {}
36
37
38 InsetSpace::InsetSpace(Kind k)
39         : kind_(k)
40 {}
41
42
43 InsetSpace::Kind InsetSpace::kind() const
44 {
45         return kind_;
46 }
47
48
49 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
50 {
51         LyXFont & font = mi.base.font;
52         dim.asc = font_metrics::maxAscent(font);
53         dim.des = font_metrics::maxDescent(font);
54
55         switch (kind_) {
56                 case THIN:
57                 case NEGTHIN:
58                         dim.wid = font_metrics::width("x", font) / 3;
59                         break;
60                 case PROTECTED:
61                 case NORMAL:
62                         dim.wid = font_metrics::width("x", font);
63                         break;
64                 case QUAD:
65                         dim.wid = 20;
66                         break;
67                 case QQUAD:
68                         dim.wid = 40;
69                         break;
70                 case ENSPACE:
71                 case ENSKIP:
72                         dim.wid = 10;
73                         break;
74         }
75         dim_ = dim;
76 }
77
78
79 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
80 {
81         int const w = width();
82         int const h = font_metrics::ascent('x', pi.base.font);
83         int xp[4], yp[4];
84
85         xp[0] = x;
86         yp[0] = y - max(h / 4, 1);
87         if (kind_ == NORMAL) {
88                 xp[1] = x;     yp[1] = y;
89                 xp[2] = x + w; yp[2] = y;
90         } else {
91                 xp[1] = x;     yp[1] = y + max(h / 4, 1);
92                 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
93         }
94         xp[3] = x + w;
95         yp[3] = y - max(h / 4, 1);
96
97         if (kind_ == PROTECTED || kind_ == ENSPACE || kind_ == NEGTHIN)
98                 pi.pain.lines(xp, yp, 4, LColor::latex);
99         else
100                 pi.pain.lines(xp, yp, 4, LColor::special);
101 }
102
103
104 void InsetSpace::write(Buffer const &, ostream & os) const
105 {
106         string command;
107         switch (kind_) {
108         case NORMAL:
109                 command = "\\space";
110                 break;
111         case PROTECTED:
112                 command = "~";
113                 break;
114         case THIN:
115                 command = "\\,";
116                 break;
117         case QUAD:
118                 command = "\\quad{}";
119                 break;
120         case QQUAD:
121                 command = "\\qquad{}";
122                 break;
123         case ENSPACE:
124                 command = "\\enspace{}";
125                 break;
126         case ENSKIP:
127                 command = "\\enskip{}";
128                 break;
129         case NEGTHIN:
130                 command = "\\negthinspace{}";
131                 break;
132         }
133         os << "\\InsetSpace " << command << "\n";
134 }
135
136
137 // This function will not be necessary when lyx3
138 void InsetSpace::read(Buffer const &, LyXLex & lex)
139 {
140         lex.nextToken();
141         string const command = lex.getString();
142
143         if (command == "\\space")
144                 kind_ = NORMAL;
145         else if (command == "~")
146                 kind_ = PROTECTED;
147         else if (command == "\\,")
148                 kind_ = THIN;
149         else if (command == "\\quad{}")
150                 kind_ = QUAD;
151         else if (command == "\\qquad{}")
152                 kind_ = QQUAD;
153         else if (command == "\\enspace{}")
154                 kind_ = ENSPACE;
155         else if (command == "\\enskip{}")
156                 kind_ = ENSKIP;
157         else if (command == "\\negthinspace{}")
158                 kind_ = NEGTHIN;
159         else
160                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
161 }
162
163
164 int InsetSpace::latex(Buffer const &, ostream & os,
165                       LatexRunParams const & runparams) const
166 {
167         switch (kind_) {
168         case NORMAL:
169                 os << (runparams.free_spacing ? " " : "\\ ");
170                 break;
171         case PROTECTED:
172                 os << (runparams.free_spacing ? ' ' : '~');
173                 break;
174         case THIN:
175                 os << (runparams.free_spacing ? " " : "\\,");
176                 break;
177         case QUAD:
178                 os << (runparams.free_spacing ? " " : "\\quad{}");
179                 break;
180         case QQUAD:
181                 os << (runparams.free_spacing ? " " : "\\qquad{}");
182                 break;
183         case ENSPACE:
184                 os << (runparams.free_spacing ? " " : "\\enspace{}");
185                 break;
186         case ENSKIP:
187                 os << (runparams.free_spacing ? " " : "\\enskip{}");
188                 break;
189         case NEGTHIN:
190                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
191                 break;
192         }
193         return 0;
194 }
195
196
197 int InsetSpace::ascii(Buffer const &, ostream & os, int) const
198 {
199         switch (kind_) {
200         case NORMAL:
201         case PROTECTED:
202         case THIN:
203         case QUAD:
204         case QQUAD:
205         case ENSPACE:
206         case ENSKIP:
207         case NEGTHIN:
208                 os << ' ';
209                 break;
210         }
211         return 0;
212 }
213
214
215 int InsetSpace::linuxdoc(Buffer const &, ostream & os) const
216 {
217         switch (kind_) {
218         case NORMAL:
219         case QUAD:
220         case QQUAD:
221         case ENSKIP:
222                 os << " ";
223                 break;
224         case PROTECTED:
225         case ENSPACE:
226         case THIN:
227         case NEGTHIN:
228                 os << "&nbsp;";
229                 break;
230         }
231         return 0;
232 }
233
234
235 int InsetSpace::docbook(Buffer const &, ostream & os, bool) const
236 {
237         switch (kind_) {
238         case NORMAL:
239         case QUAD:
240         case QQUAD:
241         case ENSKIP:
242                 os << " ";
243                 break;
244         case PROTECTED:
245         case ENSPACE:
246         case THIN:
247         case NEGTHIN:
248                 os << "&nbsp;";
249                 break;
250         }
251         return 0;
252 }
253
254
255 auto_ptr<InsetBase>  InsetSpace::clone() const
256 {
257         return auto_ptr<InsetBase>(new InsetSpace(kind_));
258 }
259
260
261 bool InsetSpace::isChar() const
262 {
263         return true;
264 }
265
266 bool InsetSpace::isLetter() const
267 {
268         return false;
269 }
270
271 bool InsetSpace::isSpace() const
272 {
273         return true;
274 }
275
276 bool InsetSpace::isLineSeparator() const
277 {
278         return false;
279 }