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 drawLogo(pi, dim.wid, 0, kind_);
250 dim.wid = fm.width(s);
254 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
256 FontInfo font = pi.base.font;
261 font.setColor(Color_special);
262 pi.pain.text(x, y, char_type('-'), font);
267 // A small vertical line
268 int const asc = theFontMetrics(pi.base.font).xHeight();
269 int const desc = theFontMetrics(pi.base.font).descent('g');
270 int const x0 = x; // x + 1; // FIXME: incline,
271 int const x1 = x; // x - 1; // similar to LibreOffice?
272 int const y0 = y + desc;
273 int const y1 = y - asc / 3;
274 pi.pain.line(x0, y1, x1, y0, Color_special);
279 font.setColor(Color_special);
280 pi.pain.text(x, y, char_type('|'), font);
283 case END_OF_SENTENCE:
285 font.setColor(Color_special);
286 pi.pain.text(x, y, char_type('.'), font);
291 font.setColor(Color_special);
292 /* \textellipsis uses a \fontdimen3 is spacing. The TeXbook
293 * tells us that \fontdimen3 is the interword stretch, and
294 * that this is usually half a space.
296 frontend::FontMetrics const & fm = theFontMetrics(font);
297 auto const fam = pi.base.font.family();
298 int const spc = fam == TYPEWRITER_FAMILY ? 0 : fm.width(char_type(' ')) / 2;
299 int wid1 = fm.width(char_type('.')) + spc;
300 pi.pain.text(x, y, char_type('.'), font);
301 pi.pain.text(x + wid1, y, char_type('.'), font);
302 pi.pain.text(x + 2 * wid1, y, char_type('.'), font);
307 frontend::FontMetrics const & fm = theFontMetrics(font);
309 // There is a \thinspace on each side of the triangle
311 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
312 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
313 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
314 font.setColor(Color_special);
315 pi.pain.text(x, y, c, font);
320 font.setColor(Color_special);
321 pi.pain.text(x, y, char_type('/'), font);
326 font.setColor(Color_latex);
327 pi.pain.text(x, y, char_type('-'), font);
334 drawLogo(pi, x, y, kind_);
340 void InsetSpecialChar::write(ostream & os) const
345 command = "softhyphen";
348 command = "allowbreak";
351 command = "ligaturebreak";
353 case END_OF_SENTENCE:
354 command = "endofsentence";
360 command = "menuseparator";
363 command = "breakableslash";
366 command = "nobreakdash";
381 os << "\\SpecialChar " << command << "\n";
385 void InsetSpecialChar::read(Lexer & lex)
388 string const command = lex.getString();
390 if (command == "softhyphen")
392 else if (command == "allowbreak")
394 else if (command == "ligaturebreak")
395 kind_ = LIGATURE_BREAK;
396 else if (command == "endofsentence")
397 kind_ = END_OF_SENTENCE;
398 else if (command == "ldots")
400 else if (command == "menuseparator")
401 kind_ = MENU_SEPARATOR;
402 else if (command == "breakableslash")
404 else if (command == "nobreakdash")
406 else if (command == "LyX")
408 else if (command == "TeX")
410 else if (command == "LaTeX2e")
411 kind_ = PHRASE_LATEX2E;
412 else if (command == "LaTeX")
413 kind_ = PHRASE_LATEX;
415 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
419 void InsetSpecialChar::latex(otexstream & os,
420 OutputParams const & rp) const
422 bool const rtl = rp.local_font->isRightToLeft();
423 bool const utf8 = rp.encoding->iconvName() == "UTF-8";
425 string lswitche = "";
426 if (rtl && !rp.use_polyglossia) {
429 if (rp.local_font->language()->lang() == "arabic_arabi"
430 || rp.local_font->language()->lang() == "farsi")
431 lswitch = "\\textLR{";
439 // U+200B not yet supported by utf8 inputenc
440 os << "\\LyXZeroWidthSpace" << termcmd;
444 // U+200C ZERO WIDTH NON-JOINER
447 os << "\\textcompwordmark" << termcmd;
449 case END_OF_SENTENCE:
453 os << "\\ldots" << termcmd;
463 os << "\\slash" << termcmd;
468 os << "\\nobreakdash-";
473 os << lswitch << "\\LyX" << termcmd << lswitche;
478 os << lswitch << "\\TeX" << termcmd << lswitche;
483 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
488 os << lswitch << "\\LaTeX" << termcmd << lswitche;
494 int InsetSpecialChar::plaintext(odocstringstream & os,
495 OutputParams const &, size_t) const
501 // U+200B ZERO WIDTH SPACE (ZWSP)
505 // U+200C ZERO WIDTH NON-JOINER
508 case END_OF_SENTENCE:
512 // … U+2026 HORIZONTAL ELLIPSIS
522 // ‑ U+2011 NON-BREAKING HYPHEN
533 // ε U+03B5 GREEK SMALL LETTER EPSILON
545 string specialCharKindToXMLEntity(InsetSpecialChar::Kind kind) {
547 case InsetSpecialChar::Kind::HYPHENATION:
550 case InsetSpecialChar::Kind::ALLOWBREAK:
553 case InsetSpecialChar::Kind::LIGATURE_BREAK:
554 // Zero width non-joiner
556 case InsetSpecialChar::Kind::END_OF_SENTENCE:
558 case InsetSpecialChar::Kind::LDOTS:
561 case InsetSpecialChar::Kind::MENU_SEPARATOR:
562 // ⇒, right arrow.
564 case InsetSpecialChar::Kind::SLASH:
565 // ⁄, fractional slash.
567 case InsetSpecialChar::Kind::NOBREAKDASH:
568 // Non-breaking hyphen.
570 case InsetSpecialChar::Kind::PHRASE_LYX:
572 case InsetSpecialChar::Kind::PHRASE_TEX:
574 case InsetSpecialChar::Kind::PHRASE_LATEX2E:
575 // Lower-case epsilon.
576 return "LaTeX2ε";
577 case InsetSpecialChar::Kind::PHRASE_LATEX:
586 void InsetSpecialChar::docbook(XMLStream & xs, OutputParams const &) const
588 xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
592 docstring InsetSpecialChar::xhtml(XMLStream & xs, OutputParams const &) const
594 xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
599 void InsetSpecialChar::toString(odocstream & os) const
604 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
605 // Spell checker would choke on it.
610 odocstringstream ods;
611 plaintext(ods, OutputParams(nullptr));
616 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
619 odocstringstream ods;
620 plaintext(ods, OutputParams(nullptr));
625 void InsetSpecialChar::validate(LaTeXFeatures & features) const
627 if (kind_ == ALLOWBREAK)
628 features.require("lyxzerowidthspace");
629 if (kind_ == MENU_SEPARATOR)
630 features.require("lyxarrow");
631 if (kind_ == NOBREAKDASH)
632 features.require("amsmath");
633 if (kind_ == PHRASE_LYX)
634 features.require("LyX");
638 bool InsetSpecialChar::isChar() const
640 return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
644 bool InsetSpecialChar::isLetter() const
646 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
647 || kind_ == NOBREAKDASH
648 || kind_ == PHRASE_LYX || kind_ == PHRASE_LATEX
649 || kind_ == PHRASE_TEX || kind_ == PHRASE_LATEX2E;
653 bool InsetSpecialChar::isLineSeparator() const
656 // this would be nice, but it does not work, since
657 // Paragraph::stripLeadingSpaces nukes the characters which
658 // have this property. I leave the code here, since it should
659 // eventually be made to work. (JMarc 20020327)
660 return kind_ == HYPHENATION || kind_ == ALLOWBREAK
661 || kind_ == MENU_SEPARATOR || kind_ == SLASH;