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