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