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