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