1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetlatexaccent.h"
23 #include "support/lstrings.h"
28 /* LatexAccent. Proper handling of accented characters */
29 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
30 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
32 InsetLatexAccent::InsetLatexAccent()
38 InsetLatexAccent::InsetLatexAccent(string const & string)
45 void InsetLatexAccent::checkContents()
46 // check, if we know the modifier and can display it ok on screen
50 if (contents.empty() || contents.length() < 2) return;
53 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
54 contents = frontStrip(strip(contents));
55 if (!contains(contents, "{") && !contains(contents, "}")) {
56 if (contents.length() == 2) {
62 } else if (contents.length() == 3) {
70 } else if (contents.length() == 4 && contents[2] == ' ') {
78 } else if (contents.length() == 4 && contents[2] == '\\'
79 && (contents[3] == 'i' || contents[3] == 'j')) {
90 if (contents[0] != '\\') return; // demand that first char is a '\\'
92 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
94 remdot = false; plusasc = false; plusdesc = false;
96 switch (contents[1]) { // second char should be one of these
98 modtype = ACUTE; // acute
99 plusasc = true; // at the top of character
102 modtype = GRAVE; // grave
103 plusasc = true; // at the top
106 modtype = MACRON; // macron
107 plusasc = true; // at the top
110 modtype = TILDE; // tilde
111 plusasc = true; // at the top
113 case 'b': // underbar
114 modtype = UNDERBAR; // underbar
115 plusdesc = true; // at the bottom
118 modtype = CEDILLA; // cedilla
119 plusdesc = true; // at the bottom
121 case 'd': // underdot
122 modtype = UNDERDOT; // underdot
123 plusdesc = true; // at the bottom
126 modtype = CIRCLE; // circle
127 plusasc = true; // at the top
130 modtype = TIE; // tie
131 plusasc = true; // at the top
134 modtype = BREVE; // breve
135 plusasc = true; // at the top
138 modtype = CARON; // caron
139 plusasc = true; // at the top
141 case 'q': // special caron
142 modtype = SPECIAL_CARON; // special caron
143 plusasc = true; // at the top
145 case 'H': // hungarian umlaut
146 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
147 plusasc = true; // at the top
150 modtype = UMLAUT; // umlaut
151 plusasc = true; // at the top
154 modtype = DOT; // dot
155 plusasc = true; // at the top
157 case '^': // circumflex
158 modtype = CIRCUMFLEX; // circumflex
159 plusasc = true; // at the top
162 modtype = OGONEK; // ogonek
165 case 'i': // dot-less-i
166 modtype = DOT_LESS_I; // dot-less-i
167 plusasc = true; // at the top (not really needed)
170 case 'j': // dot-less-j
171 modtype = DOT_LESS_J; // dot-less-j
172 plusasc = true; // at the top (not really needed)
177 plusasc = true; // at the top (not really needed)
181 plusasc = true; // at the top (not really needed)
184 printf ("Default\n");
185 // unknow accent (or something else)
189 // we demand that third char is a '{' (Lgb)
190 if (contents[2] != '{') return;
192 // special clause for \i{}, \j{} \l{} and \L{}
193 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
194 || modtype == lSLASH || modtype == LSLASH)
195 && contents[3] == '}' ) {
197 case DOT_LESS_I: ic = 'i'; break;
198 case DOT_LESS_J: ic = 'j'; break;
199 case lSLASH: ic = 'l'; break;
200 case LSLASH: ic = 'L'; break;
202 // if this happens something is really wrong
203 lyxerr << "InsetLaTexAccent: weird error." << endl;
206 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
207 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
209 << ", top: " << plusasc
210 << ", bot: " << plusdesc
211 << ", dot: " << remdot
212 << ", mod: " << modtype << endl;
213 // Special case for space
214 } else if (contents[3] == '}') {
220 ic = contents[3]; // i will always be 3 here
222 // ic should now be a alfa-char or '\\'
224 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
225 if (ic == 'i' || ic == 'j')
229 } else if ( (ic == 'i'|| ic == 'j') && contents[4] == '}') {
230 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
231 string temp = contents;
232 temp.erase(3, string::npos);
235 for(string::size_type j = 4;
236 j < contents.length(); ++j)
243 // demand a '}' at the end
244 if (contents[++i] != '}' && contents[++i]) return;
246 // fine, the char is properly decoded now (hopefully)
247 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
249 << ", top: " << plusasc
250 << ", bot: " << plusdesc
251 << ", dot: " << remdot
252 << ", mod: " << modtype << endl;
259 int InsetLatexAccent::ascent(Painter &, LyXFont const & font) const
261 // This function is a bit too simplistix and is just a
262 // "try to make a fit for all accents" approach, to
263 // make it better we need to know what kind of accent is
264 // used and add to max based on that.
268 max = font.ascent('a');
270 max = font.ascent(ic);
272 max += (font.maxAscent() + 3) / 3;
274 max = font.maxAscent() + 4;
278 int InsetLatexAccent::Ascent(LyXFont const & font) const
280 // This function is a bit too simplistix and is just a
281 // "try to make a fit for all accents" approach, to
282 // make it better we need to know what kind of accent is
283 // used and add to max based on that.
287 max = font.ascent('a');
289 max = font.ascent(ic);
291 max += (font.maxAscent() + 3) / 3;
293 max = font.maxAscent() + 4;
300 int InsetLatexAccent::descent(Painter &, LyXFont const & font) const
305 max = font.descent('a');
307 max = font.descent(ic);
311 max = font.maxDescent() + 4;
315 int InsetLatexAccent::Descent(LyXFont const & font) const
320 max = font.descent('a');
322 max = font.descent(ic);
326 max = font.maxDescent() + 4;
333 int InsetLatexAccent::width(Painter &, LyXFont const & font) const
336 return font.textWidth(&ic, 1);
338 return font.stringWidth(contents) + 4;
341 int InsetLatexAccent::Width(LyXFont const & font) const
344 return font.textWidth(&ic, 1);
346 return font.stringWidth(contents) + 4;
351 int InsetLatexAccent::Lbearing(LyXFont const & font) const
353 return font.lbearing(ic);
357 int InsetLatexAccent::Rbearing(LyXFont const & font) const
359 return font.rbearing(ic);
364 bool InsetLatexAccent::DisplayISO8859_9(Painter & pain, LyXFont const & font,
368 unsigned char tmpic = ic;
373 if (ic == 'c') tmpic = 0xe7;
374 if (ic == 'C') tmpic = 0xc7;
375 if (ic == 's') tmpic = 0xfe;
376 if (ic == 'S') tmpic = 0xde;
380 { if (ic == 'g') tmpic = 0xf0;
381 if (ic == 'G') tmpic = 0xd0;
386 if (ic == 'o') tmpic = 0xf6;
387 if (ic == 'O') tmpic = 0xd6;
388 if (ic == 'u') tmpic = 0xfc;
389 if (ic == 'U') tmpic = 0xdc;
392 case DOT: if (ic == 'I') tmpic = 0xdd; break;
393 case DOT_LESS_I: tmpic = 0xfd; break;
394 default: return false;
397 char ch = char(tmpic);
398 pain.text(x, baseline, ch, font);
399 x += width(pain, font);
406 bool InsetLatexAccent::DisplayISO8859_9(LyXFont font,
411 unsigned char tmpic = ic;
416 if (ic == 'c') tmpic = 0xe7;
417 if (ic == 'C') tmpic = 0xc7;
418 if (ic == 's') tmpic = 0xfe;
419 if (ic == 'S') tmpic = 0xde;
423 { if (ic == 'g') tmpic = 0xf0;
424 if (ic == 'G') tmpic = 0xd0;
429 if (ic == 'o') tmpic = 0xf6;
430 if (ic == 'O') tmpic = 0xd6;
431 if (ic == 'u') tmpic = 0xfc;
432 if (ic == 'U') tmpic = 0xdc;
435 case DOT: if (ic == 'I') tmpic = 0xdd; break;
436 case DOT_LESS_I: tmpic = 0xfd; break;
437 default: return false;
440 char ch = char(tmpic);
441 scr.drawText(font, &ch, 1, baseline, int(x));
452 void InsetLatexAccent::draw(Painter & pain, LyXFont const & font,
453 int baseline, float & x) const
455 if (lyxrc->font_norm == "iso8859-9")
456 if (DisplayISO8859_9(pain, font, baseline, x))
460 // All the manually drawn accents in this function could use an
461 // overhaul. Different ways of drawing (what metrics to use)
462 // should also be considered.
465 int asc = ascent(pain, font);
466 int desc = descent(pain, font);
467 int wid = width(pain, font);
468 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
473 hg = font.maxDescent();
476 if (font.shape() == LyXFont::ITALIC_SHAPE)
477 x2 += (4.0 * hg) / 5.0; // italic
484 float hg35 = float(hg * 3.0) / 5.0;
486 // display with proper accent mark
488 pain.text(int(x), baseline, ic, font);
491 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
495 int tmpvar = baseline - font.ascent('i');
497 if (font.shape() == LyXFont::ITALIC_SHAPE)
498 tmpx += (8.0 * hg) / 10.0; // italic
499 lyxerr[Debug::KEY] << "Removing dot." << endl;
500 // remove the dot first
501 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
503 font.ascent('x') - 1,
505 // the five lines below is a simple hack to
506 // make the display of accent 'i' and 'j'
507 // better. It makes the accent be written
508 // closer to the top of the dot-less 'i' or 'j'.
509 char tmpic = ic; // store the ic when we
510 ic = 'x'; // calculates the ascent of
511 asc = ascent(pain, font); // the dot-less version (here: 'x')
512 ic = tmpic; // set the orig ic back
513 y = baseline - asc; // update to new y coord.
515 // now the rest - draw within (x, y, x+wid, y+hg)
519 pain.line(int(x2), int(y + hg),
520 int(x2 + hg35), y + hg35);
525 pain.line(int(x2), int(y + hg),
526 int(x2 - hg35), y + hg35);
529 case MACRON: // macron
531 pain.line(int(x2 - wid * 0.4),
540 if (hg35 > 2.0) hg35 -= 1.0;
544 xp[0] = int(x2 - 2.0 * hg35);
547 xp[1] = int(x2 - hg35);
548 yp[1] = int(y + hg35);
553 xp[3] = int(x2 + hg35);
554 yp[3] = int(y + hg35);
556 pain.lines(xp, yp, 4);
558 pain.text(x2 - font.width('~') / 2,
559 baseline - font.ascent('~'),
564 case UNDERBAR: // underbar
566 pain.line(int(x2 - wid * 0.4),
572 case CEDILLA: // cedilla
580 yp[1] = y + int(hg / 3.0);
582 xp[2] = int(x2 + (hg / 3.0));
583 yp[2] = y + int(hg / 2.0);
585 xp[3] = int(x2 - (hg / 4.0));
588 pain.lines(xp, yp, 4);
591 case UNDERDOT: // underdot
593 pain.arc(int(x2), y + hg35,
600 pain.arc(int(x2), y + hg * 0.5,
607 case CIRCLE: // circle
609 pain.arc(int(x2 - (hg / 2.0)),
610 y + (hg / 2.0), hg, hg , 0,
616 pain.arc(int(x2 + hg35), y + hg / 2.0,
617 2 * hg, hg, 0, 360 * 32);
622 pain.arc(int(x2 - (hg / 2.0)), y,
630 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
631 xp[1] = int(x2); yp[1] = int(y + hg);
632 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
633 pain.lines(xp, yp, 3);
636 case SPECIAL_CARON: // special caron
639 case 'L': wid = int(4.0 * wid / 5.0); break;
640 case 't': y -= int(hg35 / 2.0); break;
643 xp[0] = int(x + wid);
644 yp[0] = int(y + hg35 + hg);
646 xp[1] = int(x + wid + (hg35 / 2.0));
647 yp[1] = int(y + hg + (hg35 / 2.0));
649 xp[2] = int(x + wid + (hg35 / 2.0));
652 pain.lines(xp, yp, 3);
655 case HUNGARIAN_UMLAUT: // hung. umlaut
657 int xs1[2], xs2[2], ys1[2], ys2[2];
659 xs1[0] = int(x2 - (hg / 2.0));
660 ys1[0] = int(y + hg);
662 xs2[0] = int(x2 + hg35 - (hg / 2.0));
663 ys2[0] = int(y + hg35);
665 xs1[1] = int(x2 + (hg / 2.0));
666 ys1[1] = int(y + hg);
668 xs2[1] = int(x2 + hg35 + (hg / 2.0));
669 ys2[1] = int(y + hg35);
671 pain.segments(xs1, ys1, xs2, ys2, 2);
674 case UMLAUT: // umlaut
677 float rad = hg / 2.0;
679 pain.point(int(x2 - 4.0 * hg / 7.0),
681 pain.point(int(x2 + 4.0 * hg / 7.0),
684 rad += .5; // this ensures that f.ex. 1.5 will
685 // not be rounded down to .5 and then
686 // converted to int = 0
687 pain.arc(int(x2 - 2.0 * hg / 4.0),
691 pain.arc(int(x2 + 2.0 * hg / 4.0),
693 rad, rad, 0, 360*64);
696 pain.text(x2 - font.width('¨') / 2, baseline,
701 case CIRCUMFLEX: // circumflex
705 xp[0] = int(x2 - hg35); yp[0] = y + int(hg);
706 xp[1] = int(x2); yp[1] = int(y + hg35);
707 xp[2] = int(x2 + hg35); yp[2] = y + int(hg);
708 pain.lines(xp, yp, 3);
711 case OGONEK: // ogonek
713 // this does probably not look like an ogonek, so
714 // it should certainly be refined
721 yp[1] = y + int(hg35);
723 xp[2] = int(x2 - hg35);
724 yp[2] = y + int(hg / 2.0);
726 xp[3] = int(x2 + hg / 4.0);
729 pain.lines(xp, yp, 4);
738 yp[0] = y + int(3.0 * hg);
740 xp[1] = int(x + float(wid) * 0.75);
743 pain.lines(xp, yp, 2);
746 case DOT_LESS_I: // dotless-i
747 case DOT_LESS_J: // dotless-j
749 // nothing to do for these
754 pain.fillRectangle(int(x + 1),
755 baseline - ascent(pain, font) + 1,
756 width(pain, font) - 2,
758 + descent(pain, font) - 2);
759 pain.rectangle(int(x + 1), baseline - ascent(pain, font) + 1,
760 width(pain, font) - 2,
761 ascent(pain, font) + descent(pain, font) - 2);
762 pain.text(int(x + 2), baseline, contents, font);
764 x += width(pain, font);
767 void InsetLatexAccent::Draw(LyXFont font,
772 if (lyxrc->font_norm == "iso8859-9")
773 if (DisplayISO8859_9 (font, scr, baseline, x))
777 // All the manually drawn accents in this function could use an
778 // overhaul. Different ways of drawing (what metrics to use)
779 // should also be considered.
782 int asc = Ascent(font);
783 int desc = Descent(font);
784 int wid = Width(font);
785 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
790 hg = font.maxDescent();
793 if (font.shape() == LyXFont::ITALIC_SHAPE)
794 x2 += (4.0 * hg) / 5.0; // italic
801 float hg35 = float(hg * 3.0) / 5.0;
803 // display with proper accent mark
805 scr.drawText(font, &ic, 1, baseline, int(x));
807 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
810 int tmpvar = baseline - font.ascent('i');
812 if (font.shape() == LyXFont::ITALIC_SHAPE)
813 tmpx += (8.0 * hg) / 10.0; // italic
814 lyxerr[Debug::KEY] << "Removing dot." << endl;
815 // remove the dot first
816 scr.fillRectangle(gc_clear, int(x + tmpx),
819 font.ascent('x') - 1);
820 // the five lines below is a simple hack to
821 // make the display of accent 'i' and 'j'
822 // better. It makes the accent be written
823 // closer to the top of the dot-less 'i' or 'j'.
824 char tmpic = ic; // store the ic when we
825 ic = 'x'; // calculates the ascent of
826 asc = Ascent(font); // the dot-less version (here: 'x')
827 ic = tmpic; // set the orig ic back
828 y = baseline - asc; // update to new y coord.
830 // now the rest - draw within (x, y, x+wid, y+hg)
834 scr.drawLine(pgc, int(x2), int(y + hg),
835 int(x2 + hg35), y + hg35);
840 scr.drawLine(pgc, int(x2), int(y + hg),
841 int(x2 - hg35), y + hg35);
844 case MACRON: // macron
855 if (hg35 > 2.0) hg35 -= 1.0;
858 p[0].x = int(x2 - 2.0 * hg35); p[0].y = int(y + hg);
859 p[1].x = int(x2 - hg35); p[1].y = int(y + hg35);
860 p[2].x = int(x2); p[2].y = int(y + hg);
861 p[3].x = int(x2 + hg35); p[3].y = int(y + hg35);
862 scr.drawLines(pgc, p, 4);
865 case UNDERBAR: // underbar
874 case CEDILLA: // cedilla
877 p[0].x = int(x2); p[0].y = y;
878 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
879 p[2].x = int(x2 + (hg / 3.0));
880 p[2].y = y + int(hg / 2.0);
881 p[3].x = int(x2 - (hg / 4.0)); p[3].y = y + int(hg);
882 scr.drawLines(pgc, p, 4);
885 case UNDERDOT: // underdot
887 scr.fillArc(pgc, int(x2), y + hg35,
894 scr.fillArc(pgc, int(x2), y + hg * 0.5,
896 (hg + 3.0)/5.0, 0, 360*64);
900 case CIRCLE: // circle
902 scr.drawArc(pgc, int(x2 - (hg / 2.0)),
903 y + (hg / 2.0), hg, hg, 0,
910 int(x2 + hg35), y + (hg / 2.0),
911 2 * hg, hg, 0, 360*32);
917 int(x2 - (hg / 2.0)), y,
924 p[0].x = int(x2 - hg35); p[0].y = int(y + hg35);
925 p[1].x = int(x2); p[1].y = int(y + hg);
926 p[2].x = int(x2 + hg35); p[2].y = int(y + hg35);
927 scr.drawLines(pgc, p, 3);
930 case SPECIAL_CARON: // special caron
933 case 'L': wid = int(4.0 * wid / 5.0); break;
934 case 't': y -= int(hg35 / 2.0); break;
937 p[0].x = int(x + wid); p[0].y = int(y + hg35 + hg);
938 p[1].x = int(x + wid + (hg35 / 2.0));
939 p[1].y = int(y + hg + (hg35 / 2.0));
940 p[2].x = int(x + wid + (hg35 / 2.0));
941 p[2].y = y + int(hg);
942 scr.drawLines(pgc, p, 3);
945 case HUNGARIAN_UMLAUT: // hung. umlaut
948 s[0].x1= int(x2 - (hg / 2.0)); s[0].y1 = int(y + hg);
949 s[0].x2 = int(x2 + hg35 - (hg / 2.0));
950 s[0].y2 = int(y + hg35);
951 s[1].x1 = int(x2 + (hg / 2.0));
952 s[1].y1 = int(y + hg);
953 s[1].x2 = int(x2 + hg35 + (hg / 2.0));
954 s[1].y2 = int(y + hg35);
956 scr.drawSegments(pgc, s, 2);
959 case UMLAUT: // umlaut
961 float rad = hg / 2.0;
964 int(x2 - ((4.0 * hg) / 7.0)),
967 int(x2 + ((4.0 * hg) / 7.0)),
970 rad += .5; // this ensures that f.ex. 1.5 will
971 // not be rounded down to .5 and then
972 // converted to int = 0
973 scr.fillArc(pgc, int(x2 - ((2.0 * hg) / 4.0)),
975 rad, rad, 0, 360*64);
976 scr.fillArc(pgc, int(x2 + ((2.0 * hg) / 4.0)),
978 rad, rad, 0, 360*64);
980 //scr.drawText(font, "¨", 1, baseline, x2);
983 case CIRCUMFLEX: // circumflex
986 p[0].x = int(x2 - hg35); p[0].y = y + int(hg);
987 p[1].x = int(x2); p[1].y = int(y + hg35);
988 p[2].x = int(x2 + hg35); p[2].y = y + int(hg);
989 scr.drawLines(pgc, p, 3);
992 case OGONEK: // ogonek
994 // this does probably not look like an ogonek, so
995 // it should certainly be refined
997 p[0].x = int(x2); p[0].y = y;
998 p[1].x = int(x2); p[1].y = y + int(hg35);
999 p[2].x = int(x2 - hg35);
1000 p[2].y = y + int(hg / 2.0);
1001 p[3].x = int(x2 + (hg / 4.0)); p[3].y = y + int(hg);
1002 scr.drawLines(pgc, p, 4);
1010 p[0].y = y + int(3.0 * hg);
1011 p[1].x = int(x + float(wid) * 0.75);
1012 p[1].y = y + int(hg);
1013 scr.drawLines(pgc, p, 2);
1016 case DOT_LESS_I: // dotless-i
1017 case DOT_LESS_J: // dotless-j
1019 // nothing to do for these
1024 scr.fillRectangle(gc_lighted,
1025 int(x + 1), baseline - Ascent(font) + 1,
1027 Ascent(font) + Descent(font) - 2);
1029 scr.drawRectangle(gc_lighted,
1030 int(x), baseline - Ascent(font),
1032 Ascent(font) + Descent(font) - 1);
1033 scr.drawString(font, contents, baseline, int(x + 2));
1040 void InsetLatexAccent::Write(ostream & os)
1042 os << "\\i " << contents << "\n";
1046 void InsetLatexAccent::Read(LyXLex & lex)
1049 contents = lex.GetString();
1054 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/)
1061 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/)
1068 int InsetLatexAccent::Linuxdoc(string & file)
1075 int InsetLatexAccent::DocBook(string & file)
1082 bool InsetLatexAccent::Deletable() const
1088 bool InsetLatexAccent::DirectWrite() const
1094 Inset * InsetLatexAccent::Clone() const
1096 return new InsetLatexAccent(contents);
1100 Inset::Code InsetLatexAccent::LyxCode() const
1102 return Inset::ACCENT_CODE;
1106 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
1108 return o << int(at);