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