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"
20 #include "support/lstrings.h"
24 /* LatexAccent. Proper handling of accented characters */
25 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
26 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
28 InsetLatexAccent::InsetLatexAccent()
34 InsetLatexAccent::InsetLatexAccent(string const & string)
41 void InsetLatexAccent::checkContents()
42 // check, if we know the modifier and can display it ok on screen
46 if (contents.empty() || contents.length() < 2) return;
49 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
50 contents = frontStrip(strip(contents));
51 if (!contains(contents, "{") && !contains(contents, "}")) {
52 if (contents.length() == 2) {
58 } else if (contents.length() == 3) {
66 } else if (contents.length() == 4 && contents[2] == ' ') {
74 } else if (contents.length() == 4 && contents[2] == '\\'
75 && (contents[3] == 'i' || contents[3] == 'j')) {
86 if (contents[0] != '\\') return; // demand that first char is a '\\'
88 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
90 remdot = false; plusasc = false; plusdesc = false;
92 switch (contents[1]) { // second char should be one of these
94 modtype = ACUTE; // acute
95 plusasc = true; // at the top of character
98 modtype = GRAVE; // grave
99 plusasc = true; // at the top
102 modtype = MACRON; // macron
103 plusasc = true; // at the top
106 modtype = TILDE; // tilde
107 plusasc = true; // at the top
109 case 'b': // underbar
110 modtype = UNDERBAR; // underbar
111 plusdesc = true; // at the bottom
114 modtype = CEDILLA; // cedilla
115 plusdesc = true; // at the bottom
117 case 'd': // underdot
118 modtype = UNDERDOT; // underdot
119 plusdesc = true; // at the bottom
122 modtype = CIRCLE; // circle
123 plusasc = true; // at the top
126 modtype = TIE; // tie
127 plusasc = true; // at the top
130 modtype = BREVE; // breve
131 plusasc = true; // at the top
134 modtype = CARON; // caron
135 plusasc = true; // at the top
137 case 'q': // special caron
138 modtype = SPECIAL_CARON; // special caron
139 plusasc = true; // at the top
141 case 'H': // hungarian umlaut
142 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
143 plusasc = true; // at the top
146 modtype = UMLAUT; // umlaut
147 plusasc = true; // at the top
150 modtype = DOT; // dot
151 plusasc = true; // at the top
153 case '^': // circumflex
154 modtype = CIRCUMFLEX; // circumflex
155 plusasc = true; // at the top
158 modtype = OGONEK; // ogonek
161 case 'i': // dot-less-i
162 modtype = DOT_LESS_I; // dot-less-i
163 plusasc = true; // at the top (not really needed)
166 case 'j': // dot-less-j
167 modtype = DOT_LESS_J; // dot-less-j
168 plusasc = true; // at the top (not really needed)
173 plusasc = true; // at the top (not really needed)
177 plusasc = true; // at the top (not really needed)
180 printf ("Default\n");
181 // unknow accent (or something else)
185 // we demand that third char is a '{' (Lgb)
186 if (contents[2] != '{') return;
188 // special clause for \i{}, \j{} \l{} and \L{}
189 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
190 || modtype == lSLASH || modtype == LSLASH)
191 && contents[3] == '}' ) {
193 case DOT_LESS_I: ic = 'i'; break;
194 case DOT_LESS_J: ic = 'j'; break;
195 case lSLASH: ic = 'l'; break;
196 case LSLASH: ic = 'L'; break;
198 // if this happens something is really wrong
199 lyxerr << "InsetLaTexAccent: weird error." << endl;
202 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
203 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
205 << ", top: " << plusasc
206 << ", bot: " << plusdesc
207 << ", dot: " << remdot
208 << ", mod: " << modtype << endl;
209 // Special case for space
210 } else if (contents[3] == '}') {
216 ic = contents[3]; // i will always be 3 here
218 // ic should now be a alfa-char or '\\'
220 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
221 if (ic == 'i' || ic == 'j')
225 } else if ( (ic == 'i'|| ic == 'j') && contents[4] == '}') {
226 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
227 string temp = contents;
228 temp.erase(3, string::npos);
231 for(string::size_type j = 4;
232 j < contents.length(); ++j)
239 // demand a '}' at the end
240 if (contents[++i] != '}' && contents[++i]) return;
242 // fine, the char is properly decoded now (hopefully)
243 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
245 << ", top: " << plusasc
246 << ", bot: " << plusdesc
247 << ", dot: " << remdot
248 << ", mod: " << modtype << endl;
254 int InsetLatexAccent::ascent(Painter &, LyXFont const & font) const
256 // This function is a bit too simplistix and is just a
257 // "try to make a fit for all accents" approach, to
258 // make it better we need to know what kind of accent is
259 // used and add to max based on that.
263 max = font.ascent('a');
265 max = font.ascent(ic);
267 max += (font.maxAscent() + 3) / 3;
269 max = font.maxAscent() + 4;
274 int InsetLatexAccent::descent(Painter &, LyXFont const & font) const
279 max = font.descent('a');
281 max = font.descent(ic);
285 max = font.maxDescent() + 4;
290 int InsetLatexAccent::width(Painter &, LyXFont const & font) const
293 return font.textWidth(&ic, 1);
295 return font.stringWidth(contents) + 4;
299 int InsetLatexAccent::Lbearing(LyXFont const & font) const
301 return font.lbearing(ic);
305 int InsetLatexAccent::Rbearing(LyXFont const & font) const
307 return font.rbearing(ic);
311 bool InsetLatexAccent::DisplayISO8859_9(Painter & pain, LyXFont const & font,
315 unsigned char tmpic = ic;
320 if (ic == 'c') tmpic = 0xe7;
321 if (ic == 'C') tmpic = 0xc7;
322 if (ic == 's') tmpic = 0xfe;
323 if (ic == 'S') tmpic = 0xde;
327 { if (ic == 'g') tmpic = 0xf0;
328 if (ic == 'G') tmpic = 0xd0;
333 if (ic == 'o') tmpic = 0xf6;
334 if (ic == 'O') tmpic = 0xd6;
335 if (ic == 'u') tmpic = 0xfc;
336 if (ic == 'U') tmpic = 0xdc;
339 case DOT: if (ic == 'I') tmpic = 0xdd; break;
340 case DOT_LESS_I: tmpic = 0xfd; break;
341 default: return false;
344 char ch = char(tmpic);
345 pain.text(x, baseline, ch, font);
346 x += width(pain, font);
354 void InsetLatexAccent::draw(Painter & pain, LyXFont const & font,
355 int baseline, float & x) const
357 if (lyxrc.font_norm == "iso8859-9")
358 if (DisplayISO8859_9(pain, font, baseline, x))
362 // All the manually drawn accents in this function could use an
363 // overhaul. Different ways of drawing (what metrics to use)
364 // should also be considered.
367 int asc = ascent(pain, font);
368 int desc = descent(pain, font);
369 int wid = width(pain, font);
370 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
375 hg = font.maxDescent();
378 if (font.shape() == LyXFont::ITALIC_SHAPE)
379 x2 += (4.0 * hg) / 5.0; // italic
386 float hg35 = float(hg * 3.0) / 5.0;
388 // display with proper accent mark
390 pain.text(int(x), baseline, ic, font);
393 int tmpvar = baseline - font.ascent('i');
395 if (font.shape() == LyXFont::ITALIC_SHAPE)
396 tmpx += (8.0 * hg) / 10.0; // italic
397 lyxerr[Debug::KEY] << "Removing dot." << endl;
398 // remove the dot first
399 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
401 font.ascent('x') - 1,
403 // the five lines below is a simple hack to
404 // make the display of accent 'i' and 'j'
405 // better. It makes the accent be written
406 // closer to the top of the dot-less 'i' or 'j'.
407 char tmpic = ic; // store the ic when we
408 ic = 'x'; // calculates the ascent of
409 asc = ascent(pain, font); // the dot-less version (here: 'x')
410 ic = tmpic; // set the orig ic back
411 y = baseline - asc; // update to new y coord.
413 // now the rest - draw within (x, y, x+wid, y+hg)
415 case ACUTE: // acute 0xB4
418 pain.line(int(x2), int(y + hg),
419 int(x2 + hg35), y + hg35);
421 pain.text(x2 - (font.rbearing(0xB4) - font.lbearing(0xB4)) / 2,
422 baseline - font.ascent(ic) - font.descent(0xB4) - (font.ascent(0xB4) + font.descent(0xB4)) / 2,
427 case GRAVE: // grave 0x60
430 pain.line(int(x2), int(y + hg),
431 int(x2 - hg35), y + hg35);
433 pain.text(x2 - (font.rbearing(0x60) - font.lbearing(0x60)) / 2,
434 baseline - font.ascent(ic) - font.descent(0x60) - (font.ascent(0x60) + font.descent(0x60)) / 2.0,
439 case MACRON: // macron
442 pain.line(int(x2 - wid * 0.4),
447 pain.text(x2 - (font.rbearing(0xAF) - font.lbearing(0xAF)) / 2,
448 baseline - font.ascent(ic) - font.descent(0xAF) - (font.ascent(0xAF) + font.descent(0xAF)),
456 if (hg35 > 2.0) hg35 -= 1.0;
460 xp[0] = int(x2 - 2.0 * hg35);
463 xp[1] = int(x2 - hg35);
464 yp[1] = int(y + hg35);
469 xp[3] = int(x2 + hg35);
470 yp[3] = int(y + hg35);
472 pain.lines(xp, yp, 4);
474 pain.text(x2 - (font.rbearing('~') - font.lbearing('~')) / 2,
475 baseline - font.ascent(ic) - font.descent('~') - (font.ascent('~') + font.descent('~')) / 2,
480 case UNDERBAR: // underbar 0x5F
483 pain.line(int(x2 - wid * 0.4),
488 pain.text(x2 - (font.rbearing(0x5F) - font.lbearing(0x5F)) / 2, baseline,
493 case CEDILLA: // cedilla
502 yp[1] = y + int(hg / 3.0);
504 xp[2] = int(x2 + (hg / 3.0));
505 yp[2] = y + int(hg / 2.0);
507 xp[3] = int(x2 - (hg / 4.0));
510 pain.lines(xp, yp, 4);
512 pain.text(x2 - (font.rbearing(0xB8) - font.lbearing(0xB8)) / 2, baseline,
518 case UNDERDOT: // underdot
521 pain.arc(int(x2), y + hg35,
524 pain.text(x2 - (font.rbearing('.') - font.lbearing('.')) / 2.0,
525 baseline + 3.0 / 2.0 * (font.ascent('.') + font.descent('.')),
534 pain.arc(int(x2), y + hg * 0.5,
539 pain.text(x2 - (font.rbearing('.') - font.lbearing('.')) / 2.0,
540 baseline - font.ascent(ic) - font.descent('.') - (font.ascent('.') + font.descent('.')) / 2,
546 case CIRCLE: // circle
549 pain.arc(int(x2 - (hg / 2.0)),
550 y + (hg / 2.0), hg, hg , 0,
554 tmpf.decSize().decSize();
555 pain.text(x2 - (tmpf.rbearing(0xB0) - tmpf.lbearing(0xB0)) / 2.0,
556 baseline - font.ascent(ic) - tmpf.descent(0xB0) - (tmpf.ascent(0xB0) + tmpf.descent(0xB0)) / 3.0,
563 pain.arc(int(x2 + hg35), y + hg / 2.0,
564 2 * hg, hg, 0, 360 * 32);
569 pain.arc(int(x2 - (hg / 2.0)), y,
577 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
578 xp[1] = int(x2); yp[1] = int(y + hg);
579 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
580 pain.lines(xp, yp, 3);
583 case SPECIAL_CARON: // special caron
586 case 'L': wid = int(4.0 * wid / 5.0); break;
587 case 't': y -= int(hg35 / 2.0); break;
590 xp[0] = int(x + wid);
591 yp[0] = int(y + hg35 + hg);
593 xp[1] = int(x + wid + (hg35 / 2.0));
594 yp[1] = int(y + hg + (hg35 / 2.0));
596 xp[2] = int(x + wid + (hg35 / 2.0));
599 pain.lines(xp, yp, 3);
602 case HUNGARIAN_UMLAUT: // hung. umlaut
605 int xs1[2], xs2[2], ys1[2], ys2[2];
607 xs1[0] = int(x2 - (hg / 2.0));
608 ys1[0] = int(y + hg);
610 xs2[0] = int(x2 + hg35 - (hg / 2.0));
611 ys2[0] = int(y + hg35);
613 xs1[1] = int(x2 + (hg / 2.0));
614 ys1[1] = int(y + hg);
616 xs2[1] = int(x2 + hg35 + (hg / 2.0));
617 ys2[1] = int(y + hg35);
619 pain.segments(xs1, ys1, xs2, ys2, 2);
621 pain.text(x2 - (font.rbearing('´') - font.lbearing('´')),
622 baseline - font.ascent(ic) - font.descent('´') - (font.ascent('´') + font.descent('´')) / 2,
625 baseline - font.ascent(ic) - font.descent('´') - (font.ascent('´') + font.descent('´')) / 2,
630 case UMLAUT: // umlaut
633 float rad = hg / 2.0;
635 pain.point(int(x2 - 4.0 * hg / 7.0),
637 pain.point(int(x2 + 4.0 * hg / 7.0),
640 rad += .5; // this ensures that f.ex. 1.5 will
641 // not be rounded down to .5 and then
642 // converted to int = 0
643 pain.arc(int(x2 - 2.0 * hg / 4.0),
647 pain.arc(int(x2 + 2.0 * hg / 4.0),
649 rad, rad, 0, 360*64);
652 pain.text(x2 - (font.rbearing('¨') - font.lbearing('¨')) / 2,
653 baseline - font.ascent(ic) - font.descent('¨') - ( font.ascent('¨') + font.descent('¨')) / 2,
658 case CIRCUMFLEX: // circumflex
663 xp[0] = int(x2 - hg35); yp[0] = y + int(hg);
664 xp[1] = int(x2); yp[1] = int(y + hg35);
665 xp[2] = int(x2 + hg35); yp[2] = y + int(hg);
666 pain.lines(xp, yp, 3);
669 tmpf.decSize().decSize().decSize();
670 pain.text(x2 - (tmpf.rbearing(0x5E) - tmpf.lbearing(0x5E)) / 2,
671 baseline - font.ascent(ic) - tmpf.descent(0x5E) - (tmpf.ascent(0x5E) + tmpf.descent(0x5E)) / 3.0,
676 case OGONEK: // ogonek
678 // this does probably not look like an ogonek, so
679 // it should certainly be refined
686 yp[1] = y + int(hg35);
688 xp[2] = int(x2 - hg35);
689 yp[2] = y + int(hg / 2.0);
691 xp[3] = int(x2 + hg / 4.0);
694 pain.lines(xp, yp, 4);
703 yp[0] = y + int(3.0 * hg);
705 xp[1] = int(x + float(wid) * 0.75);
708 pain.lines(xp, yp, 2);
711 case DOT_LESS_I: // dotless-i
712 case DOT_LESS_J: // dotless-j
714 // nothing to do for these
719 pain.fillRectangle(int(x + 1),
720 baseline - ascent(pain, font) + 1,
721 width(pain, font) - 2,
723 + descent(pain, font) - 2);
724 pain.rectangle(int(x + 1), baseline - ascent(pain, font) + 1,
725 width(pain, font) - 2,
726 ascent(pain, font) + descent(pain, font) - 2);
727 pain.text(int(x + 2), baseline, contents, font);
729 x += width(pain, font);
733 void InsetLatexAccent::Write(ostream & os) const
735 os << "\\i " << contents << "\n";
739 void InsetLatexAccent::Read(LyXLex & lex)
742 contents = lex.GetString();
747 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/, bool/*fs*/) const
754 int InsetLatexAccent::Linuxdoc(ostream & os) const
761 int InsetLatexAccent::DocBook(ostream & os) const
768 bool InsetLatexAccent::Deletable() const
774 bool InsetLatexAccent::DirectWrite() const
780 Inset * InsetLatexAccent::Clone() const
782 return new InsetLatexAccent(contents);
786 Inset::Code InsetLatexAccent::LyxCode() const
788 return Inset::ACCENT_CODE;
792 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)