]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
a1f7fec332837ad177fc99448c7d0fd0354c1f9d
[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                         // We rely on the fact that drawLogo updates x to compute
247                         // the width without code duplication.
248                         drawLogo(pi, dim.wid, 0, kind_);
249                         break;
250         }
251         if (dim.wid == 0)
252                 dim.wid = fm.width(s);
253 }
254
255
256 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
257 {
258         FontInfo font = pi.base.font;
259
260         switch (kind_) {
261         case HYPHENATION:
262         {
263                 font.setColor(Color_special);
264                 pi.pain.text(x, y, char_type('-'), font);
265                 break;
266         }
267         case ALLOWBREAK:
268         {
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);
277                 break;
278         }
279         case LIGATURE_BREAK:
280         {
281                 font.setColor(Color_special);
282                 pi.pain.text(x, y, char_type('|'), font);
283                 break;
284         }
285         case END_OF_SENTENCE:
286         {
287                 font.setColor(Color_special);
288                 pi.pain.text(x, y, char_type('.'), font);
289                 break;
290         }
291         case LDOTS:
292         {
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.
297                  */
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);
305                 break;
306         }
307         case MENU_SEPARATOR:
308         {
309                 frontend::FontMetrics const & fm = theFontMetrics(font);
310
311                 // There is a \thinspace on each side of the triangle
312                 x += fm.em() / 6;
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);
318                 break;
319         }
320         case SLASH:
321         {
322                 font.setColor(Color_special);
323                 pi.pain.text(x, y, char_type('/'), font);
324                 break;
325         }
326         case NOBREAKDASH:
327         {
328                 font.setColor(Color_latex);
329                 pi.pain.text(x, y, char_type('-'), font);
330                 break;
331         }
332         case PHRASE_LYX:
333         case PHRASE_TEX:
334         case PHRASE_LATEX2E:
335         case PHRASE_LATEX:
336                 drawLogo(pi, x, y, kind_);
337                 break;
338         }
339 }
340
341
342 void InsetSpecialChar::write(ostream & os) const
343 {
344         string command;
345         switch (kind_) {
346         case HYPHENATION:
347                 command = "softhyphen";
348                 break;
349         case ALLOWBREAK:
350                 command = "allowbreak";
351                 break;
352         case LIGATURE_BREAK:
353                 command = "ligaturebreak";
354                 break;
355         case END_OF_SENTENCE:
356                 command = "endofsentence";
357                 break;
358         case LDOTS:
359                 command = "ldots";
360                 break;
361         case MENU_SEPARATOR:
362                 command = "menuseparator";
363                 break;
364         case SLASH:
365                 command = "breakableslash";
366                 break;
367         case NOBREAKDASH:
368                 command = "nobreakdash";
369                 break;
370         case PHRASE_LYX:
371                 command = "LyX";
372                 break;
373         case PHRASE_TEX:
374                 command = "TeX";
375                 break;
376         case PHRASE_LATEX2E:
377                 command = "LaTeX2e";
378                 break;
379         case PHRASE_LATEX:
380                 command = "LaTeX";
381                 break;
382         }
383         os << "\\SpecialChar " << command << "\n";
384 }
385
386
387 void InsetSpecialChar::read(Lexer & lex)
388 {
389         lex.next();
390         string const command = lex.getString();
391
392         if (command == "softhyphen")
393                 kind_ = HYPHENATION;
394         else if (command == "allowbreak")
395                 kind_ = 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")
401                 kind_ = LDOTS;
402         else if (command == "menuseparator")
403                 kind_ = MENU_SEPARATOR;
404         else if (command == "breakableslash")
405                 kind_ = SLASH;
406         else if (command == "nobreakdash")
407                 kind_ = NOBREAKDASH;
408         else if (command == "LyX")
409                 kind_ = PHRASE_LYX;
410         else if (command == "TeX")
411                 kind_ = PHRASE_TEX;
412         else if (command == "LaTeX2e")
413                 kind_ = PHRASE_LATEX2E;
414         else if (command == "LaTeX")
415                 kind_ = PHRASE_LATEX;
416         else
417                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
418 }
419
420
421 void InsetSpecialChar::latex(otexstream & os,
422                              OutputParams const & rp) const
423 {
424         bool const rtl = rp.local_font && rp.local_font->isRightToLeft();
425         bool const utf8 = rp.encoding->iconvName() == "UTF-8";
426         string lswitch = "";
427         string lswitche = "";
428         if (rtl && !rp.use_polyglossia) {
429                 lswitch = "\\L{";
430                 lswitche = "}";
431                 if (getLocalOrDefaultLang(rp)->lang() == "arabic_arabi"
432                     || getLocalOrDefaultLang(rp)->lang() == "farsi")
433                         lswitch = "\\textLR{";
434         }
435
436         switch (kind_) {
437         case HYPHENATION:
438                 os << "\\-";
439                 break;
440         case ALLOWBREAK:
441                 // U+200B not yet supported by utf8 inputenc
442                 os << "\\LyXZeroWidthSpace" << termcmd;
443                 break;
444         case LIGATURE_BREAK:
445                 if (utf8)
446                         // U+200C ZERO WIDTH NON-JOINER
447                         os.put(0x200c);
448                 else
449                         os << "\\textcompwordmark" << termcmd;
450                 break;
451         case END_OF_SENTENCE:
452                 os << "\\@.";
453                 break;
454         case LDOTS:
455                 os << "\\ldots" << termcmd;
456                 break;
457         case MENU_SEPARATOR:
458                 if (rtl)
459                         os << "\\lyxarrow*";
460                 else
461                         os << "\\lyxarrow";
462                 os << termcmd;
463                 break;
464         case SLASH:
465                 os << "\\slash" << termcmd;
466                 break;
467         case NOBREAKDASH:
468                 if (rp.moving_arg)
469                         os << "\\protect";
470                 os << "\\nobreakdash-";
471                 break;
472         case PHRASE_LYX:
473                 if (rp.moving_arg)
474                         os << "\\protect";
475                 os << lswitch << "\\LyX" << termcmd << lswitche;
476                 break;
477         case PHRASE_TEX:
478                 if (rp.moving_arg)
479                         os << "\\protect";
480                 os << lswitch << "\\TeX" << termcmd << lswitche;
481                 break;
482         case PHRASE_LATEX2E:
483                 if (rp.moving_arg)
484                         os << "\\protect";
485                 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
486                 break;
487         case PHRASE_LATEX:
488                 if (rp.moving_arg)
489                         os << "\\protect";
490                 os << lswitch << "\\LaTeX" << termcmd << lswitche;
491                 break;
492         }
493 }
494
495
496 int InsetSpecialChar::plaintext(odocstringstream & os,
497         OutputParams const &, size_t) const
498 {
499         switch (kind_) {
500         case HYPHENATION:
501                 return 0;
502         case ALLOWBREAK:
503                 // U+200B ZERO WIDTH SPACE (ZWSP)
504                 os.put(0x200b);
505                 return 1;
506         case LIGATURE_BREAK:
507                 // U+200C ZERO WIDTH NON-JOINER
508                 os.put(0x200c);
509                 return 1;
510         case END_OF_SENTENCE:
511                 os << '.';
512                 return 1;
513         case LDOTS:
514                 // … U+2026 HORIZONTAL ELLIPSIS
515                 os.put(0x2026);
516                 return 1;
517         case MENU_SEPARATOR:
518                 os << "->";
519                 return 2;
520         case SLASH:
521                 os << '/';
522                 return 1;
523         case NOBREAKDASH:
524                 // ‑ U+2011 NON-BREAKING HYPHEN
525                 os.put(0x2011);
526                 return 1;
527         case PHRASE_LYX:
528                 os << "LyX";
529                 return 3;
530         case PHRASE_TEX:
531                 os << "TeX";
532                 return 3;
533         case PHRASE_LATEX2E:
534                 os << "LaTeX2";
535                 // ε U+03B5 GREEK SMALL LETTER EPSILON
536                 os.put(0x03b5);
537                 return 7;
538         case PHRASE_LATEX:
539                 os << "LaTeX";
540                 return 5;
541         }
542         return 0;
543 }
544
545
546 namespace {
547 string specialCharKindToXMLEntity(InsetSpecialChar::Kind kind) {
548         switch (kind) {
549         case InsetSpecialChar::Kind::HYPHENATION:
550                 // Soft hyphen.
551                 return "&#xAD;";
552         case InsetSpecialChar::Kind::ALLOWBREAK:
553                 // Zero-width space
554                 return "&#x200B;";
555         case InsetSpecialChar::Kind::LIGATURE_BREAK:
556                 // Zero width non-joiner
557                 return "&#x200C;";
558         case InsetSpecialChar::Kind::END_OF_SENTENCE:
559                 return ".";
560         case InsetSpecialChar::Kind::LDOTS:
561                 // &hellip;
562                 return "&#x2026;";
563         case InsetSpecialChar::Kind::MENU_SEPARATOR:
564                 // &rArr;, right arrow.
565                 return "&#x21D2;";
566         case InsetSpecialChar::Kind::SLASH:
567                 // &frasl;, fractional slash.
568                 return "&#x2044;";
569         case InsetSpecialChar::Kind::NOBREAKDASH:
570                 // Non-breaking hyphen.
571                 return "&#x2011;";
572         case InsetSpecialChar::Kind::PHRASE_LYX:
573                 return "LyX";
574         case InsetSpecialChar::Kind::PHRASE_TEX:
575                 return "TeX";
576         case InsetSpecialChar::Kind::PHRASE_LATEX2E:
577                 // Lower-case epsilon.
578                 return "LaTeX2&#x03b5;";
579         case InsetSpecialChar::Kind::PHRASE_LATEX:
580                 return "LaTeX";
581         default:
582                 return "";
583         }
584 }
585 }
586
587
588 void InsetSpecialChar::docbook(XMLStream & xs, OutputParams const &) const
589 {
590         xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
591 }
592
593
594 docstring InsetSpecialChar::xhtml(XMLStream & xs, OutputParams const &) const
595 {
596         xs << XMLStream::ESCAPE_NONE << from_ascii(specialCharKindToXMLEntity(kind_));
597         return docstring();
598 }
599
600
601 void InsetSpecialChar::toString(odocstream & os) const
602 {
603         switch (kind_) {
604         case ALLOWBREAK:
605         case LIGATURE_BREAK:
606                 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
607                 // Spell checker would choke on it.
608                 return;
609         default:
610                 break;
611         }
612         odocstringstream ods;
613         plaintext(ods, OutputParams(nullptr));
614         os << ods.str();
615 }
616
617
618 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
619                                                                    bool const) const
620 {
621         odocstringstream ods;
622         plaintext(ods, OutputParams(nullptr));
623         os += ods.str();
624 }
625
626
627 void InsetSpecialChar::validate(LaTeXFeatures & features) const
628 {
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");
637 }
638
639
640 bool InsetSpecialChar::isChar() const
641 {
642         return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
643 }
644
645
646 bool InsetSpecialChar::isLetter() const
647 {
648         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
649                 || kind_ == NOBREAKDASH
650                 || kind_ == PHRASE_LYX || kind_ == PHRASE_LATEX
651                 || kind_ == PHRASE_TEX || kind_ == PHRASE_LATEX2E;
652 }
653
654
655 bool InsetSpecialChar::isLineSeparator() const
656 {
657 #if 0
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;
664 #else
665         return false;
666 #endif
667 }
668
669
670 } // namespace lyx