]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
Use context-sensitive command termination
[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
90 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
91 {
92         frontend::FontMetrics const & fm =
93                 theFontMetrics(mi.base.font);
94         dim.asc = fm.maxAscent();
95         dim.des = fm.maxDescent();
96         dim.wid = 0;
97
98         docstring s;
99         switch (kind_) {
100                 case LIGATURE_BREAK:
101                         s = from_ascii("|");
102                         break;
103                 case END_OF_SENTENCE:
104                         s = from_ascii(".");
105                         break;
106                 case LDOTS:
107                         s = from_ascii(". . .");
108                         break;
109                 case MENU_SEPARATOR:
110                         s = from_ascii(" x ");
111                         break;
112                 case HYPHENATION:
113                         dim.wid = fm.width(from_ascii("-"));
114                         if (dim.wid > 5)
115                                 dim.wid -= 2; // to make it look shorter
116                         break;
117                 case SLASH:
118                         s = from_ascii("/");
119                         break;
120                 case NOBREAKDASH:
121                         s = from_ascii("-");
122                         break;
123                 case PHRASE_LYX:
124                 case PHRASE_TEX:
125                 case PHRASE_LATEX2E:
126                 case PHRASE_LATEX:
127                         dim.wid = logoWidth(mi.base.font, kind_);
128                         break;
129         }
130         if (dim.wid == 0)
131                 dim.wid = fm.width(s);
132 }
133
134
135 namespace {
136
137 // helper function: draw text and update x.
138 void drawChar(PainterInfo & pi, int & x, int const y, char_type ch)
139 {
140         FontInfo font = pi.base.font;
141         font.setPaintColor(pi.textColor(font.realColor()));
142         pi.pain.text(x, y, ch, font);
143         x += theFontMetrics(font).width(ch);
144 }
145
146
147 void drawLogo(PainterInfo & pi, int & x, int const y, InsetSpecialChar::Kind kind)
148 {
149         FontInfo const & font = pi.base.font;
150         int const em = theFontMetrics(font).em();
151         switch (kind) {
152         case InsetSpecialChar::PHRASE_LYX:
153                 /** Reference macro:
154                  *  \providecommand{\LyX}{L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\\@};
155                  */
156                 drawChar(pi, x, y, 'L');
157                 x -= em / 6;
158                 drawChar(pi, x, y + em / 4, 'Y');
159                 x -= em / 8;
160                 drawChar(pi, x, y, 'X');
161                 break;
162
163         case InsetSpecialChar::PHRASE_TEX: {
164                 /** Reference macro:
165                  *  \def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
166                  */
167                 int const ex = theFontMetrics(font).ascent('x');
168                 drawChar(pi, x, y, 'T');
169                 x -= em / 6;
170                 drawChar(pi, x, y + ex / 2, 'E');
171                 x -= em / 8;
172                 drawChar(pi, x, y, 'X');
173                 break;
174         }
175         case InsetSpecialChar::PHRASE_LATEX2E:
176                 /** Reference macro:
177                  *  \DeclareRobustCommand{\LaTeXe}{\mbox{\m@th
178                  *    \if b\expandafter\@car\f@series\@nil\boldmath\fi
179                  *    \LaTeX\kern.15em2$_{\textstyle\varepsilon}$}}
180                  */
181                 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_LATEX);
182                 x += 3 * em / 20;
183                 drawChar(pi, x, y, '2');
184                 drawChar(pi, x, y + em / 4, char_type(0x03b5));
185                 break;
186
187         case InsetSpecialChar::PHRASE_LATEX: {
188                 /** Reference macro:
189                  * \DeclareRobustCommand{\LaTeX}{L\kern-.36em%
190                  *        {\sbox\z@ T%
191                  *         \vbox to\ht\z@{\hbox{\check@mathfonts
192                  *                              \fontsize\sf@size\z@
193                  *                              \math@fontsfalse\selectfont
194                  *                              A}%
195                  *                        \vss}%
196                  *        }%
197                  *        \kern-.15em%
198                  *        \TeX}
199                  */
200                 drawChar(pi, x, y, 'L');
201                 x -= 9 * em / 25;
202                 PainterInfo pi2 = pi;
203                 pi2.base.font.decSize().decSize();
204                 drawChar(pi2, x, y - em / 5, 'A');
205                 x -= 3 * em / 20;
206                 drawLogo(pi, x, y, InsetSpecialChar::PHRASE_TEX);
207                 break;
208         }
209         default:
210                 LYXERR0("No information for drawing logo " << kind);
211         }
212 }
213
214 }
215
216 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
217 {
218         FontInfo font = pi.base.font;
219
220         switch (kind_) {
221         case HYPHENATION:
222         {
223                 font.setColor(Color_special);
224                 pi.pain.text(x, y, char_type('-'), font);
225                 break;
226         }
227         case LIGATURE_BREAK:
228         {
229                 font.setColor(Color_special);
230                 pi.pain.text(x, y, char_type('|'), font);
231                 break;
232         }
233         case END_OF_SENTENCE:
234         {
235                 font.setColor(Color_special);
236                 pi.pain.text(x, y, char_type('.'), font);
237                 break;
238         }
239         case LDOTS:
240         {
241                 font.setColor(Color_special);
242                 string ell = ". . . ";
243                 docstring dell(ell.begin(), ell.end());
244                 pi.pain.text(x, y, dell, font);
245                 break;
246         }
247         case MENU_SEPARATOR:
248         {
249                 frontend::FontMetrics const & fm =
250                         theFontMetrics(font);
251
252                 // A triangle the width and height of an 'x'
253                 int w = fm.width(char_type('x'));
254                 int ox = fm.width(char_type(' ')) + x;
255                 int h = fm.ascent(char_type('x'));
256                 int xp[4], yp[4];
257
258                 xp[0] = ox;     yp[0] = y;
259                 xp[1] = ox;     yp[1] = y - h;
260                 xp[2] = ox + w; yp[2] = y - h/2;
261                 xp[3] = ox;     yp[3] = y;
262
263                 pi.pain.lines(xp, yp, 4, Color_special);
264                 break;
265         }
266         case SLASH:
267         {
268                 font.setColor(Color_special);
269                 pi.pain.text(x, y, char_type('/'), font);
270                 break;
271         }
272         case NOBREAKDASH:
273         {
274                 font.setColor(Color_latex);
275                 pi.pain.text(x, y, char_type('-'), font);
276                 break;
277         }
278         case PHRASE_LYX:
279         case PHRASE_TEX:
280         case PHRASE_LATEX2E:
281         case PHRASE_LATEX:
282                 drawLogo(pi, x, y, kind_);
283                 break;
284         }
285 }
286
287
288 // In lyxf3 this will be just LaTeX
289 void InsetSpecialChar::write(ostream & os) const
290 {
291         string command;
292         switch (kind_) {
293         case HYPHENATION:
294                 command = "softhyphen";
295                 break;
296         case LIGATURE_BREAK:
297                 command = "ligaturebreak";
298                 break;
299         case END_OF_SENTENCE:
300                 command = "endofsentence";
301                 break;
302         case LDOTS:
303                 command = "ldots";
304                 break;
305         case MENU_SEPARATOR:
306                 command = "menuseparator";
307                 break;
308         case SLASH:
309                 command = "breakableslash";
310                 break;
311         case NOBREAKDASH:
312                 command = "nobreakdash";
313                 break;
314         case PHRASE_LYX:
315                 command = "LyX";
316                 break;
317         case PHRASE_TEX:
318                 command = "TeX";
319                 break;
320         case PHRASE_LATEX2E:
321                 command = "LaTeX2e";
322                 break;
323         case PHRASE_LATEX:
324                 command = "LaTeX";
325                 break;
326         }
327         os << "\\SpecialChar " << command << "\n";
328 }
329
330
331 // This function will not be necessary when lyx3
332 void InsetSpecialChar::read(Lexer & lex)
333 {
334         lex.next();
335         string const command = lex.getString();
336
337         if (command == "softhyphen")
338                 kind_ = HYPHENATION;
339         else if (command == "ligaturebreak")
340                 kind_ = LIGATURE_BREAK;
341         else if (command == "endofsentence")
342                 kind_ = END_OF_SENTENCE;
343         else if (command == "ldots")
344                 kind_ = LDOTS;
345         else if (command == "menuseparator")
346                 kind_ = MENU_SEPARATOR;
347         else if (command == "breakableslash")
348                 kind_ = SLASH;
349         else if (command == "nobreakdash")
350                 kind_ = NOBREAKDASH;
351         else if (command == "LyX")
352                 kind_ = PHRASE_LYX;
353         else if (command == "TeX")
354                 kind_ = PHRASE_TEX;
355         else if (command == "LaTeX2e")
356                 kind_ = PHRASE_LATEX2E;
357         else if (command == "LaTeX")
358                 kind_ = PHRASE_LATEX;
359         else
360                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
361 }
362
363
364 void InsetSpecialChar::latex(otexstream & os,
365                              OutputParams const & rp) const
366 {
367         switch (kind_) {
368         case HYPHENATION:
369                 os << "\\-";
370                 break;
371         case LIGATURE_BREAK:
372                 os << "\\textcompwordmark" << termcmd;
373                 break;
374         case END_OF_SENTENCE:
375                 os << "\\@.";
376                 break;
377         case LDOTS:
378                 os << "\\ldots" << termcmd;
379                 break;
380         case MENU_SEPARATOR:
381                 if (rp.local_font->isRightToLeft())
382                         os << "\\lyxarrow*";
383                 else
384                         os << "\\lyxarrow";
385                 os << termcmd;
386                 break;
387         case SLASH:
388                 os << "\\slash" << termcmd;
389                 break;
390         case NOBREAKDASH:
391                 if (rp.moving_arg)
392                         os << "\\protect";
393                 os << "\\nobreakdash-";
394                 break;
395         case PHRASE_LYX:
396                 if (rp.moving_arg)
397                         os << "\\protect";
398                 os << "\\LyX" << termcmd;
399                 break;
400         case PHRASE_TEX:
401                 if (rp.moving_arg)
402                         os << "\\protect";
403                 os << "\\TeX" << termcmd;
404                 break;
405         case PHRASE_LATEX2E:
406                 if (rp.moving_arg)
407                         os << "\\protect";
408                 os << "\\LaTeXe" << termcmd;
409                 break;
410         case PHRASE_LATEX:
411                 if (rp.moving_arg)
412                         os << "\\protect";
413                 os << "\\LaTeX" << termcmd;
414                 break;
415         }
416 }
417
418
419 int InsetSpecialChar::plaintext(odocstringstream & os,
420         OutputParams const &, size_t) const
421 {
422         switch (kind_) {
423         case HYPHENATION:
424                 return 0;
425         case LIGATURE_BREAK:
426                 os.put(0x200c);
427                 return 1;
428         case END_OF_SENTENCE:
429                 os << '.';
430                 return 1;
431         case LDOTS:
432                 os.put(0x2026);
433                 return 1;
434         case MENU_SEPARATOR:
435                 os << "->";
436                 return 2;
437         case SLASH:
438                 os << '/';
439                 return 1;
440         case NOBREAKDASH:
441                 os.put(0x2011);
442                 return 1;
443         case PHRASE_LYX:
444                 os << "LyX";
445                 return 3;
446         case PHRASE_TEX:
447                 os << "TeX";
448                 return 3;
449         case PHRASE_LATEX2E:
450                 os << "LaTeX2";
451                 os.put(0x03b5);
452                 return 7;
453         case PHRASE_LATEX:
454                 os << "LaTeX";
455                 return 5;
456         }
457         return 0;
458 }
459
460
461 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
462 {
463         switch (kind_) {
464         case HYPHENATION:
465         case LIGATURE_BREAK:
466                 break;
467         case END_OF_SENTENCE:
468                 os << '.';
469                 break;
470         case LDOTS:
471                 os << "&hellip;";
472                 break;
473         case MENU_SEPARATOR:
474                 os << "&lyxarrow;";
475                 break;
476         case SLASH:
477                 os << '/';
478                 break;
479         case NOBREAKDASH:
480                 os << '-';
481                 break;
482         case PHRASE_LYX:
483                 os << "LyX";
484                 break;
485         case PHRASE_TEX:
486                 os << "TeX";
487                 break;
488         case PHRASE_LATEX2E:
489                 os << "LaTeX2";
490                 os.put(0x03b5);
491                 break;
492         case PHRASE_LATEX:
493                 os << "LaTeX";
494                 break;
495         }
496         return 0;
497 }
498
499
500 docstring InsetSpecialChar::xhtml(XHTMLStream & xs, OutputParams const &) const
501 {
502         switch (kind_) {
503         case HYPHENATION:
504                 break;
505         case LIGATURE_BREAK:
506                 xs << XHTMLStream::ESCAPE_NONE << "&#8204;";
507                 break;
508         case END_OF_SENTENCE:
509                 xs << '.';
510                 break;
511         case LDOTS:
512                 xs << XHTMLStream::ESCAPE_NONE << "&hellip;";
513                 break;
514         case MENU_SEPARATOR:
515                 xs << XHTMLStream::ESCAPE_NONE << "&rArr;";
516                 break;
517         case SLASH:
518                 xs << XHTMLStream::ESCAPE_NONE << "&frasl;";
519                 break;
520         case NOBREAKDASH:
521                 xs << XHTMLStream::ESCAPE_NONE << "&#8209;";
522                 break;
523         case PHRASE_LYX:
524                 xs << "LyX";
525                 break;
526         case PHRASE_TEX:
527                 xs << "TeX";
528                 break;
529         case PHRASE_LATEX2E:
530                 xs << "LaTeX2" << XHTMLStream::ESCAPE_NONE << "&#x3b5;";
531                 break;
532         case PHRASE_LATEX:
533                 xs << "LaTeX";
534                 break;
535         }
536         return docstring();
537 }
538
539
540 void InsetSpecialChar::toString(odocstream & os) const
541 {
542         switch (kind_) {
543         case LIGATURE_BREAK:
544                 // Do not output ZERO WIDTH NON JOINER here
545                 // Spell checker would choke on it.
546                 return;
547         default:
548                 break;
549         }
550         odocstringstream ods;
551         plaintext(ods, OutputParams(0));
552         os << ods.str();
553 }
554
555
556 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
557                                                                    bool const) const
558 {
559         odocstringstream ods;
560         plaintext(ods, OutputParams(0));
561         os += ods.str();
562 }
563
564
565 void InsetSpecialChar::validate(LaTeXFeatures & features) const
566 {
567         if (kind_ == MENU_SEPARATOR)
568                 features.require("lyxarrow");
569         if (kind_ == NOBREAKDASH)
570                 features.require("amsmath");
571         if (kind_ == PHRASE_LYX)
572                 features.require("LyX");
573 }
574
575
576 bool InsetSpecialChar::isLetter() const
577 {
578         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
579                 || kind_ == NOBREAKDASH;
580 }
581
582
583 bool InsetSpecialChar::isLineSeparator() const
584 {
585 #if 0
586         // this would be nice, but it does not work, since
587         // Paragraph::stripLeadingSpaces nukes the characters which
588         // have this property. I leave the code here, since it should
589         // eventually be made to work. (JMarc 20020327)
590         return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR
591                 || kind_ == SLASH;
592 #else
593         return false;
594 #endif
595 }
596
597
598 } // namespace lyx