]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
Rename LatexRunParams::fragile as moving_arg.
[lyx.git] / src / insets / insetlatexaccent.C
1 /**
2  * \file insetlatexaccent.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "insetlatexaccent.h"
14
15 #include "debug.h"
16 #include "dimension.h"
17 #include "lyxrc.h"
18 #include "support/lstrings.h"
19 #include "BufferView.h"
20 #include "frontends/Painter.h"
21 #include "frontends/font_metrics.h"
22 #include "language.h"
23 #include "lyxlex.h"
24
25 using std::ostream;
26 using std::endl;
27
28
29 /* LatexAccent. Proper handling of accented characters */
30 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
31 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
32
33 InsetLatexAccent::InsetLatexAccent()
34         : candisp(false)
35 {}
36
37
38 InsetLatexAccent::InsetLatexAccent(string const & str)
39         : contents(str)
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) {
51                 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
52                 return;
53         }
54
55         // REMOVE IN 0.13
56         // Dirty Hack for backward compability. remove in 0.13 (Lgb)
57         contents = trim(contents);
58         if (!contains(contents, "{") && !contains(contents, "}")) {
59                 if (contents.length() == 2) {
60                         string tmp;
61                         tmp += contents[0];
62                         tmp += contents[1];
63                         tmp += "{}";
64                         contents = tmp;
65                 } else if (contents.length() == 3) {
66                         string tmp;
67                         tmp += contents[0];
68                         tmp += contents[1];
69                         tmp += '{';
70                         tmp += contents[2];
71                         tmp += '}';
72                         contents = tmp;
73                 } else if (contents.length() == 4 && contents[2] == ' ') {
74                         string tmp;
75                         tmp += contents[0];
76                         tmp += contents[1];
77                         tmp += '{';
78                         tmp += contents[3];
79                         tmp += '}';
80                         contents = tmp;
81                 } else if  (contents.length() == 4 && contents[2] == '\\'
82                             && (contents[3] == 'i' || contents[3] == 'j')) {
83                         string tmp;
84                         tmp += contents[0];
85                         tmp += contents[1];
86                         tmp += '{';
87                         tmp += contents[2];
88                         tmp += contents[3];
89                         tmp += '}';
90                         contents = tmp;
91                 }
92         }
93         if (contents[0] != '\\') { // demand that first char is a '\\'
94                 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
95                 return;
96         }
97
98         lyxerr[Debug::KEY] << "Decode: " << contents << endl;
99
100         remdot = false;
101         plusasc = false;
102         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                 lyxerr[Debug::KEY] << "Default" << endl;
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::KEY] << "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::KEY] << "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 void InsetLatexAccent::dimension(BufferView *, LyXFont const & font,
267         Dimension & dim) const
268 {
269         // This function is a bit too simplistic 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         if (candisp) {
274                 if (ic == ' ')
275                         dim.a = font_metrics::ascent('a', font);
276                 else
277                         dim.a = font_metrics::ascent(ic, font);
278                 if (plusasc)
279                         dim.a += (font_metrics::maxAscent(font) + 3) / 3;
280
281                 if (ic == ' ')
282                         dim.d = font_metrics::descent('a', font);
283                 else
284                         dim.d = font_metrics::descent(ic, font);
285                 if (plusdesc)
286                         dim.d += 3;
287
288                 dim.w = font_metrics::width(ic, font);
289         } else {
290                 dim.a = font_metrics::maxAscent(font) + 4;
291                 dim.d = font_metrics::maxDescent(font) + 4;
292                 dim.w = font_metrics::width(contents, font) + 4;
293         }
294 }
295
296
297 int InsetLatexAccent::lbearing(LyXFont const & font) const
298 {
299         return font_metrics::lbearing(ic, font);
300 }
301
302
303 int InsetLatexAccent::rbearing(LyXFont const & font) const
304 {
305         return font_metrics::rbearing(ic, font);
306 }
307
308
309 bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
310                                         int baseline,
311                                         float & x) const
312 {
313         unsigned char tmpic = ic;
314
315         switch (modtype) {
316         case CEDILLA:
317         {
318                 if (ic == 'c') tmpic = 0xe7;
319                 if (ic == 'C') tmpic = 0xc7;
320                 if (ic == 's') tmpic = 0xfe;
321                 if (ic == 'S') tmpic = 0xde;
322                 break;
323         }
324         case BREVE:
325         {       if (ic == 'g') tmpic = 0xf0;
326         if (ic == 'G') tmpic = 0xd0;
327         break;
328         }
329         case UMLAUT:
330         {
331                 if (ic == 'o') tmpic = 0xf6;
332                 if (ic == 'O') tmpic = 0xd6;
333                 if (ic == 'u') tmpic = 0xfc;
334                 if (ic == 'U') tmpic = 0xdc;
335                 break;
336         }
337         case DOT:        if (ic == 'I') tmpic = 0xdd; break;
338         case DOT_LESS_I: tmpic = 0xfd; break;
339         default:         return false;
340         }
341         if (tmpic != ic) {
342                 char ch = char(tmpic);
343                 bv->painter().text(int(x), baseline, ch, font);
344                 x += width(bv, font);
345                 return true;
346         }
347         else
348                 return false;
349 }
350
351
352 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
353                             int baseline, float & x) const
354 {
355         Painter & pain = bv->painter();
356
357         if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
358                 if (displayISO8859_9(bv, font0, baseline, x))
359                         return;
360
361         /* draw it! */
362         // All the manually drawn accents in this function could use an
363         // overhaul. Different ways of drawing (what metrics to use)
364         // should also be considered.
365
366         LyXFont font(font0);
367         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
368                 font.setLanguage(english_language);
369
370         if (candisp) {
371                 int asc = ascent(bv, font);
372                 int desc = descent(bv, font);
373                 int wid = width(bv, font);
374                 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
375                 float hg;
376                 int y;
377                 if (plusasc) {
378                         // mark at the top
379                         hg = font_metrics::maxDescent(font);
380                         y = baseline - asc;
381
382                         if (font.shape() == LyXFont::ITALIC_SHAPE)
383                                 x2 += (4.0 * hg) / 5.0; // italic
384                 } else {
385                         // at the bottom
386                         hg = desc;
387                         y = baseline;
388                 }
389
390                 float hg35 = float(hg * 3.0) / 5.0;
391
392                 // display with proper accent mark
393                 // first the letter
394                 pain.text(int(x), baseline, ic, font);
395
396                 if (remdot) {
397                         int tmpvar = baseline - font_metrics::ascent('i', font);
398                         float tmpx = 0;
399                         if (font.shape() == LyXFont::ITALIC_SHAPE)
400                                 tmpx += (8.0 * hg) / 10.0; // italic
401                         lyxerr[Debug::KEY] << "Removing dot." << endl;
402                         // remove the dot first
403                         pain.fillRectangle(int(x + tmpx), tmpvar, wid,
404                                            font_metrics::ascent('i', font) -
405                                            font_metrics::ascent('x', font) - 1,
406                                            backgroundColor());
407                         // the five lines below is a simple hack to
408                         // make the display of accent 'i' and 'j'
409                         // better. It makes the accent be written
410                         // closer to the top of the dot-less 'i' or 'j'.
411                         char tmpic = ic; // store the ic when we
412                         ic = 'x';        // calculates the ascent of
413                         asc = ascent(bv, font); // the dot-less version (here: 'x')
414                         ic = tmpic;      // set the orig ic back
415                         y = baseline - asc; // update to new y coord.
416                 }
417                 // now the rest - draw within (x, y, x+wid, y+hg)
418                 switch (modtype) {
419                 case ACUTE:     // acute 0xB4
420                 {
421                         pain.text(int(x2 - (font_metrics::rbearing(0xB4, font) - font_metrics::lbearing(0xB4, font)) / 2),
422                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB4, font) - (font_metrics::ascent(0xB4, font) + font_metrics::descent(0xB4, font)) / 2,
423                                   char(0xB4), font);
424                         break;
425                 }
426                 case GRAVE:     // grave 0x60
427                 {
428                         pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
429                                   int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
430                                   char(0x60), font);
431                         break;
432                 }
433                 case MACRON:     // macron
434                 {
435                         pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
436                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
437                                   char(0xAF), font);
438                         break;
439                 }
440                 case TILDE:     // tilde
441                 {
442                         pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
443                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
444                                   '~', font);
445                         break;
446                 }
447                 case UNDERBAR:     // underbar 0x5F
448                 {
449                         pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
450                                   char(0x5F), font);
451                         break;
452                 }
453                 case CEDILLA:     // cedilla
454                 {
455                         pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
456                                   char(0xB8), font);
457
458                         break;
459                 }
460                 case UNDERDOT:     // underdot
461                 {
462                         pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
463                                   int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
464                                   '.', font);
465                         break;
466                 }
467
468                 case DOT:    // dot
469                 {
470                         pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
471                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
472                                   '.', font);
473                         break;
474                 }
475
476                 case CIRCLE:     // circle
477                 {
478                         LyXFont tmpf(font);
479                         tmpf.decSize().decSize();
480                         pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
481                                   int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
482                                   char(0xB0), tmpf);
483                         break;
484                 }
485                 case TIE:     // tie
486                 {
487                         pain.arc(int(x2 + hg35), int(y + hg / 2.0),
488                                  int(2 * hg), int(hg), 0, 360 * 32);
489                         break;
490                 }
491                 case BREVE:     // breve
492                 {
493                         pain.arc(int(x2 - (hg / 2.0)), y,
494                                  int(hg), int(hg), 0, -360*32);
495                         break;
496                 }
497                 case CARON:    // caron
498                 {
499                         int xp[3], yp[3];
500
501                         xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
502                         xp[1] = int(x2);        yp[1] = int(y + hg);
503                         xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
504                         pain.lines(xp, yp, 3);
505                         break;
506                 }
507                 case SPECIAL_CARON:    // special caron
508                 {
509                         switch (ic) {
510                         case 'L': wid = int(4.0 * wid / 5.0); break;
511                         case 't': y -= int(hg35 / 2.0); break;
512                         }
513                         int xp[3], yp[3];
514                         xp[0] = int(x + wid);
515                         yp[0] = int(y + hg35 + hg);
516
517                         xp[1] = int(x + wid + (hg35 / 2.0));
518                         yp[1] = int(y + hg + (hg35 / 2.0));
519
520                         xp[2] = int(x + wid + (hg35 / 2.0));
521                         yp[2] = y + int(hg);
522
523                         pain.lines(xp, yp, 3);
524                         break;
525                 }
526                 case HUNGARIAN_UMLAUT:    // hung. umlaut
527                 {
528                         pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
529                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
530                                   '´', font);
531                         pain.text(int(x2),
532                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
533                                   '´', font);
534                         break;
535                 }
536                 case UMLAUT:    // umlaut
537                 {
538                         pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
539                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
540                                   '¨', font);
541                         break;
542                 }
543                 case CIRCUMFLEX:    // circumflex
544                 {
545                         LyXFont tmpf(font);
546                         tmpf.decSize().decSize().decSize();
547                         pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
548                                   int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
549                                   char(0x5E), tmpf);
550                         break;
551                 }
552                 case OGONEK:    // ogonek
553                 {
554                         // this does probably not look like an ogonek, so
555                         // it should certainly be refined
556                         int xp[4], yp[4];
557
558                         xp[0] = int(x2);
559                         yp[0] = y;
560
561                         xp[1] = int(x2);
562                         yp[1] = y + int(hg35);
563
564                         xp[2] = int(x2 - hg35);
565                         yp[2] = y + int(hg / 2.0);
566
567                         xp[3] = int(x2 + hg / 4.0);
568                         yp[3] = y + int(hg);
569
570                         pain.lines(xp, yp, 4);
571                         break;
572                 }
573                 case lSLASH:
574                 case LSLASH:
575                 {
576                         int xp[2], yp[2];
577
578                         xp[0] = int(x);
579                         yp[0] = y + int(3.0 * hg);
580
581                         xp[1] = int(x + float(wid) * 0.75);
582                         yp[1] = y + int(hg);
583
584                         pain.lines(xp, yp, 2);
585                         break;
586                 }
587                 case DOT_LESS_I: // dotless-i
588                 case DOT_LESS_J: // dotless-j
589                 {
590                         // nothing to do for these
591                         break;
592                 }
593                 }
594         } else {
595                 pain.fillRectangle(int(x + 1),
596                                    baseline - ascent(bv, font) + 1,
597                                    width(bv, font) - 2,
598                                    ascent(bv, font)
599                                    + descent(bv, font) - 2,
600                                    backgroundColor());
601                 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
602                                width(bv, font) - 2,
603                                ascent(bv, font) + descent(bv, font) - 2);
604                 pain.text(int(x + 2), baseline, contents, font);
605         }
606         x +=  width(bv, font);
607 }
608
609
610 void InsetLatexAccent::write(Buffer const *, ostream & os) const
611 {
612         os << "\\i " << contents << "\n";
613 }
614
615
616 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
617 {
618         lex.eatLine();
619         contents = lex.getString();
620         checkContents();
621 }
622
623
624 int InsetLatexAccent::latex(Buffer const *, ostream & os,
625                             LatexRunParams const &) const
626 {
627         os << contents;
628         return 0;
629 }
630
631
632 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
633 {
634         os << contents;
635         return 0;
636 }
637
638
639 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
640 {
641         os << contents;
642         return 0;
643 }
644
645
646 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
647 {
648         os << contents;
649         return 0;
650 }
651
652
653 bool InsetLatexAccent::directWrite() const
654 {
655         return true;
656 }
657
658
659 Inset * InsetLatexAccent::clone(Buffer const &, bool) const
660 {
661         return new InsetLatexAccent(contents);
662 }
663
664
665 Inset::Code InsetLatexAccent::lyxCode() const
666 {
667         return Inset::ACCENT_CODE;
668 }
669
670
671 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
672 {
673         return o << int(at);
674 }