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