2 * \file insetspecialchar.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup Nielsen
7 * \author Jean-Marc Lasgouttes
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS
16 #include "insetspecialchar.h"
18 #include "LaTeXFeatures.h"
19 #include "BufferView.h"
20 #include "frontends/Painter.h"
21 #include "frontends/font_metrics.h"
28 InsetSpecialChar::InsetSpecialChar(Kind k)
33 InsetSpecialChar::Kind InsetSpecialChar::kind() const
39 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
41 return font_metrics::maxAscent(font);
45 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
47 return font_metrics::maxDescent(font);
51 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
56 int w = font_metrics::width('-', font);
58 w -= 2; // to make it look shorter
63 return font_metrics::width('|', font);
67 return font_metrics::width('.', font);
71 return font_metrics::width(". . .", font);
75 return font_metrics::width(" x ", font);
77 case PROTECTED_SEPARATOR:
79 return font_metrics::width('x', font);
83 return 1; // To shut up gcc
87 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
88 int baseline, float & x) const
90 Painter & pain = bv->painter();
96 font.setColor(LColor::special);
97 pain.text(int(x), baseline, '-', font);
103 font.setColor(LColor::special);
104 pain.text(int(x), baseline, '|', font);
105 x += width(bv, font);
108 case END_OF_SENTENCE:
110 font.setColor(LColor::special);
111 pain.text(int(x), baseline, '.', font);
112 x += width(bv, font);
117 font.setColor(LColor::special);
118 pain.text(int(x), baseline, ". . .", font);
119 x += width(bv, font);
124 // A triangle the width and height of an 'x'
125 int w = font_metrics::width('x', font);
126 int ox = font_metrics::width(' ', font) + int(x);
127 int h = font_metrics::ascent('x', font);
130 xp[0] = ox; yp[0] = baseline;
131 xp[1] = ox; yp[1] = baseline - h;
132 xp[2] = ox + w; yp[2] = baseline - h/2;
133 xp[3] = ox; yp[3] = baseline;
135 pain.lines(xp, yp, 4, LColor::special);
136 x += width(bv, font);
139 case PROTECTED_SEPARATOR:
141 float w = width(bv, font);
142 int h = font_metrics::ascent('x', font);
146 yp[0] = baseline - max(h / 4, 1);
155 yp[3] = baseline - max(h / 4, 1);
157 pain.lines(xp, yp, 4, LColor::special);
165 // In lyxf3 this will be just LaTeX
166 void InsetSpecialChar::write(Buffer const *, ostream & os) const
174 command = "\\textcompwordmark{}";
176 case END_OF_SENTENCE:
180 command = "\\ldots{}";
183 command = "\\menuseparator";
185 case PROTECTED_SEPARATOR:
189 os << "\\SpecialChar " << command << "\n";
193 // This function will not be necessary when lyx3
194 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
197 string const command = lex.getString();
199 if (command == "\\-")
201 else if (command == "\\textcompwordmark{}")
202 kind_ = LIGATURE_BREAK;
203 else if (command == "\\@.")
204 kind_ = END_OF_SENTENCE;
205 else if (command == "\\ldots{}")
207 else if (command == "\\menuseparator")
208 kind_ = MENU_SEPARATOR;
209 else if (command == "~")
210 kind_ = PROTECTED_SEPARATOR;
212 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
216 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
217 bool free_space) const
224 os << "\\textcompwordmark{}";
226 case END_OF_SENTENCE:
233 os << "\\lyxarrow{}";
235 case PROTECTED_SEPARATOR:
236 os << (free_space ? ' ' : '~');
243 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
249 case END_OF_SENTENCE:
258 case PROTECTED_SEPARATOR:
266 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
272 case END_OF_SENTENCE:
281 case PROTECTED_SEPARATOR:
289 int InsetSpecialChar::docbook(Buffer const *, ostream & os, bool) const
295 case END_OF_SENTENCE:
304 case PROTECTED_SEPARATOR:
312 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
314 return new InsetSpecialChar(kind_);
318 void InsetSpecialChar::validate(LaTeXFeatures & features) const
320 if (kind_ == MENU_SEPARATOR) {
321 features.require("lyxarrow");
326 bool InsetSpecialChar::isChar() const
332 bool InsetSpecialChar::isLetter() const
334 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
338 bool InsetSpecialChar::isSpace() const
340 return kind_ == PROTECTED_SEPARATOR;
344 bool InsetSpecialChar::isLineSeparator() const
347 // this would be nice, but it does not work, since
348 // Paragraph::stripLeadingSpaces nukes the characters which
349 // have this property. I leave the code here, since it should
350 // eventually be made to work. (JMarc 20020327)
351 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;