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:
191 os << "\\SpecialChar " << command << "\n";
195 // This function will not be necessary when lyx3
196 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
199 string const command = lex.getString();
201 if (command == "\\-")
203 else if (command == "\\textcompwordmark{}")
204 kind_ = LIGATURE_BREAK;
205 else if (command == "\\@.")
206 kind_ = END_OF_SENTENCE;
207 else if (command == "\\ldots{}")
209 else if (command == "\\menuseparator")
210 kind_ = MENU_SEPARATOR;
211 else if (command == "~")
212 kind_ = PROTECTED_SEPARATOR;
214 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
218 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
219 bool free_space) const
226 os << "\\textcompwordmark{}";
228 case END_OF_SENTENCE:
235 os << "\\lyxarrow{}";
237 case PROTECTED_SEPARATOR:
238 os << (free_space ? " " : "~");
245 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
251 case END_OF_SENTENCE:
260 case PROTECTED_SEPARATOR:
268 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
274 case END_OF_SENTENCE:
283 case PROTECTED_SEPARATOR:
291 int InsetSpecialChar::docbook(Buffer const *, ostream & os, bool) const
297 case END_OF_SENTENCE:
306 case PROTECTED_SEPARATOR:
314 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
316 return new InsetSpecialChar(kind_);
320 void InsetSpecialChar::validate(LaTeXFeatures & features) const
322 if (kind_ == MENU_SEPARATOR) {
323 features.require("lyxarrow");
328 bool InsetSpecialChar::isChar() const
334 bool InsetSpecialChar::isLetter() const
336 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
340 bool InsetSpecialChar::isSpace() const
342 return kind_ == PROTECTED_SEPARATOR;
346 bool InsetSpecialChar::isLineSeparator() const
349 // this would be nice, but it does not work, since
350 // Paragraph::stripLeadingSpaces nukes the characters which
351 // have this property. I leave the code here, since it should
352 // eventually be made to work. (JMarc 20020327)
353 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;