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