]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
unify API for insets export
[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 Jürgen Spitzmüller
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 "latexrunparams.h"
20 #include "LColor.h"
21 #include "lyxlex.h"
22 #include "metricsinfo.h"
23
24 #include "frontends/font_metrics.h"
25 #include "frontends/Painter.h"
26
27
28 using std::string;
29 using std::max;
30 using std::auto_ptr;
31 using std::ostream;
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,
198                       LatexRunParams const &) const
199 {
200         switch (kind_) {
201         case NORMAL:
202         case PROTECTED:
203         case THIN:
204         case QUAD:
205         case QQUAD:
206         case ENSPACE:
207         case ENSKIP:
208         case NEGTHIN:
209                 os << ' ';
210                 break;
211         }
212         return 0;
213 }
214
215
216 int InsetSpace::linuxdoc(Buffer const &, ostream & os,
217                          LatexRunParams const &) const
218 {
219         switch (kind_) {
220         case NORMAL:
221         case QUAD:
222         case QQUAD:
223         case ENSKIP:
224                 os << " ";
225                 break;
226         case PROTECTED:
227         case ENSPACE:
228         case THIN:
229         case NEGTHIN:
230                 os << "&nbsp;";
231                 break;
232         }
233         return 0;
234 }
235
236
237 int InsetSpace::docbook(Buffer const &, ostream & os,
238                         LatexRunParams const &) const
239 {
240         switch (kind_) {
241         case NORMAL:
242         case QUAD:
243         case QQUAD:
244         case ENSKIP:
245                 os << " ";
246                 break;
247         case PROTECTED:
248         case ENSPACE:
249         case THIN:
250         case NEGTHIN:
251                 os << "&nbsp;";
252                 break;
253         }
254         return 0;
255 }
256
257
258 auto_ptr<InsetBase>  InsetSpace::clone() const
259 {
260         return auto_ptr<InsetBase>(new InsetSpace(kind_));
261 }
262
263
264 bool InsetSpace::isChar() const
265 {
266         return true;
267 }
268
269 bool InsetSpace::isLetter() const
270 {
271         return false;
272 }
273
274 bool InsetSpace::isSpace() const
275 {
276         return true;
277 }
278
279 bool InsetSpace::isLineSeparator() const
280 {
281         return false;
282 }