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