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"
21 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
24 #include "output_xhtml.h"
26 #include "texstream.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/NullPainter.h"
30 #include "frontends/Painter.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
40 InsetSpecialChar::InsetSpecialChar(Kind k)
41 : Inset(nullptr), kind_(k)
45 InsetSpecialChar::Kind InsetSpecialChar::kind() const
51 docstring InsetSpecialChar::toolTip(BufferView const &, int, int) const
56 message = from_ascii("Optional Line Break (ZWSP)");
59 message = from_ascii("Ligature Break (ZWNJ)");
62 message = from_ascii("End of Sentence");
65 message = from_ascii("Hyphenation Point");
68 message = from_ascii("Breakable Slash");
71 message = from_ascii("Protected Hyphen (SHY)");
79 // no tooltip for these ones.
86 int InsetSpecialChar::rowFlags() const
92 // these are the elements that allow line breaking
111 // helper function: draw text and update x.
112 void drawChar(PainterInfo & pi, int & x, int const y, char_type ch)
114 FontInfo font = pi.base.font;
115 font.setPaintColor(pi.textColor(font.realColor()));
116 pi.pain.text(x, y, ch, font);
117 x += theFontMetrics(font).width(ch);
121 void drawLogo(PainterInfo & pi, int & x, int const y, InsetSpecialChar::Kind kind)
123 FontInfo const & font = pi.base.font;
124 int const em = theFontMetrics(font).em();
126 case InsetSpecialChar::PHRASE_LYX:
128 * \providecommand{\LyX}{L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\\@};
130 drawChar(pi, x, y, 'L');
132 drawChar(pi, x, y + em / 4, 'Y');
134 drawChar(pi, x, y, 'X');
137 case InsetSpecialChar::PHRASE_TEX: {
139 * \def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
141 int const ex = theFontMetrics(font).xHeight();
142 drawChar(pi, x, y, 'T');
144 drawChar(pi, x, y + ex / 2, 'E');
146 drawChar(pi, x, y, 'X');
149 case InsetSpecialChar::PHRASE_LATEX2E:
151 * \DeclareRobustCommand{\LaTeXe}{\mbox{\m@th
152 * \if b\expandafter\@car\f@series\@nil\boldmath\fi
153 * \LaTeX\kern.15em2$_{\textstyle\varepsilon}$}}
155 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_LATEX);
157 drawChar(pi, x, y, '2');
158 // ε U+03B5 GREEK SMALL LETTER EPSILON
159 drawChar(pi, x, y + em / 4, char_type(0x03b5));
162 case InsetSpecialChar::PHRASE_LATEX: {
164 * \DeclareRobustCommand{\LaTeX}{L\kern-.36em%
166 * \vbox to\ht\z@{\hbox{\check@mathfonts
167 * \fontsize\sf@size\z@
168 * \math@fontsfalse\selectfont
175 drawChar(pi, x, y, 'L');
177 PainterInfo pi2 = pi;
178 pi2.base.font.decSize().decSize();
179 drawChar(pi2, x, y - em / 5, 'A');
181 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_TEX);
185 LYXERR0("No information for drawing logo " << kind);
192 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
194 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
195 dim.asc = fm.maxAscent();
202 dim.asc = fm.xHeight();
203 dim.des = fm.descent('g');
204 dim.wid = fm.em() / 8;
209 case END_OF_SENTENCE:
213 // see comment in draw().
214 auto const fam = mi.base.font.family();
215 // Multiplication by 3 is done here to limit rounding effects.
216 int const spc3 = fam == TYPEWRITER_FAMILY ? 0 : 3 * fm.width(char_type(' ')) / 2;
217 dim.wid = 3 * fm.width(char_type('.')) + spc3;
221 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
222 // There is a \thinspace on each side of the triangle
223 dim.wid = 2 * fm.em() / 6 + fm.width(char_type(0x25B9));
226 dim.wid = fm.width(from_ascii("-"));
228 dim.wid -= 2; // to make it look shorter
232 dim.des = fm.descent(s[0]);
241 dim.asc = fm.maxAscent();
242 dim.des = fm.maxDescent();
243 frontend::NullPainter np;
244 PainterInfo pi(mi.base.bv, np);
245 pi.base.font = mi.base.font;
246 // We rely on the fact that drawLogo updates x to compute
247 // the width without code duplication.
248 drawLogo(pi, dim.wid, 0, kind_);
252 dim.wid = fm.width(s);
256 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
258 FontInfo font = pi.base.font;
263 font.setColor(Color_special);
264 pi.pain.text(x, y, char_type('-'), font);
269 // A small vertical line
270 int const asc = theFontMetrics(pi.base.font).xHeight();
271 int const desc = theFontMetrics(pi.base.font).descent('g');
272 int const x0 = x; // x + 1; // FIXME: incline,
273 int const x1 = x; // x - 1; // similar to LibreOffice?
274 int const y0 = y + desc;
275 int const y1 = y - asc / 3;
276 pi.pain.line(x0, y1, x1, y0, Color_special);
281 font.setColor(Color_special);
282 pi.pain.text(x, y, char_type('|'), font);
285 case END_OF_SENTENCE:
287 font.setColor(Color_special);
288 pi.pain.text(x, y, char_type('.'), font);
293 font.setColor(Color_special);
294 /* \textellipsis uses a \fontdimen3 is spacing. The TeXbook
295 * tells us that \fontdimen3 is the interword stretch, and
296 * that this is usually half a space.
298 frontend::FontMetrics const & fm = theFontMetrics(font);
299 auto const fam = pi.base.font.family();
300 int const spc = fam == TYPEWRITER_FAMILY ? 0 : fm.width(char_type(' ')) / 2;
301 int wid1 = fm.width(char_type('.')) + spc;
302 pi.pain.text(x, y, char_type('.'), font);
303 pi.pain.text(x + wid1, y, char_type('.'), font);
304 pi.pain.text(x + 2 * wid1, y, char_type('.'), font);
309 frontend::FontMetrics const & fm = theFontMetrics(font);
311 // There is a \thinspace on each side of the triangle
313 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
314 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
315 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
316 font.setColor(Color_special);
317 pi.pain.text(x, y, c, font);
322 font.setColor(Color_special);
323 pi.pain.text(x, y, char_type('/'), font);
328 font.setColor(Color_latex);
329 pi.pain.text(x, y, char_type('-'), font);
336 drawLogo(pi, x, y, kind_);
342 void InsetSpecialChar::write(ostream & os) const
347 command = "softhyphen";
350 command = "allowbreak";
353 command = "ligaturebreak";
355 case END_OF_SENTENCE:
356 command = "endofsentence";
362 command = "menuseparator";
365 command = "breakableslash";
368 command = "nobreakdash";
383 os << "\\SpecialChar " << command << "\n";
387 void InsetSpecialChar::read(Lexer & lex)
390 string const command = lex.getString();
392 if (command == "softhyphen")
394 else if (command == "allowbreak")
396 else if (command == "ligaturebreak")
397 kind_ = LIGATURE_BREAK;
398 else if (command == "endofsentence")
399 kind_ = END_OF_SENTENCE;
400 else if (command == "ldots")
402 else if (command == "menuseparator")
403 kind_ = MENU_SEPARATOR;
404 else if (command == "breakableslash")
406 else if (command == "nobreakdash")
408 else if (command == "LyX")
410 else if (command == "TeX")
412 else if (command == "LaTeX2e")
413 kind_ = PHRASE_LATEX2E;
414 else if (command == "LaTeX")
415 kind_ = PHRASE_LATEX;
417 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
421 void InsetSpecialChar::latex(otexstream & os,
422 OutputParams const & rp) const
424 bool const rtl = rp.local_font->isRightToLeft();
425 bool const utf8 = rp.encoding->iconvName() == "UTF-8";
427 string lswitche = "";
428 if (rtl && !rp.use_polyglossia) {
431 if (rp.local_font->language()->lang() == "arabic_arabi"
432 || rp.local_font->language()->lang() == "farsi")
433 lswitch = "\\textLR{";
441 // U+200B not yet supported by utf8 inputenc
442 os << "\\LyXZeroWidthSpace" << termcmd;
446 // U+200C ZERO WIDTH NON-JOINER
449 os << "\\textcompwordmark" << termcmd;
451 case END_OF_SENTENCE:
455 os << "\\ldots" << termcmd;
465 os << "\\slash" << termcmd;
470 os << "\\nobreakdash-";
475 os << lswitch << "\\LyX" << termcmd << lswitche;
480 os << lswitch << "\\TeX" << termcmd << lswitche;
485 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
490 os << lswitch << "\\LaTeX" << termcmd << lswitche;
496 int InsetSpecialChar::plaintext(odocstringstream & os,
497 OutputParams const &, size_t) const
503 // U+200B ZERO WIDTH SPACE (ZWSP)
507 // U+200C ZERO WIDTH NON-JOINER
510 case END_OF_SENTENCE:
514 // … U+2026 HORIZONTAL ELLIPSIS
524 // ‑ U+2011 NON-BREAKING HYPHEN
535 // ε U+03B5 GREEK SMALL LETTER EPSILON
547 string specialCharKindToXMLEntity(InsetSpecialChar::Kind kind) {
549 case InsetSpecialChar::Kind::HYPHENATION:
552 case InsetSpecialChar::Kind::ALLOWBREAK:
555 case InsetSpecialChar::Kind::LIGATURE_BREAK:
556 // Zero width non-joiner
558 case InsetSpecialChar::Kind::END_OF_SENTENCE:
560 case InsetSpecialChar::Kind::LDOTS:
563 case InsetSpecialChar::Kind::MENU_SEPARATOR:
564 // ⇒, right arrow.
566 case InsetSpecialChar::Kind::SLASH:
567 // ⁄, fractional slash.
569 case InsetSpecialChar::Kind::NOBREAKDASH:
570 // Non-breaking hyphen.
572 case InsetSpecialChar::Kind::PHRASE_LYX:
574 case InsetSpecialChar::Kind::PHRASE_TEX:
576 case InsetSpecialChar::Kind::PHRASE_LATEX2E:
577 // Lower-case epsilon.
578 return "LaTeX2ε";
579 case InsetSpecialChar::Kind::PHRASE_LATEX:
588 void InsetSpecialChar::docbook(XMLStream & xs, OutputParams const &) const
590 xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
594 docstring InsetSpecialChar::xhtml(XMLStream & xs, OutputParams const &) const
596 xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
601 void InsetSpecialChar::toString(odocstream & os) const
606 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
607 // Spell checker would choke on it.
612 odocstringstream ods;
613 plaintext(ods, OutputParams(nullptr));
618 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
621 odocstringstream ods;
622 plaintext(ods, OutputParams(nullptr));
627 void InsetSpecialChar::validate(LaTeXFeatures & features) const
629 if (kind_ == ALLOWBREAK)
630 features.require("lyxzerowidthspace");
631 if (kind_ == MENU_SEPARATOR)
632 features.require("lyxarrow");
633 if (kind_ == NOBREAKDASH)
634 features.require("amsmath");
635 if (kind_ == PHRASE_LYX)
636 features.require("LyX");
640 bool InsetSpecialChar::isChar() const
642 return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
646 bool InsetSpecialChar::isLetter() const
648 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
649 || kind_ == NOBREAKDASH
650 || kind_ == PHRASE_LYX || kind_ == PHRASE_LATEX
651 || kind_ == PHRASE_TEX || kind_ == PHRASE_LATEX2E;
655 bool InsetSpecialChar::isLineSeparator() const
658 // this would be nice, but it does not work, since
659 // Paragraph::stripLeadingSpaces nukes the characters which
660 // have this property. I leave the code here, since it should
661 // eventually be made to work. (JMarc 20020327)
662 return kind_ == HYPHENATION || kind_ == ALLOWBREAK
663 || kind_ == MENU_SEPARATOR || kind_ == SLASH;