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