2 * \file InsetSpecialChar.cpp
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 "LaTeXFeatures.h"
21 #include "MetricsInfo.h"
23 #include "frontends/FontMetrics.h"
24 #include "frontends/Painter.h"
33 InsetSpecialChar::InsetSpecialChar(Kind k)
38 InsetSpecialChar::Kind InsetSpecialChar::kind() const
44 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
46 frontend::FontMetrics const & fm =
47 theFontMetrics(mi.base.font);
48 dim.asc = fm.maxAscent();
49 dim.des = fm.maxDescent();
53 case LIGATURE_BREAK: s = "|"; break;
54 case END_OF_SENTENCE: s = "."; break;
55 case LDOTS: s = ". . ."; break;
56 case MENU_SEPARATOR: s = " x "; break;
57 case HYPHENATION: s = "-"; break;
59 docstring ds(s.begin(), s.end());
60 dim.wid = fm.width(ds);
61 if (kind_ == HYPHENATION && dim.wid > 5)
62 dim.wid -= 2; // to make it look shorter
66 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
68 Font font = pi.base.font;
73 font.setColor(Color::special);
74 pi.pain.text(x, y, char_type('-'), font);
79 font.setColor(Color::special);
80 pi.pain.text(x, y, char_type('|'), font);
85 font.setColor(Color::special);
86 pi.pain.text(x, y, char_type('.'), font);
91 font.setColor(Color::special);
92 string ell = ". . . ";
93 docstring dell(ell.begin(), ell.end());
94 pi.pain.text(x, y, dell, font);
99 frontend::FontMetrics const & fm =
100 theFontMetrics(font);
102 // A triangle the width and height of an 'x'
103 int w = fm.width(char_type('x'));
104 int ox = fm.width(char_type(' ')) + x;
105 int h = fm.ascent(char_type('x'));
108 xp[0] = ox; yp[0] = y;
109 xp[1] = ox; yp[1] = y - h;
110 xp[2] = ox + w; yp[2] = y - h/2;
111 xp[3] = ox; yp[3] = y;
113 pi.pain.lines(xp, yp, 4, Color::special);
120 // In lyxf3 this will be just LaTeX
121 void InsetSpecialChar::write(Buffer const &, ostream & os) const
129 command = "\\textcompwordmark{}";
131 case END_OF_SENTENCE:
135 command = "\\ldots{}";
138 command = "\\menuseparator";
141 os << "\\SpecialChar " << command << "\n";
145 // This function will not be necessary when lyx3
146 void InsetSpecialChar::read(Buffer const &, Lexer & lex)
149 string const command = lex.getString();
151 if (command == "\\-")
153 else if (command == "\\textcompwordmark{}")
154 kind_ = LIGATURE_BREAK;
155 else if (command == "\\@.")
156 kind_ = END_OF_SENTENCE;
157 else if (command == "\\ldots{}")
159 else if (command == "\\menuseparator")
160 kind_ = MENU_SEPARATOR;
162 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
166 int InsetSpecialChar::latex(Buffer const &, odocstream & os,
167 OutputParams const &) const
174 os << "\\textcompwordmark{}";
176 case END_OF_SENTENCE:
183 os << "\\lyxarrow{}";
190 int InsetSpecialChar::plaintext(Buffer const &, odocstream & os,
191 OutputParams const &) const
197 case END_OF_SENTENCE:
211 int InsetSpecialChar::docbook(Buffer const &, odocstream & os,
212 OutputParams const &) const
218 case END_OF_SENTENCE:
232 int InsetSpecialChar::textString(Buffer const & buf, odocstream & os,
233 OutputParams const & op) const
235 return plaintext(buf, os, op);
239 Inset * InsetSpecialChar::clone() const
241 return new InsetSpecialChar(kind_);
245 void InsetSpecialChar::validate(LaTeXFeatures & features) const
247 if (kind_ == MENU_SEPARATOR)
248 features.require("lyxarrow");
252 bool InsetSpecialChar::isChar() const
258 bool InsetSpecialChar::isLetter() const
260 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
264 bool InsetSpecialChar::isLineSeparator() const
267 // this would be nice, but it does not work, since
268 // Paragraph::stripLeadingSpaces nukes the characters which
269 // have this property. I leave the code here, since it should
270 // eventually be made to work. (JMarc 20020327)
271 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;