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 void 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
68 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
70 LyXFont font = pi.base.font;
75 font.setColor(LColor::special);
76 pi.pain.text(x, y, char_type('-'), font);
81 font.setColor(LColor::special);
82 pi.pain.text(x, y, char_type('|'), font);
87 font.setColor(LColor::special);
88 pi.pain.text(x, y, char_type('.'), font);
93 font.setColor(LColor::special);
94 string ell = ". . . ";
95 docstring dell(ell.begin(), ell.end());
96 pi.pain.text(x, y, dell, font);
101 frontend::FontMetrics const & fm =
102 theFontMetrics(font);
104 // A triangle the width and height of an 'x'
105 int w = fm.width(char_type('x'));
106 int ox = fm.width(char_type(' ')) + x;
107 int h = fm.ascent(char_type('x'));
110 xp[0] = ox; yp[0] = y;
111 xp[1] = ox; yp[1] = y - h;
112 xp[2] = ox + w; yp[2] = y - h/2;
113 xp[3] = ox; yp[3] = y;
115 pi.pain.lines(xp, yp, 4, LColor::special);
122 // In lyxf3 this will be just LaTeX
123 void InsetSpecialChar::write(Buffer const &, ostream & os) const
131 command = "\\textcompwordmark{}";
133 case END_OF_SENTENCE:
137 command = "\\ldots{}";
140 command = "\\menuseparator";
143 os << "\\SpecialChar " << command << "\n";
147 // This function will not be necessary when lyx3
148 void InsetSpecialChar::read(Buffer const &, LyXLex & lex)
151 string const command = lex.getString();
153 if (command == "\\-")
155 else if (command == "\\textcompwordmark{}")
156 kind_ = LIGATURE_BREAK;
157 else if (command == "\\@.")
158 kind_ = END_OF_SENTENCE;
159 else if (command == "\\ldots{}")
161 else if (command == "\\menuseparator")
162 kind_ = MENU_SEPARATOR;
164 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
168 int InsetSpecialChar::latex(Buffer const &, odocstream & os,
169 OutputParams const &) const
176 os << "\\textcompwordmark{}";
178 case END_OF_SENTENCE:
185 os << "\\lyxarrow{}";
192 int InsetSpecialChar::plaintext(Buffer const &, odocstream & os,
193 OutputParams const &) const
199 case END_OF_SENTENCE:
213 int InsetSpecialChar::docbook(Buffer const &, odocstream & os,
214 OutputParams const &) const
220 case END_OF_SENTENCE:
234 int InsetSpecialChar::textString(Buffer const & buf, odocstream & os,
235 OutputParams const & op) const
237 return plaintext(buf, os, op);
241 auto_ptr<InsetBase> InsetSpecialChar::doClone() const
243 return auto_ptr<InsetBase>(new InsetSpecialChar(kind_));
247 void InsetSpecialChar::validate(LaTeXFeatures & features) const
249 if (kind_ == MENU_SEPARATOR) {
250 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;