]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
Remove \\protected_separator.
[lyx.git] / src / insets / insetspecialchar.C
1 /**
2  * \file insetspecialchar.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  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "insetspecialchar.h"
20 #include "debug.h"
21 #include "LaTeXFeatures.h"
22 #include "BufferView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
25 #include "lyxlex.h"
26 #include "lyxfont.h"
27
28 using std::ostream;
29 using std::max;
30
31 InsetSpecialChar::InsetSpecialChar(Kind k)
32         : kind_(k)
33 {}
34
35
36 InsetSpecialChar::Kind InsetSpecialChar::kind() const
37 {
38         return kind_;
39 }
40
41 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
42 {
43         return font_metrics::maxAscent(font);
44 }
45
46
47 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
48 {
49         return font_metrics::maxDescent(font);
50 }
51
52
53 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
54 {
55         switch (kind_) {
56         case HYPHENATION:
57         {
58                 int w = font_metrics::width('-', font);
59                 if (w > 5)
60                         w -= 2; // to make it look shorter
61                 return w;
62         }
63         case LIGATURE_BREAK:
64         {
65                 return font_metrics::width('|', font);
66         }
67         case END_OF_SENTENCE:
68         {
69                 return font_metrics::width('.', font);
70         }
71         case LDOTS:
72         {
73                 return font_metrics::width(". . .", font);
74         }
75         case MENU_SEPARATOR:
76         {
77                 return font_metrics::width(" x ", font);
78         }
79         case PROTECTED_SEPARATOR:
80         {
81                 return font_metrics::width('x', font);
82         }
83
84         }
85         return 1; // To shut up gcc
86 }
87
88
89 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
90                             int baseline, float & x, bool) const
91 {
92         Painter & pain = bv->painter();
93         LyXFont font(f);
94
95         switch (kind_) {
96         case HYPHENATION:
97         {
98                 font.setColor(LColor::special);
99                 pain.text(int(x), baseline, "-", font);
100                 x += width(bv, font);
101                 break;
102         }
103         case LIGATURE_BREAK:
104         {
105                 font.setColor(LColor::special);
106                 pain.text(int(x), baseline, "|", font);
107                 x += width(bv, font);
108                 break;
109         }
110         case END_OF_SENTENCE:
111         {
112                 font.setColor(LColor::special);
113                 pain.text(int(x), baseline, ".", font);
114                 x += width(bv, font);
115                 break;
116         }
117         case LDOTS:
118         {
119                 font.setColor(LColor::special);
120                 pain.text(int(x), baseline, ". . .", font);
121                 x += width(bv, font);
122                 break;
123         }
124         case MENU_SEPARATOR:
125         {
126                 // A triangle the width and height of an 'x'
127                 int w = font_metrics::width('x', font);
128                 int ox = font_metrics::width(' ', font) + int(x);
129                 int h = font_metrics::ascent('x', font);
130                 int xp[4], yp[4];
131
132                 xp[0] = ox;     yp[0] = baseline;
133                 xp[1] = ox;     yp[1] = baseline - h;
134                 xp[2] = ox + w; yp[2] = baseline - h/2;
135                 xp[3] = ox;     yp[3] = baseline;
136
137                 pain.lines(xp, yp, 4, LColor::special);
138                 x += width(bv, font);
139                 break;
140         }
141         case PROTECTED_SEPARATOR:
142         {
143                 float w = width(bv, font);
144                 int h = font_metrics::ascent('x', font);
145                 int xp[4], yp[4];
146
147                 xp[0] = int(x);
148                 yp[0] = baseline - max(h / 4, 1);
149
150                 xp[1] = int(x);
151                 yp[1] = baseline;
152
153                 xp[2] = int(x + w);
154                 yp[2] = baseline;
155
156                 xp[3] = int(x + w);
157                 yp[3] = baseline - max(h / 4, 1);
158
159                 pain.lines(xp, yp, 4, LColor::special);
160                 x += w;
161                 break;
162         }
163         }
164 }
165
166
167 // In lyxf3 this will be just LaTeX
168 void InsetSpecialChar::write(Buffer const *, ostream & os) const
169 {
170         string command;
171         switch (kind_) {
172         case HYPHENATION:
173                 command = "\\-";
174                 break;
175         case LIGATURE_BREAK:
176                 command = "\\textcompwordmark{}";
177                 break;
178         case END_OF_SENTENCE:
179                 command = "\\@.";
180                 break;
181         case LDOTS:
182                 command = "\\ldots{}";
183                 break;
184         case MENU_SEPARATOR:
185                 command = "\\menuseparator";
186                 break;
187         case PROTECTED_SEPARATOR:
188                 command = "~";
189                 break;
190         }
191         os << "\\SpecialChar " << command << "\n";
192 }
193
194
195 // This function will not be necessary when lyx3
196 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
197 {
198         lex.nextToken();
199         string const command = lex.getString();
200
201         if (command == "\\-")
202                 kind_ = HYPHENATION;
203         else if (command == "\\textcompwordmark{}")
204                 kind_ = LIGATURE_BREAK;
205         else if (command == "\\@.")
206                 kind_ = END_OF_SENTENCE;
207         else if (command == "\\ldots{}")
208                 kind_ = LDOTS;
209         else if (command == "\\menuseparator")
210                 kind_ = MENU_SEPARATOR;
211         else if (command == "~")
212                 kind_ = PROTECTED_SEPARATOR;
213         else
214                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
215 }
216
217
218 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
219                             bool free_space) const
220 {
221         switch (kind_) {
222         case HYPHENATION:
223                 os << "\\-";
224                 break;
225         case LIGATURE_BREAK:
226                 os << "\\textcompwordmark{}";
227                 break;
228         case END_OF_SENTENCE:
229                 os << "\\@.";
230                 break;
231         case LDOTS:
232                 os << "\\ldots{}";
233                 break;
234         case MENU_SEPARATOR:
235                 os << "\\lyxarrow{}";
236                 break;
237         case PROTECTED_SEPARATOR:
238                 os << (free_space ? " " : "~");
239                 break;
240         }
241         return 0;
242 }
243
244
245 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
246 {
247         switch (kind_) {
248         case HYPHENATION:
249         case LIGATURE_BREAK:
250                 break;
251         case END_OF_SENTENCE:
252                 os << ".";
253                 break;
254         case LDOTS:
255                 os << "...";
256                 break;
257         case MENU_SEPARATOR:
258                 os << "->";
259                 break;
260         case PROTECTED_SEPARATOR:
261                 os << " ";
262                 break;
263         }
264         return 0;
265 }
266
267
268 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
269 {
270         switch (kind_) {
271         case HYPHENATION:
272         case LIGATURE_BREAK:
273                 break;
274         case END_OF_SENTENCE:
275                 os << ".";
276                 break;
277         case LDOTS:
278                 os << "...";
279                 break;
280         case MENU_SEPARATOR:
281                 os << "&lyxarrow;";
282                 break;
283         case PROTECTED_SEPARATOR:
284                 os << "&nbsp;";
285                 break;
286         }
287         return 0;
288 }
289
290
291 int InsetSpecialChar::docbook(Buffer const *, ostream & os, bool) const
292 {
293         switch (kind_) {
294         case HYPHENATION:
295         case LIGATURE_BREAK:
296                 break;
297         case END_OF_SENTENCE:
298                 os << ".";
299                 break;
300         case LDOTS:
301                 os << "...";
302                 break;
303         case MENU_SEPARATOR:
304                 os << "&lyxarrow;";
305                 break;
306         case PROTECTED_SEPARATOR:
307                 os << "&nbsp;";
308                 break;
309         }
310         return 0;
311 }
312
313
314 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
315 {
316         return new InsetSpecialChar(kind_);
317 }
318
319
320 void InsetSpecialChar::validate(LaTeXFeatures & features) const
321 {
322         if (kind_ == MENU_SEPARATOR) {
323                 features.require("lyxarrow");
324         }
325 }
326
327
328 bool InsetSpecialChar::isChar() const
329 {
330         return true;
331 }
332
333
334 bool InsetSpecialChar::isLetter() const
335 {
336         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
337 }
338
339
340 bool InsetSpecialChar::isSpace() const
341 {
342         return kind_ == PROTECTED_SEPARATOR;
343 }
344
345
346 bool InsetSpecialChar::isLineSeparator() const
347 {
348 #if 0
349         // this would be nice, but it does not work, since
350         // Paragraph::stripLeadingSpaces nukes the characters which
351         // have this property. I leave the code here, since it should
352         // eventually be made to work. (JMarc 20020327)
353         return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;
354 #else
355         return false;
356 #endif
357 }