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