]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
Merge branch 'master' into biblatex2
[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 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 void InsetSpecialChar::read(Lexer & lex)
331 {
332         lex.next();
333         string const command = lex.getString();
334
335         if (command == "softhyphen")
336                 kind_ = HYPHENATION;
337         else if (command == "ligaturebreak")
338                 kind_ = LIGATURE_BREAK;
339         else if (command == "endofsentence")
340                 kind_ = END_OF_SENTENCE;
341         else if (command == "ldots")
342                 kind_ = LDOTS;
343         else if (command == "menuseparator")
344                 kind_ = MENU_SEPARATOR;
345         else if (command == "breakableslash")
346                 kind_ = SLASH;
347         else if (command == "nobreakdash")
348                 kind_ = NOBREAKDASH;
349         else if (command == "LyX")
350                 kind_ = PHRASE_LYX;
351         else if (command == "TeX")
352                 kind_ = PHRASE_TEX;
353         else if (command == "LaTeX2e")
354                 kind_ = PHRASE_LATEX2E;
355         else if (command == "LaTeX")
356                 kind_ = PHRASE_LATEX;
357         else
358                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
359 }
360
361
362 void InsetSpecialChar::latex(otexstream & os,
363                              OutputParams const & rp) const
364 {
365         switch (kind_) {
366         case HYPHENATION:
367                 os << "\\-";
368                 break;
369         case LIGATURE_BREAK:
370                 os << "\\textcompwordmark" << termcmd;
371                 break;
372         case END_OF_SENTENCE:
373                 os << "\\@.";
374                 break;
375         case LDOTS:
376                 os << "\\ldots" << termcmd;
377                 break;
378         case MENU_SEPARATOR:
379                 if (rp.local_font->isRightToLeft())
380                         os << "\\lyxarrow*";
381                 else
382                         os << "\\lyxarrow";
383                 os << termcmd;
384                 break;
385         case SLASH:
386                 os << "\\slash" << termcmd;
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" << termcmd;
397                 break;
398         case PHRASE_TEX:
399                 if (rp.moving_arg)
400                         os << "\\protect";
401                 os << "\\TeX" << termcmd;
402                 break;
403         case PHRASE_LATEX2E:
404                 if (rp.moving_arg)
405                         os << "\\protect";
406                 os << "\\LaTeXe" << termcmd;
407                 break;
408         case PHRASE_LATEX:
409                 if (rp.moving_arg)
410                         os << "\\protect";
411                 os << "\\LaTeX" << termcmd;
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::isChar() const
575 {
576         return kind_ != HYPHENATION || kind_ != LIGATURE_BREAK;
577 }
578
579
580 bool InsetSpecialChar::isLetter() const
581 {
582         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
583                 || kind_ == NOBREAKDASH;
584 }
585
586
587 bool InsetSpecialChar::isLineSeparator() const
588 {
589 #if 0
590         // this would be nice, but it does not work, since
591         // Paragraph::stripLeadingSpaces nukes the characters which
592         // have this property. I leave the code here, since it should
593         // eventually be made to work. (JMarc 20020327)
594         return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR
595                 || kind_ == SLASH;
596 #else
597         return false;
598 #endif
599 }
600
601
602 } // namespace lyx