]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
patch from Dekel + some simplifications
[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 "BufferView.h"
22 #include "Painter.h"
23 #include "font.h"
24
25 using std::ostream;
26 using std::endl;
27
28 /* LatexAccent. Proper handling of accented characters */
29 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
30 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
31
32 InsetLatexAccent::InsetLatexAccent()
33         : candisp(false)
34 {}
35
36
37 InsetLatexAccent::InsetLatexAccent(string const & str)
38         : contents(str)
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(BufferView *, 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(BufferView *, 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(BufferView *, 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(BufferView * bv, 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                 bv->painter().text(int(x), baseline, ch, font);
349                 x += width(bv, font);
350                 return true;
351         }
352         else
353                 return false;
354 }
355
356
357 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
358                             int baseline, float & x, bool) const
359 {
360         Painter & pain = bv->painter();
361
362         if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
363                 if (DisplayISO8859_9(bv, font0, baseline, x))   
364                         return;
365         
366         /* draw it! */ 
367         // All the manually drawn accents in this function could use an
368         // overhaul. Different ways of drawing (what metrics to use)
369         // should also be considered.
370
371         LyXFont font(font0);
372         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
373                 font.setLanguage(english_language);
374         
375         if (candisp) {
376                 int asc = ascent(bv, font);
377                 int desc = descent(bv, font);
378                 int wid = width(bv, font);
379                 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
380                 float hg;
381                 int y;
382                 if (plusasc) {
383                         // mark at the top
384                         hg = lyxfont::maxDescent(font);
385                         y = baseline - asc;
386
387                         if (font.shape() == LyXFont::ITALIC_SHAPE)
388                                 x2 += (4.0 * hg) / 5.0; // italic
389                 } else {
390                         // at the bottom
391                         hg = desc;
392                         y = baseline;
393                 }
394
395                 float hg35 = float(hg * 3.0) / 5.0;
396
397                 // display with proper accent mark
398                 // first the letter
399                 pain.text(int(x), baseline, ic, font);
400
401                 if (remdot) {
402                         int tmpvar = baseline - lyxfont::ascent('i', font);
403                         float tmpx = 0;
404                         if (font.shape() == LyXFont::ITALIC_SHAPE)
405                                 tmpx += (8.0 * hg) / 10.0; // italic
406                         lyxerr[Debug::KEY] << "Removing dot." << endl;
407                         // remove the dot first
408                         pain.fillRectangle(int(x + tmpx), tmpvar, wid,
409                                            lyxfont::ascent('i', font) -
410                                            lyxfont::ascent('x', font) - 1,
411                                            LColor::background);
412                         // the five lines below is a simple hack to
413                         // make the display of accent 'i' and 'j'
414                         // better. It makes the accent be written
415                         // closer to the top of the dot-less 'i' or 'j'.
416                         char tmpic = ic; // store the ic when we
417                         ic = 'x';        // calculates the ascent of
418                         asc = ascent(bv, font); // the dot-less version (here: 'x')
419                         ic = tmpic;      // set the orig ic back
420                         y = baseline - asc; // update to new y coord.
421                 }
422                 // now the rest - draw within (x, y, x+wid, y+hg)
423                 switch (modtype) {
424                 case ACUTE:     // acute 0xB4
425                 {
426                         pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
427                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
428                                   char(0xB4), font);
429                         break;
430                 }
431                 case GRAVE:     // grave 0x60
432                 {
433                         pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
434                                   int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
435                                   char(0x60), font);
436                         break;
437                 }
438                 case MACRON:     // macron
439                 {
440                         pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
441                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
442                                   char(0xAF), font);
443                         break;
444                 }
445                 case TILDE:     // tilde
446                 {
447                         pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
448                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
449                                   '~', font);
450                         break;
451                 }
452                 case UNDERBAR:     // underbar 0x5F
453                 {
454                         pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
455                                   char(0x5F), font);
456                         break;
457                 }
458                 case CEDILLA:     // cedilla
459                 {
460                         pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
461                                   char(0xB8), font);
462                         
463                         break;
464                 }
465                 case UNDERDOT:     // underdot
466                 {
467                         pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
468                                   int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
469                                   '.', font);
470                         break;
471                 }
472
473                 case DOT:    // dot
474                 {
475                         pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
476                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
477                                   '.', font);
478                         break;
479                 }
480
481                 case CIRCLE:     // circle
482                 {
483                         LyXFont tmpf(font);
484                         tmpf.decSize().decSize();
485                         pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
486                                   int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
487                                   char(0xB0), tmpf);
488                         break;
489                 }
490                 case TIE:     // tie
491                 {
492                         pain.arc(int(x2 + hg35), int(y + hg / 2.0),
493                                  int(2 * hg), int(hg), 0, 360 * 32);
494                         break;
495                 }
496                 case BREVE:     // breve
497                 {
498                         pain.arc(int(x2 - (hg / 2.0)), y,
499                                  int(hg), int(hg), 0, -360*32);
500                         break;
501                 }
502                 case CARON:    // caron
503                 {
504                         int xp[3], yp[3];
505                         
506                         xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
507                         xp[1] = int(x2);        yp[1] = int(y + hg);
508                         xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
509                         pain.lines(xp, yp, 3);
510                         break;
511                 }
512                 case SPECIAL_CARON:    // special caron
513                 {
514                         switch (ic) {
515                         case 'L': wid = int(4.0 * wid / 5.0); break;
516                         case 't': y -= int(hg35 / 2.0); break;
517                         }
518                         int xp[3], yp[3];
519                         xp[0] = int(x + wid);
520                         yp[0] = int(y + hg35 + hg);
521                         
522                         xp[1] = int(x + wid + (hg35 / 2.0));
523                         yp[1] = int(y + hg + (hg35 / 2.0));
524                         
525                         xp[2] = int(x + wid + (hg35 / 2.0));
526                         yp[2] = y + int(hg);
527
528                         pain.lines(xp, yp, 3);
529                         break;
530                 }
531                 case HUNGARIAN_UMLAUT:    // hung. umlaut
532                 {
533                         pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
534                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
535                                   '´', font);
536                         pain.text(int(x2),
537                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
538                                   '´', font);
539                         break;
540                 }
541                 case UMLAUT:    // umlaut
542                 {
543                         pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
544                                   baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
545                                   '¨', font);
546                         break;
547                 }
548                 case CIRCUMFLEX:    // circumflex
549                 {
550                         LyXFont tmpf(font);
551                         tmpf.decSize().decSize().decSize();
552                         pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
553                                   int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
554                                   char(0x5E), tmpf);
555                         break;
556                 }
557                 case OGONEK:    // ogonek
558                 {
559                         // this does probably not look like an ogonek, so
560                         // it should certainly be refined
561                         int xp[4], yp[4];
562                         
563                         xp[0] = int(x2);
564                         yp[0] = y;
565                         
566                         xp[1] = int(x2);
567                         yp[1] = y + int(hg35);
568                         
569                         xp[2] = int(x2 - hg35);
570                         yp[2] = y + int(hg / 2.0);
571                         
572                         xp[3] = int(x2 + hg / 4.0);
573                         yp[3] = y + int(hg);
574
575                         pain.lines(xp, yp, 4);
576                         break;
577                 }
578                 case lSLASH:
579                 case LSLASH:
580                 {
581                         int xp[2], yp[2];
582                         
583                         xp[0] = int(x);
584                         yp[0] = y + int(3.0 * hg);
585                         
586                         xp[1] = int(x + float(wid) * 0.75);
587                         yp[1] = y + int(hg);
588                         
589                         pain.lines(xp, yp, 2);
590                         break;
591                 }
592                 case DOT_LESS_I: // dotless-i
593                 case DOT_LESS_J: // dotless-j
594                 {
595                         // nothing to do for these
596                         break;
597                 }
598                 }
599         } else {
600                 pain.fillRectangle(int(x + 1),
601                                    baseline - ascent(bv, font) + 1,
602                                    width(bv, font) - 2,
603                                    ascent(bv, font)
604                                    + descent(bv, font) - 2);
605                 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
606                                width(bv, font) - 2,
607                                ascent(bv, font) + descent(bv, font) - 2);
608                 pain.text(int(x + 2), baseline, contents, font);
609         }
610         x +=  width(bv, font);
611 }
612
613
614 void InsetLatexAccent::Write(Buffer const *, ostream & os) const
615 {
616         os << "\\i " << contents << "\n";
617 }
618
619
620 void InsetLatexAccent::Read(Buffer const *, LyXLex & lex)
621 {
622         lex.EatLine();
623         contents = lex.GetString();
624         checkContents();
625 }
626
627
628 int InsetLatexAccent::Latex(Buffer const *, ostream & os,
629                             bool /*fragile*/, bool/*fs*/) const
630 {
631         os << contents;
632         return 0;
633 }
634
635
636 int InsetLatexAccent::Ascii(Buffer const *, ostream & os, int) const
637 {
638         os << contents;
639         return 0;
640 }
641
642
643 int InsetLatexAccent::Linuxdoc(Buffer const *, ostream & os) const
644 {
645         os << contents;
646         return 0;
647 }
648
649
650 int InsetLatexAccent::DocBook(Buffer const *, ostream & os) const
651 {
652         os << contents;
653         return 0;
654 }
655
656
657 bool InsetLatexAccent::Deletable() const
658 {
659         return true;
660 }
661
662
663 bool InsetLatexAccent::DirectWrite() const
664 {
665         return true;
666 }
667
668
669 Inset * InsetLatexAccent::Clone(Buffer const &) const
670 {
671         return new InsetLatexAccent(contents);
672 }
673
674
675 Inset::Code InsetLatexAccent::LyxCode() const
676 {
677         return Inset::ACCENT_CODE;
678 }
679
680
681 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
682 {
683         return o << int(at);
684 }