]> 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
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         lyx::frontend::FontMetrics const & fm =
52                 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(lyx::char_type('x')) / 3;
60                         break;
61                 case PROTECTED:
62                 case NORMAL:
63                     dim.wid = fm.width(lyx::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         dim_ = dim;
77 }
78
79
80 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
81 {
82         int const w = width();
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, LColor::latex);
101         else
102                 pi.pain.lines(xp, yp, 4, LColor::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 &, LyXLex & 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 &, ostream & 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 &, lyx::odocstream & os,
199                       OutputParams const &) const
200 {
201         switch (kind_) {
202         case NORMAL:
203         case PROTECTED:
204         case THIN:
205         case QUAD:
206         case QQUAD:
207         case ENSPACE:
208         case ENSKIP:
209         case NEGTHIN:
210                 os << ' ';
211                 break;
212         }
213         return 0;
214 }
215
216
217 int InsetSpace::docbook(Buffer const &, ostream & os,
218                         OutputParams const &) const
219 {
220         switch (kind_) {
221         case NORMAL:
222         case QUAD:
223         case QQUAD:
224         case ENSKIP:
225                 os << " ";
226                 break;
227         case PROTECTED:
228         case ENSPACE:
229         case THIN:
230         case NEGTHIN:
231                 os << "&nbsp;";
232                 break;
233         }
234         return 0;
235 }
236
237
238 int InsetSpace::textString(Buffer const & buf, lyx::odocstream & os,
239                        OutputParams const & op) const
240 {
241         return plaintext(buf, os, op);
242 }
243
244
245 auto_ptr<InsetBase> InsetSpace::doClone() const
246 {
247         return auto_ptr<InsetBase>(new InsetSpace(kind_));
248 }
249
250
251 bool InsetSpace::isChar() const
252 {
253         return true;
254 }
255
256 bool InsetSpace::isLetter() const
257 {
258         return false;
259 }
260
261 bool InsetSpace::isSpace() const
262 {
263         return true;
264 }