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