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