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