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