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 dim.wid = 3 * fm.width(char_type('.'))
215 + 3 * fm.width(char_type(' ')) / 2;
218 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
219 // There is a \thinspace on each side of the triangle
220 dim.wid = 2 * fm.em() / 6 + fm.width(char_type(0x25B9));
223 dim.wid = fm.width(from_ascii("-"));
225 dim.wid -= 2; // to make it look shorter
229 dim.des = fm.descent(s[0]);
238 dim.asc = fm.maxAscent();
239 dim.des = fm.maxDescent();
240 frontend::NullPainter np;
241 PainterInfo pi(mi.base.bv, np);
242 pi.base.font = mi.base.font;
243 drawLogo(pi, dim.wid, 0, kind_);
247 dim.wid = fm.width(s);
251 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
253 FontInfo font = pi.base.font;
258 font.setColor(Color_special);
259 pi.pain.text(x, y, char_type('-'), font);
264 // A small vertical line
265 int const asc = theFontMetrics(pi.base.font).xHeight();
266 int const desc = theFontMetrics(pi.base.font).descent('g');
267 int const x0 = x; // x + 1; // FIXME: incline,
268 int const x1 = x; // x - 1; // similar to LibreOffice?
269 int const y0 = y + desc;
270 int const y1 = y - asc / 3;
271 pi.pain.line(x0, y1, x1, y0, Color_special);
276 font.setColor(Color_special);
277 pi.pain.text(x, y, char_type('|'), font);
280 case END_OF_SENTENCE:
282 font.setColor(Color_special);
283 pi.pain.text(x, y, char_type('.'), font);
288 font.setColor(Color_special);
289 /* \textellipsis uses a \fontdimen3 is spacing. The TeXbook
290 * tells us that \fontdimen3 is the interword stretch, and
291 * that this is usually half a space.
293 frontend::FontMetrics const & fm = theFontMetrics(font);
294 int wid1 = fm.width(char_type('.')) + fm.width(char_type(' ')) / 2;
295 pi.pain.text(x, y, char_type('.'), font);
296 pi.pain.text(x + wid1, y, char_type('.'), font);
297 pi.pain.text(x + 2 * wid1, y, char_type('.'), font);
302 frontend::FontMetrics const & fm = theFontMetrics(font);
304 // There is a \thinspace on each side of the triangle
306 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
307 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
308 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
309 font.setColor(Color_special);
310 pi.pain.text(x, y, c, font);
315 font.setColor(Color_special);
316 pi.pain.text(x, y, char_type('/'), font);
321 font.setColor(Color_latex);
322 pi.pain.text(x, y, char_type('-'), font);
329 drawLogo(pi, x, y, kind_);
335 void InsetSpecialChar::write(ostream & os) const
340 command = "softhyphen";
343 command = "allowbreak";
346 command = "ligaturebreak";
348 case END_OF_SENTENCE:
349 command = "endofsentence";
355 command = "menuseparator";
358 command = "breakableslash";
361 command = "nobreakdash";
376 os << "\\SpecialChar " << command << "\n";
380 void InsetSpecialChar::read(Lexer & lex)
383 string const command = lex.getString();
385 if (command == "softhyphen")
387 else if (command == "allowbreak")
389 else if (command == "ligaturebreak")
390 kind_ = LIGATURE_BREAK;
391 else if (command == "endofsentence")
392 kind_ = END_OF_SENTENCE;
393 else if (command == "ldots")
395 else if (command == "menuseparator")
396 kind_ = MENU_SEPARATOR;
397 else if (command == "breakableslash")
399 else if (command == "nobreakdash")
401 else if (command == "LyX")
403 else if (command == "TeX")
405 else if (command == "LaTeX2e")
406 kind_ = PHRASE_LATEX2E;
407 else if (command == "LaTeX")
408 kind_ = PHRASE_LATEX;
410 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
414 void InsetSpecialChar::latex(otexstream & os,
415 OutputParams const & rp) const
417 bool const rtl = rp.local_font->isRightToLeft();
418 bool const utf8 = rp.encoding->iconvName() == "UTF-8";
420 string lswitche = "";
421 if (rtl && !rp.use_polyglossia) {
424 if (rp.local_font->language()->lang() == "arabic_arabi"
425 || rp.local_font->language()->lang() == "farsi")
426 lswitch = "\\textLR{";
434 // U+200B not yet supported by utf8 inputenc
435 os << "\\LyXZeroWidthSpace" << termcmd;
439 // U+200C ZERO WIDTH NON-JOINER
442 os << "\\textcompwordmark" << termcmd;
444 case END_OF_SENTENCE:
448 os << "\\ldots" << termcmd;
458 os << "\\slash" << termcmd;
463 os << "\\nobreakdash-";
468 os << lswitch << "\\LyX" << termcmd << lswitche;
473 os << lswitch << "\\TeX" << termcmd << lswitche;
478 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
483 os << lswitch << "\\LaTeX" << termcmd << lswitche;
489 int InsetSpecialChar::plaintext(odocstringstream & os,
490 OutputParams const &, size_t) const
496 // U+200B ZERO WIDTH SPACE (ZWSP)
500 // U+200C ZERO WIDTH NON-JOINER
503 case END_OF_SENTENCE:
507 // … U+2026 HORIZONTAL ELLIPSIS
517 // ‑ U+2011 NON-BREAKING HYPHEN
528 // ε U+03B5 GREEK SMALL LETTER EPSILON
540 string specialCharKindToXMLEntity(InsetSpecialChar::Kind kind) {
542 case InsetSpecialChar::Kind::HYPHENATION:
545 case InsetSpecialChar::Kind::ALLOWBREAK:
548 case InsetSpecialChar::Kind::LIGATURE_BREAK:
549 // Zero width non-joiner
551 case InsetSpecialChar::Kind::END_OF_SENTENCE:
553 case InsetSpecialChar::Kind::LDOTS:
556 case InsetSpecialChar::Kind::MENU_SEPARATOR:
557 // ⇒, right arrow.
559 case InsetSpecialChar::Kind::SLASH:
560 // ⁄, fractional slash.
562 case InsetSpecialChar::Kind::NOBREAKDASH:
563 // Non-breaking hyphen.
565 case InsetSpecialChar::Kind::PHRASE_LYX:
567 case InsetSpecialChar::Kind::PHRASE_TEX:
569 case InsetSpecialChar::Kind::PHRASE_LATEX2E:
570 // Lower-case epsilon.
571 return "LaTeX2ε";
572 case InsetSpecialChar::Kind::PHRASE_LATEX:
581 void InsetSpecialChar::docbook(XMLStream & xs, OutputParams const &) const
583 xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
587 docstring InsetSpecialChar::xhtml(XMLStream & xs, OutputParams const &) const
589 xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
594 void InsetSpecialChar::toString(odocstream & os) const
599 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
600 // Spell checker would choke on it.
605 odocstringstream ods;
606 plaintext(ods, OutputParams(nullptr));
611 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
614 odocstringstream ods;
615 plaintext(ods, OutputParams(nullptr));
620 void InsetSpecialChar::validate(LaTeXFeatures & features) const
622 if (kind_ == ALLOWBREAK)
623 features.require("lyxzerowidthspace");
624 if (kind_ == MENU_SEPARATOR)
625 features.require("lyxarrow");
626 if (kind_ == NOBREAKDASH)
627 features.require("amsmath");
628 if (kind_ == PHRASE_LYX)
629 features.require("LyX");
633 bool InsetSpecialChar::isChar() const
635 return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
639 bool InsetSpecialChar::isLetter() const
641 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
642 || kind_ == NOBREAKDASH
643 || kind_ == PHRASE_LYX || kind_ == PHRASE_LATEX
644 || kind_ == PHRASE_TEX || kind_ == PHRASE_LATEX2E;
648 bool InsetSpecialChar::isLineSeparator() const
651 // this would be nice, but it does not work, since
652 // Paragraph::stripLeadingSpaces nukes the characters which
653 // have this property. I leave the code here, since it should
654 // eventually be made to work. (JMarc 20020327)
655 return kind_ == HYPHENATION || kind_ == ALLOWBREAK
656 || kind_ == MENU_SEPARATOR || kind_ == SLASH;