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
15 #include "insetspecialchar.h"
18 #include "dimension.h"
19 #include "LaTeXFeatures.h"
20 #include "BufferView.h"
21 #include "frontends/Painter.h"
22 #include "frontends/font_metrics.h"
30 InsetSpecialChar::InsetSpecialChar(Kind k)
35 InsetSpecialChar::Kind InsetSpecialChar::kind() const
41 void InsetSpecialChar::dimension(BufferView *, LyXFont const & font,
42 Dimension & dim) const
44 dim.a = font_metrics::maxAscent(font);
45 dim.d = font_metrics::maxDescent(font);
49 case LIGATURE_BREAK: s = "|"; break;
50 case END_OF_SENTENCE: s = "."; break;
51 case LDOTS: s = ". . ."; break;
52 case MENU_SEPARATOR: s = " x "; break;
53 case PROTECTED_SEPARATOR: s = "x"; break;
55 dim.w = font_metrics::width(s, font);
56 if (kind_ == HYPHENATION && dim.w > 5)
57 dim.w -= 2; // to make it look shorter
61 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
62 int baseline, float & x) const
64 Painter & pain = bv->painter();
70 font.setColor(LColor::special);
71 pain.text(int(x), baseline, '-', font);
77 font.setColor(LColor::special);
78 pain.text(int(x), baseline, '|', font);
84 font.setColor(LColor::special);
85 pain.text(int(x), baseline, '.', font);
91 font.setColor(LColor::special);
92 pain.text(int(x), baseline, ". . .", font);
98 // A triangle the width and height of an 'x'
99 int w = font_metrics::width('x', font);
100 int ox = font_metrics::width(' ', font) + int(x);
101 int h = font_metrics::ascent('x', font);
104 xp[0] = ox; yp[0] = baseline;
105 xp[1] = ox; yp[1] = baseline - h;
106 xp[2] = ox + w; yp[2] = baseline - h/2;
107 xp[3] = ox; yp[3] = baseline;
109 pain.lines(xp, yp, 4, LColor::special);
110 x += width(bv, font);
113 case PROTECTED_SEPARATOR:
115 float w = width(bv, font);
116 int h = font_metrics::ascent('x', font);
120 yp[0] = baseline - max(h / 4, 1);
129 yp[3] = baseline - max(h / 4, 1);
131 pain.lines(xp, yp, 4, LColor::special);
139 // In lyxf3 this will be just LaTeX
140 void InsetSpecialChar::write(Buffer const *, ostream & os) const
148 command = "\\textcompwordmark{}";
150 case END_OF_SENTENCE:
154 command = "\\ldots{}";
157 command = "\\menuseparator";
159 case PROTECTED_SEPARATOR:
163 os << "\\SpecialChar " << command << "\n";
167 // This function will not be necessary when lyx3
168 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
171 string const command = lex.getString();
173 if (command == "\\-")
175 else if (command == "\\textcompwordmark{}")
176 kind_ = LIGATURE_BREAK;
177 else if (command == "\\@.")
178 kind_ = END_OF_SENTENCE;
179 else if (command == "\\ldots{}")
181 else if (command == "\\menuseparator")
182 kind_ = MENU_SEPARATOR;
183 else if (command == "~")
184 kind_ = PROTECTED_SEPARATOR;
186 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
190 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
191 bool free_space) const
198 os << "\\textcompwordmark{}";
200 case END_OF_SENTENCE:
207 os << "\\lyxarrow{}";
209 case PROTECTED_SEPARATOR:
210 os << (free_space ? ' ' : '~');
217 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
223 case END_OF_SENTENCE:
232 case PROTECTED_SEPARATOR:
240 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
246 case END_OF_SENTENCE:
255 case PROTECTED_SEPARATOR:
263 int InsetSpecialChar::docbook(Buffer const *, ostream & os, bool) const
269 case END_OF_SENTENCE:
278 case PROTECTED_SEPARATOR:
286 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
288 return new InsetSpecialChar(kind_);
292 void InsetSpecialChar::validate(LaTeXFeatures & features) const
294 if (kind_ == MENU_SEPARATOR) {
295 features.require("lyxarrow");
300 bool InsetSpecialChar::isChar() const
306 bool InsetSpecialChar::isLetter() const
308 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
312 bool InsetSpecialChar::isSpace() const
314 return kind_ == PROTECTED_SEPARATOR;
318 bool InsetSpecialChar::isLineSeparator() const
321 // this would be nice, but it does not work, since
322 // Paragraph::stripLeadingSpaces nukes the characters which
323 // have this property. I leave the code here, since it should
324 // eventually be made to work. (JMarc 20020327)
325 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;