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