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