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