]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
Less expensive OP first as this might be called often.
[lyx.git] / src / insets / InsetSpecialChar.cpp
1 /**
2  * \file InsetSpecialChar.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Jean-Marc Lasgouttes
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetSpecialChar.h"
16
17 #include "Dimension.h"
18 #include "Encoding.h"
19 #include "Font.h"
20 #include "Language.h"
21 #include "LaTeXFeatures.h"
22 #include "Lexer.h"
23 #include "MetricsInfo.h"
24 #include "output_xhtml.h"
25 #include "xml.h"
26 #include "texstream.h"
27
28 #include "frontends/FontMetrics.h"
29 #include "frontends/NullPainter.h"
30 #include "frontends/Painter.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34
35 using namespace std;
36
37 namespace lyx {
38
39
40 InsetSpecialChar::InsetSpecialChar(Kind k)
41         : Inset(nullptr), kind_(k)
42 {}
43
44
45 InsetSpecialChar::Kind InsetSpecialChar::kind() const
46 {
47         return kind_;
48 }
49
50
51 docstring InsetSpecialChar::toolTip(BufferView const &, int, int) const
52 {
53         docstring message;
54         switch (kind_) {
55                 case ALLOWBREAK:
56                         message = from_ascii("Optional Line Break (ZWSP)");
57                         break;
58                 case LIGATURE_BREAK:
59                         message = from_ascii("Ligature Break (ZWNJ)");
60                         break;
61                 case END_OF_SENTENCE:
62                         message = from_ascii("End of Sentence");
63                         break;
64                 case HYPHENATION:
65                         message = from_ascii("Hyphenation Point");
66                         break;
67                 case SLASH:
68                         message = from_ascii("Breakable Slash");
69                         break;
70                 case NOBREAKDASH:
71                         message = from_ascii("Protected Hyphen (SHY)");
72                         break;
73                 case LDOTS:
74                 case MENU_SEPARATOR:
75                 case PHRASE_LYX:
76                 case PHRASE_TEX:
77                 case PHRASE_LATEX2E:
78                 case PHRASE_LATEX:
79                         // no tooltip for these ones.
80                         break;
81         }
82         return message;
83 }
84
85
86 int InsetSpecialChar::rowFlags() const
87 {
88         switch (kind_) {
89         case ALLOWBREAK:
90         case HYPHENATION:
91         case SLASH:
92                 // these are the elements that allow line breaking
93                 return CanBreakAfter;
94         case NOBREAKDASH:
95         case END_OF_SENTENCE:
96         case LIGATURE_BREAK:
97         case LDOTS:
98         case MENU_SEPARATOR:
99         case PHRASE_LYX:
100         case PHRASE_TEX:
101         case PHRASE_LATEX2E:
102         case PHRASE_LATEX:
103                 break;
104         }
105         return Inline;
106 }
107
108
109 namespace {
110
111 // helper function: draw text and update x.
112 void drawChar(PainterInfo & pi, int & x, int const y, char_type ch)
113 {
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);
118 }
119
120
121 void drawLogo(PainterInfo & pi, int & x, int const y, InsetSpecialChar::Kind kind)
122 {
123         FontInfo const & font = pi.base.font;
124         int const em = theFontMetrics(font).em();
125         switch (kind) {
126         case InsetSpecialChar::PHRASE_LYX:
127                 /** Reference macro:
128                  *  \providecommand{\LyX}{L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\\@};
129                  */
130                 drawChar(pi, x, y, 'L');
131                 x -= em / 6;
132                 drawChar(pi, x, y + em / 4, 'Y');
133                 x -= em / 8;
134                 drawChar(pi, x, y, 'X');
135                 break;
136
137         case InsetSpecialChar::PHRASE_TEX: {
138                 /** Reference macro:
139                  *  \def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
140                  */
141                 int const ex = theFontMetrics(font).xHeight();
142                 drawChar(pi, x, y, 'T');
143                 x -= em / 6;
144                 drawChar(pi, x, y + ex / 2, 'E');
145                 x -= em / 8;
146                 drawChar(pi, x, y, 'X');
147                 break;
148         }
149         case InsetSpecialChar::PHRASE_LATEX2E:
150                 /** Reference macro:
151                  *  \DeclareRobustCommand{\LaTeXe}{\mbox{\m@th
152                  *    \if b\expandafter\@car\f@series\@nil\boldmath\fi
153                  *    \LaTeX\kern.15em2$_{\textstyle\varepsilon}$}}
154                  */
155                 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_LATEX);
156                 x += 3 * em / 20;
157                 drawChar(pi, x, y, '2');
158                 // ε U+03B5 GREEK SMALL LETTER EPSILON
159                 drawChar(pi, x, y + em / 4, char_type(0x03b5));
160                 break;
161
162         case InsetSpecialChar::PHRASE_LATEX: {
163                 /** Reference macro:
164                  * \DeclareRobustCommand{\LaTeX}{L\kern-.36em%
165                  *        {\sbox\z@ T%
166                  *         \vbox to\ht\z@{\hbox{\check@mathfonts
167                  *                              \fontsize\sf@size\z@
168                  *                              \math@fontsfalse\selectfont
169                  *                              A}%
170                  *                        \vss}%
171                  *        }%
172                  *        \kern-.15em%
173                  *        \TeX}
174                  */
175                 drawChar(pi, x, y, 'L');
176                 x -= 9 * em / 25;
177                 PainterInfo pi2 = pi;
178                 pi2.base.font.decSize().decSize();
179                 drawChar(pi2, x, y - em / 5, 'A');
180                 x -= 3 * em / 20;
181                 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_TEX);
182                 break;
183         }
184         default:
185                 LYXERR0("No information for drawing logo " << kind);
186         }
187 }
188
189 } // namespace
190
191
192 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
193 {
194         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
195         dim.asc = fm.maxAscent();
196         dim.des = 0;
197         dim.wid = 0;
198
199         docstring s;
200         switch (kind_) {
201                 case ALLOWBREAK:
202                         dim.asc = fm.xHeight();
203                         dim.des = fm.descent('g');
204                         dim.wid = fm.em() / 8;
205                         break;
206                 case LIGATURE_BREAK:
207                         s = from_ascii("|");
208                         break;
209                 case END_OF_SENTENCE:
210                         s = from_ascii(".");
211                         break;
212                 case LDOTS: {
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;
218                         break;
219                 }
220                 case MENU_SEPARATOR:
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));
224                         break;
225                 case HYPHENATION:
226                         dim.wid = fm.width(from_ascii("-"));
227                         if (dim.wid > 5)
228                                 dim.wid -= 2; // to make it look shorter
229                         break;
230                 case SLASH:
231                         s = from_ascii("/");
232                         dim.des = fm.descent(s[0]);
233                         break;
234                 case NOBREAKDASH:
235                         s = from_ascii("-");
236                         break;
237                 case PHRASE_LYX:
238                 case PHRASE_TEX:
239                 case PHRASE_LATEX2E:
240                 case PHRASE_LATEX:
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_);
247                         break;
248         }
249         if (dim.wid == 0)
250                 dim.wid = fm.width(s);
251 }
252
253
254 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
255 {
256         FontInfo font = pi.base.font;
257
258         switch (kind_) {
259         case HYPHENATION:
260         {
261                 font.setColor(Color_special);
262                 pi.pain.text(x, y, char_type('-'), font);
263                 break;
264         }
265         case ALLOWBREAK:
266         {
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);
275                 break;
276         }
277         case LIGATURE_BREAK:
278         {
279                 font.setColor(Color_special);
280                 pi.pain.text(x, y, char_type('|'), font);
281                 break;
282         }
283         case END_OF_SENTENCE:
284         {
285                 font.setColor(Color_special);
286                 pi.pain.text(x, y, char_type('.'), font);
287                 break;
288         }
289         case LDOTS:
290         {
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.
295                  */
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);
303                 break;
304         }
305         case MENU_SEPARATOR:
306         {
307                 frontend::FontMetrics const & fm = theFontMetrics(font);
308
309                 // There is a \thinspace on each side of the triangle
310                 x += fm.em() / 6;
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);
316                 break;
317         }
318         case SLASH:
319         {
320                 font.setColor(Color_special);
321                 pi.pain.text(x, y, char_type('/'), font);
322                 break;
323         }
324         case NOBREAKDASH:
325         {
326                 font.setColor(Color_latex);
327                 pi.pain.text(x, y, char_type('-'), font);
328                 break;
329         }
330         case PHRASE_LYX:
331         case PHRASE_TEX:
332         case PHRASE_LATEX2E:
333         case PHRASE_LATEX:
334                 drawLogo(pi, x, y, kind_);
335                 break;
336         }
337 }
338
339
340 void InsetSpecialChar::write(ostream & os) const
341 {
342         string command;
343         switch (kind_) {
344         case HYPHENATION:
345                 command = "softhyphen";
346                 break;
347         case ALLOWBREAK:
348                 command = "allowbreak";
349                 break;
350         case LIGATURE_BREAK:
351                 command = "ligaturebreak";
352                 break;
353         case END_OF_SENTENCE:
354                 command = "endofsentence";
355                 break;
356         case LDOTS:
357                 command = "ldots";
358                 break;
359         case MENU_SEPARATOR:
360                 command = "menuseparator";
361                 break;
362         case SLASH:
363                 command = "breakableslash";
364                 break;
365         case NOBREAKDASH:
366                 command = "nobreakdash";
367                 break;
368         case PHRASE_LYX:
369                 command = "LyX";
370                 break;
371         case PHRASE_TEX:
372                 command = "TeX";
373                 break;
374         case PHRASE_LATEX2E:
375                 command = "LaTeX2e";
376                 break;
377         case PHRASE_LATEX:
378                 command = "LaTeX";
379                 break;
380         }
381         os << "\\SpecialChar " << command << "\n";
382 }
383
384
385 void InsetSpecialChar::read(Lexer & lex)
386 {
387         lex.next();
388         string const command = lex.getString();
389
390         if (command == "softhyphen")
391                 kind_ = HYPHENATION;
392         else if (command == "allowbreak")
393                 kind_ = 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")
399                 kind_ = LDOTS;
400         else if (command == "menuseparator")
401                 kind_ = MENU_SEPARATOR;
402         else if (command == "breakableslash")
403                 kind_ = SLASH;
404         else if (command == "nobreakdash")
405                 kind_ = NOBREAKDASH;
406         else if (command == "LyX")
407                 kind_ = PHRASE_LYX;
408         else if (command == "TeX")
409                 kind_ = PHRASE_TEX;
410         else if (command == "LaTeX2e")
411                 kind_ = PHRASE_LATEX2E;
412         else if (command == "LaTeX")
413                 kind_ = PHRASE_LATEX;
414         else
415                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
416 }
417
418
419 void InsetSpecialChar::latex(otexstream & os,
420                              OutputParams const & rp) const
421 {
422         bool const rtl = rp.local_font->isRightToLeft();
423         bool const utf8 = rp.encoding->iconvName() == "UTF-8";
424         string lswitch = "";
425         string lswitche = "";
426         if (rtl && !rp.use_polyglossia) {
427                 lswitch = "\\L{";
428                 lswitche = "}";
429                 if (rp.local_font->language()->lang() == "arabic_arabi"
430                     || rp.local_font->language()->lang() == "farsi")
431                         lswitch = "\\textLR{";
432         }
433
434         switch (kind_) {
435         case HYPHENATION:
436                 os << "\\-";
437                 break;
438         case ALLOWBREAK:
439                 // U+200B not yet supported by utf8 inputenc
440                 os << "\\LyXZeroWidthSpace" << termcmd;
441                 break;
442         case LIGATURE_BREAK:
443                 if (utf8)
444                         // U+200C ZERO WIDTH NON-JOINER
445                         os.put(0x200c);
446                 else
447                         os << "\\textcompwordmark" << termcmd;
448                 break;
449         case END_OF_SENTENCE:
450                 os << "\\@.";
451                 break;
452         case LDOTS:
453                 os << "\\ldots" << termcmd;
454                 break;
455         case MENU_SEPARATOR:
456                 if (rtl)
457                         os << "\\lyxarrow*";
458                 else
459                         os << "\\lyxarrow";
460                 os << termcmd;
461                 break;
462         case SLASH:
463                 os << "\\slash" << termcmd;
464                 break;
465         case NOBREAKDASH:
466                 if (rp.moving_arg)
467                         os << "\\protect";
468                 os << "\\nobreakdash-";
469                 break;
470         case PHRASE_LYX:
471                 if (rp.moving_arg)
472                         os << "\\protect";
473                 os << lswitch << "\\LyX" << termcmd << lswitche;
474                 break;
475         case PHRASE_TEX:
476                 if (rp.moving_arg)
477                         os << "\\protect";
478                 os << lswitch << "\\TeX" << termcmd << lswitche;
479                 break;
480         case PHRASE_LATEX2E:
481                 if (rp.moving_arg)
482                         os << "\\protect";
483                 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
484                 break;
485         case PHRASE_LATEX:
486                 if (rp.moving_arg)
487                         os << "\\protect";
488                 os << lswitch << "\\LaTeX" << termcmd << lswitche;
489                 break;
490         }
491 }
492
493
494 int InsetSpecialChar::plaintext(odocstringstream & os,
495         OutputParams const &, size_t) const
496 {
497         switch (kind_) {
498         case HYPHENATION:
499                 return 0;
500         case ALLOWBREAK:
501                 // U+200B ZERO WIDTH SPACE (ZWSP)
502                 os.put(0x200b);
503                 return 1;
504         case LIGATURE_BREAK:
505                 // U+200C ZERO WIDTH NON-JOINER
506                 os.put(0x200c);
507                 return 1;
508         case END_OF_SENTENCE:
509                 os << '.';
510                 return 1;
511         case LDOTS:
512                 // … U+2026 HORIZONTAL ELLIPSIS
513                 os.put(0x2026);
514                 return 1;
515         case MENU_SEPARATOR:
516                 os << "->";
517                 return 2;
518         case SLASH:
519                 os << '/';
520                 return 1;
521         case NOBREAKDASH:
522                 // ‑ U+2011 NON-BREAKING HYPHEN
523                 os.put(0x2011);
524                 return 1;
525         case PHRASE_LYX:
526                 os << "LyX";
527                 return 3;
528         case PHRASE_TEX:
529                 os << "TeX";
530                 return 3;
531         case PHRASE_LATEX2E:
532                 os << "LaTeX2";
533                 // ε U+03B5 GREEK SMALL LETTER EPSILON
534                 os.put(0x03b5);
535                 return 7;
536         case PHRASE_LATEX:
537                 os << "LaTeX";
538                 return 5;
539         }
540         return 0;
541 }
542
543
544 namespace {
545 string specialCharKindToXMLEntity(InsetSpecialChar::Kind kind) {
546         switch (kind) {
547         case InsetSpecialChar::Kind::HYPHENATION:
548                 // Soft hyphen.
549                 return "&#xAD;";
550         case InsetSpecialChar::Kind::ALLOWBREAK:
551                 // Zero-width space
552                 return "&#x200B;";
553         case InsetSpecialChar::Kind::LIGATURE_BREAK:
554                 // Zero width non-joiner
555                 return "&#x200C;";
556         case InsetSpecialChar::Kind::END_OF_SENTENCE:
557                 return ".";
558         case InsetSpecialChar::Kind::LDOTS:
559                 // &hellip;
560                 return "&#x2026;";
561         case InsetSpecialChar::Kind::MENU_SEPARATOR:
562                 // &rArr;, right arrow.
563                 return "&#x21D2;";
564         case InsetSpecialChar::Kind::SLASH:
565                 // &frasl;, fractional slash.
566                 return "&#x2044;";
567         case InsetSpecialChar::Kind::NOBREAKDASH:
568                 // Non-breaking hyphen.
569                 return "&#x2011;";
570         case InsetSpecialChar::Kind::PHRASE_LYX:
571                 return "LyX";
572         case InsetSpecialChar::Kind::PHRASE_TEX:
573                 return "TeX";
574         case InsetSpecialChar::Kind::PHRASE_LATEX2E:
575                 // Lower-case epsilon.
576                 return "LaTeX2&#x03b5;";
577         case InsetSpecialChar::Kind::PHRASE_LATEX:
578                 return "LaTeX";
579         default:
580                 return "";
581         }
582 }
583 }
584
585
586 void InsetSpecialChar::docbook(XMLStream & xs, OutputParams const &) const
587 {
588         xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
589 }
590
591
592 docstring InsetSpecialChar::xhtml(XMLStream & xs, OutputParams const &) const
593 {
594         xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
595         return docstring();
596 }
597
598
599 void InsetSpecialChar::toString(odocstream & os) const
600 {
601         switch (kind_) {
602         case ALLOWBREAK:
603         case LIGATURE_BREAK:
604                 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
605                 // Spell checker would choke on it.
606                 return;
607         default:
608                 break;
609         }
610         odocstringstream ods;
611         plaintext(ods, OutputParams(nullptr));
612         os << ods.str();
613 }
614
615
616 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
617                                                                    bool const) const
618 {
619         odocstringstream ods;
620         plaintext(ods, OutputParams(nullptr));
621         os += ods.str();
622 }
623
624
625 void InsetSpecialChar::validate(LaTeXFeatures & features) const
626 {
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");
635 }
636
637
638 bool InsetSpecialChar::isChar() const
639 {
640         return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
641 }
642
643
644 bool InsetSpecialChar::isLetter() const
645 {
646         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
647                 || kind_ == NOBREAKDASH
648                 || kind_ == PHRASE_LYX || kind_ == PHRASE_LATEX
649                 || kind_ == PHRASE_TEX || kind_ == PHRASE_LATEX2E;
650 }
651
652
653 bool InsetSpecialChar::isLineSeparator() const
654 {
655 #if 0
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;
662 #else
663         return false;
664 #endif
665 }
666
667
668 } // namespace lyx