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