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
38 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
40 return font_metrics::maxAscent(font);
44 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
46 return font_metrics::maxDescent(font);
50 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
55 int w = font_metrics::width('-', font);
57 w -= 2; // to make it look shorter
62 return font_metrics::width('|', font);
66 return font_metrics::width('.', font);
70 return font_metrics::width(". . .", font);
74 return font_metrics::width(" x ", font);
76 case PROTECTED_SEPARATOR:
78 return font_metrics::width('x', font);
82 return 1; // To shut up gcc
86 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
87 int baseline, float & x, bool) const
89 Painter & pain = bv->painter();
95 font.setColor(LColor::special);
96 pain.text(int(x), baseline, '-', font);
102 font.setColor(LColor::special);
103 pain.text(int(x), baseline, '|', font);
104 x += width(bv, font);
107 case END_OF_SENTENCE:
109 font.setColor(LColor::special);
110 pain.text(int(x), baseline, '.', font);
111 x += width(bv, font);
116 font.setColor(LColor::special);
117 pain.text(int(x), baseline, ". . .", font);
118 x += width(bv, font);
123 // A triangle the width and height of an 'x'
124 int w = font_metrics::width('x', font);
125 int ox = font_metrics::width(' ', font) + int(x);
126 int h = font_metrics::ascent('x', font);
129 xp[0] = ox; yp[0] = baseline;
130 xp[1] = ox; yp[1] = baseline - h;
131 xp[2] = ox + w; yp[2] = baseline - h/2;
132 xp[3] = ox; yp[3] = baseline;
134 pain.lines(xp, yp, 4, LColor::special);
135 x += width(bv, font);
138 case PROTECTED_SEPARATOR:
140 float w = width(bv, font);
141 int h = font_metrics::ascent('x', font);
145 yp[0] = baseline - max(h / 4, 1);
154 yp[3] = baseline - max(h / 4, 1);
156 pain.lines(xp, yp, 4, LColor::special);
164 // In lyxf3 this will be just LaTeX
165 void InsetSpecialChar::write(Buffer const *, ostream & os) const
173 command = "\\textcompwordmark{}";
175 case END_OF_SENTENCE:
179 command = "\\ldots{}";
182 command = "\\menuseparator";
184 case PROTECTED_SEPARATOR:
188 os << "\\SpecialChar " << command << "\n";
192 // This function will not be necessary when lyx3
193 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
196 string const command = lex.getString();
198 if (command == "\\-")
200 else if (command == "\\textcompwordmark{}")
201 kind_ = LIGATURE_BREAK;
202 else if (command == "\\@.")
203 kind_ = END_OF_SENTENCE;
204 else if (command == "\\ldots{}")
206 else if (command == "\\menuseparator")
207 kind_ = MENU_SEPARATOR;
208 else if (command == "~")
209 kind_ = PROTECTED_SEPARATOR;
211 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
215 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
216 bool free_space) const
223 os << "\\textcompwordmark{}";
225 case END_OF_SENTENCE:
232 os << "\\lyxarrow{}";
234 case PROTECTED_SEPARATOR:
235 os << (free_space ? ' ' : '~');
242 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
248 case END_OF_SENTENCE:
257 case PROTECTED_SEPARATOR:
265 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
271 case END_OF_SENTENCE:
280 case PROTECTED_SEPARATOR:
288 int InsetSpecialChar::docbook(Buffer const *, ostream & os, bool) const
294 case END_OF_SENTENCE:
303 case PROTECTED_SEPARATOR:
311 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
313 return new InsetSpecialChar(kind_);
317 void InsetSpecialChar::validate(LaTeXFeatures & features) const
319 if (kind_ == MENU_SEPARATOR) {
320 features.require("lyxarrow");
325 bool InsetSpecialChar::isChar() const
331 bool InsetSpecialChar::isLetter() const
333 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
337 bool InsetSpecialChar::isSpace() const
339 return kind_ == PROTECTED_SEPARATOR;
343 bool InsetSpecialChar::isLineSeparator() const
346 // this would be nice, but it does not work, since
347 // Paragraph::stripLeadingSpaces nukes the characters which
348 // have this property. I leave the code here, since it should
349 // eventually be made to work. (JMarc 20020327)
350 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;