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