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"
33 InsetSpecialChar::InsetSpecialChar(Kind k)
38 InsetSpecialChar::Kind InsetSpecialChar::kind() const
44 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
46 lyx::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
67 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
69 LyXFont font = pi.base.font;
74 font.setColor(LColor::special);
75 pi.pain.text(x, y, lyx::char_type('-'), font);
80 font.setColor(LColor::special);
81 pi.pain.text(x, y, lyx::char_type('|'), font);
86 font.setColor(LColor::special);
87 pi.pain.text(x, y, lyx::char_type('.'), font);
92 font.setColor(LColor::special);
93 string ell = ". . . ";
94 docstring dell(ell.begin(), ell.end());
95 pi.pain.text(x, y, dell, font);
100 lyx::frontend::FontMetrics const & fm =
101 theFontMetrics(font);
103 // A triangle the width and height of an 'x'
104 int w = fm.width(lyx::char_type('x'));
105 int ox = fm.width(lyx::char_type(' ')) + x;
106 int h = fm.ascent(lyx::char_type('x'));
109 xp[0] = ox; yp[0] = y;
110 xp[1] = ox; yp[1] = y - h;
111 xp[2] = ox + w; yp[2] = y - h/2;
112 xp[3] = ox; yp[3] = y;
114 pi.pain.lines(xp, yp, 4, LColor::special);
121 // In lyxf3 this will be just LaTeX
122 void InsetSpecialChar::write(Buffer const &, ostream & os) const
130 command = "\\textcompwordmark{}";
132 case END_OF_SENTENCE:
136 command = "\\ldots{}";
139 command = "\\menuseparator";
142 os << "\\SpecialChar " << command << "\n";
146 // This function will not be necessary when lyx3
147 void InsetSpecialChar::read(Buffer const &, LyXLex & lex)
150 string const command = lex.getString();
152 if (command == "\\-")
154 else if (command == "\\textcompwordmark{}")
155 kind_ = LIGATURE_BREAK;
156 else if (command == "\\@.")
157 kind_ = END_OF_SENTENCE;
158 else if (command == "\\ldots{}")
160 else if (command == "\\menuseparator")
161 kind_ = MENU_SEPARATOR;
163 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
167 int InsetSpecialChar::latex(Buffer const &, ostream & os,
168 OutputParams const &) const
175 os << "\\textcompwordmark{}";
177 case END_OF_SENTENCE:
184 os << "\\lyxarrow{}";
191 int InsetSpecialChar::plaintext(Buffer const &, lyx::odocstream & os,
192 OutputParams const &) const
198 case END_OF_SENTENCE:
212 int InsetSpecialChar::docbook(Buffer const &, ostream & os,
213 OutputParams const &) const
219 case END_OF_SENTENCE:
233 int InsetSpecialChar::textString(Buffer const & buf, lyx::odocstream & os,
234 OutputParams const & op) const
236 return plaintext(buf, os, op);
240 auto_ptr<InsetBase> InsetSpecialChar::doClone() const
242 return auto_ptr<InsetBase>(new InsetSpecialChar(kind_));
246 void InsetSpecialChar::validate(LaTeXFeatures & features) const
248 if (kind_ == MENU_SEPARATOR) {
249 features.require("lyxarrow");
254 bool InsetSpecialChar::isChar() const
260 bool InsetSpecialChar::isLetter() const
262 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
266 bool InsetSpecialChar::isLineSeparator() const
269 // this would be nice, but it does not work, since
270 // Paragraph::stripLeadingSpaces nukes the characters which
271 // have this property. I leave the code here, since it should
272 // eventually be made to work. (JMarc 20020327)
273 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;