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