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