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