]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
Wrap all #warning calls inside #ifdef WITH_WARNINGS blocks.
[lyx.git] / src / insets / insetlatexaccent.C
1 /**
2  * \file insetlatexaccent.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetlatexaccent.h"
14
15 #include "debug.h"
16 #include "language.h"
17 #include "LColor.h"
18 #include "lyxlex.h"
19 #include "lyxrc.h"
20 #include "metricsinfo.h"
21
22 #include "frontends/font_metrics.h"
23 #include "frontends/Painter.h"
24
25 #include "support/lstrings.h"
26
27 using lyx::support::contains;
28 using lyx::support::trim;
29
30 using std::endl;
31 using std::string;
32 using std::auto_ptr;
33 using std::ostream;
34
35
36 /* LatexAccent. Proper handling of accented characters */
37 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
38 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
39
40 InsetLatexAccent::InsetLatexAccent()
41         : candisp(false)
42 {}
43
44
45 InsetLatexAccent::InsetLatexAccent(string const & str)
46         : contents(str)
47 {
48         checkContents();
49 }
50
51
52 auto_ptr<InsetBase> InsetLatexAccent::clone() const
53 {
54         return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
55 }
56
57
58 void InsetLatexAccent::checkContents()
59         // check, if we know the modifier and can display it ok on screen
60 {
61         candisp = false;
62
63         if (contents.empty() || contents.length() < 2) {
64                 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
65                 return;
66         }
67
68         // REMOVE IN 0.13
69         // Dirty Hack for backward compability. remove in 0.13 (Lgb)
70         contents = trim(contents);
71         if (!contains(contents, '{') && !contains(contents, '}')) {
72                 if (contents.length() == 2) {
73                         string tmp;
74                         tmp += contents[0];
75                         tmp += contents[1];
76                         tmp += "{}";
77                         contents = tmp;
78                 } else if (contents.length() == 3) {
79                         string tmp;
80                         tmp += contents[0];
81                         tmp += contents[1];
82                         tmp += '{';
83                         tmp += contents[2];
84                         tmp += '}';
85                         contents = tmp;
86                 } else if (contents.length() == 4 && contents[2] == ' ') {
87                         string tmp;
88                         tmp += contents[0];
89                         tmp += contents[1];
90                         tmp += '{';
91                         tmp += contents[3];
92                         tmp += '}';
93                         contents = tmp;
94                 } else if  (contents.length() == 4 && contents[2] == '\\'
95                             && (contents[3] == 'i' || contents[3] == 'j')) {
96                         string tmp;
97                         tmp += contents[0];
98                         tmp += contents[1];
99                         tmp += '{';
100                         tmp += contents[2];
101                         tmp += contents[3];
102                         tmp += '}';
103                         contents = tmp;
104                 }
105         }
106         if (contents[0] != '\\') { // demand that first char is a '\\'
107                 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
108                 return;
109         }
110
111         lyxerr[Debug::KEY] << "Decode: " << contents << endl;
112
113         remdot = false;
114         plusasc = false;
115         plusdesc = false;
116
117         switch (contents[1]) { // second char should be one of these
118         case '\'':  // acute
119                 modtype = ACUTE;    // acute
120                 plusasc = true;    // at the top of character
121                 break;
122         case '`':   // grave
123                 modtype = GRAVE;    // grave
124                 plusasc = true;    // at the top
125                 break;
126         case '=':   // macron
127                 modtype = MACRON;    // macron
128                 plusasc = true;    // at the top
129                 break;
130         case '~':   // tilde
131                 modtype = TILDE;    // tilde
132                 plusasc = true;    // at the top
133                 break;
134         case 'b':   // underbar
135                 modtype = UNDERBAR;    // underbar
136                 plusdesc = true;   // at the bottom
137                 break;
138         case 'c':   // cedilla
139                 modtype = CEDILLA;    // cedilla
140                 plusdesc = true;   // at the bottom
141                 break;
142         case 'd':   // underdot
143                 modtype = UNDERDOT;    // underdot
144                 plusdesc = true;   // at the bottom
145                 break;
146         case 'r':   // circle
147                 modtype = CIRCLE;    // circle
148                 plusasc = true;    // at the top
149                 break;
150         case 't':   // tie
151                 modtype = TIE;    // tie
152                 plusasc = true;    // at the top
153                 break;
154         case 'u':   // breve
155                 modtype = BREVE;    // breve
156                 plusasc = true;    // at the top
157                 break;
158         case 'v':   // caron
159                 modtype = CARON;   // caron
160                 plusasc = true;    // at the top
161                 break;
162         case 'q':   // special caron
163                 modtype = SPECIAL_CARON;   // special caron
164                 plusasc = true;    // at the top
165                 break;
166         case 'H':   // hungarian umlaut
167                 modtype = HUNGARIAN_UMLAUT;   // hungarian umlaut
168                 plusasc = true;    // at the top
169                 break;
170         case '"':   // umlaut
171                 modtype = UMLAUT;   // umlaut
172                 plusasc = true;    // at the top
173                 break;
174         case '.':   // dot
175                 modtype = DOT;   // dot
176                 plusasc = true;    // at the top
177                 break;
178         case '^':   // circumflex
179                 modtype = CIRCUMFLEX;   // circumflex
180                 plusasc = true;    // at the top
181                 break;
182         case 'k':   // ogonek
183                 modtype = OGONEK;  // ogonek
184                 plusdesc = true;
185                 break;
186         case 'i': // dot-less-i
187                 modtype = DOT_LESS_I;  // dot-less-i
188                 plusasc = true; // at the top (not really needed)
189                 remdot = true;
190                 break;
191         case 'j': // dot-less-j
192                 modtype = DOT_LESS_J; // dot-less-j
193                 plusasc = true; // at the top (not really needed)
194                 remdot = true;
195                 break;
196         case 'l': // lslash
197                 modtype = lSLASH;
198                 plusasc = true; // at the top (not really needed)
199                 break;
200         case 'L': // lslash
201                 modtype = LSLASH;
202                 plusasc = true; // at the top (not really needed)
203                 break;
204         default:
205                 lyxerr[Debug::KEY] << "Default" << endl;
206                 // unknow accent (or something else)
207                 return;
208         }
209
210         // we demand that third char is a '{' (Lgb)
211         if (contents[2] != '{') return;
212
213         // special clause for \i{}, \j{} \l{} and \L{}
214         if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
215              || modtype == lSLASH || modtype == LSLASH)
216             && contents[3] == '}') {
217                 switch (modtype) {
218                 case DOT_LESS_I: ic = 'i'; break;
219                 case DOT_LESS_J: ic = 'j'; break;
220                 case lSLASH:     ic = 'l'; break;
221                 case LSLASH:     ic = 'L'; break;
222                 default:
223                         // if this happens something is really wrong
224                         lyxerr << "InsetLaTexAccent: weird error." << endl;
225                         break;
226                 }
227                 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
228                 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
229                                    << ", ic: " << ic
230                                    << ", top: " << plusasc
231                                    << ", bot: " << plusdesc
232                                    << ", dot: " << remdot
233                                    << ", mod: " << modtype << endl;
234                 // Special case for space
235         } else if (contents[3] == '}') {
236                 ic = ' ';
237         } else {
238                 int i = 3;
239
240                 // now get the char
241                 ic = contents[3]; // i will always be 3 here
242
243                 // ic should now be a alfa-char or '\\'
244                 if (ic == '\\') {
245                         ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
246                         if (ic == 'i' || ic == 'j')
247                                 remdot = true;
248                         else
249                                 return;
250                 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
251                         // Do a rewrite: \<foo>{i} --> \<foo>{\i}
252                         string temp = contents;
253                         temp.erase(3, string::npos);
254                         temp += '\\';
255                         temp += char(ic);
256                         for (string::size_type j = 4;
257                             j < contents.length(); ++j)
258                                 temp+= contents[j];
259                         contents= temp;
260                         ++i;
261                         remdot = true;
262                 }
263
264                 // demand a '}' at the end
265                 if (contents[++i] != '}' && contents[++i]) return;
266
267                 // fine, the char is properly decoded now (hopefully)
268                 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
269                                    << ", ic: " << ic
270                                    << ", top: " << plusasc
271                                    << ", bot: " << plusdesc
272                                    << ", dot: " << remdot
273                                    << ", mod: " << modtype << endl;
274         }
275         candisp = true;
276 }
277
278
279 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
280 {
281         LyXFont & font = mi.base.font;
282         // This function is a bit too simplistic and is just a
283         // "try to make a fit for all accents" approach, to
284         // make it better we need to know what kind of accent is
285         // used and add to max based on that.
286         if (candisp) {
287                 if (ic == ' ')
288                         dim.asc = font_metrics::ascent('a', font);
289                 else
290                         dim.asc = font_metrics::ascent(ic, font);
291                 if (plusasc)
292                         dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
293
294                 if (ic == ' ')
295                         dim.des = font_metrics::descent('a', font);
296                 else
297                         dim.des = font_metrics::descent(ic, font);
298                 if (plusdesc)
299                         dim.des += 3;
300
301                 dim.wid = font_metrics::width(ic, font);
302         } else {
303                 dim.asc = font_metrics::maxAscent(font) + 4;
304                 dim.des = font_metrics::maxDescent(font) + 4;
305                 dim.wid = font_metrics::width(contents, font) + 4;
306         }
307         dim_ = dim;
308 }
309
310
311 int InsetLatexAccent::lbearing(LyXFont const & font) const
312 {
313         return font_metrics::lbearing(ic, font);
314 }
315
316
317 int InsetLatexAccent::rbearing(LyXFont const & font) const
318 {
319         return font_metrics::rbearing(ic, font);
320 }
321
322
323 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
324 {
325         unsigned char tmpic = ic;
326
327         switch (modtype) {
328
329         case CEDILLA: {
330                 if (ic == 'c') tmpic = 0xe7;
331                 if (ic == 'C') tmpic = 0xc7;
332                 if (ic == 's') tmpic = 0xfe;
333                 if (ic == 'S') tmpic = 0xde;
334                 break;
335         }
336
337         case BREVE: {
338                 if (ic == 'g') tmpic = 0xf0;
339                 if (ic == 'G') tmpic = 0xd0;
340                 break;
341         }
342
343         case UMLAUT: {
344                 if (ic == 'o') tmpic = 0xf6;
345                 if (ic == 'O') tmpic = 0xd6;
346                 if (ic == 'u') tmpic = 0xfc;
347                 if (ic == 'U') tmpic = 0xdc;
348                 break;
349         }
350
351         case DOT:
352                 if (ic == 'I') tmpic = 0xdd;
353                 break;
354
355         case DOT_LESS_I:
356                 tmpic = 0xfd;
357                 break;
358
359         default:
360                 return false;
361         }
362
363         if (tmpic == ic)
364                 return false;
365
366         pi.pain.text(x, y, char(tmpic), pi.base.font);
367         return true;
368 }
369
370
371 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
372         char accent) const
373 {
374         LyXFont const & font = pi.base.font;
375         x -= font_metrics::center(accent, font);
376         y -= font_metrics::ascent(ic, font);
377         y -= font_metrics::descent(accent, font);
378         y -= font_metrics::height(accent, font) / 2;
379         pi.pain.text(x, y, accent, font);
380 }
381
382
383 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
384 {
385         if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
386                 if (displayISO8859_9(pi, x, baseline))
387                         return;
388
389         // All the manually drawn accents in this function could use an
390         // overhaul. Different ways of drawing (what metrics to use)
391         // should also be considered.
392
393         LyXFont font = pi.base.font;
394         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
395                 font.setLanguage(english_language);
396
397         if (candisp) {
398                 int x2 = int(x + (rbearing(font) - lbearing(font)) / 2);
399                 int hg;
400                 int y;
401                 if (plusasc) {
402                         // mark at the top
403                         hg = font_metrics::maxDescent(font);
404                         y = baseline - dim_.asc;
405                         if (font.shape() == LyXFont::ITALIC_SHAPE)
406                                 x2 += int(0.8 * hg); // italic
407                 } else {
408                         // at the bottom
409                         hg = dim_.des;
410                         y = baseline;
411                 }
412
413                 double hg35 = hg * 0.6;
414
415                 // display with proper accent mark
416                 // first the letter
417                 pi.pain.text(x, baseline, ic, font);
418
419                 if (remdot) {
420                         int tmpvar = baseline - font_metrics::ascent('i', font);
421                         int tmpx = 0;
422                         if (font.shape() == LyXFont::ITALIC_SHAPE)
423                                 tmpx += int(0.8 * hg); // italic
424                         lyxerr[Debug::KEY] << "Removing dot." << endl;
425                         // remove the dot first
426                         pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
427                                            font_metrics::ascent('i', pi.base.font) -
428                                            font_metrics::ascent('x', pi.base.font) - 1,
429                                            backgroundColor());
430                         // the five lines below is a simple hack to
431                         // make the display of accent 'i' and 'j'
432                         // better. It makes the accent be written
433                         // closer to the top of the dot-less 'i' or 'j'.
434                         char tmpic = ic; // store the ic when we
435                         ic = 'x';        // calculates the ascent of
436 #ifdef WITH_WARNINGS
437 #warning metrics?
438 #endif
439                         int asc = ascent(); // the dot-less version (here: 'x')
440                         ic = tmpic;      // set the orig ic back
441                         y = baseline - asc; // update to new y coord.
442                 }
443
444                 // now the rest - draw within (x, y, x + wid, y + hg)
445                 switch (modtype) {
446                 case ACUTE:
447                         drawAccent(pi, x2, baseline, char(0xB4));
448                         break;
449
450                 case GRAVE:
451                         drawAccent(pi, x2, baseline, char(0x60));
452                         break;
453
454                 case MACRON:
455                         drawAccent(pi, x2, baseline, char(0xAF));
456                         break;
457
458                 case TILDE:
459                         drawAccent(pi, x2, baseline, '~');
460                         break;
461
462                 case UNDERBAR:     // underbar 0x5F
463                         pi.pain.text(x2 - font_metrics::center(0x5F, font), baseline,
464                                   char(0x5F), font);
465                         break;
466
467                 case CEDILLA:
468                         pi.pain.text(x2  - font_metrics::center(0xB8, font), baseline,
469                                   char(0xB8), font);
470                         break;
471
472                 case UNDERDOT:
473                         pi.pain.text(x2  - font_metrics::center('.', font),
474                                   int(baseline + 1.5 * font_metrics::height('.', font)),
475                                   '.', font);
476                         break;
477
478                 case DOT:
479                         drawAccent(pi, x2, baseline, '.');
480                         break;
481
482                 case CIRCLE:
483                         drawAccent(pi, x2, baseline, char(0xB0));
484                         break;
485
486                 case TIE:
487                         pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
488                                     LColor::foreground);
489                         break;
490
491                 case BREVE:
492                         pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
493                                     LColor::foreground);
494                         break;
495
496                 case CARON: {
497                         int xp[3], yp[3];
498                         xp[0] = int(x2 - hg35);    yp[0] = int(y + hg35);
499                         xp[1] = int(x2);           yp[1] = int(y + hg);
500                         xp[2] = int(x2 + hg35);    yp[2] = int(y + hg35);
501                         pi.pain.lines(xp, yp, 3, LColor::foreground);
502                         break;
503                 }
504
505                 case SPECIAL_CARON: {
506                         switch (ic) {
507                                 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
508                                 case 't': y -= int(hg35 / 2.0); break;
509                         }
510                         int xp[3], yp[3];
511                         xp[0] = int(x + dim_.wid);
512                         yp[0] = int(y + hg35 + hg);
513
514                         xp[1] = int(x + dim_.wid + (hg35 / 2.0));
515                         yp[1] = int(y + hg + (hg35 / 2.0));
516
517                         xp[2] = int(x + dim_.wid + (hg35 / 2.0));
518                         yp[2] = y + int(hg);
519
520                         pi.pain.lines(xp, yp, 3, LColor::foreground);
521                         break;
522                 }
523
524                 case HUNGARIAN_UMLAUT:
525                         drawAccent(pi, x2 - font_metrics::center('´', font), baseline, '´');
526                         drawAccent(pi, x2 + font_metrics::center('´', font), baseline, '´');
527                         break;
528
529                 case UMLAUT:
530                         drawAccent(pi, x2, baseline, '"');
531                         break;
532
533                 case CIRCUMFLEX:
534                         drawAccent(pi, x2, baseline, 0x5E);
535                         break;
536
537                 case OGONEK: {
538                         // this does probably not look like an ogonek, so
539                         // it should certainly be refined
540                         int xp[4], yp[4];
541
542                         xp[0] = x2;
543                         yp[0] = y;
544
545                         xp[1] = x2;
546                         yp[1] = y + int(hg35);
547
548                         xp[2] = int(x2 - hg35);
549                         yp[2] = y + hg / 2;
550
551                         xp[3] = x2 + hg / 4;
552                         yp[3] = y + int(hg);
553
554                         pi.pain.lines(xp, yp, 4, LColor::foreground);
555                         break;
556                 }
557
558                 case lSLASH:
559                 case LSLASH: {
560                         int xp[2], yp[2];
561
562                         xp[0] = x;
563                         yp[0] = y + int(3 * hg);
564
565                         xp[1] = int(x + dim_.wid * 0.75);
566                         yp[1] = y + int(hg);
567
568                         pi.pain.lines(xp, yp, 2, LColor::foreground);
569                         break;
570                 }
571
572                 case DOT_LESS_I: // dotless-i
573                 case DOT_LESS_J: // dotless-j
574                         // nothing to do for these
575                         break;
576                 }
577
578         } else {
579                 pi.pain.fillRectangle(x + 1,
580                                       baseline - dim_.asc + 1, dim_.wid - 2,
581                                       dim_.asc + dim_.des - 2,
582                                       backgroundColor());
583                 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
584                                   dim_.wid - 2, dim_.asc + dim_.des - 2,
585                                   LColor::foreground);
586                 pi.pain.text(x + 2, baseline, contents, font);
587         }
588 }
589
590
591 void InsetLatexAccent::write(Buffer const &, ostream & os) const
592 {
593         os << "\\i " << contents << "\n";
594 }
595
596
597 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
598 {
599         lex.eatLine();
600         contents = lex.getString();
601         checkContents();
602 }
603
604
605 int InsetLatexAccent::latex(Buffer const &, ostream & os,
606                             OutputParams const &) const
607 {
608         os << contents;
609         return 0;
610 }
611
612
613 int InsetLatexAccent::plaintext(Buffer const &, ostream & os,
614                             OutputParams const &) const
615 {
616         os << contents;
617         return 0;
618 }
619
620
621 int InsetLatexAccent::linuxdoc(Buffer const &, ostream & os,
622                                OutputParams const &) const
623 {
624         os << contents;
625         return 0;
626 }
627
628
629 int InsetLatexAccent::docbook(Buffer const &, ostream & os,
630                               OutputParams const &) const
631 {
632         os << contents;
633         return 0;
634 }
635
636
637 bool InsetLatexAccent::directWrite() const
638 {
639         return true;
640 }
641
642
643 InsetOld::Code InsetLatexAccent::lyxCode() const
644 {
645         return InsetOld::ACCENT_CODE;
646 }
647
648
649 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
650 {
651         return o << int(at);
652 }