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