]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
Properly fix handling of title layouts within insets (#11787)
[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 "texstream.h"
26
27 #include "frontends/FontMetrics.h"
28 #include "frontends/NullPainter.h"
29 #include "frontends/Painter.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33
34 using namespace std;
35
36 namespace lyx {
37
38
39 InsetSpecialChar::InsetSpecialChar(Kind k)
40         : Inset(0), kind_(k)
41 {}
42
43
44 InsetSpecialChar::Kind InsetSpecialChar::kind() const
45 {
46         return kind_;
47 }
48
49
50 docstring InsetSpecialChar::toolTip(BufferView const &, int, int) const
51 {
52         docstring message;
53         switch (kind_) {
54                 case ALLOWBREAK:
55                         message = from_ascii("Optional Line Break (ZWSP)");
56                         break;
57                 case LIGATURE_BREAK:
58                         message = from_ascii("Ligature Break (ZWNJ)");
59                         break;
60                 case END_OF_SENTENCE:
61                         message = from_ascii("End of Sentence");
62                         break;
63                 case HYPHENATION:
64                         message = from_ascii("Hyphenation Point");
65                         break;
66                 case SLASH:
67                         message = from_ascii("Breakable Slash");
68                         break;
69                 case NOBREAKDASH:
70                         message = from_ascii("Protected Hyphen (SHY)");
71                         break;
72                 case LDOTS:
73                 case MENU_SEPARATOR:
74                 case PHRASE_LYX:
75                 case PHRASE_TEX:
76                 case PHRASE_LATEX2E:
77                 case PHRASE_LATEX:
78                         // no tooltip for these ones.
79                         break;
80         }
81         return message;
82 }
83
84
85 Inset::RowFlags InsetSpecialChar::rowFlags() const
86 {
87         switch (kind_) {
88         case ALLOWBREAK:
89         case HYPHENATION:
90         case SLASH:
91                 // these are the elements that allow line breaking
92                 return CanBreakAfter;
93         case NOBREAKDASH:
94         case END_OF_SENTENCE:
95         case LIGATURE_BREAK:
96         case LDOTS:
97         case MENU_SEPARATOR:
98         case PHRASE_LYX:
99         case PHRASE_TEX:
100         case PHRASE_LATEX2E:
101         case PHRASE_LATEX:
102                 break;
103         }
104         return Inline;
105 }
106
107
108 namespace {
109
110 // helper function: draw text and update x.
111 void drawChar(PainterInfo & pi, int & x, int const y, char_type ch)
112 {
113         FontInfo font = pi.base.font;
114         font.setPaintColor(pi.textColor(font.realColor()));
115         pi.pain.text(x, y, ch, font);
116         x += theFontMetrics(font).width(ch);
117 }
118
119
120 void drawLogo(PainterInfo & pi, int & x, int const y, InsetSpecialChar::Kind kind)
121 {
122         FontInfo const & font = pi.base.font;
123         int const em = theFontMetrics(font).em();
124         switch (kind) {
125         case InsetSpecialChar::PHRASE_LYX:
126                 /** Reference macro:
127                  *  \providecommand{\LyX}{L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\\@};
128                  */
129                 drawChar(pi, x, y, 'L');
130                 x -= em / 6;
131                 drawChar(pi, x, y + em / 4, 'Y');
132                 x -= em / 8;
133                 drawChar(pi, x, y, 'X');
134                 break;
135
136         case InsetSpecialChar::PHRASE_TEX: {
137                 /** Reference macro:
138                  *  \def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
139                  */
140                 int const ex = theFontMetrics(font).xHeight();
141                 drawChar(pi, x, y, 'T');
142                 x -= em / 6;
143                 drawChar(pi, x, y + ex / 2, 'E');
144                 x -= em / 8;
145                 drawChar(pi, x, y, 'X');
146                 break;
147         }
148         case InsetSpecialChar::PHRASE_LATEX2E:
149                 /** Reference macro:
150                  *  \DeclareRobustCommand{\LaTeXe}{\mbox{\m@th
151                  *    \if b\expandafter\@car\f@series\@nil\boldmath\fi
152                  *    \LaTeX\kern.15em2$_{\textstyle\varepsilon}$}}
153                  */
154                 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_LATEX);
155                 x += 3 * em / 20;
156                 drawChar(pi, x, y, '2');
157                 // ε U+03B5 GREEK SMALL LETTER EPSILON
158                 drawChar(pi, x, y + em / 4, char_type(0x03b5));
159                 break;
160
161         case InsetSpecialChar::PHRASE_LATEX: {
162                 /** Reference macro:
163                  * \DeclareRobustCommand{\LaTeX}{L\kern-.36em%
164                  *        {\sbox\z@ T%
165                  *         \vbox to\ht\z@{\hbox{\check@mathfonts
166                  *                              \fontsize\sf@size\z@
167                  *                              \math@fontsfalse\selectfont
168                  *                              A}%
169                  *                        \vss}%
170                  *        }%
171                  *        \kern-.15em%
172                  *        \TeX}
173                  */
174                 drawChar(pi, x, y, 'L');
175                 x -= 9 * em / 25;
176                 PainterInfo pi2 = pi;
177                 pi2.base.font.decSize().decSize();
178                 drawChar(pi2, x, y - em / 5, 'A');
179                 x -= 3 * em / 20;
180                 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_TEX);
181                 break;
182         }
183         default:
184                 LYXERR0("No information for drawing logo " << kind);
185         }
186 }
187
188 } // namespace
189
190
191 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
192 {
193         frontend::FontMetrics const & fm =
194                 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                         s = from_ascii(". . .");
214                         break;
215                 case MENU_SEPARATOR:
216                         // ▹  U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
217                         // There is a \thinspace on each side of the triangle
218                         dim.wid = 2 * fm.em() / 6 + fm.width(char_type(0x25B9));
219                         break;
220                 case HYPHENATION:
221                         dim.wid = fm.width(from_ascii("-"));
222                         if (dim.wid > 5)
223                                 dim.wid -= 2; // to make it look shorter
224                         break;
225                 case SLASH:
226                         s = from_ascii("/");
227                         dim.des = fm.descent(s[0]);
228                         break;
229                 case NOBREAKDASH:
230                         s = from_ascii("-");
231                         break;
232                 case PHRASE_LYX:
233                 case PHRASE_TEX:
234                 case PHRASE_LATEX2E:
235                 case PHRASE_LATEX:
236                         dim.asc = fm.maxAscent();
237                         dim.des = fm.maxDescent();
238                         frontend::NullPainter np;
239                         PainterInfo pi(mi.base.bv, np);
240                         pi.base.font = mi.base.font;
241                         drawLogo(pi, dim.wid, 0, kind_);
242                         break;
243         }
244         if (dim.wid == 0)
245                 dim.wid = fm.width(s);
246 }
247
248
249 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
250 {
251         FontInfo font = pi.base.font;
252
253         switch (kind_) {
254         case HYPHENATION:
255         {
256                 font.setColor(Color_special);
257                 pi.pain.text(x, y, char_type('-'), font);
258                 break;
259         }
260         case ALLOWBREAK:
261         {
262                 // A small vertical line
263                 int const asc = theFontMetrics(pi.base.font).xHeight();
264                 int const desc = theFontMetrics(pi.base.font).descent('g');
265                 int const x0 = x; // x + 1; // FIXME: incline,
266                 int const x1 = x; // x - 1; // similar to LibreOffice?
267                 int const y0 = y + desc;
268                 int const y1 = y - asc / 3;
269                 pi.pain.line(x0, y1, x1, y0, Color_special);
270                 break;
271         }
272         case LIGATURE_BREAK:
273         {
274                 font.setColor(Color_special);
275                 pi.pain.text(x, y, char_type('|'), font);
276                 break;
277         }
278         case END_OF_SENTENCE:
279         {
280                 font.setColor(Color_special);
281                 pi.pain.text(x, y, char_type('.'), font);
282                 break;
283         }
284         case LDOTS:
285         {
286                 font.setColor(Color_special);
287                 string ell = ". . . ";
288                 docstring dell(ell.begin(), ell.end());
289                 pi.pain.text(x, y, dell, font);
290                 break;
291         }
292         case MENU_SEPARATOR:
293         {
294                 frontend::FontMetrics const & fm =
295                         theFontMetrics(font);
296
297                 // There is a \thinspace on each side of the triangle
298                 x += fm.em() / 6;
299                 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
300                 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
301                 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
302                 font.setColor(Color_special);
303                 pi.pain.text(x, y, c, font);
304                 break;
305         }
306         case SLASH:
307         {
308                 font.setColor(Color_special);
309                 pi.pain.text(x, y, char_type('/'), font);
310                 break;
311         }
312         case NOBREAKDASH:
313         {
314                 font.setColor(Color_latex);
315                 pi.pain.text(x, y, char_type('-'), font);
316                 break;
317         }
318         case PHRASE_LYX:
319         case PHRASE_TEX:
320         case PHRASE_LATEX2E:
321         case PHRASE_LATEX:
322                 drawLogo(pi, x, y, kind_);
323                 break;
324         }
325 }
326
327
328 void InsetSpecialChar::write(ostream & os) const
329 {
330         string command;
331         switch (kind_) {
332         case HYPHENATION:
333                 command = "softhyphen";
334                 break;
335         case ALLOWBREAK:
336                 command = "allowbreak";
337                 break;
338         case LIGATURE_BREAK:
339                 command = "ligaturebreak";
340                 break;
341         case END_OF_SENTENCE:
342                 command = "endofsentence";
343                 break;
344         case LDOTS:
345                 command = "ldots";
346                 break;
347         case MENU_SEPARATOR:
348                 command = "menuseparator";
349                 break;
350         case SLASH:
351                 command = "breakableslash";
352                 break;
353         case NOBREAKDASH:
354                 command = "nobreakdash";
355                 break;
356         case PHRASE_LYX:
357                 command = "LyX";
358                 break;
359         case PHRASE_TEX:
360                 command = "TeX";
361                 break;
362         case PHRASE_LATEX2E:
363                 command = "LaTeX2e";
364                 break;
365         case PHRASE_LATEX:
366                 command = "LaTeX";
367                 break;
368         }
369         os << "\\SpecialChar " << command << "\n";
370 }
371
372
373 void InsetSpecialChar::read(Lexer & lex)
374 {
375         lex.next();
376         string const command = lex.getString();
377
378         if (command == "softhyphen")
379                 kind_ = HYPHENATION;
380         else if (command == "allowbreak")
381                 kind_ = ALLOWBREAK;
382         else if (command == "ligaturebreak")
383                 kind_ = LIGATURE_BREAK;
384         else if (command == "endofsentence")
385                 kind_ = END_OF_SENTENCE;
386         else if (command == "ldots")
387                 kind_ = LDOTS;
388         else if (command == "menuseparator")
389                 kind_ = MENU_SEPARATOR;
390         else if (command == "breakableslash")
391                 kind_ = SLASH;
392         else if (command == "nobreakdash")
393                 kind_ = NOBREAKDASH;
394         else if (command == "LyX")
395                 kind_ = PHRASE_LYX;
396         else if (command == "TeX")
397                 kind_ = PHRASE_TEX;
398         else if (command == "LaTeX2e")
399                 kind_ = PHRASE_LATEX2E;
400         else if (command == "LaTeX")
401                 kind_ = PHRASE_LATEX;
402         else
403                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
404 }
405
406
407 void InsetSpecialChar::latex(otexstream & os,
408                              OutputParams const & rp) const
409 {
410         bool const rtl = rp.local_font->isRightToLeft();
411         bool const utf8 = rp.encoding->iconvName() == "UTF-8";
412         string lswitch = "";
413         string lswitche = "";
414         if (rtl && !rp.use_polyglossia) {
415                 lswitch = "\\L{";
416                 lswitche = "}";
417                 if (rp.local_font->language()->lang() == "arabic_arabi"
418                     || rp.local_font->language()->lang() == "farsi")
419                         lswitch = "\\textLR{";
420         }
421
422         switch (kind_) {
423         case HYPHENATION:
424                 os << "\\-";
425                 break;
426         case ALLOWBREAK:
427                 // U+200B not yet supported by utf8 inputenc
428                 os << "\\LyXZeroWidthSpace" << termcmd;
429                 break;
430         case LIGATURE_BREAK:
431                 if (utf8)
432                         // U+200C ZERO WIDTH NON-JOINER
433                         os.put(0x200c);
434                 else
435                         os << "\\textcompwordmark" << termcmd;
436                 break;
437         case END_OF_SENTENCE:
438                 os << "\\@.";
439                 break;
440         case LDOTS:
441                 os << "\\ldots" << termcmd;
442                 break;
443         case MENU_SEPARATOR:
444                 if (rtl)
445                         os << "\\lyxarrow*";
446                 else
447                         os << "\\lyxarrow";
448                 os << termcmd;
449                 break;
450         case SLASH:
451                 os << "\\slash" << termcmd;
452                 break;
453         case NOBREAKDASH:
454                 if (rp.moving_arg)
455                         os << "\\protect";
456                 os << "\\nobreakdash-";
457                 break;
458         case PHRASE_LYX:
459                 if (rp.moving_arg)
460                         os << "\\protect";
461                 os << lswitch << "\\LyX" << termcmd << lswitche;
462                 break;
463         case PHRASE_TEX:
464                 if (rp.moving_arg)
465                         os << "\\protect";
466                 os << lswitch << "\\TeX" << termcmd << lswitche;
467                 break;
468         case PHRASE_LATEX2E:
469                 if (rp.moving_arg)
470                         os << "\\protect";
471                 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
472                 break;
473         case PHRASE_LATEX:
474                 if (rp.moving_arg)
475                         os << "\\protect";
476                 os << lswitch << "\\LaTeX" << termcmd << lswitche;
477                 break;
478         }
479 }
480
481
482 int InsetSpecialChar::plaintext(odocstringstream & os,
483         OutputParams const &, size_t) const
484 {
485         switch (kind_) {
486         case HYPHENATION:
487                 return 0;
488         case ALLOWBREAK:
489                 // U+200B ZERO WIDTH SPACE (ZWSP)
490                 os.put(0x200b);
491                 return 1;
492         case LIGATURE_BREAK:
493                 // U+200C ZERO WIDTH NON-JOINER
494                 os.put(0x200c);
495                 return 1;
496         case END_OF_SENTENCE:
497                 os << '.';
498                 return 1;
499         case LDOTS:
500                 // … U+2026 HORIZONTAL ELLIPSIS
501                 os.put(0x2026);
502                 return 1;
503         case MENU_SEPARATOR:
504                 os << "->";
505                 return 2;
506         case SLASH:
507                 os << '/';
508                 return 1;
509         case NOBREAKDASH:
510                 // ‑ U+2011 NON-BREAKING HYPHEN
511                 os.put(0x2011);
512                 return 1;
513         case PHRASE_LYX:
514                 os << "LyX";
515                 return 3;
516         case PHRASE_TEX:
517                 os << "TeX";
518                 return 3;
519         case PHRASE_LATEX2E:
520                 os << "LaTeX2";
521                 // ε U+03B5 GREEK SMALL LETTER EPSILON
522                 os.put(0x03b5);
523                 return 7;
524         case PHRASE_LATEX:
525                 os << "LaTeX";
526                 return 5;
527         }
528         return 0;
529 }
530
531
532 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
533 {
534         switch (kind_) {
535         case HYPHENATION:
536                 break;
537         case ALLOWBREAK:
538                 // U+200B ZERO WIDTH SPACE (ZWSP)
539                 os.put(0x200b);
540                 break;
541         case LIGATURE_BREAK:
542                 break;
543         case END_OF_SENTENCE:
544                 os << '.';
545                 break;
546         case LDOTS:
547                 os << "&hellip;";
548                 break;
549         case MENU_SEPARATOR:
550                 os << "&lyxarrow;";
551                 break;
552         case SLASH:
553                 os << '/';
554                 break;
555         case NOBREAKDASH:
556                 os << '-';
557                 break;
558         case PHRASE_LYX:
559                 os << "LyX";
560                 break;
561         case PHRASE_TEX:
562                 os << "TeX";
563                 break;
564         case PHRASE_LATEX2E:
565                 os << "LaTeX2";
566                 // ε U+03B5 GREEK SMALL LETTER EPSILON
567                 os.put(0x03b5);
568                 break;
569         case PHRASE_LATEX:
570                 os << "LaTeX";
571                 break;
572         }
573         return 0;
574 }
575
576
577 docstring InsetSpecialChar::xhtml(XMLStream & xs, OutputParams const &) const
578 {
579         switch (kind_) {
580         case HYPHENATION:
581                 break;
582         case ALLOWBREAK:
583                 xs << XMLStream::ESCAPE_NONE << "&#8203;";
584                 break;
585         case LIGATURE_BREAK:
586                 xs << XMLStream::ESCAPE_NONE << "&#8204;";
587                 break;
588         case END_OF_SENTENCE:
589                 xs << '.';
590                 break;
591         case LDOTS:
592                 xs << XMLStream::ESCAPE_NONE << "&hellip;";
593                 break;
594         case MENU_SEPARATOR:
595                 xs << XMLStream::ESCAPE_NONE << "&rArr;";
596                 break;
597         case SLASH:
598                 xs << XMLStream::ESCAPE_NONE << "&frasl;";
599                 break;
600         case NOBREAKDASH:
601                 xs << XMLStream::ESCAPE_NONE << "&#8209;";
602                 break;
603         case PHRASE_LYX:
604                 xs << "LyX";
605                 break;
606         case PHRASE_TEX:
607                 xs << "TeX";
608                 break;
609         case PHRASE_LATEX2E:
610                 xs << "LaTeX2" << XMLStream::ESCAPE_NONE << "&#x3b5;";
611                 break;
612         case PHRASE_LATEX:
613                 xs << "LaTeX";
614                 break;
615         }
616         return docstring();
617 }
618
619
620 void InsetSpecialChar::toString(odocstream & os) const
621 {
622         switch (kind_) {
623         case ALLOWBREAK:
624         case LIGATURE_BREAK:
625                 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
626                 // Spell checker would choke on it.
627                 return;
628         default:
629                 break;
630         }
631         odocstringstream ods;
632         plaintext(ods, OutputParams(0));
633         os << ods.str();
634 }
635
636
637 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
638                                                                    bool const) const
639 {
640         odocstringstream ods;
641         plaintext(ods, OutputParams(0));
642         os += ods.str();
643 }
644
645
646 void InsetSpecialChar::validate(LaTeXFeatures & features) const
647 {
648         if (kind_ == ALLOWBREAK)
649                 features.require("lyxzerowidthspace");
650         if (kind_ == MENU_SEPARATOR)
651                 features.require("lyxarrow");
652         if (kind_ == NOBREAKDASH)
653                 features.require("amsmath");
654         if (kind_ == PHRASE_LYX)
655                 features.require("LyX");
656 }
657
658
659 bool InsetSpecialChar::isChar() const
660 {
661         return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
662 }
663
664
665 bool InsetSpecialChar::isLetter() const
666 {
667         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
668                 || kind_ == NOBREAKDASH;
669 }
670
671
672 bool InsetSpecialChar::isLineSeparator() const
673 {
674 #if 0
675         // this would be nice, but it does not work, since
676         // Paragraph::stripLeadingSpaces nukes the characters which
677         // have this property. I leave the code here, since it should
678         // eventually be made to work. (JMarc 20020327)
679         return kind_ == HYPHENATION || kind_ == ALLOWBREAK
680             || kind_ == MENU_SEPARATOR || kind_ == SLASH;
681 #else
682         return false;
683 #endif
684 }
685
686
687 } // namespace lyx