]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
780da5c0e5c2c3348a8924fcf5ab9d911f981423
[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-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. Bjønnes, larsbj@lyx.org */
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;
244                             j < contents.length(); ++j)
245                                 temp+= contents[j];
246                         contents= temp;
247                         ++i;
248                         remdot = true;
249                 }    
250
251                 // demand a '}' at the end
252                 if (contents[++i] != '}' && contents[++i]) return;
253                                            
254                 // fine, the char is properly decoded now (hopefully)
255                 lyxerr.debug() << "Contents: [" << contents << "]"
256                                << ", ic: " << ic
257                                << ", top: " << plusasc 
258                                << ", bot: " << plusdesc 
259                                << ", dot: " << remdot
260                                << ", mod: " << modtype << endl;
261         }
262         candisp = true;
263 }
264
265
266 int InsetLatexAccent::Ascent(LyXFont const & font) const
267 {
268         // This function is a bit too simplistix and is just a
269         // "try to make a fit for all accents" approach, to
270         // make it better we need to know what kind of accent is
271         // used and add to max based on that.
272         int max;
273         if (candisp) {
274                 if (ic == ' ')
275                         max = font.ascent('a');
276                 else
277                         max = font.ascent(ic);
278                 if (plusasc) 
279                         max += (font.maxAscent() + 3) / 3;
280         } else
281                 max = font.maxAscent() + 4;
282         return max;
283 }
284
285
286 int InsetLatexAccent::Descent(LyXFont const & font) const
287 {
288         int max;
289         if (candisp) {
290                 if (ic == ' ') 
291                         max = font.descent('a');
292                 else 
293                         max = font.descent(ic);
294                 if (plusdesc)
295                         max += 3;
296         } else
297                 max = font.maxDescent() + 4;
298         return max;
299 }
300
301
302 int InsetLatexAccent::Width(LyXFont const & font) const
303 {
304         if (candisp)
305                 return font.textWidth(&ic, 1);
306         else
307                 return font.stringWidth(contents) + 4;
308 }
309
310
311 int InsetLatexAccent::Lbearing(LyXFont const & font) const
312 {
313         return font.lbearing(ic);
314 }
315
316
317 int InsetLatexAccent::Rbearing(LyXFont const & font) const
318 {
319         return font.rbearing(ic);
320 }
321
322
323 bool InsetLatexAccent::DisplayISO8859_9(LyXFont font,
324                                         LyXScreen & scr,
325                                         int baseline, 
326                                         float & x)
327 {
328         unsigned char tmpic = ic;
329         
330         switch (modtype) {
331         case CEDILLA:
332         {
333                 if (ic == 'c') tmpic = 0xe7;
334                 if (ic == 'C') tmpic = 0xc7;
335                 if (ic == 's') tmpic = 0xfe;
336                 if (ic == 'S') tmpic = 0xde;
337                 break;
338         }
339         case BREVE:
340         {       if (ic == 'g') tmpic = 0xf0;
341         if (ic == 'G') tmpic = 0xd0;
342         break;
343         }
344         case UMLAUT:
345         {
346                 if (ic == 'o') tmpic = 0xf6;
347                 if (ic == 'O') tmpic = 0xd6;
348                 if (ic == 'u') tmpic = 0xfc;
349                 if (ic == 'U') tmpic = 0xdc;
350                 break;
351         }
352         case DOT:        if (ic == 'I') tmpic = 0xdd; break;
353         case DOT_LESS_I: tmpic = 0xfd; break;
354         default:         return false;
355         }
356         if (tmpic != ic) {
357                 char ch = char(tmpic);
358                 scr.drawText(font, &ch, 1, baseline, int(x));
359                 x += Width (font);
360                 return true;
361         }
362         else
363                 return false;
364 }
365
366
367 void InsetLatexAccent::Draw(LyXFont font,
368                             LyXScreen & scr,
369                             int baseline, 
370                             float & x)
371 {
372         if (lyxrc->font_norm == "iso8859-9")
373                 if (DisplayISO8859_9 (font, scr, baseline, x))  
374                         return;
375         
376         /* draw it! */ 
377         // All the manually drawn accents in this function could use an
378         // overhaul. Different ways of drawing (what metrics to use)
379         // should also be considered.
380         
381         if (candisp) {
382                 int asc = Ascent(font);
383                 int desc = Descent(font);
384                 int wid = Width(font);
385                 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
386                 float hg35;
387                 float hg;
388                 int y;
389                 if (plusasc) {
390                         // mark at the top
391                         hg = font.maxDescent();
392                         y = baseline - asc;
393
394                         if (font.shape() == LyXFont::ITALIC_SHAPE)
395                                 x2 += (4.0 * hg) / 5.0; // italic
396                 } else {
397                         // at the bottom
398                         hg = desc;
399                         y = baseline;
400                 }
401
402                 hg35 = float(hg * 3.0) / 5.0;
403
404                 // display with proper accent mark
405                 // first the letter
406                 scr.drawText(font, &ic, 1, baseline, int(x));
407
408                 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
409
410                 if (remdot) {
411                         int tmpvar = baseline - font.ascent('i');
412                         float tmpx = 0;
413                         if (font.shape() == LyXFont::ITALIC_SHAPE)
414                                 tmpx += (8.0 * hg) / 10.0; // italic
415                         lyxerr.debug() << "Removing dot." << endl;
416                         // remove the dot first
417                         scr.fillRectangle(gc_clear, int(x + tmpx),
418                                           tmpvar, wid,
419                                           font.ascent('i') -
420                                           font.ascent('x') - 1);
421                         // the five lines below is a simple hack to
422                         // make the display of accent 'i' and 'j'
423                         // better. It makes the accent be written
424                         // closer to the top of the dot-less 'i' or 'j'.
425                         char tmpic = ic; // store the ic when we
426                         ic = 'x';        // calculates the ascent of
427                         asc = Ascent(font); // the dot-less version (here: 'x')
428                         ic = tmpic;      // set the orig ic back
429                         y = baseline - asc; // update to new y coord.
430                 }
431                 // now the rest - draw within (x, y, x+wid, y+hg)
432                 switch (modtype) {
433                 case ACUTE:     // acute
434                 {
435                         scr.drawLine(pgc, int(x2), int(y + hg35),
436                                      int(x2 + hg35), y);
437                         break;
438                 }
439                 case GRAVE:     // grave
440                 {
441                         scr.drawLine(pgc, int(x2), int(y + hg35),
442                                      int(x2 - hg35), y); 
443                         break;
444                 }
445                 case MACRON:     // macron
446                 {
447                         scr.drawLine(pgc,
448                                      int(x2 - (3.0 * wid / 7.0)),
449                                      int(y + (hg / 2.0) + hg35),
450                                      int(x2 + (3.0 * wid / 7.0)),
451                                      int(y + (hg / 2.0) + hg35));
452                         break;
453                 }
454                 case TILDE:     // tilde
455                 {
456                         if (hg35 > 2.0) hg35 -= 1.0;
457                         x2 += (hg35 / 2.0);
458                         XPoint p[4];
459                         p[0].x = int(x2 - 2.0 * hg35); p[0].y = int(y + hg);
460                         p[1].x = int(x2 - hg35);   p[1].y = int(y + hg35);
461                         p[2].x = int(x2);          p[2].y = int(y + hg);
462                         p[3].x = int(x2 + hg35);   p[3].y = int(y + hg35);
463                         scr.drawLines(pgc, p, 4);
464                         break;
465                 }
466                 case UNDERBAR:     // underbar
467                 {
468                         scr.drawLine(pgc,
469                                      int(x2 - (3.0 * wid / 7.0)),
470                                      y + (hg / 2.0),
471                                      int(x2 + (3.0 * wid / 7.0)),
472                                      y + (hg / 2.0));
473                         break;
474                 }
475                 case CEDILLA:     // cedilla
476                 {
477                         XPoint p[4];
478                         p[0].x = int(x2);          p[0].y = y;
479                         p[1].x = int(x2);          p[1].y = y + int(hg / 3.0);
480                         p[2].x = int(x2 + (hg / 3.0));
481                         p[2].y = y + int(hg / 2.0);
482                         p[3].x = int(x2 - (hg / 4.0)); p[3].y = y + int(hg);
483                         scr.drawLines(pgc, p, 4);
484                         break;
485                 }
486                 case UNDERDOT:     // underdot
487                 case DOT:    // dot
488                 {
489                         scr.fillArc(pgc, int(x2), y + (hg / 2.0),
490                                     1, 1, 0, 360*64); 
491                         break;
492                 }
493                 case CIRCLE:     // circle
494                 {
495                         scr.drawArc(pgc, int(x2 - (hg / 2.0)),
496                                     y + (hg / 2.0), hg, hg, 0,
497                                     360*64);
498                         break;
499                 }
500                 case TIE:     // tie
501                 {
502                         scr.drawArc(pgc,
503                                     int(x2), y + (hg / 4.0),
504                                     hg, hg, 0, 11519);
505                         break;
506                 }
507                 case BREVE:     // breve
508                 {
509                         scr.drawArc(pgc,
510                                     int(x2 - (hg / 2.0)), y - (hg / 4.0),
511                                     hg, hg, 0, -11519);
512                         break;
513                 }
514                 case CARON:    // caron
515                 {
516                         XPoint p[3];
517                         p[0].x = int(x2 - hg35); p[0].y = y;
518                         p[1].x = int(x2);        p[1].y = int(y + hg35);
519                         p[2].x = int(x2 + hg35); p[2].y = y;
520                         scr.drawLines(pgc, p, 3);
521                         break;
522                 }
523                 case SPECIAL_CARON:    // special caron
524                 {
525                         switch (ic) {
526                         case 'L': wid = int(4.0 * wid / 5.0); break;
527                         case 't': y -= int(hg35 / 2.0); break;
528                         }
529                         XPoint p[3];
530                         p[0].x = int(x + wid);   p[0].y = int(y + hg35 + hg);
531                         p[1].x = int(x + wid + (hg35 / 2.0));
532                         p[1].y = int(y + hg + (hg35 / 2.0));
533                         p[2].x = int(x + wid + (hg35 / 2.0));
534                         p[2].y = y + int(hg);
535                         scr.drawLines(pgc, p, 3);
536                         break;
537                 }
538                 case HUNGARIAN_UMLAUT:    // hung. umlaut
539                 {
540                         XSegment s[2];
541                         s[0].x1= int(x2 - (hg / 2.0));     s[0].y1 = int(y + hg35);
542                         s[0].x2= int(x2 + hg35 - (hg / 2.0)); s[0].y2 = y;
543                         s[1].x1= int(x2 + (hg / 2.0));
544                         s[1].y1 = int(y + hg35);
545                         s[1].x2= int(x2 + hg35 + (hg / 2.0)); s[1].y2 = y;
546
547                         scr.drawSegments(pgc, s, 2);
548                         break;
549                 }
550                 case UMLAUT:    // umlaut
551                 {
552                         float tmpadd = y;
553                         tmpadd += (remdot) ?
554                                 asc / 3.0 :
555                                 asc / 5.0; // if (remdot) -> i or j
556                         float rad = hg / 2.0;
557                         if (rad <= 1.0) {
558                                 scr.drawPoint(pgc,
559                                               int(x2 - ((4.0 * hg) / 7.0)),
560                                               tmpadd);
561                                 scr.drawPoint(pgc,
562                                               int(x2 + ((4.0 * hg) / 7.0)),
563                                               tmpadd);
564                         } else {
565                                 rad += .5; // this ensures that f.ex. 1.5 will
566                                 // not be rounded down to .5 and then
567                                 // converted to int = 0
568                                 scr.fillArc(pgc, int(x2 - ((2.0 * hg) / 4.0)),
569                                             tmpadd,
570                                             rad, rad, 0, 360*64);
571                                 scr.fillArc(pgc, int(x2 + ((2.0 * hg) / 4.0)),
572                                             tmpadd,
573                                             rad, rad, 0, 360*64);
574                         }
575                         break;
576                 }
577                 case CIRCUMFLEX:    // circumflex
578                 {
579                         XPoint p[3];
580                         p[0].x = int(x2 - hg35); p[0].y = y + int(hg);
581                         p[1].x = int(x2);        p[1].y = int(y + hg35);
582                         p[2].x = int(x2 + hg35); p[2].y = y + int(hg);
583                         scr.drawLines(pgc, p, 3);
584                         break;
585                 }
586                 case OGONEK:    // ogonek
587                 {
588                         // this does probably not look like an ogonek, so
589                         // it should certainly be refined
590                         XPoint p[4];
591                         p[0].x = int(x2);          p[0].y = y;
592                         p[1].x = int(x2);          p[1].y = y + int(hg / 3.0);
593                         p[2].x = int(x2 - (hg / 3.0));
594                         p[2].y = y + int(hg / 2.0);
595                         p[3].x = int(x2 + (hg / 4.0)); p[3].y = y + int(hg);
596                         scr.drawLines(pgc, p, 4);
597                         break;
598                 }
599                 case lSLASH:
600                 case LSLASH:
601                 {
602                         XPoint p[2];
603                         p[0].x = int(x);
604                         p[0].y = y + int(3.0 * hg);
605                         p[1].x = int(x + float(wid) * 0.75);
606                         p[1].y = y + int(hg);
607                         scr.drawLines(pgc, p, 2);
608                         break;
609                 }
610                 case DOT_LESS_I: // dotless-i
611                 case DOT_LESS_J: // dotless-j
612                 {
613                         // nothing to do for these
614                         break;
615                 }
616                 }
617         } else {
618                 scr.fillRectangle(gc_lighted,
619                                   int(x + 1), baseline - Ascent(font) + 1,
620                                   Width(font) - 2,
621                                   Ascent(font) + Descent(font) - 2);
622                 
623                 scr.drawRectangle(gc_lighted,
624                                   int(x), baseline - Ascent(font),
625                                   Width(font) - 1,
626                                   Ascent(font) + Descent(font) - 1);
627                 scr.drawString(font, contents, baseline, int(x + 2));
628         }
629         x +=  Width(font);
630 }
631
632
633 void InsetLatexAccent::Write(ostream & os)
634 {
635         os << "\\i " << contents << "\n";
636 }
637
638
639 void InsetLatexAccent::Read(LyXLex & lex)
640 {
641         lex.EatLine();
642         contents = lex.GetString();
643         checkContents();
644 }
645
646
647 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/)
648 {
649         os << contents;
650         return 0;
651 }
652
653
654 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/)
655 {
656         file += contents;
657         return 0;
658 }
659
660
661 int InsetLatexAccent::Linuxdoc(string & file)
662 {
663         file += contents;
664         return 0;
665 }
666
667
668 int InsetLatexAccent::DocBook(string & file)
669 {
670         file += contents;
671         return 0;
672 }
673
674
675 bool InsetLatexAccent::Deletable() const
676 {
677         return true;
678 }
679
680
681 bool InsetLatexAccent::DirectWrite() const
682 {
683         return true;
684 }
685
686
687 Inset * InsetLatexAccent::Clone() const
688 {
689         return new InsetLatexAccent(contents);
690 }
691
692
693 Inset::Code InsetLatexAccent::LyxCode() const
694 {
695         return Inset::ACCENT_CODE;
696 }
697
698
699 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
700 {
701         return o << int(at);
702 }