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 #pragma implementation
19 #include "insetspecialchar.h"
21 #include "LaTeXFeatures.h"
22 #include "BufferView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
31 InsetSpecialChar::InsetSpecialChar(Kind k)
36 InsetSpecialChar::Kind InsetSpecialChar::kind() const
41 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
43 return font_metrics::maxAscent(font);
47 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
49 return font_metrics::maxDescent(font);
53 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
58 int w = font_metrics::width('-', font);
60 w -= 2; // to make it look shorter
65 return font_metrics::width('|', font);
69 return font_metrics::width('.', font);
73 return font_metrics::width(". . .", font);
77 return font_metrics::width(" x ", font);
79 case PROTECTED_SEPARATOR:
81 return font_metrics::width('x', font);
85 return 1; // To shut up gcc
89 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
90 int baseline, float & x, bool) const
92 Painter & pain = bv->painter();
98 font.setColor(LColor::special);
99 pain.text(int(x), baseline, "-", font);
100 x += width(bv, font);
105 font.setColor(LColor::special);
106 pain.text(int(x), baseline, "|", font);
107 x += width(bv, font);
110 case END_OF_SENTENCE:
112 font.setColor(LColor::special);
113 pain.text(int(x), baseline, ".", font);
114 x += width(bv, font);
119 font.setColor(LColor::special);
120 pain.text(int(x), baseline, ". . .", font);
121 x += width(bv, font);
126 // A triangle the width and height of an 'x'
127 int w = font_metrics::width('x', font);
128 int ox = font_metrics::width(' ', font) + int(x);
129 int h = font_metrics::ascent('x', font);
132 xp[0] = ox; yp[0] = baseline;
133 xp[1] = ox; yp[1] = baseline - h;
134 xp[2] = ox + w; yp[2] = baseline - h/2;
135 xp[3] = ox; yp[3] = baseline;
137 pain.lines(xp, yp, 4, LColor::special);
138 x += width(bv, font);
141 case PROTECTED_SEPARATOR:
143 float w = width(bv, font);
144 int h = font_metrics::ascent('x', font);
148 yp[0] = baseline - max(h / 4, 1);
157 yp[3] = baseline - max(h / 4, 1);
159 pain.lines(xp, yp, 4, LColor::special);
167 // In lyxf3 this will be just LaTeX
168 void InsetSpecialChar::write(Buffer const *, ostream & os) const
176 command = "\\textcompwordmark{}";
178 case END_OF_SENTENCE:
182 command = "\\ldots{}";
185 command = "\\menuseparator";
187 case PROTECTED_SEPARATOR:
188 //command = "\\protected_separator";
192 os << "\\SpecialChar " << command << "\n";
196 // This function will not be necessary when lyx3
197 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
200 string const command = lex.getString();
202 if (command == "\\-")
204 else if (command == "\\textcompwordmark{}")
205 kind_ = LIGATURE_BREAK;
206 else if (command == "\\@.")
207 kind_ = END_OF_SENTENCE;
208 else if (command == "\\ldots{}")
210 else if (command == "\\menuseparator")
211 kind_ = MENU_SEPARATOR;
212 else if (command == "\\protected_separator"
214 kind_ = PROTECTED_SEPARATOR;
216 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
220 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
221 bool free_space) const
228 os << "\\textcompwordmark{}";
230 case END_OF_SENTENCE:
237 os << "\\lyxarrow{}";
239 case PROTECTED_SEPARATOR:
240 os << (free_space ? " " : "~");
247 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
253 case END_OF_SENTENCE:
262 case PROTECTED_SEPARATOR:
270 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
276 case END_OF_SENTENCE:
285 case PROTECTED_SEPARATOR:
293 int InsetSpecialChar::docbook(Buffer const *, ostream & os, bool) const
299 case END_OF_SENTENCE:
308 case PROTECTED_SEPARATOR:
316 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
318 return new InsetSpecialChar(kind_);
322 void InsetSpecialChar::validate(LaTeXFeatures & features) const
324 if (kind_ == MENU_SEPARATOR) {
325 features.require("lyxarrow");
330 bool InsetSpecialChar::isChar() const
336 bool InsetSpecialChar::isLetter() const
338 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
342 bool InsetSpecialChar::isSpace() const
344 return kind_ == PROTECTED_SEPARATOR;
348 bool InsetSpecialChar::isLineSeparator() const
351 // this would be nice, but it does not work, since
352 // Paragraph::stripLeadingSpaces nukes the characters which
353 // have this property. I leave the code here, since it should
354 // eventually be made to work. (JMarc 20020327)
355 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;