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"
20 #include "LaTeXFeatures.h"
22 #include "MetricsInfo.h"
23 #include "output_xhtml.h"
24 #include "texstream.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/NullPainter.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
38 InsetSpecialChar::InsetSpecialChar(Kind k)
43 InsetSpecialChar::Kind InsetSpecialChar::kind() const
49 docstring InsetSpecialChar::toolTip(BufferView const &, int, int) const
54 message = from_ascii("Optional Line Break (ZWSP)");
57 message = from_ascii("Ligature Break (ZWNJ)");
60 message = from_ascii("End of Sentence");
63 message = from_ascii("Hyphenation Point");
66 message = from_ascii("Breakable Slash");
69 message = from_ascii("Protected Hyphen (SHY)");
77 // no tooltip for these ones.
86 // helper function: draw text and update x.
87 void drawChar(PainterInfo & pi, int & x, int const y, char_type ch)
89 FontInfo font = pi.base.font;
90 font.setPaintColor(pi.textColor(font.realColor()));
91 pi.pain.text(x, y, ch, font);
92 x += theFontMetrics(font).width(ch);
96 void drawLogo(PainterInfo & pi, int & x, int const y, InsetSpecialChar::Kind kind)
98 FontInfo const & font = pi.base.font;
99 int const em = theFontMetrics(font).em();
101 case InsetSpecialChar::PHRASE_LYX:
103 * \providecommand{\LyX}{L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\\@};
105 drawChar(pi, x, y, 'L');
107 drawChar(pi, x, y + em / 4, 'Y');
109 drawChar(pi, x, y, 'X');
112 case InsetSpecialChar::PHRASE_TEX: {
114 * \def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
116 int const ex = theFontMetrics(font).xHeight();
117 drawChar(pi, x, y, 'T');
119 drawChar(pi, x, y + ex / 2, 'E');
121 drawChar(pi, x, y, 'X');
124 case InsetSpecialChar::PHRASE_LATEX2E:
126 * \DeclareRobustCommand{\LaTeXe}{\mbox{\m@th
127 * \if b\expandafter\@car\f@series\@nil\boldmath\fi
128 * \LaTeX\kern.15em2$_{\textstyle\varepsilon}$}}
130 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_LATEX);
132 drawChar(pi, x, y, '2');
133 drawChar(pi, x, y + em / 4, char_type(0x03b5));
136 case InsetSpecialChar::PHRASE_LATEX: {
138 * \DeclareRobustCommand{\LaTeX}{L\kern-.36em%
140 * \vbox to\ht\z@{\hbox{\check@mathfonts
141 * \fontsize\sf@size\z@
142 * \math@fontsfalse\selectfont
149 drawChar(pi, x, y, 'L');
151 PainterInfo pi2 = pi;
152 pi2.base.font.decSize().decSize();
153 drawChar(pi2, x, y - em / 5, 'A');
155 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_TEX);
159 LYXERR0("No information for drawing logo " << kind);
166 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
168 frontend::FontMetrics const & fm =
169 theFontMetrics(mi.base.font);
170 dim.asc = fm.maxAscent();
177 dim.asc = fm.xHeight();
178 dim.des = fm.descent('g');
179 dim.wid = fm.em() / 8;
184 case END_OF_SENTENCE:
188 s = from_ascii(". . .");
191 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
192 // There is a \thinspace on each side of the triangle
193 dim.wid = 2 * fm.em() / 6 + fm.width(char_type(0x25B9));
196 dim.wid = fm.width(from_ascii("-"));
198 dim.wid -= 2; // to make it look shorter
202 dim.des = fm.descent(s[0]);
211 dim.asc = fm.maxAscent();
212 dim.des = fm.maxDescent();
213 frontend::NullPainter np;
214 PainterInfo pi(mi.base.bv, np);
215 drawLogo(pi, dim.wid, 0, kind_);
219 dim.wid = fm.width(s);
223 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
225 FontInfo font = pi.base.font;
230 font.setColor(Color_special);
231 pi.pain.text(x, y, char_type('-'), font);
236 // A small vertical line
237 int const asc = theFontMetrics(pi.base.font).xHeight();
238 int const desc = theFontMetrics(pi.base.font).descent('g');
239 int const x0 = x; // x + 1; // FIXME: incline,
240 int const x1 = x; // x - 1; // similar to LibreOffice?
241 int const y0 = y + desc;
242 int const y1 = y - asc / 3;
243 pi.pain.line(x0, y1, x1, y0, Color_special);
248 font.setColor(Color_special);
249 pi.pain.text(x, y, char_type('|'), font);
252 case END_OF_SENTENCE:
254 font.setColor(Color_special);
255 pi.pain.text(x, y, char_type('.'), font);
260 font.setColor(Color_special);
261 string ell = ". . . ";
262 docstring dell(ell.begin(), ell.end());
263 pi.pain.text(x, y, dell, font);
268 frontend::FontMetrics const & fm =
269 theFontMetrics(font);
271 // There is a \thinspace on each side of the triangle
273 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
274 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
275 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
276 font.setColor(Color_special);
277 pi.pain.text(x, y, c, font);
282 font.setColor(Color_special);
283 pi.pain.text(x, y, char_type('/'), font);
288 font.setColor(Color_latex);
289 pi.pain.text(x, y, char_type('-'), font);
296 drawLogo(pi, x, y, kind_);
302 void InsetSpecialChar::write(ostream & os) const
307 command = "softhyphen";
310 command = "allowbreak";
313 command = "ligaturebreak";
315 case END_OF_SENTENCE:
316 command = "endofsentence";
322 command = "menuseparator";
325 command = "breakableslash";
328 command = "nobreakdash";
343 os << "\\SpecialChar " << command << "\n";
347 void InsetSpecialChar::read(Lexer & lex)
350 string const command = lex.getString();
352 if (command == "softhyphen")
354 else if (command == "allowbreak")
356 else if (command == "ligaturebreak")
357 kind_ = LIGATURE_BREAK;
358 else if (command == "endofsentence")
359 kind_ = END_OF_SENTENCE;
360 else if (command == "ldots")
362 else if (command == "menuseparator")
363 kind_ = MENU_SEPARATOR;
364 else if (command == "breakableslash")
366 else if (command == "nobreakdash")
368 else if (command == "LyX")
370 else if (command == "TeX")
372 else if (command == "LaTeX2e")
373 kind_ = PHRASE_LATEX2E;
374 else if (command == "LaTeX")
375 kind_ = PHRASE_LATEX;
377 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
381 void InsetSpecialChar::latex(otexstream & os,
382 OutputParams const & rp) const
384 bool const rtl = rp.local_font->isRightToLeft();
386 string lswitche = "";
387 if (rtl && !rp.use_polyglossia) {
390 if (rp.local_font->language()->lang() == "arabic_arabi"
391 || rp.local_font->language()->lang() == "farsi")
392 lswitch = "\\textLR{";
400 os << "\\LyXZeroWidthSpace" << termcmd;
403 os << "\\textcompwordmark" << termcmd;
405 case END_OF_SENTENCE:
409 os << "\\ldots" << termcmd;
419 os << "\\slash" << termcmd;
424 os << "\\nobreakdash-";
429 os << lswitch << "\\LyX" << termcmd << lswitche;
434 os << lswitch << "\\TeX" << termcmd << lswitche;
439 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
444 os << lswitch << "\\LaTeX" << termcmd << lswitche;
450 int InsetSpecialChar::plaintext(odocstringstream & os,
451 OutputParams const &, size_t) const
462 case END_OF_SENTENCE:
495 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
505 case END_OF_SENTENCE:
538 docstring InsetSpecialChar::xhtml(XHTMLStream & xs, OutputParams const &) const
544 xs << XHTMLStream::ESCAPE_NONE << "​";
547 xs << XHTMLStream::ESCAPE_NONE << "‌";
549 case END_OF_SENTENCE:
553 xs << XHTMLStream::ESCAPE_NONE << "…";
556 xs << XHTMLStream::ESCAPE_NONE << "⇒";
559 xs << XHTMLStream::ESCAPE_NONE << "⁄";
562 xs << XHTMLStream::ESCAPE_NONE << "‑";
571 xs << "LaTeX2" << XHTMLStream::ESCAPE_NONE << "ε";
581 void InsetSpecialChar::toString(odocstream & os) const
586 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
587 // Spell checker would choke on it.
592 odocstringstream ods;
593 plaintext(ods, OutputParams(0));
598 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
601 odocstringstream ods;
602 plaintext(ods, OutputParams(0));
607 void InsetSpecialChar::validate(LaTeXFeatures & features) const
609 if (kind_ == ALLOWBREAK)
610 features.require("lyxzerowidthspace");
611 if (kind_ == MENU_SEPARATOR)
612 features.require("lyxarrow");
613 if (kind_ == NOBREAKDASH)
614 features.require("amsmath");
615 if (kind_ == PHRASE_LYX)
616 features.require("LyX");
620 bool InsetSpecialChar::isChar() const
622 return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
626 bool InsetSpecialChar::isLetter() const
628 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
629 || kind_ == NOBREAKDASH;
633 bool InsetSpecialChar::isLineSeparator() const
636 // this would be nice, but it does not work, since
637 // Paragraph::stripLeadingSpaces nukes the characters which
638 // have this property. I leave the code here, since it should
639 // eventually be made to work. (JMarc 20020327)
640 return kind_ == HYPHENATION || kind_ == ALLOWBREAK
641 || kind_ == MENU_SEPARATOR || kind_ == SLASH;