]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
two-phase-drawing for InsetText & InsetTabular
[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
30
31 /* LatexAccent. Proper handling of accented characters */
32 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
33 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
34
35 InsetLatexAccent::InsetLatexAccent()
36         : candisp(false)
37 {}
38
39
40 InsetLatexAccent::InsetLatexAccent(string const & str)
41         : contents(str)
42 {
43         checkContents();
44 }
45
46
47 void InsetLatexAccent::checkContents()
48         // check, if we know the modifier and can display it ok on screen
49 {
50         candisp = false;
51
52         if (contents.empty() || contents.length() < 2) {
53                 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
54                 return;
55         }
56
57         // REMOVE IN 0.13
58         // Dirty Hack for backward compability. remove in 0.13 (Lgb)
59         contents = trim(contents);
60         if (!contains(contents, "{") && !contains(contents, "}")) {
61                 if (contents.length() == 2) {
62                         string tmp;
63                         tmp += contents[0];
64                         tmp += contents[1];
65                         tmp += "{}";
66                         contents = tmp;
67                 } else if (contents.length() == 3) {
68                         string tmp;
69                         tmp += contents[0];
70                         tmp += contents[1];
71                         tmp += '{';
72                         tmp += contents[2];
73                         tmp += '}';
74                         contents = tmp;
75                 } else if (contents.length() == 4 && contents[2] == ' ') {
76                         string tmp;
77                         tmp += contents[0];
78                         tmp += contents[1];
79                         tmp += '{';
80                         tmp += contents[3];
81                         tmp += '}';
82                         contents = tmp;
83                 } else if  (contents.length() == 4 && contents[2] == '\\'
84                             && (contents[3] == 'i' || contents[3] == 'j')) {
85                         string tmp;
86                         tmp += contents[0];
87                         tmp += contents[1];
88                         tmp += '{';
89                         tmp += contents[2];
90                         tmp += contents[3];
91                         tmp += '}';
92                         contents = tmp;
93                 }
94         }
95         if (contents[0] != '\\') { // demand that first char is a '\\'
96                 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
97                 return;
98         }
99
100         lyxerr[Debug::KEY] << "Decode: " << contents << endl;
101
102         remdot = false;
103         plusasc = false;
104         plusdesc = false;
105
106         switch (contents[1]) { // second char should be one of these
107         case '\'':  // acute
108                 modtype = ACUTE;    // acute
109                 plusasc = true;    // at the top of character
110                 break;
111         case '`':   // grave
112                 modtype = GRAVE;    // grave
113                 plusasc = true;    // at the top
114                 break;
115         case '=':   // macron
116                 modtype = MACRON;    // macron
117                 plusasc = true;    // at the top
118                 break;
119         case '~':   // tilde
120                 modtype = TILDE;    // tilde
121                 plusasc = true;    // at the top
122                 break;
123         case 'b':   // underbar
124                 modtype = UNDERBAR;    // underbar
125                 plusdesc = true;   // at the bottom
126                 break;
127         case 'c':   // cedilla
128                 modtype = CEDILLA;    // cedilla
129                 plusdesc = true;   // at the bottom
130                 break;
131         case 'd':   // underdot
132                 modtype = UNDERDOT;    // underdot
133                 plusdesc = true;   // at the bottom
134                 break;
135         case 'r':   // circle
136                 modtype = CIRCLE;    // circle
137                 plusasc = true;    // at the top
138                 break;
139         case 't':   // tie
140                 modtype = TIE;    // tie
141                 plusasc = true;    // at the top
142                 break;
143         case 'u':   // breve
144                 modtype = BREVE;    // breve
145                 plusasc = true;    // at the top
146                 break;
147         case 'v':   // caron
148                 modtype = CARON;   // caron
149                 plusasc = true;    // at the top
150                 break;
151         case 'q':   // special caron
152                 modtype = SPECIAL_CARON;   // special caron
153                 plusasc = true;    // at the top
154                 break;
155         case 'H':   // hungarian umlaut
156                 modtype = HUNGARIAN_UMLAUT;   // hungarian umlaut
157                 plusasc = true;    // at the top
158                 break;
159         case '"':   // umlaut
160                 modtype = UMLAUT;   // umlaut
161                 plusasc = true;    // at the top
162                 break;
163         case '.':   // dot
164                 modtype = DOT;   // dot
165                 plusasc = true;    // at the top
166                 break;
167         case '^':   // circumflex
168                 modtype = CIRCUMFLEX;   // circumflex
169                 plusasc = true;    // at the top
170                 break;
171         case 'k':   // ogonek
172                 modtype = OGONEK;  // ogonek
173                 plusdesc = true;
174                 break;
175         case 'i': // dot-less-i
176                 modtype = DOT_LESS_I;  // dot-less-i
177                 plusasc = true; // at the top (not really needed)
178                 remdot = true;
179                 break;
180         case 'j': // dot-less-j
181                 modtype = DOT_LESS_J; // dot-less-j
182                 plusasc = true; // at the top (not really needed)
183                 remdot = true;
184                 break;
185         case 'l': // lslash
186                 modtype = lSLASH;
187                 plusasc = true; // at the top (not really needed)
188                 break;
189         case 'L': // lslash
190                 modtype = LSLASH;
191                 plusasc = true; // at the top (not really needed)
192                 break;
193         default:
194                 lyxerr[Debug::KEY] << "Default" << endl;
195                 // unknow accent (or something else)
196                 return;
197         }
198
199         // we demand that third char is a '{' (Lgb)
200         if (contents[2] != '{') return;
201
202         // special clause for \i{}, \j{} \l{} and \L{}
203         if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
204              || modtype == lSLASH || modtype == LSLASH)
205             && contents[3] == '}') {
206                 switch (modtype) {
207                 case DOT_LESS_I: ic = 'i'; break;
208                 case DOT_LESS_J: ic = 'j'; break;
209                 case lSLASH:     ic = 'l'; break;
210                 case LSLASH:     ic = 'L'; break;
211                 default:
212                         // if this happens something is really wrong
213                         lyxerr << "InsetLaTexAccent: weird error." << endl;
214                         break;
215                 }
216                 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
217                 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
218                                    << ", ic: " << ic
219                                    << ", top: " << plusasc
220                                    << ", bot: " << plusdesc
221                                    << ", dot: " << remdot
222                                    << ", mod: " << modtype << endl;
223                 // Special case for space
224         } else if (contents[3] == '}') {
225                 ic = ' ';
226         } else {
227                 int i = 3;
228
229                 // now get the char
230                 ic = contents[3]; // i will always be 3 here
231
232                 // ic should now be a alfa-char or '\\'
233                 if (ic == '\\') {
234                         ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
235                         if (ic == 'i' || ic == 'j')
236                                 remdot = true;
237                         else
238                                 return;
239                 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
240                         // Do a rewrite: \<foo>{i} --> \<foo>{\i}
241                         string temp = contents;
242                         temp.erase(3, string::npos);
243                         temp += '\\';
244                         temp += char(ic);
245                         for (string::size_type j = 4;
246                             j < contents.length(); ++j)
247                                 temp+= contents[j];
248                         contents= temp;
249                         ++i;
250                         remdot = true;
251                 }
252
253                 // demand a '}' at the end
254                 if (contents[++i] != '}' && contents[++i]) return;
255
256                 // fine, the char is properly decoded now (hopefully)
257                 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
258                                    << ", ic: " << ic
259                                    << ", top: " << plusasc
260                                    << ", bot: " << plusdesc
261                                    << ", dot: " << remdot
262                                    << ", mod: " << modtype << endl;
263         }
264         candisp = true;
265 }
266
267
268 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
269 {
270         LyXFont & font = mi.base.font;
271         // This function is a bit too simplistic and is just a
272         // "try to make a fit for all accents" approach, to
273         // make it better we need to know what kind of accent is
274         // used and add to max based on that.
275         if (candisp) {
276                 if (ic == ' ')
277                         dim.asc = font_metrics::ascent('a', font);
278                 else
279                         dim.asc = font_metrics::ascent(ic, font);
280                 if (plusasc)
281                         dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
282
283                 if (ic == ' ')
284                         dim.des = font_metrics::descent('a', font);
285                 else
286                         dim.des = font_metrics::descent(ic, font);
287                 if (plusdesc)
288                         dim.des += 3;
289
290                 dim.wid = font_metrics::width(ic, font);
291         } else {
292                 dim.asc = font_metrics::maxAscent(font) + 4;
293                 dim.des = font_metrics::maxDescent(font) + 4;
294                 dim.wid = font_metrics::width(contents, font) + 4;
295         }
296         dim_ = dim;
297 }
298
299
300 int InsetLatexAccent::lbearing(LyXFont const & font) const
301 {
302         return font_metrics::lbearing(ic, font);
303 }
304
305
306 int InsetLatexAccent::rbearing(LyXFont const & font) const
307 {
308         return font_metrics::rbearing(ic, font);
309 }
310
311
312 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
313 {
314         unsigned char tmpic = ic;
315
316         switch (modtype) {
317         case CEDILLA:
318         {
319                 if (ic == 'c') tmpic = 0xe7;
320                 if (ic == 'C') tmpic = 0xc7;
321                 if (ic == 's') tmpic = 0xfe;
322                 if (ic == 'S') tmpic = 0xde;
323                 break;
324         }
325         case BREVE:
326         {       if (ic == 'g') tmpic = 0xf0;
327         if (ic == 'G') tmpic = 0xd0;
328         break;
329         }
330         case UMLAUT:
331         {
332                 if (ic == 'o') tmpic = 0xf6;
333                 if (ic == 'O') tmpic = 0xd6;
334                 if (ic == 'u') tmpic = 0xfc;
335                 if (ic == 'U') tmpic = 0xdc;
336                 break;
337         }
338         case DOT:        if (ic == 'I') tmpic = 0xdd; break;
339         case DOT_LESS_I: tmpic = 0xfd; break;
340         default:         return false;
341         }
342         if (tmpic != ic) {
343                 pi.pain.text(x, y, char(tmpic), pi.base.font);
344                 return true;
345         }
346         else
347                 return false;
348 }
349
350
351 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
352 {
353         if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
354                 if (displayISO8859_9(pi, x, baseline))
355                         return;
356
357         /* draw it! */
358         // All the manually drawn accents in this function could use an
359         // overhaul. Different ways of drawing (what metrics to use)
360         // should also be considered.
361
362         BufferView * bv = pi.base.bv;
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 InsetBase * InsetLatexAccent::clone() const
654 {
655         return new InsetLatexAccent(contents);
656 }
657
658
659 Inset::Code InsetLatexAccent::lyxCode() const
660 {
661         return Inset::ACCENT_CODE;
662 }
663
664
665 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
666 {
667         return o << int(at);
668 }