]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
Rationalise includes
[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         pi.pain.text(x, y, ch, pi.base.font);
143         x += theFontMetrics(pi.base.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{}";
373                 break;
374         case END_OF_SENTENCE:
375                 os << "\\@.";
376                 break;
377         case LDOTS:
378                 os << "\\ldots{}";
379                 break;
380         case MENU_SEPARATOR:
381                 if (rp.local_font->isRightToLeft())
382                         os << "\\lyxarrow*{}";
383                 else
384                         os << "\\lyxarrow{}";
385                 break;
386         case SLASH:
387                 os << "\\slash{}";
388                 break;
389         case NOBREAKDASH:
390                 if (rp.moving_arg)
391                         os << "\\protect";
392                 os << "\\nobreakdash-";
393                 break;
394         case PHRASE_LYX:
395                 if (rp.moving_arg)
396                         os << "\\protect";
397                 os << "\\LyX{}";
398                 break;
399         case PHRASE_TEX:
400                 if (rp.moving_arg)
401                         os << "\\protect";
402                 os << "\\TeX{}";
403                 break;
404         case PHRASE_LATEX2E:
405                 if (rp.moving_arg)
406                         os << "\\protect";
407                 os << "\\LaTeXe{}";
408                 break;
409         case PHRASE_LATEX:
410                 if (rp.moving_arg)
411                         os << "\\protect";
412                 os << "\\LaTeX{}";
413                 break;
414         }
415 }
416
417
418 int InsetSpecialChar::plaintext(odocstringstream & os,
419         OutputParams const &, size_t) const
420 {
421         switch (kind_) {
422         case HYPHENATION:
423                 return 0;
424         case LIGATURE_BREAK:
425                 os.put(0x200c);
426                 return 1;
427         case END_OF_SENTENCE:
428                 os << '.';
429                 return 1;
430         case LDOTS:
431                 os.put(0x2026);
432                 return 1;
433         case MENU_SEPARATOR:
434                 os << "->";
435                 return 2;
436         case SLASH:
437                 os << '/';
438                 return 1;
439         case NOBREAKDASH:
440                 os.put(0x2011);
441                 return 1;
442         case PHRASE_LYX:
443                 os << "LyX";
444                 return 3;
445         case PHRASE_TEX:
446                 os << "TeX";
447                 return 3;
448         case PHRASE_LATEX2E:
449                 os << "LaTeX2";
450                 os.put(0x03b5);
451                 return 7;
452         case PHRASE_LATEX:
453                 os << "LaTeX";
454                 return 5;
455         }
456         return 0;
457 }
458
459
460 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
461 {
462         switch (kind_) {
463         case HYPHENATION:
464         case LIGATURE_BREAK:
465                 break;
466         case END_OF_SENTENCE:
467                 os << '.';
468                 break;
469         case LDOTS:
470                 os << "&hellip;";
471                 break;
472         case MENU_SEPARATOR:
473                 os << "&lyxarrow;";
474                 break;
475         case SLASH:
476                 os << '/';
477                 break;
478         case NOBREAKDASH:
479                 os << '-';
480                 break;
481         case PHRASE_LYX:
482                 os << "LyX";
483                 break;
484         case PHRASE_TEX:
485                 os << "TeX";
486                 break;
487         case PHRASE_LATEX2E:
488                 os << "LaTeX2";
489                 os.put(0x03b5);
490                 break;
491         case PHRASE_LATEX:
492                 os << "LaTeX";
493                 break;
494         }
495         return 0;
496 }
497
498
499 docstring InsetSpecialChar::xhtml(XHTMLStream & xs, OutputParams const &) const
500 {
501         switch (kind_) {
502         case HYPHENATION:
503                 break;
504         case LIGATURE_BREAK:
505                 xs << XHTMLStream::ESCAPE_NONE << "&#8204;";
506                 break;
507         case END_OF_SENTENCE:
508                 xs << '.';
509                 break;
510         case LDOTS:
511                 xs << XHTMLStream::ESCAPE_NONE << "&hellip;";
512                 break;
513         case MENU_SEPARATOR:
514                 xs << XHTMLStream::ESCAPE_NONE << "&rArr;";
515                 break;
516         case SLASH:
517                 xs << XHTMLStream::ESCAPE_NONE << "&frasl;";
518                 break;
519         case NOBREAKDASH:
520                 xs << XHTMLStream::ESCAPE_NONE << "&#8209;";
521                 break;
522         case PHRASE_LYX:
523                 xs << "LyX";
524                 break;
525         case PHRASE_TEX:
526                 xs << "TeX";
527                 break;
528         case PHRASE_LATEX2E:
529                 xs << "LaTeX2" << XHTMLStream::ESCAPE_NONE << "&#x3b5;";
530                 break;
531         case PHRASE_LATEX:
532                 xs << "LaTeX";
533                 break;
534         }
535         return docstring();
536 }
537
538
539 void InsetSpecialChar::toString(odocstream & os) const
540 {
541         switch (kind_) {
542         case LIGATURE_BREAK:
543                 // Do not output ZERO WIDTH NON JOINER here
544                 // Spell checker would choke on it.
545                 return;
546         default:
547                 break;
548         }
549         odocstringstream ods;
550         plaintext(ods, OutputParams(0));
551         os << ods.str();
552 }
553
554
555 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
556                                                                    bool const) const
557 {
558         odocstringstream ods;
559         plaintext(ods, OutputParams(0));
560         os += ods.str();
561 }
562
563
564 void InsetSpecialChar::validate(LaTeXFeatures & features) const
565 {
566         if (kind_ == MENU_SEPARATOR)
567                 features.require("lyxarrow");
568         if (kind_ == NOBREAKDASH)
569                 features.require("amsmath");
570         if (kind_ == PHRASE_LYX)
571                 features.require("LyX");
572 }
573
574
575 bool InsetSpecialChar::isLetter() const
576 {
577         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
578                 || kind_ == NOBREAKDASH;
579 }
580
581
582 bool InsetSpecialChar::isLineSeparator() const
583 {
584 #if 0
585         // this would be nice, but it does not work, since
586         // Paragraph::stripLeadingSpaces nukes the characters which
587         // have this property. I leave the code here, since it should
588         // eventually be made to work. (JMarc 20020327)
589         return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR
590                 || kind_ == SLASH;
591 #else
592         return false;
593 #endif
594 }
595
596
597 } // namespace lyx