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 bool 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
63 bool const changed = dim_ != dim;
69 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
71 Font font = pi.base.font;
76 font.setColor(Color::special);
77 pi.pain.text(x, y, char_type('-'), font);
82 font.setColor(Color::special);
83 pi.pain.text(x, y, char_type('|'), font);
88 font.setColor(Color::special);
89 pi.pain.text(x, y, char_type('.'), font);
94 font.setColor(Color::special);
95 string ell = ". . . ";
96 docstring dell(ell.begin(), ell.end());
97 pi.pain.text(x, y, dell, font);
102 frontend::FontMetrics const & fm =
103 theFontMetrics(font);
105 // A triangle the width and height of an 'x'
106 int w = fm.width(char_type('x'));
107 int ox = fm.width(char_type(' ')) + x;
108 int h = fm.ascent(char_type('x'));
111 xp[0] = ox; yp[0] = y;
112 xp[1] = ox; yp[1] = y - h;
113 xp[2] = ox + w; yp[2] = y - h/2;
114 xp[3] = ox; yp[3] = y;
116 pi.pain.lines(xp, yp, 4, Color::special);
123 // In lyxf3 this will be just LaTeX
124 void InsetSpecialChar::write(Buffer const &, ostream & os) const
132 command = "\\textcompwordmark{}";
134 case END_OF_SENTENCE:
138 command = "\\ldots{}";
141 command = "\\menuseparator";
144 os << "\\SpecialChar " << command << "\n";
148 // This function will not be necessary when lyx3
149 void InsetSpecialChar::read(Buffer const &, Lexer & lex)
152 string const command = lex.getString();
154 if (command == "\\-")
156 else if (command == "\\textcompwordmark{}")
157 kind_ = LIGATURE_BREAK;
158 else if (command == "\\@.")
159 kind_ = END_OF_SENTENCE;
160 else if (command == "\\ldots{}")
162 else if (command == "\\menuseparator")
163 kind_ = MENU_SEPARATOR;
165 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
169 int InsetSpecialChar::latex(Buffer const &, odocstream & os,
170 OutputParams const &) const
177 os << "\\textcompwordmark{}";
179 case END_OF_SENTENCE:
186 os << "\\lyxarrow{}";
193 int InsetSpecialChar::plaintext(Buffer const &, odocstream & os,
194 OutputParams const &) const
200 case END_OF_SENTENCE:
214 int InsetSpecialChar::docbook(Buffer const &, odocstream & os,
215 OutputParams const &) const
221 case END_OF_SENTENCE:
235 int InsetSpecialChar::textString(Buffer const & buf, odocstream & os,
236 OutputParams const & op) const
238 return plaintext(buf, os, op);
242 Inset * InsetSpecialChar::clone() const
244 return new InsetSpecialChar(kind_);
248 void InsetSpecialChar::validate(LaTeXFeatures & features) const
250 if (kind_ == MENU_SEPARATOR)
251 features.require("lyxarrow");
255 bool InsetSpecialChar::isChar() const
261 bool InsetSpecialChar::isLetter() const
263 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
267 bool InsetSpecialChar::isLineSeparator() const
270 // this would be nice, but it does not work, since
271 // Paragraph::stripLeadingSpaces nukes the characters which
272 // have this property. I leave the code here, since it should
273 // eventually be made to work. (JMarc 20020327)
274 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;