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)
517 case ACUTE: // acute 0xB4
520 pain.line(int(x2), int(y + hg),
521 int(x2 + hg35), y + hg35);
523 pain.text(x2 - font.width(0xB4) / 2, baseline,
528 case GRAVE: // grave 0x60
531 pain.line(int(x2), int(y + hg),
532 int(x2 - hg35), y + hg35);
534 pain.text(x2 - font.width(0x60) / 2, baseline,
539 case MACRON: // macron
542 pain.line(int(x2 - wid * 0.4),
547 pain.text(x2 - font.width(0xAF) / 2, baseline,
555 if (hg35 > 2.0) hg35 -= 1.0;
559 xp[0] = int(x2 - 2.0 * hg35);
562 xp[1] = int(x2 - hg35);
563 yp[1] = int(y + hg35);
568 xp[3] = int(x2 + hg35);
569 yp[3] = int(y + hg35);
571 pain.lines(xp, yp, 4);
573 pain.text(x2 - font.width('~') / 2,
574 baseline - font.ascent('~'),
579 case UNDERBAR: // underbar 0x5F
582 pain.line(int(x2 - wid * 0.4),
587 pain.text(x2 - font.width(0x5F) / 2, baseline,
592 case CEDILLA: // cedilla
601 yp[1] = y + int(hg / 3.0);
603 xp[2] = int(x2 + (hg / 3.0));
604 yp[2] = y + int(hg / 2.0);
606 xp[3] = int(x2 - (hg / 4.0));
609 pain.lines(xp, yp, 4);
611 pain.text(x2 - font.width(0xB8) / 2, baseline,
617 case UNDERDOT: // underdot
619 pain.arc(int(x2), y + hg35,
626 pain.arc(int(x2), y + hg * 0.5,
633 case CIRCLE: // circle
636 pain.arc(int(x2 - (hg / 2.0)),
637 y + (hg / 2.0), hg, hg , 0,
640 pain.text(x2 - font.width(0xB0) / 2, baseline,
647 pain.arc(int(x2 + hg35), y + hg / 2.0,
648 2 * hg, hg, 0, 360 * 32);
653 pain.arc(int(x2 - (hg / 2.0)), y,
661 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
662 xp[1] = int(x2); yp[1] = int(y + hg);
663 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
664 pain.lines(xp, yp, 3);
667 case SPECIAL_CARON: // special caron
670 case 'L': wid = int(4.0 * wid / 5.0); break;
671 case 't': y -= int(hg35 / 2.0); break;
674 xp[0] = int(x + wid);
675 yp[0] = int(y + hg35 + hg);
677 xp[1] = int(x + wid + (hg35 / 2.0));
678 yp[1] = int(y + hg + (hg35 / 2.0));
680 xp[2] = int(x + wid + (hg35 / 2.0));
683 pain.lines(xp, yp, 3);
686 case HUNGARIAN_UMLAUT: // hung. umlaut
688 int xs1[2], xs2[2], ys1[2], ys2[2];
690 xs1[0] = int(x2 - (hg / 2.0));
691 ys1[0] = int(y + hg);
693 xs2[0] = int(x2 + hg35 - (hg / 2.0));
694 ys2[0] = int(y + hg35);
696 xs1[1] = int(x2 + (hg / 2.0));
697 ys1[1] = int(y + hg);
699 xs2[1] = int(x2 + hg35 + (hg / 2.0));
700 ys2[1] = int(y + hg35);
702 pain.segments(xs1, ys1, xs2, ys2, 2);
705 case UMLAUT: // umlaut
708 float rad = hg / 2.0;
710 pain.point(int(x2 - 4.0 * hg / 7.0),
712 pain.point(int(x2 + 4.0 * hg / 7.0),
715 rad += .5; // this ensures that f.ex. 1.5 will
716 // not be rounded down to .5 and then
717 // converted to int = 0
718 pain.arc(int(x2 - 2.0 * hg / 4.0),
722 pain.arc(int(x2 + 2.0 * hg / 4.0),
724 rad, rad, 0, 360*64);
727 pain.text(x2 - font.width('¨') / 2, baseline,
732 case CIRCUMFLEX: // circumflex
737 xp[0] = int(x2 - hg35); yp[0] = y + int(hg);
738 xp[1] = int(x2); yp[1] = int(y + hg35);
739 xp[2] = int(x2 + hg35); yp[2] = y + int(hg);
740 pain.lines(xp, yp, 3);
742 pain.text(x2 - font.width(0x5E) / 2, baseline,
747 case OGONEK: // ogonek
749 // this does probably not look like an ogonek, so
750 // it should certainly be refined
757 yp[1] = y + int(hg35);
759 xp[2] = int(x2 - hg35);
760 yp[2] = y + int(hg / 2.0);
762 xp[3] = int(x2 + hg / 4.0);
765 pain.lines(xp, yp, 4);
774 yp[0] = y + int(3.0 * hg);
776 xp[1] = int(x + float(wid) * 0.75);
779 pain.lines(xp, yp, 2);
782 case DOT_LESS_I: // dotless-i
783 case DOT_LESS_J: // dotless-j
785 // nothing to do for these
790 pain.fillRectangle(int(x + 1),
791 baseline - ascent(pain, font) + 1,
792 width(pain, font) - 2,
794 + descent(pain, font) - 2);
795 pain.rectangle(int(x + 1), baseline - ascent(pain, font) + 1,
796 width(pain, font) - 2,
797 ascent(pain, font) + descent(pain, font) - 2);
798 pain.text(int(x + 2), baseline, contents, font);
800 x += width(pain, font);
803 void InsetLatexAccent::Draw(LyXFont font,
808 if (lyxrc->font_norm == "iso8859-9")
809 if (DisplayISO8859_9 (font, scr, baseline, x))
813 // All the manually drawn accents in this function could use an
814 // overhaul. Different ways of drawing (what metrics to use)
815 // should also be considered.
818 int asc = Ascent(font);
819 int desc = Descent(font);
820 int wid = Width(font);
821 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
826 hg = font.maxDescent();
829 if (font.shape() == LyXFont::ITALIC_SHAPE)
830 x2 += (4.0 * hg) / 5.0; // italic
837 float hg35 = float(hg * 3.0) / 5.0;
839 // display with proper accent mark
841 scr.drawText(font, &ic, 1, baseline, int(x));
843 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
846 int tmpvar = baseline - font.ascent('i');
848 if (font.shape() == LyXFont::ITALIC_SHAPE)
849 tmpx += (8.0 * hg) / 10.0; // italic
850 lyxerr[Debug::KEY] << "Removing dot." << endl;
851 // remove the dot first
852 scr.fillRectangle(gc_clear, int(x + tmpx),
855 font.ascent('x') - 1);
856 // the five lines below is a simple hack to
857 // make the display of accent 'i' and 'j'
858 // better. It makes the accent be written
859 // closer to the top of the dot-less 'i' or 'j'.
860 char tmpic = ic; // store the ic when we
861 ic = 'x'; // calculates the ascent of
862 asc = Ascent(font); // the dot-less version (here: 'x')
863 ic = tmpic; // set the orig ic back
864 y = baseline - asc; // update to new y coord.
866 // now the rest - draw within (x, y, x+wid, y+hg)
870 scr.drawLine(pgc, int(x2), int(y + hg),
871 int(x2 + hg35), y + hg35);
876 scr.drawLine(pgc, int(x2), int(y + hg),
877 int(x2 - hg35), y + hg35);
880 case MACRON: // macron
891 if (hg35 > 2.0) hg35 -= 1.0;
894 p[0].x = int(x2 - 2.0 * hg35); p[0].y = int(y + hg);
895 p[1].x = int(x2 - hg35); p[1].y = int(y + hg35);
896 p[2].x = int(x2); p[2].y = int(y + hg);
897 p[3].x = int(x2 + hg35); p[3].y = int(y + hg35);
898 scr.drawLines(pgc, p, 4);
901 case UNDERBAR: // underbar
910 case CEDILLA: // cedilla
913 p[0].x = int(x2); p[0].y = y;
914 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
915 p[2].x = int(x2 + (hg / 3.0));
916 p[2].y = y + int(hg / 2.0);
917 p[3].x = int(x2 - (hg / 4.0)); p[3].y = y + int(hg);
918 scr.drawLines(pgc, p, 4);
921 case UNDERDOT: // underdot
923 scr.fillArc(pgc, int(x2), y + hg35,
930 scr.fillArc(pgc, int(x2), y + hg * 0.5,
932 (hg + 3.0)/5.0, 0, 360*64);
936 case CIRCLE: // circle
938 scr.drawArc(pgc, int(x2 - (hg / 2.0)),
939 y + (hg / 2.0), hg, hg, 0,
946 int(x2 + hg35), y + (hg / 2.0),
947 2 * hg, hg, 0, 360*32);
953 int(x2 - (hg / 2.0)), y,
960 p[0].x = int(x2 - hg35); p[0].y = int(y + hg35);
961 p[1].x = int(x2); p[1].y = int(y + hg);
962 p[2].x = int(x2 + hg35); p[2].y = int(y + hg35);
963 scr.drawLines(pgc, p, 3);
966 case SPECIAL_CARON: // special caron
969 case 'L': wid = int(4.0 * wid / 5.0); break;
970 case 't': y -= int(hg35 / 2.0); break;
973 p[0].x = int(x + wid); p[0].y = int(y + hg35 + hg);
974 p[1].x = int(x + wid + (hg35 / 2.0));
975 p[1].y = int(y + hg + (hg35 / 2.0));
976 p[2].x = int(x + wid + (hg35 / 2.0));
977 p[2].y = y + int(hg);
978 scr.drawLines(pgc, p, 3);
981 case HUNGARIAN_UMLAUT: // hung. umlaut
984 s[0].x1= int(x2 - (hg / 2.0)); s[0].y1 = int(y + hg);
985 s[0].x2 = int(x2 + hg35 - (hg / 2.0));
986 s[0].y2 = int(y + hg35);
987 s[1].x1 = int(x2 + (hg / 2.0));
988 s[1].y1 = int(y + hg);
989 s[1].x2 = int(x2 + hg35 + (hg / 2.0));
990 s[1].y2 = int(y + hg35);
992 scr.drawSegments(pgc, s, 2);
995 case UMLAUT: // umlaut
997 float rad = hg / 2.0;
1000 int(x2 - ((4.0 * hg) / 7.0)),
1003 int(x2 + ((4.0 * hg) / 7.0)),
1006 rad += .5; // this ensures that f.ex. 1.5 will
1007 // not be rounded down to .5 and then
1008 // converted to int = 0
1009 scr.fillArc(pgc, int(x2 - ((2.0 * hg) / 4.0)),
1011 rad, rad, 0, 360*64);
1012 scr.fillArc(pgc, int(x2 + ((2.0 * hg) / 4.0)),
1014 rad, rad, 0, 360*64);
1016 //scr.drawText(font, "¨", 1, baseline, x2);
1019 case CIRCUMFLEX: // circumflex
1022 p[0].x = int(x2 - hg35); p[0].y = y + int(hg);
1023 p[1].x = int(x2); p[1].y = int(y + hg35);
1024 p[2].x = int(x2 + hg35); p[2].y = y + int(hg);
1025 scr.drawLines(pgc, p, 3);
1028 case OGONEK: // ogonek
1030 // this does probably not look like an ogonek, so
1031 // it should certainly be refined
1033 p[0].x = int(x2); p[0].y = y;
1034 p[1].x = int(x2); p[1].y = y + int(hg35);
1035 p[2].x = int(x2 - hg35);
1036 p[2].y = y + int(hg / 2.0);
1037 p[3].x = int(x2 + (hg / 4.0)); p[3].y = y + int(hg);
1038 scr.drawLines(pgc, p, 4);
1046 p[0].y = y + int(3.0 * hg);
1047 p[1].x = int(x + float(wid) * 0.75);
1048 p[1].y = y + int(hg);
1049 scr.drawLines(pgc, p, 2);
1052 case DOT_LESS_I: // dotless-i
1053 case DOT_LESS_J: // dotless-j
1055 // nothing to do for these
1060 scr.fillRectangle(gc_lighted,
1061 int(x + 1), baseline - Ascent(font) + 1,
1063 Ascent(font) + Descent(font) - 2);
1065 scr.drawRectangle(gc_lighted,
1066 int(x), baseline - Ascent(font),
1068 Ascent(font) + Descent(font) - 1);
1069 scr.drawString(font, contents, baseline, int(x + 2));
1076 void InsetLatexAccent::Write(ostream & os)
1078 os << "\\i " << contents << "\n";
1082 void InsetLatexAccent::Read(LyXLex & lex)
1085 contents = lex.GetString();
1090 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/)
1097 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/)
1104 int InsetLatexAccent::Linuxdoc(string & file)
1111 int InsetLatexAccent::DocBook(string & file)
1118 bool InsetLatexAccent::Deletable() const
1124 bool InsetLatexAccent::DirectWrite() const
1130 Inset * InsetLatexAccent::Clone() const
1132 return new InsetLatexAccent(contents);
1136 Inset::Code InsetLatexAccent::LyxCode() const
1138 return Inset::ACCENT_CODE;
1142 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)
1144 return o << int(at);