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