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