]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
IU of drawing phase one without 'semantic changes' as requested by John
[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 "lyxrc.h"
17 #include "support/lstrings.h"
18 #include "BufferView.h"
19 #include "frontends/Painter.h"
20 #include "frontends/font_metrics.h"
21 #include "language.h"
22 #include "lyxlex.h"
23 #include "metricsinfo.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::metrics(MetricsInfo & mi, Dimension & dim) const
267 {
268         LyXFont & font = mi.base.font;
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.asc = font_metrics::ascent('a', font);
276                 else
277                         dim.asc = font_metrics::ascent(ic, font);
278                 if (plusasc)
279                         dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
280
281                 if (ic == ' ')
282                         dim.des = font_metrics::descent('a', font);
283                 else
284                         dim.des = font_metrics::descent(ic, font);
285                 if (plusdesc)
286                         dim.des += 3;
287
288                 dim.wid = font_metrics::width(ic, font);
289         } else {
290                 dim.asc = font_metrics::maxAscent(font) + 4;
291                 dim.des = font_metrics::maxDescent(font) + 4;
292                 dim.wid = font_metrics::width(contents, font) + 4;
293         }
294         dim_ = dim;
295 }
296
297
298 int InsetLatexAccent::lbearing(LyXFont const & font) const
299 {
300         return font_metrics::lbearing(ic, font);
301 }
302
303
304 int InsetLatexAccent::rbearing(LyXFont const & font) const
305 {
306         return font_metrics::rbearing(ic, font);
307 }
308
309
310 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
311 {
312         unsigned char tmpic = ic;
313
314         switch (modtype) {
315         case CEDILLA:
316         {
317                 if (ic == 'c') tmpic = 0xe7;
318                 if (ic == 'C') tmpic = 0xc7;
319                 if (ic == 's') tmpic = 0xfe;
320                 if (ic == 'S') tmpic = 0xde;
321                 break;
322         }
323         case BREVE:
324         {       if (ic == 'g') tmpic = 0xf0;
325         if (ic == 'G') tmpic = 0xd0;
326         break;
327         }
328         case UMLAUT:
329         {
330                 if (ic == 'o') tmpic = 0xf6;
331                 if (ic == 'O') tmpic = 0xd6;
332                 if (ic == 'u') tmpic = 0xfc;
333                 if (ic == 'U') tmpic = 0xdc;
334                 break;
335         }
336         case DOT:        if (ic == 'I') tmpic = 0xdd; break;
337         case DOT_LESS_I: tmpic = 0xfd; break;
338         default:         return false;
339         }
340         if (tmpic != ic) {
341                 pi.pain.text(x, y, char(tmpic), pi.base.font);
342                 return true;
343         }
344         else
345                 return false;
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         /* draw it! */
356         // All the manually drawn accents in this function could use an
357         // overhaul. Different ways of drawing (what metrics to use)
358         // should also be considered.
359
360         BufferView * bv = pi.base.bv;
361         LyXFont font = pi.base.font;
362         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
363                 font.setLanguage(english_language);
364
365         Dimension dim;
366         MetricsInfo mi;
367         mi.base.bv = pi.base.bv;
368         mi.base.font = pi.base.font;
369         metrics(mi, dim);
370         dim_ = dim;
371
372         if (candisp) {
373                 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
374                 float hg;
375                 int y;
376                 if (plusasc) {
377                         // mark at the top
378                         hg = font_metrics::maxDescent(font);
379                         y = baseline - dim_.asc;
380
381                         if (font.shape() == LyXFont::ITALIC_SHAPE)
382                                 x2 += (4.0 * hg) / 5.0; // italic
383                 } else {
384                         // at the bottom
385                         hg = dim_.des;
386                         y = baseline;
387                 }
388
389                 float hg35 = float(hg * 3.0) / 5.0;
390
391                 // display with proper accent mark
392                 // first the letter
393                 pi.pain.text(x, baseline, ic, font);
394
395                 if (remdot) {
396                         int tmpvar = baseline - font_metrics::ascent('i', font);
397                         int tmpx = 0;
398                         if (font.shape() == LyXFont::ITALIC_SHAPE)
399                                 tmpx += int(0.8 * hg); // italic
400                         lyxerr[Debug::KEY] << "Removing dot." << endl;
401                         // remove the dot first
402                         pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
403                                            font_metrics::ascent('i', pi.base.font) -
404                                            font_metrics::ascent('x', pi.base.font) - 1,
405                                            backgroundColor());
406                         // the five lines below is a simple hack to
407                         // make the display of accent 'i' and 'j'
408                         // better. It makes the accent be written
409                         // closer to the top of the dot-less 'i' or 'j'.
410                         char tmpic = ic; // store the ic when we
411                         ic = 'x';        // calculates the ascent of
412                         int asc = ascent(bv, font); // the dot-less version (here: 'x')
413                         ic = tmpic;      // set the orig ic back
414                         y = baseline - asc; // update to new y coord.
415                 }
416                 // now the rest - draw within (x, y, x+wid, y+hg)
417                 switch (modtype) {
418                 case ACUTE:     // acute 0xB4
419                 {
420                         pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
421                                       - font_metrics::lbearing(0xB4, font)) / 2),
422                                   baseline - font_metrics::ascent(ic, font)
423                                                  - font_metrics::descent(0xB4, font)
424                                                  - (font_metrics::ascent(0xB4, font)
425                                                   + font_metrics::descent(0xB4, font)) / 2,
426                                   char(0xB4), font);
427                         break;
428                 }
429                 case GRAVE:     // grave 0x60
430                 {
431                         pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
432                                   int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
433                                   char(0x60), font);
434                         break;
435                 }
436                 case MACRON:     // macron
437                 {
438                         pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
439                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
440                                   char(0xAF), font);
441                         break;
442                 }
443                 case TILDE:     // tilde
444                 {
445                         pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
446                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
447                                   '~', font);
448                         break;
449                 }
450                 case UNDERBAR:     // underbar 0x5F
451                 {
452                         pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
453                                   char(0x5F), font);
454                         break;
455                 }
456                 case CEDILLA:     // cedilla
457                 {
458                         pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
459                                   char(0xB8), font);
460
461                         break;
462                 }
463                 case UNDERDOT:     // underdot
464                 {
465                         pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
466                                   int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
467                                   '.', font);
468                         break;
469                 }
470
471                 case DOT:    // dot
472                 {
473                         pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
474                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
475                                   '.', font);
476                         break;
477                 }
478
479                 case CIRCLE:     // circle
480                 {
481                         LyXFont tmpf(font);
482                         tmpf.decSize().decSize();
483                         pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
484                                   int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
485                                   char(0xB0), tmpf);
486                         break;
487                 }
488                 case TIE:     // tie
489                 {
490                         pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
491                                  int(2 * hg), int(hg), 0, 360 * 32);
492                         break;
493                 }
494                 case BREVE:     // breve
495                 {
496                         pi.pain.arc(int(x2 - (hg / 2.0)), y,
497                                  int(hg), int(hg), 0, -360*32);
498                         break;
499                 }
500                 case CARON:    // caron
501                 {
502                         int xp[3], yp[3];
503
504                         xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
505                         xp[1] = int(x2);        yp[1] = int(y + hg);
506                         xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
507                         pi.pain.lines(xp, yp, 3);
508                         break;
509                 }
510                 case SPECIAL_CARON:    // special caron
511                 {
512                         switch (ic) {
513                         case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
514                         case 't': y -= int(hg35 / 2.0); break;
515                         }
516                         int xp[3], yp[3];
517                         xp[0] = int(x + dim_.wid);
518                         yp[0] = int(y + hg35 + hg);
519
520                         xp[1] = int(x + dim_.wid + (hg35 / 2.0));
521                         yp[1] = int(y + hg + (hg35 / 2.0));
522
523                         xp[2] = int(x + dim_.wid + (hg35 / 2.0));
524                         yp[2] = y + int(hg);
525
526                         pi.pain.lines(xp, yp, 3);
527                         break;
528                 }
529                 case HUNGARIAN_UMLAUT:    // hung. umlaut
530                 {
531                         pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
532                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
533                                   '´', font);
534                         pi.pain.text(int(x2),
535                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
536                                   '´', font);
537                         break;
538                 }
539                 case UMLAUT:    // umlaut
540                 {
541                         pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
542                                   baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
543                                   '¨', font);
544                         break;
545                 }
546                 case CIRCUMFLEX:    // circumflex
547                 {
548                         LyXFont tmpf(font);
549                         tmpf.decSize().decSize().decSize();
550                         pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
551                                   int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
552                                   char(0x5E), tmpf);
553                         break;
554                 }
555                 case OGONEK:    // ogonek
556                 {
557                         // this does probably not look like an ogonek, so
558                         // it should certainly be refined
559                         int xp[4], yp[4];
560
561                         xp[0] = int(x2);
562                         yp[0] = y;
563
564                         xp[1] = int(x2);
565                         yp[1] = y + int(hg35);
566
567                         xp[2] = int(x2 - hg35);
568                         yp[2] = y + int(hg / 2.0);
569
570                         xp[3] = int(x2 + hg / 4.0);
571                         yp[3] = y + int(hg);
572
573                         pi.pain.lines(xp, yp, 4);
574                         break;
575                 }
576                 case lSLASH:
577                 case LSLASH:
578                 {
579                         int xp[2], yp[2];
580
581                         xp[0] = int(x);
582                         yp[0] = y + int(3.0 * hg);
583
584                         xp[1] = int(x + float(dim_.wid) * 0.75);
585                         yp[1] = y + int(hg);
586
587                         pi.pain.lines(xp, yp, 2);
588                         break;
589                 }
590                 case DOT_LESS_I: // dotless-i
591                 case DOT_LESS_J: // dotless-j
592                 {
593                         // nothing to do for these
594                         break;
595                 }
596                 }
597         } else {
598                 pi.pain.fillRectangle(x + 1,
599                                    baseline - dim_.asc + 1, dim_.wid - 2,
600                                    dim_.asc + dim_.des - 2, backgroundColor());
601                 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
602                                dim_.wid - 2, dim_.asc + dim_.des - 2);
603                 pi.pain.text(x + 2, baseline, contents, font);
604         }
605 }
606
607
608 void InsetLatexAccent::write(Buffer const *, ostream & os) const
609 {
610         os << "\\i " << contents << "\n";
611 }
612
613
614 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
615 {
616         lex.eatLine();
617         contents = lex.getString();
618         checkContents();
619 }
620
621
622 int InsetLatexAccent::latex(Buffer const *, ostream & os,
623                             LatexRunParams const &) const
624 {
625         os << contents;
626         return 0;
627 }
628
629
630 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
631 {
632         os << contents;
633         return 0;
634 }
635
636
637 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
638 {
639         os << contents;
640         return 0;
641 }
642
643
644 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
645 {
646         os << contents;
647         return 0;
648 }
649
650
651 bool InsetLatexAccent::directWrite() const
652 {
653         return true;
654 }
655
656
657 Inset * InsetLatexAccent::clone(Buffer const &) const
658 {
659         return new InsetLatexAccent(contents);
660 }
661
662
663 // Inset * InsetLatexAccent::clone(Buffer const &, bool) const
664 // {
665 //      return new InsetLatexAccent(contents);
666 // }
667
668
669 Inset::Code InsetLatexAccent::lyxCode() const
670 {
671         return Inset::ACCENT_CODE;
672 }
673
674
675 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
676 {
677         return o << int(at);
678 }