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"
20 #include "MetricsInfo.h"
22 #include "frontends/FontMetrics.h"
23 #include "frontends/Painter.h"
32 InsetSpecialChar::InsetSpecialChar(Kind k)
37 InsetSpecialChar::Kind InsetSpecialChar::kind() const
43 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
45 frontend::FontMetrics const & fm =
46 theFontMetrics(mi.base.font);
47 dim.asc = fm.maxAscent();
48 dim.des = fm.maxDescent();
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 = fm.width(ds);
60 if (kind_ == HYPHENATION && dim.wid > 5)
61 dim.wid -= 2; // to make it look shorter
67 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
69 FontInfo font = pi.base.font;
74 font.setColor(Color_special);
75 pi.pain.text(x, y, char_type('-'), font);
80 font.setColor(Color_special);
81 pi.pain.text(x, y, char_type('|'), font);
86 font.setColor(Color_special);
87 pi.pain.text(x, y, char_type('.'), font);
92 font.setColor(Color_special);
93 string ell = ". . . ";
94 docstring dell(ell.begin(), ell.end());
95 pi.pain.text(x, y, dell, font);
100 frontend::FontMetrics const & fm =
101 theFontMetrics(font);
103 // A triangle the width and height of an 'x'
104 int w = fm.width(char_type('x'));
105 int ox = fm.width(char_type(' ')) + x;
106 int h = fm.ascent(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, Color_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 &, Lexer & 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 &, odocstream & os,
168 OutputParams const &) const
175 os << "\\textcompwordmark{}";
177 case END_OF_SENTENCE:
184 os << "\\lyxarrow{}";
191 int InsetSpecialChar::plaintext(Buffer const &, odocstream & os,
192 OutputParams const &) const
198 case END_OF_SENTENCE:
212 int InsetSpecialChar::docbook(Buffer const &, odocstream & os,
213 OutputParams const &) const
219 case END_OF_SENTENCE:
233 int InsetSpecialChar::textString(Buffer const & buf, odocstream & os,
234 OutputParams const & op) const
236 return plaintext(buf, os, op);
240 Inset * InsetSpecialChar::clone() const
242 return new InsetSpecialChar(kind_);
246 void InsetSpecialChar::validate(LaTeXFeatures & features) const
248 if (kind_ == MENU_SEPARATOR)
249 features.require("lyxarrow");
253 bool InsetSpecialChar::isChar() const
259 bool InsetSpecialChar::isLetter() const
261 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
265 bool InsetSpecialChar::isLineSeparator() const
268 // this would be nice, but it does not work, since
269 // Paragraph::stripLeadingSpaces nukes the characters which
270 // have this property. I leave the code here, since it should
271 // eventually be made to work. (JMarc 20020327)
272 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;