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