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