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