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 pi.base.font = mi.base.font;
216 drawLogo(pi, dim.wid, 0, kind_);
220 dim.wid = fm.width(s);
224 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
226 FontInfo font = pi.base.font;
231 font.setColor(Color_special);
232 pi.pain.text(x, y, char_type('-'), font);
237 // A small vertical line
238 int const asc = theFontMetrics(pi.base.font).xHeight();
239 int const desc = theFontMetrics(pi.base.font).descent('g');
240 int const x0 = x; // x + 1; // FIXME: incline,
241 int const x1 = x; // x - 1; // similar to LibreOffice?
242 int const y0 = y + desc;
243 int const y1 = y - asc / 3;
244 pi.pain.line(x0, y1, x1, y0, Color_special);
249 font.setColor(Color_special);
250 pi.pain.text(x, y, char_type('|'), font);
253 case END_OF_SENTENCE:
255 font.setColor(Color_special);
256 pi.pain.text(x, y, char_type('.'), font);
261 font.setColor(Color_special);
262 string ell = ". . . ";
263 docstring dell(ell.begin(), ell.end());
264 pi.pain.text(x, y, dell, font);
269 frontend::FontMetrics const & fm =
270 theFontMetrics(font);
272 // There is a \thinspace on each side of the triangle
274 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
275 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
276 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
277 font.setColor(Color_special);
278 pi.pain.text(x, y, c, font);
283 font.setColor(Color_special);
284 pi.pain.text(x, y, char_type('/'), font);
289 font.setColor(Color_latex);
290 pi.pain.text(x, y, char_type('-'), font);
297 drawLogo(pi, x, y, kind_);
303 void InsetSpecialChar::write(ostream & os) const
308 command = "softhyphen";
311 command = "allowbreak";
314 command = "ligaturebreak";
316 case END_OF_SENTENCE:
317 command = "endofsentence";
323 command = "menuseparator";
326 command = "breakableslash";
329 command = "nobreakdash";
344 os << "\\SpecialChar " << command << "\n";
348 void InsetSpecialChar::read(Lexer & lex)
351 string const command = lex.getString();
353 if (command == "softhyphen")
355 else if (command == "allowbreak")
357 else if (command == "ligaturebreak")
358 kind_ = LIGATURE_BREAK;
359 else if (command == "endofsentence")
360 kind_ = END_OF_SENTENCE;
361 else if (command == "ldots")
363 else if (command == "menuseparator")
364 kind_ = MENU_SEPARATOR;
365 else if (command == "breakableslash")
367 else if (command == "nobreakdash")
369 else if (command == "LyX")
371 else if (command == "TeX")
373 else if (command == "LaTeX2e")
374 kind_ = PHRASE_LATEX2E;
375 else if (command == "LaTeX")
376 kind_ = PHRASE_LATEX;
378 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
382 void InsetSpecialChar::latex(otexstream & os,
383 OutputParams const & rp) const
385 bool const rtl = rp.local_font->isRightToLeft();
387 string lswitche = "";
388 if (rtl && !rp.use_polyglossia) {
391 if (rp.local_font->language()->lang() == "arabic_arabi"
392 || rp.local_font->language()->lang() == "farsi")
393 lswitch = "\\textLR{";
401 os << "\\LyXZeroWidthSpace" << termcmd;
404 os << "\\textcompwordmark" << termcmd;
406 case END_OF_SENTENCE:
410 os << "\\ldots" << termcmd;
420 os << "\\slash" << termcmd;
425 os << "\\nobreakdash-";
430 os << lswitch << "\\LyX" << termcmd << lswitche;
435 os << lswitch << "\\TeX" << termcmd << lswitche;
440 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
445 os << lswitch << "\\LaTeX" << termcmd << lswitche;
451 int InsetSpecialChar::plaintext(odocstringstream & os,
452 OutputParams const &, size_t) const
463 case END_OF_SENTENCE:
496 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
506 case END_OF_SENTENCE:
539 docstring InsetSpecialChar::xhtml(XHTMLStream & xs, OutputParams const &) const
545 xs << XHTMLStream::ESCAPE_NONE << "​";
548 xs << XHTMLStream::ESCAPE_NONE << "‌";
550 case END_OF_SENTENCE:
554 xs << XHTMLStream::ESCAPE_NONE << "…";
557 xs << XHTMLStream::ESCAPE_NONE << "⇒";
560 xs << XHTMLStream::ESCAPE_NONE << "⁄";
563 xs << XHTMLStream::ESCAPE_NONE << "‑";
572 xs << "LaTeX2" << XHTMLStream::ESCAPE_NONE << "ε";
582 void InsetSpecialChar::toString(odocstream & os) const
587 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
588 // Spell checker would choke on it.
593 odocstringstream ods;
594 plaintext(ods, OutputParams(0));
599 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
602 odocstringstream ods;
603 plaintext(ods, OutputParams(0));
608 void InsetSpecialChar::validate(LaTeXFeatures & features) const
610 if (kind_ == ALLOWBREAK)
611 features.require("lyxzerowidthspace");
612 if (kind_ == MENU_SEPARATOR)
613 features.require("lyxarrow");
614 if (kind_ == NOBREAKDASH)
615 features.require("amsmath");
616 if (kind_ == PHRASE_LYX)
617 features.require("LyX");
621 bool InsetSpecialChar::isChar() const
623 return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
627 bool InsetSpecialChar::isLetter() const
629 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
630 || kind_ == NOBREAKDASH;
634 bool InsetSpecialChar::isLineSeparator() const
637 // this would be nice, but it does not work, since
638 // Paragraph::stripLeadingSpaces nukes the characters which
639 // have this property. I leave the code here, since it should
640 // eventually be made to work. (JMarc 20020327)
641 return kind_ == HYPHENATION || kind_ == ALLOWBREAK
642 || kind_ == MENU_SEPARATOR || kind_ == SLASH;