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/font_metrics.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 LyXFont & font = mi.base.font;
47 dim.asc = font_metrics::maxAscent(font);
48 dim.des = font_metrics::maxDescent(font);
52 case LIGATURE_BREAK: s = "|"; break;
53 case END_OF_SENTENCE: s = "."; break;
54 case LDOTS: s = ". . ."; break;
55 case MENU_SEPARATOR: s = " x "; break;
56 case HYPHENATION: s = "-"; break;
58 docstring ds(s.begin(), s.end());
59 dim.wid = font_metrics::width(ds, font);
60 if (kind_ == HYPHENATION && dim.wid > 5)
61 dim.wid -= 2; // to make it look shorter
66 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
68 LyXFont font = pi.base.font;
73 font.setColor(LColor::special);
74 pi.pain.text(x, y, lyx::char_type('-'), font);
79 font.setColor(LColor::special);
80 pi.pain.text(x, y, lyx::char_type('|'), font);
85 font.setColor(LColor::special);
86 pi.pain.text(x, y, lyx::char_type('.'), font);
91 font.setColor(LColor::special);
92 string ell = ". . . ";
93 docstring dell(ell.begin(), ell.end());
94 pi.pain.text(x, y, dell, font);
99 // A triangle the width and height of an 'x'
100 int w = font_metrics::width(lyx::char_type('x'), font);
101 int ox = font_metrics::width(lyx::char_type(' '), font) + x;
102 int h = font_metrics::ascent(lyx::char_type('x'), font);
105 xp[0] = ox; yp[0] = y;
106 xp[1] = ox; yp[1] = y - h;
107 xp[2] = ox + w; yp[2] = y - h/2;
108 xp[3] = ox; yp[3] = y;
110 pi.pain.lines(xp, yp, 4, LColor::special);
117 // In lyxf3 this will be just LaTeX
118 void InsetSpecialChar::write(Buffer const &, ostream & os) const
126 command = "\\textcompwordmark{}";
128 case END_OF_SENTENCE:
132 command = "\\ldots{}";
135 command = "\\menuseparator";
138 os << "\\SpecialChar " << command << "\n";
142 // This function will not be necessary when lyx3
143 void InsetSpecialChar::read(Buffer const &, LyXLex & lex)
146 string const command = lex.getString();
148 if (command == "\\-")
150 else if (command == "\\textcompwordmark{}")
151 kind_ = LIGATURE_BREAK;
152 else if (command == "\\@.")
153 kind_ = END_OF_SENTENCE;
154 else if (command == "\\ldots{}")
156 else if (command == "\\menuseparator")
157 kind_ = MENU_SEPARATOR;
159 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
163 int InsetSpecialChar::latex(Buffer const &, ostream & os,
164 OutputParams const &) const
171 os << "\\textcompwordmark{}";
173 case END_OF_SENTENCE:
180 os << "\\lyxarrow{}";
187 int InsetSpecialChar::plaintext(Buffer const &, ostream & os,
188 OutputParams const &) const
194 case END_OF_SENTENCE:
208 int InsetSpecialChar::docbook(Buffer const &, ostream & os,
209 OutputParams const &) const
215 case END_OF_SENTENCE:
229 int InsetSpecialChar::textString(Buffer const & buf, ostream & os,
230 OutputParams const & op) const
232 return plaintext(buf, os, op);
236 auto_ptr<InsetBase> InsetSpecialChar::doClone() const
238 return auto_ptr<InsetBase>(new InsetSpecialChar(kind_));
242 void InsetSpecialChar::validate(LaTeXFeatures & features) const
244 if (kind_ == MENU_SEPARATOR) {
245 features.require("lyxarrow");
250 bool InsetSpecialChar::isChar() const
256 bool InsetSpecialChar::isLetter() const
258 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
262 bool InsetSpecialChar::isLineSeparator() const
265 // this would be nice, but it does not work, since
266 // Paragraph::stripLeadingSpaces nukes the characters which
267 // have this property. I leave the code here, since it should
268 // eventually be made to work. (JMarc 20020327)
269 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;