]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpace.cpp
pimpl not needed here
[lyx.git] / src / insets / InsetSpace.cpp
1 /**
2  * \file InsetSpace.cpp
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 "Dimension.h"
20 #include "Lexer.h"
21 #include "MetricsInfo.h"
22 #include "OutputParams.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27
28 namespace lyx {
29
30 using std::string;
31 using std::max;
32 using std::ostream;
33
34
35 InsetSpace::InsetSpace()
36 {}
37
38
39 InsetSpace::InsetSpace(Kind k)
40         : kind_(k)
41 {}
42
43
44 InsetSpace::Kind InsetSpace::kind() const
45 {
46         return kind_;
47 }
48
49
50 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
51 {
52         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
53         dim.asc = fm.maxAscent();
54         dim.des = fm.maxDescent();
55
56         switch (kind_) {
57                 case THIN:
58                 case NEGTHIN:
59                     dim.wid = fm.width(char_type('x')) / 3;
60                         break;
61                 case PROTECTED:
62                 case NORMAL:
63                     dim.wid = fm.width(char_type('x'));
64                         break;
65                 case QUAD:
66                         dim.wid = 20;
67                         break;
68                 case QQUAD:
69                         dim.wid = 40;
70                         break;
71                 case ENSPACE:
72                 case ENSKIP:
73                         dim.wid = 10;
74                         break;
75         }
76         // Cache the inset dimension. 
77         setDimCache(mi, dim);
78 }
79
80
81 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
82 {
83         Dimension const dim = dimension(*pi.base.bv);
84         int const w = dim.wid;
85         int const h = theFontMetrics(pi.base.font)
86                 .ascent('x');
87         int xp[4], yp[4];
88
89         xp[0] = x;
90         yp[0] = y - max(h / 4, 1);
91         if (kind_ == NORMAL) {
92                 xp[1] = x;     yp[1] = y;
93                 xp[2] = x + w; yp[2] = y;
94         } else {
95                 xp[1] = x;     yp[1] = y + max(h / 4, 1);
96                 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
97         }
98         xp[3] = x + w;
99         yp[3] = y - max(h / 4, 1);
100
101         if (kind_ == PROTECTED || kind_ == ENSPACE || kind_ == NEGTHIN)
102                 pi.pain.lines(xp, yp, 4, Color_latex);
103         else
104                 pi.pain.lines(xp, yp, 4, Color_special);
105 }
106
107
108 void InsetSpace::write(Buffer const &, ostream & os) const
109 {
110         string command;
111         switch (kind_) {
112         case NORMAL:
113                 command = "\\space{}";
114                 break;
115         case PROTECTED:
116                 command = "~";
117                 break;
118         case THIN:
119                 command = "\\thinspace{}";
120                 break;
121         case QUAD:
122                 command = "\\quad{}";
123                 break;
124         case QQUAD:
125                 command = "\\qquad{}";
126                 break;
127         case ENSPACE:
128                 command = "\\enspace{}";
129                 break;
130         case ENSKIP:
131                 command = "\\enskip{}";
132                 break;
133         case NEGTHIN:
134                 command = "\\negthinspace{}";
135                 break;
136         }
137         os << "\\InsetSpace " << command << "\n";
138 }
139
140
141 void InsetSpace::read(Buffer const &, Lexer & lex)
142 {
143         lex.next();
144         string const command = lex.getString();
145
146         if (command == "\\space{}")
147                 kind_ = NORMAL;
148         else if (command == "~")
149                 kind_ = PROTECTED;
150         else if (command == "\\thinspace{}")
151                 kind_ = THIN;
152         else if (command == "\\quad{}")
153                 kind_ = QUAD;
154         else if (command == "\\qquad{}")
155                 kind_ = QQUAD;
156         else if (command == "\\enspace{}")
157                 kind_ = ENSPACE;
158         else if (command == "\\enskip{}")
159                 kind_ = ENSKIP;
160         else if (command == "\\negthinspace{}")
161                 kind_ = NEGTHIN;
162         else
163                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
164 }
165
166
167 int InsetSpace::latex(Buffer const &, odocstream & os,
168                       OutputParams const & runparams) const
169 {
170         switch (kind_) {
171         case NORMAL:
172                 os << (runparams.free_spacing ? " " : "\\ ");
173                 break;
174         case PROTECTED:
175                 os << (runparams.free_spacing ? ' ' : '~');
176                 break;
177         case THIN:
178                 os << (runparams.free_spacing ? " " : "\\,");
179                 break;
180         case QUAD:
181                 os << (runparams.free_spacing ? " " : "\\quad{}");
182                 break;
183         case QQUAD:
184                 os << (runparams.free_spacing ? " " : "\\qquad{}");
185                 break;
186         case ENSPACE:
187                 os << (runparams.free_spacing ? " " : "\\enspace{}");
188                 break;
189         case ENSKIP:
190                 os << (runparams.free_spacing ? " " : "\\enskip{}");
191                 break;
192         case NEGTHIN:
193                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
194                 break;
195         }
196         return 0;
197 }
198
199
200 int InsetSpace::plaintext(Buffer const &, odocstream & os,
201                           OutputParams const &) const
202 {
203         os << ' ';
204         return 1;
205 }
206
207
208 int InsetSpace::docbook(Buffer const &, odocstream & os,
209                         OutputParams const &) const
210 {
211         switch (kind_) {
212         case NORMAL:
213         case QUAD:
214         case QQUAD:
215         case ENSKIP:
216                 os << " ";
217                 break;
218         case PROTECTED:
219         case ENSPACE:
220         case THIN:
221         case NEGTHIN:
222                 os << "&nbsp;";
223                 break;
224         }
225         return 0;
226 }
227
228
229 int InsetSpace::textString(Buffer const & buf, odocstream & os,
230                        OutputParams const & op) const
231 {
232         return plaintext(buf, os, op);
233 }
234
235
236 Inset * InsetSpace::clone() const
237 {
238         return new InsetSpace(kind_);
239 }
240
241
242 bool InsetSpace::isChar() const
243 {
244         return true;
245 }
246
247 bool InsetSpace::isLetter() const
248 {
249         return false;
250 }
251
252 bool InsetSpace::isSpace() const
253 {
254         return true;
255 }
256
257
258 } // namespace lyx