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"
17 #include "Dimension.h"
19 #include "LaTeXFeatures.h"
21 #include "MetricsInfo.h"
22 #include "output_xhtml.h"
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
27 #include "support/debug.h"
28 #include "support/docstream.h"
35 InsetSpecialChar::InsetSpecialChar(Kind k)
40 InsetSpecialChar::Kind InsetSpecialChar::kind() const
46 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
48 frontend::FontMetrics const & fm =
49 theFontMetrics(mi.base.font);
50 dim.asc = fm.maxAscent();
51 dim.des = fm.maxDescent();
62 s = from_ascii(". . .");
65 s = from_ascii(" x ");
77 s = from_ascii("LyX");
80 s = from_ascii("TeX");
83 s = from_ascii("LaTeX2") + char_type(0x03b5);
86 s = from_ascii("LaTeX");
89 dim.wid = fm.width(s);
90 if (kind_ == HYPHENATION && dim.wid > 5)
91 dim.wid -= 2; // to make it look shorter
97 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
99 FontInfo font = pi.base.font;
104 font.setColor(Color_special);
105 pi.pain.text(x, y, char_type('-'), font);
110 font.setColor(Color_special);
111 pi.pain.text(x, y, char_type('|'), font);
114 case END_OF_SENTENCE:
116 font.setColor(Color_special);
117 pi.pain.text(x, y, char_type('.'), font);
122 font.setColor(Color_special);
123 string ell = ". . . ";
124 docstring dell(ell.begin(), ell.end());
125 pi.pain.text(x, y, dell, font);
130 frontend::FontMetrics const & fm =
131 theFontMetrics(font);
133 // A triangle the width and height of an 'x'
134 int w = fm.width(char_type('x'));
135 int ox = fm.width(char_type(' ')) + x;
136 int h = fm.ascent(char_type('x'));
139 xp[0] = ox; yp[0] = y;
140 xp[1] = ox; yp[1] = y - h;
141 xp[2] = ox + w; yp[2] = y - h/2;
142 xp[3] = ox; yp[3] = y;
144 pi.pain.lines(xp, yp, 4, Color_special);
149 font.setColor(Color_special);
150 pi.pain.text(x, y, char_type('/'), font);
155 font.setColor(Color_latex);
156 pi.pain.text(x, y, char_type('-'), font);
160 font.setColor(Color_special);
161 pi.pain.text(x, y, from_ascii("LyX"), font);
164 font.setColor(Color_special);
165 pi.pain.text(x, y, from_ascii("TeX"), font);
168 font.setColor(Color_special);
169 pi.pain.text(x, y, from_ascii("LaTeX2") + char_type(0x03b5), font);
172 font.setColor(Color_special);
173 pi.pain.text(x, y, from_ascii("LaTeX"), font);
179 // In lyxf3 this will be just LaTeX
180 void InsetSpecialChar::write(ostream & os) const
188 command = "\\textcompwordmark{}";
190 case END_OF_SENTENCE:
194 command = "\\ldots{}";
197 command = "\\menuseparator";
200 command = "\\slash{}";
203 command = "\\nobreakdash-";
212 command = "\\LaTeX2e";
218 os << "\\SpecialChar " << command << "\n";
222 // This function will not be necessary when lyx3
223 void InsetSpecialChar::read(Lexer & lex)
226 string const command = lex.getString();
228 if (command == "\\-")
230 else if (command == "\\textcompwordmark{}")
231 kind_ = LIGATURE_BREAK;
232 else if (command == "\\@.")
233 kind_ = END_OF_SENTENCE;
234 else if (command == "\\ldots{}")
236 else if (command == "\\menuseparator")
237 kind_ = MENU_SEPARATOR;
238 else if (command == "\\slash{}")
240 else if (command == "\\nobreakdash-")
242 else if (command == "\\LyX")
244 else if (command == "\\TeX")
246 else if (command == "\\LaTeX2e")
247 kind_ = PHRASE_LATEX2E;
248 else if (command == "\\LaTeX")
249 kind_ = PHRASE_LATEX;
251 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
255 void InsetSpecialChar::latex(otexstream & os,
256 OutputParams const & rp) const
263 os << "\\textcompwordmark{}";
265 case END_OF_SENTENCE:
272 if (rp.local_font->isRightToLeft())
273 os << "\\lyxarrow*{}";
275 os << "\\lyxarrow{}";
283 os << "\\nobreakdash-";
309 int InsetSpecialChar::plaintext(odocstringstream & os,
310 OutputParams const &, size_t) const
318 case END_OF_SENTENCE:
351 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
357 case END_OF_SENTENCE:
390 docstring InsetSpecialChar::xhtml(XHTMLStream & xs, OutputParams const &) const
396 xs << XHTMLStream::ESCAPE_NONE << "‌";
398 case END_OF_SENTENCE:
402 xs << XHTMLStream::ESCAPE_NONE << "…";
405 xs << XHTMLStream::ESCAPE_NONE << "⇒";
408 xs << XHTMLStream::ESCAPE_NONE << "⁄";
411 xs << XHTMLStream::ESCAPE_NONE << "‑";
420 xs << "LaTeX2" << XHTMLStream::ESCAPE_NONE << "ε";
430 void InsetSpecialChar::toString(odocstream & os) const
434 // Do not output ZERO WIDTH NON JOINER here
435 // Spell checker would choke on it.
440 odocstringstream ods;
441 plaintext(ods, OutputParams(0));
446 void InsetSpecialChar::forOutliner(docstring & os, size_t) const
448 odocstringstream ods;
449 plaintext(ods, OutputParams(0));
454 void InsetSpecialChar::validate(LaTeXFeatures & features) const
456 if (kind_ == MENU_SEPARATOR)
457 features.require("lyxarrow");
458 if (kind_ == NOBREAKDASH)
459 features.require("amsmath");
460 if (kind_ == PHRASE_LYX)
461 features.require("LyX");
465 bool InsetSpecialChar::isLetter() const
467 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
468 || kind_ == NOBREAKDASH;
472 bool InsetSpecialChar::isLineSeparator() const
475 // this would be nice, but it does not work, since
476 // Paragraph::stripLeadingSpaces nukes the characters which
477 // have this property. I leave the code here, since it should
478 // eventually be made to work. (JMarc 20020327)
479 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR