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