]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
fix the smallcaps drawing, move xfont metrics functions out from LyXFont, move non...
[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 "Painter.h"
20 #include "font.h"
21
22 using std::ostream;
23 using std::max;
24
25 InsetSpecialChar::InsetSpecialChar(Kind k)
26         : kind(k)
27 {}
28
29
30 int InsetSpecialChar::ascent(Painter &, LyXFont const & font) const
31 {
32         return lyxfont::maxAscent(font);
33 }
34
35
36 int InsetSpecialChar::descent(Painter &, LyXFont const & font) const
37 {
38         return lyxfont::maxDescent(font);
39 }
40
41
42 int InsetSpecialChar::width(Painter &, LyXFont const & font) const
43 {
44         switch (kind) {
45         case HYPHENATION:
46         {
47                 int w = lyxfont::width('-', font);
48                 if (w > 5) 
49                         w -= 2; // to make it look shorter
50                 return w;
51         }
52         case END_OF_SENTENCE:
53         {
54                 return lyxfont::width('.', font);
55         }
56         case LDOTS:
57         {
58                 return lyxfont::width(". . .", font);
59         }
60         case MENU_SEPARATOR:
61         {
62                 return lyxfont::width(" x ", font);
63         }
64 #if 0
65         case NEWLINE:
66         {
67         }
68 #endif
69         case PROTECTED_SEPARATOR:
70         {
71                 return lyxfont::width('x', font);
72         }
73         
74         }
75         return 1; // To shut up gcc
76 }
77
78
79 void InsetSpecialChar::draw(Painter & pain, LyXFont const & f,
80                             int baseline, float & x) const
81 {
82         LyXFont font(f);
83         switch (kind) {
84         case HYPHENATION:
85         {
86                 font.setColor(LColor::special);
87                 pain.text(int(x), baseline, "-", font);
88                 x += width(pain, font);
89                 break;
90         }
91         case END_OF_SENTENCE:
92         {
93                 font.setColor(LColor::special);
94                 pain.text(int(x), baseline, ".", font);
95                 x += width(pain, font);
96                 break;
97         }
98         case LDOTS:
99         {
100                 font.setColor(LColor::special);
101                 pain.text(int(x), baseline, ". . .", font);
102                 x += width(pain, font);
103                 break;
104         }
105         case MENU_SEPARATOR:
106         {
107                 // A triangle the width and height of an 'x'
108                 int w = lyxfont::width('x', font);
109                 int ox = lyxfont::width(' ', font) + int(x);
110                 int h = lyxfont::ascent('x', font);
111                 int xp[4], yp[4];
112                 
113                 xp[0] = ox;     yp[0] = baseline;
114                 xp[1] = ox;     yp[1] = baseline - h;
115                 xp[2] = ox + w; yp[2] = baseline - h/2;
116                 xp[3] = ox;     yp[3] = baseline;
117                 
118                 pain.lines(xp, yp, 4, LColor::special);
119                 x += width(pain, font);
120                 break;
121         }
122 #if 0
123         case NEWLINE:
124         {
125         }
126 #endif
127         case PROTECTED_SEPARATOR:
128         {
129                 float w = width(pain, font);
130                 int h = lyxfont::ascent('x', font);
131                 int xp[4], yp[4];
132                 
133                 xp[0] = int(x);
134                 yp[0] = baseline - max(h / 4, 1);
135
136                 xp[1] = int(x);
137                 yp[1] = baseline;
138
139                 xp[2] = int(x + w);
140                 yp[2] = baseline;
141
142                 xp[3] = int(x + w);
143                 yp[3] = baseline - max(h / 4, 1);
144                 
145                 pain.lines(xp, yp, 4, LColor::special);
146                 x += w;
147                 break;
148         }
149         }
150 }
151
152
153 // In lyxf3 this will be just LaTeX
154 void InsetSpecialChar::Write(ostream & os) const
155 {
156         string command;
157         switch (kind) {
158         case HYPHENATION:       command = "\\-";        break;
159         case END_OF_SENTENCE:   command = "\\@.";       break;
160         case LDOTS:             command = "\\ldots{}";  break;
161         case MENU_SEPARATOR:    command = "\\menuseparator"; break;
162 #if 0
163         case NEWLINE:           command = "\\newline";  break;
164 #endif
165         case PROTECTED_SEPARATOR:
166                                 command = "\\protected_separator";          break;
167         }
168         os << "\\SpecialChar " << command << "\n";
169 }
170
171
172 // This function will not be necessary when lyx3
173 void InsetSpecialChar::Read(LyXLex & lex)
174 {    
175         lex.nextToken();
176         string command = lex.GetString();
177
178         if (command == "\\-")
179                 kind = HYPHENATION;
180         else if (command == "\\@.")
181                 kind = END_OF_SENTENCE;
182         else if (command == "\\ldots{}")
183                 kind = LDOTS;
184         else if (command == "\\menuseparator")
185                 kind = MENU_SEPARATOR;
186         else if (command == "\\protected_separator")
187                 kind = PROTECTED_SEPARATOR;
188         else
189                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
190 }
191
192
193 int InsetSpecialChar::Latex(ostream & os, signed char /*fragile*/,
194                             bool free_space) const
195 {
196         switch (kind) {
197         case HYPHENATION:         os << "\\-";  break;
198         case END_OF_SENTENCE:     os << "\\@."; break;
199         case LDOTS:               os << "\\ldots{}";    break;
200         case MENU_SEPARATOR:      os << "\\lyxarrow{}"; break;
201         case PROTECTED_SEPARATOR: os << (free_space ? " " : "~"); break;
202         }
203         return 0;
204 }
205
206
207 int InsetSpecialChar::Linuxdoc(ostream & os) const
208 {
209         switch (kind) {
210         case HYPHENATION:         os << "";     break;
211         case END_OF_SENTENCE:     os << "";     break;
212         case LDOTS:               os << "...";  break;
213         case MENU_SEPARATOR:      os << "->";   break;
214         case PROTECTED_SEPARATOR: os << " ";   break;
215         }
216         return 0;
217 }
218
219
220 int InsetSpecialChar::DocBook(ostream & os) const
221 {
222         switch (kind) {
223         case HYPHENATION:         os << "";     break;
224         case END_OF_SENTENCE:     os << "";     break;
225         case LDOTS:               os << "...";  break;
226         case MENU_SEPARATOR:      os << "->";   break;
227         case PROTECTED_SEPARATOR: os << " ";   break;
228         }
229         return 0;
230 }
231
232
233 Inset * InsetSpecialChar::Clone() const
234 {
235         return new InsetSpecialChar(kind);
236 }
237
238
239 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
240 {
241         if (kind == MENU_SEPARATOR) {
242                 features.lyxarrow = true;
243         }
244 }