]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
Rename LatexRunParams::fragile as moving_arg.
[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
28 using std::ostream;
29 using std::max;
30
31
32 InsetSpace::InsetSpace(Kind k)
33         : kind_(k)
34 {}
35
36
37 InsetSpace::Kind InsetSpace::kind() const
38 {
39         return kind_;
40 }
41
42
43 void InsetSpace::dimension(BufferView *, LyXFont const & font,
44                            Dimension & dim) const
45 {
46         dim.a = font_metrics::maxAscent(font);
47         dim.d = font_metrics::maxDescent(font);
48
49         switch (kind_) {
50                 case THIN:
51                 case NEGTHIN:
52                         dim.w = font_metrics::width("x", font) / 3;
53                         break;
54                 case PROTECTED:
55                 case NORMAL:
56                         dim.w = font_metrics::width("x", font);
57                         break;
58                 case QUAD:
59                         dim.w = 20;
60                         break;
61                 case QQUAD:
62                         dim.w = 40;
63                         break;
64                 case ENSPACE:
65                 case ENSKIP:
66                         dim.w = 10;
67                         break;
68         }
69 }
70
71
72 void InsetSpace::draw(BufferView * bv, LyXFont const & f,
73                             int baseline, float & x) const
74 {
75         Painter & pain = bv->painter();
76         LyXFont font(f);
77
78         float w = width(bv, font);
79         int h = font_metrics::ascent('x', font);
80         int xp[4], yp[4];
81
82         xp[0] = int(x); yp[0] = baseline - max(h / 4, 1);
83         if (kind_ == NORMAL) {
84                 xp[1] = int(x); yp[1] = baseline;
85                 xp[2] = int(x + w); yp[2] = baseline;
86         } else {
87                 xp[1] = int(x); yp[1] = baseline + max(h / 4, 1);
88                 xp[2] = int(x + w); yp[2] = baseline + max(h / 4, 1);
89         }
90         xp[3] = int(x + w); yp[3] = baseline - max(h / 4, 1);
91
92         if (kind_ == PROTECTED || kind_ == ENSPACE || kind_ == NEGTHIN)
93                 pain.lines(xp, yp, 4, LColor::latex);
94         else
95                 pain.lines(xp, yp, 4, LColor::special);
96         x += w;
97 }
98
99
100 void InsetSpace::write(Buffer const *, ostream & os) const
101 {
102         string command;
103         switch (kind_) {
104         case NORMAL:
105                 command = "\\space";
106                 break;
107         case PROTECTED:
108                 command = "~";
109                 break;
110         case THIN:
111                 command = "\\,";
112                 break;
113         case QUAD:
114                 command = "\\quad{}";
115                 break;
116         case QQUAD:
117                 command = "\\qquad{}";
118                 break;
119         case ENSPACE:
120                 command = "\\enspace{}";
121                 break;
122         case ENSKIP:
123                 command = "\\enskip{}";
124                 break;
125         case NEGTHIN:
126                 command = "\\negthinspace{}";
127                 break;
128         }
129         os << "\\InsetSpace " << command << "\n";
130 }
131
132
133 // This function will not be necessary when lyx3
134 void InsetSpace::read(Buffer const *, LyXLex & lex)
135 {
136         lex.nextToken();
137         string const command = lex.getString();
138
139         if (command == "\\space")
140                 kind_ = NORMAL;
141         else if (command == "~")
142                 kind_ = PROTECTED;
143         else if (command == "\\,")
144                 kind_ = THIN;
145         else if (command == "\\quad{}")
146                 kind_ = QUAD;
147         else if (command == "\\qquad{}")
148                 kind_ = QQUAD;
149         else if (command == "\\enspace{}")
150                 kind_ = ENSPACE;
151         else if (command == "\\enskip{}")
152                 kind_ = ENSKIP;
153         else if (command == "\\negthinspace{}")
154                 kind_ = NEGTHIN;
155         else
156                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
157 }
158
159
160 int InsetSpace::latex(Buffer const *, ostream & os,
161                       LatexRunParams const & runparams) const
162 {
163         switch (kind_) {
164         case NORMAL:
165                 os << (runparams.free_spacing ? " " : "\\ ");
166                 break;
167         case PROTECTED:
168                 os << (runparams.free_spacing ? ' ' : '~');
169                 break;
170         case THIN:
171                 os << (runparams.free_spacing ? " " : "\\,");
172                 break;
173         case QUAD:
174                 os << (runparams.free_spacing ? " " : "\\quad{}");
175                 break;
176         case QQUAD:
177                 os << (runparams.free_spacing ? " " : "\\qquad{}");
178                 break;
179         case ENSPACE:
180                 os << (runparams.free_spacing ? " " : "\\enspace{}");
181                 break;
182         case ENSKIP:
183                 os << (runparams.free_spacing ? " " : "\\enskip{}");
184                 break;
185         case NEGTHIN:
186                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
187                 break;
188         }
189         return 0;
190 }
191
192
193 int InsetSpace::ascii(Buffer const *, ostream & os, int) const
194 {
195         switch (kind_) {
196         case NORMAL:
197         case PROTECTED:
198         case THIN:
199         case QUAD:
200         case QQUAD:
201         case ENSPACE:
202         case ENSKIP:
203         case NEGTHIN:
204                 os << ' ';
205                 break;
206         }
207         return 0;
208 }
209
210
211 int InsetSpace::linuxdoc(Buffer const *, ostream & os) const
212 {
213         switch (kind_) {
214         // fixme: correct?
215         case NORMAL:
216         case QUAD:
217         case QQUAD:
218         case ENSKIP:
219                 os << " ";
220                 break;
221         case PROTECTED:
222         case ENSPACE:
223         case THIN:
224         case NEGTHIN:
225                 os << "&nbsp;";
226                 break;
227         }
228         return 0;
229 }
230
231
232 int InsetSpace::docbook(Buffer const *, ostream & os, bool) const
233 {
234         switch (kind_) {
235         // fixme: correct?
236         case NORMAL:
237         case QUAD:
238         case QQUAD:
239         case ENSKIP:
240                 os << " ";
241                 break;
242         case PROTECTED:
243         case ENSPACE:
244         case THIN:
245         case NEGTHIN:
246                 os << "&nbsp;";
247                 break;
248         }
249         return 0;
250 }
251
252
253 Inset * InsetSpace::clone(Buffer const &, bool) const
254 {
255         return new InsetSpace(kind_);
256 }
257
258
259 bool InsetSpace::isChar() const
260 {
261         return true;
262 }
263
264 bool InsetSpace::isLetter() const
265 {
266         return false;
267 }
268
269 bool InsetSpace::isSpace() const
270 {
271         return true;
272 }
273
274 bool InsetSpace::isLineSeparator() const
275 {
276         return false;
277 }