]> git.lyx.org Git - lyx.git/blob - src/insets/insetlatexaccent.C
Introduce wide streams. This fixes the remaining problems of plain text
[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/FontMetrics.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         lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
248
249         // This function is a bit too simplistic and is just a
250         // "try to make a fit for all accents" approach, to
251         // make it better we need to know what kind of accent is
252         // used and add to max based on that.
253         if (candisp) {
254                 if (ic == ' ')
255                         dim.asc = fm.ascent('a');
256                 else
257                         dim.asc = fm.ascent(ic);
258                 if (plusasc)
259                         dim.asc += (fm.maxAscent() + 3) / 3;
260
261                 if (ic == ' ')
262                         dim.des = fm.descent('a');
263                 else
264                         dim.des = fm.descent(ic);
265                 if (plusdesc)
266                         dim.des += 3;
267
268                 dim.wid = fm.width(ic);
269         } else {
270                 dim.asc = fm.maxAscent() + 4;
271                 dim.des = fm.maxDescent() + 4;
272                 docstring dcon(contents.begin(), contents.end());
273                 dim.wid = fm.width(dcon) + 4;
274         }
275         dim_ = dim;
276 }
277
278
279 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
280 {
281         unsigned char tmpic = ic;
282
283         switch (modtype) {
284
285         case CEDILLA: {
286                 if (ic == 'c') tmpic = '\xe7';
287                 if (ic == 'C') tmpic = '\xc7';
288                 if (ic == 's') tmpic = '\xfe';
289                 if (ic == 'S') tmpic = '\xde';
290                 break;
291         }
292
293         case BREVE: {
294                 if (ic == 'g') tmpic = '\xf0';
295                 if (ic == 'G') tmpic = '\xd0';
296                 break;
297         }
298
299         case UMLAUT: {
300                 if (ic == 'o') tmpic = '\xf6';
301                 if (ic == 'O') tmpic = '\xd6';
302                 if (ic == 'u') tmpic = '\xfc';
303                 if (ic == 'U') tmpic = '\xdc';
304                 break;
305         }
306
307         case DOT:
308                 if (ic == 'I') tmpic = '\xdd';
309                 break;
310
311         case DOT_LESS_I:
312                 tmpic = '\xfd';
313                 break;
314
315         default:
316                 return false;
317         }
318
319         if (tmpic == ic)
320                 return false;
321
322         pi.pain.text(x, y, char(tmpic), pi.base.font);
323         return true;
324 }
325
326
327 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
328         char_type accent) const
329 {
330         LyXFont const & font = pi.base.font;
331         lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
332
333         x -= fm.center(accent);
334         y -= fm.ascent(ic);
335         y -= fm.descent(accent);
336         y -= fm.height(accent) / 2;
337         pi.pain.text(x, y, accent, font);
338 }
339
340
341 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
342 {
343         if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
344                 if (displayISO8859_9(pi, x, baseline))
345                         return;
346
347         // All the manually drawn accents in this function could use an
348         // overhaul. Different ways of drawing (what metrics to use)
349         // should also be considered.
350
351         LyXFont font = pi.base.font;
352         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
353                 font.setLanguage(english_language);
354
355         lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
356
357         if (candisp) {
358                 int x2 = int(x + (fm.rbearing(ic) - fm.lbearing(ic)) / 2);
359                 int hg;
360                 int y;
361                 if (plusasc) {
362                         // mark at the top
363                         hg = fm.maxDescent();
364                         y = baseline - dim_.asc;
365                         if (font.shape() == LyXFont::ITALIC_SHAPE)
366                                 x2 += int(0.8 * hg); // italic
367                 } else {
368                         // at the bottom
369                         hg = dim_.des;
370                         y = baseline;
371                 }
372
373                 double hg35 = hg * 0.6;
374
375                 // display with proper accent mark
376                 // first the letter
377                 pi.pain.text(x, baseline, ic, font);
378
379                 if (remdot) {
380                         int tmpvar = baseline - fm.ascent('i');
381                         int tmpx = 0;
382                         if (font.shape() == LyXFont::ITALIC_SHAPE)
383                                 tmpx += int(0.8 * hg); // italic
384                         lyxerr[Debug::KEY] << "Removing dot." << endl;
385                         // remove the dot first
386                         pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
387                                            fm.ascent('i') -
388                                            fm.ascent('x') - 1,
389                                            backgroundColor());
390                         // the five lines below is a simple hack to
391                         // make the display of accent 'i' and 'j'
392                         // better. It makes the accent be written
393                         // closer to the top of the dot-less 'i' or 'j'.
394                         char tmpic = ic; // store the ic when we
395                         ic = 'x';        // calculates the ascent of
396 #ifdef WITH_WARNINGS
397 #warning metrics?
398 #endif
399                         int asc = ascent(); // the dot-less version (here: 'x')
400                         ic = tmpic;      // set the orig ic back
401                         y = baseline - asc; // update to new y coord.
402                 }
403
404                 // now the rest - draw within (x, y, x + wid, y + hg)
405                 switch (modtype) {
406                 case ACUTE:
407                         //drawAccent(pi, x2, baseline, '\xB4');
408                         drawAccent(pi, x2, baseline, 0xB4);
409                         break;
410
411                 case GRAVE:
412                         //drawAccent(pi, x2, baseline, '\x60');
413                         drawAccent(pi, x2, baseline, 0x60);
414                         break;
415
416                 case MACRON:
417                         //drawAccent(pi, x2, baseline, '\xAF');
418                         drawAccent(pi, x2, baseline, 0xAF);
419                         break;
420
421                 case TILDE:
422                         drawAccent(pi, x2, baseline, '~');
423                         break;
424
425                 case UNDERBAR: {
426                         char_type const underbar = 0x5F; //('\x5F');
427                         pi.pain.text(x2 - fm.center(underbar),
428                                      baseline, underbar, font);
429                         break;
430                 }
431
432                 case CEDILLA: {
433                         char_type const cedilla = 0xB8; //('\xB8');
434                         pi.pain.text(x2  - fm.center(cedilla),
435                                      baseline, cedilla, font);
436                         break;
437                 }
438
439                 case UNDERDOT:
440                         pi.pain.text(x2  - fm.center('.'),
441                                   int(baseline + 1.5 * fm.height('.')),
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                         drawAccent(pi, x2, baseline, 0xB0);
452                         break;
453
454                 case TIE:
455                         pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
456                                     LColor::foreground);
457                         break;
458
459                 case BREVE:
460                         pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
461                                     LColor::foreground);
462                         break;
463
464                 case CARON: {
465                         int xp[3], yp[3];
466                         xp[0] = int(x2 - hg35);    yp[0] = int(y + hg35);
467                         xp[1] = int(x2);           yp[1] = int(y + hg);
468                         xp[2] = int(x2 + hg35);    yp[2] = int(y + hg35);
469                         pi.pain.lines(xp, yp, 3, LColor::foreground);
470                         break;
471                 }
472
473                 case SPECIAL_CARON: {
474                         switch (ic) {
475                                 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
476                                 case 't': y -= int(hg35 / 2.0); break;
477                         }
478                         int xp[3], yp[3];
479                         xp[0] = int(x + dim_.wid);
480                         yp[0] = int(y + hg35 + hg);
481
482                         xp[1] = int(x + dim_.wid + (hg35 / 2.0));
483                         yp[1] = int(y + hg + (hg35 / 2.0));
484
485                         xp[2] = int(x + dim_.wid + (hg35 / 2.0));
486                         yp[2] = y + int(hg);
487
488                         pi.pain.lines(xp, yp, 3, LColor::foreground);
489                         break;
490                 }
491
492                 case HUNGARIAN_UMLAUT:
493                         drawAccent(pi, x2 - fm.center('´'), baseline, '´');
494                         drawAccent(pi, x2 + fm.center('´'), baseline, '´');
495                         break;
496
497                 case UMLAUT:
498                         drawAccent(pi, x2, baseline, '"');
499                         break;
500
501                 case CIRCUMFLEX:
502                         drawAccent(pi, x2, baseline, '\x5E');
503                         break;
504
505                 case OGONEK: {
506                         // this does probably not look like an ogonek, so
507                         // it should certainly be refined
508                         int xp[4], yp[4];
509
510                         xp[0] = x2;
511                         yp[0] = y;
512
513                         xp[1] = x2;
514                         yp[1] = y + int(hg35);
515
516                         xp[2] = int(x2 - hg35);
517                         yp[2] = y + hg / 2;
518
519                         xp[3] = x2 + hg / 4;
520                         yp[3] = y + int(hg);
521
522                         pi.pain.lines(xp, yp, 4, LColor::foreground);
523                         break;
524                 }
525
526                 case lSLASH:
527                 case LSLASH: {
528                         int xp[2], yp[2];
529
530                         xp[0] = x;
531                         yp[0] = y + int(3 * hg);
532
533                         xp[1] = int(x + dim_.wid * 0.75);
534                         yp[1] = y + int(hg);
535
536                         pi.pain.lines(xp, yp, 2, LColor::foreground);
537                         break;
538                 }
539
540                 case DOT_LESS_I: // dotless-i
541                 case DOT_LESS_J: // dotless-j
542                         // nothing to do for these
543                         break;
544                 }
545
546         } else {
547                 pi.pain.fillRectangle(x + 1,
548                                       baseline - dim_.asc + 1, dim_.wid - 2,
549                                       dim_.asc + dim_.des - 2,
550                                       backgroundColor());
551                 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
552                                   dim_.wid - 2, dim_.asc + dim_.des - 2,
553                                   LColor::foreground);
554                 docstring dcon(contents.begin(), contents.end());
555                 pi.pain.text(x + 2, baseline, dcon, font);
556         }
557 }
558
559
560 void InsetLatexAccent::write(Buffer const &, ostream & os) const
561 {
562         os << "\\i " << contents << "\n";
563 }
564
565
566 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
567 {
568         lex.eatLine();
569         contents = lex.getString();
570         checkContents();
571 }
572
573
574 int InsetLatexAccent::latex(Buffer const &, ostream & os,
575                             OutputParams const &) const
576 {
577         os << contents;
578         return 0;
579 }
580
581
582 int InsetLatexAccent::plaintext(Buffer const &, lyx::odocstream & os,
583                             OutputParams const &) const
584 {
585         os << lyx::from_ascii(contents);
586         return 0;
587 }
588
589
590 int InsetLatexAccent::docbook(Buffer const &, ostream & os,
591                               OutputParams const &) const
592 {
593         os << contents;
594         return 0;
595 }
596
597
598 int InsetLatexAccent::textString(Buffer const & buf, lyx::odocstream & os,
599                        OutputParams const & op) const
600 {
601         return plaintext(buf, os, op);
602 }
603
604
605 bool InsetLatexAccent::directWrite() const
606 {
607         return true;
608 }
609
610
611 InsetBase::Code InsetLatexAccent::lyxCode() const
612 {
613         return InsetBase::ACCENT_CODE;
614 }
615
616
617 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
618 {
619         return o << int(at);
620 }