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 "LaTeXFeatures.h"
21 #include "metricsinfo.h"
23 #include "frontends/FontMetrics.h"
24 #include "frontends/Painter.h"
34 InsetSpecialChar::InsetSpecialChar(Kind k)
39 InsetSpecialChar::Kind InsetSpecialChar::kind() const
45 bool InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
47 frontend::FontMetrics const & fm =
48 theFontMetrics(mi.base.font);
49 dim.asc = fm.maxAscent();
50 dim.des = fm.maxDescent();
54 case LIGATURE_BREAK: s = "|"; break;
55 case END_OF_SENTENCE: s = "."; break;
56 case LDOTS: s = ". . ."; break;
57 case MENU_SEPARATOR: s = " x "; break;
58 case HYPHENATION: s = "-"; break;
60 docstring ds(s.begin(), s.end());
61 dim.wid = fm.width(ds);
62 if (kind_ == HYPHENATION && dim.wid > 5)
63 dim.wid -= 2; // to make it look shorter
64 bool const changed = dim_ != dim;
70 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
72 LyXFont font = pi.base.font;
77 font.setColor(LColor::special);
78 pi.pain.text(x, y, char_type('-'), font);
83 font.setColor(LColor::special);
84 pi.pain.text(x, y, char_type('|'), font);
89 font.setColor(LColor::special);
90 pi.pain.text(x, y, char_type('.'), font);
95 font.setColor(LColor::special);
96 string ell = ". . . ";
97 docstring dell(ell.begin(), ell.end());
98 pi.pain.text(x, y, dell, font);
103 frontend::FontMetrics const & fm =
104 theFontMetrics(font);
106 // A triangle the width and height of an 'x'
107 int w = fm.width(char_type('x'));
108 int ox = fm.width(char_type(' ')) + x;
109 int h = fm.ascent(char_type('x'));
112 xp[0] = ox; yp[0] = y;
113 xp[1] = ox; yp[1] = y - h;
114 xp[2] = ox + w; yp[2] = y - h/2;
115 xp[3] = ox; yp[3] = y;
117 pi.pain.lines(xp, yp, 4, LColor::special);
124 // In lyxf3 this will be just LaTeX
125 void InsetSpecialChar::write(Buffer const &, ostream & os) const
133 command = "\\textcompwordmark{}";
135 case END_OF_SENTENCE:
139 command = "\\ldots{}";
142 command = "\\menuseparator";
145 os << "\\SpecialChar " << command << "\n";
149 // This function will not be necessary when lyx3
150 void InsetSpecialChar::read(Buffer const &, LyXLex & lex)
153 string const command = lex.getString();
155 if (command == "\\-")
157 else if (command == "\\textcompwordmark{}")
158 kind_ = LIGATURE_BREAK;
159 else if (command == "\\@.")
160 kind_ = END_OF_SENTENCE;
161 else if (command == "\\ldots{}")
163 else if (command == "\\menuseparator")
164 kind_ = MENU_SEPARATOR;
166 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
170 int InsetSpecialChar::latex(Buffer const &, odocstream & os,
171 OutputParams const &) const
178 os << "\\textcompwordmark{}";
180 case END_OF_SENTENCE:
187 os << "\\lyxarrow{}";
194 int InsetSpecialChar::plaintext(Buffer const &, odocstream & os,
195 OutputParams const &) const
201 case END_OF_SENTENCE:
215 int InsetSpecialChar::docbook(Buffer const &, odocstream & os,
216 OutputParams const &) const
222 case END_OF_SENTENCE:
236 int InsetSpecialChar::textString(Buffer const & buf, odocstream & os,
237 OutputParams const & op) const
239 return plaintext(buf, os, op);
243 auto_ptr<InsetBase> InsetSpecialChar::doClone() const
245 return auto_ptr<InsetBase>(new InsetSpecialChar(kind_));
249 void InsetSpecialChar::validate(LaTeXFeatures & features) const
251 if (kind_ == MENU_SEPARATOR) {
252 features.require("lyxarrow");
257 bool InsetSpecialChar::isChar() const
263 bool InsetSpecialChar::isLetter() const
265 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
269 bool InsetSpecialChar::isLineSeparator() const
272 // this would be nice, but it does not work, since
273 // Paragraph::stripLeadingSpaces nukes the characters which
274 // have this property. I leave the code here, since it should
275 // eventually be made to work. (JMarc 20020327)
276 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;