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