]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpecialChar.cpp
ec9daba0e9f9de69eccdea7b71a951ec52f93fab
[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                         drawLogo(pi, dim.wid, 0, kind_);
216                         break;
217         }
218         if (dim.wid == 0)
219                 dim.wid = fm.width(s);
220 }
221
222
223 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
224 {
225         FontInfo font = pi.base.font;
226
227         switch (kind_) {
228         case HYPHENATION:
229         {
230                 font.setColor(Color_special);
231                 pi.pain.text(x, y, char_type('-'), font);
232                 break;
233         }
234         case ALLOWBREAK:
235         {
236                 // A small vertical line
237                 int const asc = theFontMetrics(pi.base.font).xHeight();
238                 int const desc = theFontMetrics(pi.base.font).descent('g');
239                 int const x0 = x; // x + 1; // FIXME: incline,
240                 int const x1 = x; // x - 1; // similar to LibreOffice?
241                 int const y0 = y + desc;
242                 int const y1 = y - asc / 3;
243                 pi.pain.line(x0, y1, x1, y0, Color_special);
244                 break;
245         }
246         case LIGATURE_BREAK:
247         {
248                 font.setColor(Color_special);
249                 pi.pain.text(x, y, char_type('|'), font);
250                 break;
251         }
252         case END_OF_SENTENCE:
253         {
254                 font.setColor(Color_special);
255                 pi.pain.text(x, y, char_type('.'), font);
256                 break;
257         }
258         case LDOTS:
259         {
260                 font.setColor(Color_special);
261                 string ell = ". . . ";
262                 docstring dell(ell.begin(), ell.end());
263                 pi.pain.text(x, y, dell, font);
264                 break;
265         }
266         case MENU_SEPARATOR:
267         {
268                 frontend::FontMetrics const & fm =
269                         theFontMetrics(font);
270
271                 // There is a \thinspace on each side of the triangle
272                 x += fm.em() / 6;
273                 // ▹ U+25B9 WHITE RIGHT-POINTING SMALL TRIANGLE
274                 // ◃ U+25C3 WHITE LEFT-POINTING SMALL TRIANGLE
275                 char_type const c = pi.ltr_pos ? 0x25B9 : 0x25C3;
276                 font.setColor(Color_special);
277                 pi.pain.text(x, y, c, font);
278                 break;
279         }
280         case SLASH:
281         {
282                 font.setColor(Color_special);
283                 pi.pain.text(x, y, char_type('/'), font);
284                 break;
285         }
286         case NOBREAKDASH:
287         {
288                 font.setColor(Color_latex);
289                 pi.pain.text(x, y, char_type('-'), font);
290                 break;
291         }
292         case PHRASE_LYX:
293         case PHRASE_TEX:
294         case PHRASE_LATEX2E:
295         case PHRASE_LATEX:
296                 drawLogo(pi, x, y, kind_);
297                 break;
298         }
299 }
300
301
302 void InsetSpecialChar::write(ostream & os) const
303 {
304         string command;
305         switch (kind_) {
306         case HYPHENATION:
307                 command = "softhyphen";
308                 break;
309         case ALLOWBREAK:
310                 command = "allowbreak";
311                 break;
312         case LIGATURE_BREAK:
313                 command = "ligaturebreak";
314                 break;
315         case END_OF_SENTENCE:
316                 command = "endofsentence";
317                 break;
318         case LDOTS:
319                 command = "ldots";
320                 break;
321         case MENU_SEPARATOR:
322                 command = "menuseparator";
323                 break;
324         case SLASH:
325                 command = "breakableslash";
326                 break;
327         case NOBREAKDASH:
328                 command = "nobreakdash";
329                 break;
330         case PHRASE_LYX:
331                 command = "LyX";
332                 break;
333         case PHRASE_TEX:
334                 command = "TeX";
335                 break;
336         case PHRASE_LATEX2E:
337                 command = "LaTeX2e";
338                 break;
339         case PHRASE_LATEX:
340                 command = "LaTeX";
341                 break;
342         }
343         os << "\\SpecialChar " << command << "\n";
344 }
345
346
347 void InsetSpecialChar::read(Lexer & lex)
348 {
349         lex.next();
350         string const command = lex.getString();
351
352         if (command == "softhyphen")
353                 kind_ = HYPHENATION;
354         else if (command == "allowbreak")
355                 kind_ = ALLOWBREAK;
356         else if (command == "ligaturebreak")
357                 kind_ = LIGATURE_BREAK;
358         else if (command == "endofsentence")
359                 kind_ = END_OF_SENTENCE;
360         else if (command == "ldots")
361                 kind_ = LDOTS;
362         else if (command == "menuseparator")
363                 kind_ = MENU_SEPARATOR;
364         else if (command == "breakableslash")
365                 kind_ = SLASH;
366         else if (command == "nobreakdash")
367                 kind_ = NOBREAKDASH;
368         else if (command == "LyX")
369                 kind_ = PHRASE_LYX;
370         else if (command == "TeX")
371                 kind_ = PHRASE_TEX;
372         else if (command == "LaTeX2e")
373                 kind_ = PHRASE_LATEX2E;
374         else if (command == "LaTeX")
375                 kind_ = PHRASE_LATEX;
376         else
377                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
378 }
379
380
381 void InsetSpecialChar::latex(otexstream & os,
382                              OutputParams const & rp) const
383 {
384         bool const rtl = rp.local_font->isRightToLeft();
385         string lswitch = "";
386         string lswitche = "";
387         if (rtl && !rp.use_polyglossia) {
388                 lswitch = "\\L{";
389                 lswitche = "}";
390                 if (rp.local_font->language()->lang() == "arabic_arabi"
391                     || rp.local_font->language()->lang() == "farsi")
392                         lswitch = "\\textLR{";
393         }
394
395         switch (kind_) {
396         case HYPHENATION:
397                 os << "\\-";
398                 break;
399         case ALLOWBREAK:
400                 os << "\\LyXZeroWidthSpace" << termcmd;
401                 break;
402         case LIGATURE_BREAK:
403                 os << "\\textcompwordmark" << termcmd;
404                 break;
405         case END_OF_SENTENCE:
406                 os << "\\@.";
407                 break;
408         case LDOTS:
409                 os << "\\ldots" << termcmd;
410                 break;
411         case MENU_SEPARATOR:
412                 if (rtl)
413                         os << "\\lyxarrow*";
414                 else
415                         os << "\\lyxarrow";
416                 os << termcmd;
417                 break;
418         case SLASH:
419                 os << "\\slash" << termcmd;
420                 break;
421         case NOBREAKDASH:
422                 if (rp.moving_arg)
423                         os << "\\protect";
424                 os << "\\nobreakdash-";
425                 break;
426         case PHRASE_LYX:
427                 if (rp.moving_arg)
428                         os << "\\protect";
429                 os << lswitch << "\\LyX" << termcmd << lswitche;
430                 break;
431         case PHRASE_TEX:
432                 if (rp.moving_arg)
433                         os << "\\protect";
434                 os << lswitch << "\\TeX" << termcmd << lswitche;
435                 break;
436         case PHRASE_LATEX2E:
437                 if (rp.moving_arg)
438                         os << "\\protect";
439                 os << lswitch << "\\LaTeXe" << termcmd << lswitche;
440                 break;
441         case PHRASE_LATEX:
442                 if (rp.moving_arg)
443                         os << "\\protect";
444                 os << lswitch << "\\LaTeX" << termcmd << lswitche;
445                 break;
446         }
447 }
448
449
450 int InsetSpecialChar::plaintext(odocstringstream & os,
451         OutputParams const &, size_t) const
452 {
453         switch (kind_) {
454         case HYPHENATION:
455                 return 0;
456         case ALLOWBREAK:
457                 os.put(0x200b);
458                 return 1;
459         case LIGATURE_BREAK:
460                 os.put(0x200c);
461                 return 1;
462         case END_OF_SENTENCE:
463                 os << '.';
464                 return 1;
465         case LDOTS:
466                 os.put(0x2026);
467                 return 1;
468         case MENU_SEPARATOR:
469                 os << "->";
470                 return 2;
471         case SLASH:
472                 os << '/';
473                 return 1;
474         case NOBREAKDASH:
475                 os.put(0x2011);
476                 return 1;
477         case PHRASE_LYX:
478                 os << "LyX";
479                 return 3;
480         case PHRASE_TEX:
481                 os << "TeX";
482                 return 3;
483         case PHRASE_LATEX2E:
484                 os << "LaTeX2";
485                 os.put(0x03b5);
486                 return 7;
487         case PHRASE_LATEX:
488                 os << "LaTeX";
489                 return 5;
490         }
491         return 0;
492 }
493
494
495 int InsetSpecialChar::docbook(odocstream & os, OutputParams const &) const
496 {
497         switch (kind_) {
498         case HYPHENATION:
499                 break;
500         case ALLOWBREAK:
501                 os.put(0x200b);
502                 break;
503         case LIGATURE_BREAK:
504                 break;
505         case END_OF_SENTENCE:
506                 os << '.';
507                 break;
508         case LDOTS:
509                 os << "&hellip;";
510                 break;
511         case MENU_SEPARATOR:
512                 os << "&lyxarrow;";
513                 break;
514         case SLASH:
515                 os << '/';
516                 break;
517         case NOBREAKDASH:
518                 os << '-';
519                 break;
520         case PHRASE_LYX:
521                 os << "LyX";
522                 break;
523         case PHRASE_TEX:
524                 os << "TeX";
525                 break;
526         case PHRASE_LATEX2E:
527                 os << "LaTeX2";
528                 os.put(0x03b5);
529                 break;
530         case PHRASE_LATEX:
531                 os << "LaTeX";
532                 break;
533         }
534         return 0;
535 }
536
537
538 docstring InsetSpecialChar::xhtml(XHTMLStream & xs, OutputParams const &) const
539 {
540         switch (kind_) {
541         case HYPHENATION:
542                 break;
543         case ALLOWBREAK:
544                 xs << XHTMLStream::ESCAPE_NONE << "&#8203;";
545                 break;
546         case LIGATURE_BREAK:
547                 xs << XHTMLStream::ESCAPE_NONE << "&#8204;";
548                 break;
549         case END_OF_SENTENCE:
550                 xs << '.';
551                 break;
552         case LDOTS:
553                 xs << XHTMLStream::ESCAPE_NONE << "&hellip;";
554                 break;
555         case MENU_SEPARATOR:
556                 xs << XHTMLStream::ESCAPE_NONE << "&rArr;";
557                 break;
558         case SLASH:
559                 xs << XHTMLStream::ESCAPE_NONE << "&frasl;";
560                 break;
561         case NOBREAKDASH:
562                 xs << XHTMLStream::ESCAPE_NONE << "&#8209;";
563                 break;
564         case PHRASE_LYX:
565                 xs << "LyX";
566                 break;
567         case PHRASE_TEX:
568                 xs << "TeX";
569                 break;
570         case PHRASE_LATEX2E:
571                 xs << "LaTeX2" << XHTMLStream::ESCAPE_NONE << "&#x3b5;";
572                 break;
573         case PHRASE_LATEX:
574                 xs << "LaTeX";
575                 break;
576         }
577         return docstring();
578 }
579
580
581 void InsetSpecialChar::toString(odocstream & os) const
582 {
583         switch (kind_) {
584         case ALLOWBREAK:
585         case LIGATURE_BREAK:
586                 // Do not output ZERO WIDTH SPACE and ZERO WIDTH NON JOINER here
587                 // Spell checker would choke on it.
588                 return;
589         default:
590                 break;
591         }
592         odocstringstream ods;
593         plaintext(ods, OutputParams(0));
594         os << ods.str();
595 }
596
597
598 void InsetSpecialChar::forOutliner(docstring & os, size_t const,
599                                                                    bool const) const
600 {
601         odocstringstream ods;
602         plaintext(ods, OutputParams(0));
603         os += ods.str();
604 }
605
606
607 void InsetSpecialChar::validate(LaTeXFeatures & features) const
608 {
609         if (kind_ == ALLOWBREAK)
610                 features.require("lyxzerowidthspace");
611         if (kind_ == MENU_SEPARATOR)
612                 features.require("lyxarrow");
613         if (kind_ == NOBREAKDASH)
614                 features.require("amsmath");
615         if (kind_ == PHRASE_LYX)
616                 features.require("LyX");
617 }
618
619
620 bool InsetSpecialChar::isChar() const
621 {
622         return kind_ != HYPHENATION && kind_ != LIGATURE_BREAK;
623 }
624
625
626 bool InsetSpecialChar::isLetter() const
627 {
628         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK
629                 || kind_ == NOBREAKDASH;
630 }
631
632
633 bool InsetSpecialChar::isLineSeparator() const
634 {
635 #if 0
636         // this would be nice, but it does not work, since
637         // Paragraph::stripLeadingSpaces nukes the characters which
638         // have this property. I leave the code here, since it should
639         // eventually be made to work. (JMarc 20020327)
640         return kind_ == HYPHENATION || kind_ == ALLOWBREAK
641             || kind_ == MENU_SEPARATOR || kind_ == SLASH;
642 #else
643         return false;
644 #endif
645 }
646
647
648 } // namespace lyx