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"
25 /* LatexAccent. Proper handling of accented characters */
26 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
27 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
29 InsetLatexAccent::InsetLatexAccent()
35 InsetLatexAccent::InsetLatexAccent(string const & string)
42 void InsetLatexAccent::checkContents()
43 // check, if we know the modifier and can display it ok on screen
47 if (contents.empty() || contents.length() < 2) return;
50 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
51 contents = frontStrip(strip(contents));
52 if (!contains(contents, "{") && !contains(contents, "}")) {
53 if (contents.length() == 2) {
59 } else if (contents.length() == 3) {
67 } else if (contents.length() == 4 && contents[2] == ' ') {
75 } else if (contents.length() == 4 && contents[2] == '\\'
76 && (contents[3] == 'i' || contents[3] == 'j')) {
87 if (contents[0] != '\\') return; // demand that first char is a '\\'
89 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
91 remdot = false; plusasc = false; plusdesc = false;
93 switch (contents[1]) { // second char should be one of these
95 modtype = ACUTE; // acute
96 plusasc = true; // at the top of character
99 modtype = GRAVE; // grave
100 plusasc = true; // at the top
103 modtype = MACRON; // macron
104 plusasc = true; // at the top
107 modtype = TILDE; // tilde
108 plusasc = true; // at the top
110 case 'b': // underbar
111 modtype = UNDERBAR; // underbar
112 plusdesc = true; // at the bottom
115 modtype = CEDILLA; // cedilla
116 plusdesc = true; // at the bottom
118 case 'd': // underdot
119 modtype = UNDERDOT; // underdot
120 plusdesc = true; // at the bottom
123 modtype = CIRCLE; // circle
124 plusasc = true; // at the top
127 modtype = TIE; // tie
128 plusasc = true; // at the top
131 modtype = BREVE; // breve
132 plusasc = true; // at the top
135 modtype = CARON; // caron
136 plusasc = true; // at the top
138 case 'q': // special caron
139 modtype = SPECIAL_CARON; // special caron
140 plusasc = true; // at the top
142 case 'H': // hungarian umlaut
143 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
144 plusasc = true; // at the top
147 modtype = UMLAUT; // umlaut
148 plusasc = true; // at the top
151 modtype = DOT; // dot
152 plusasc = true; // at the top
154 case '^': // circumflex
155 modtype = CIRCUMFLEX; // circumflex
156 plusasc = true; // at the top
159 modtype = OGONEK; // ogonek
162 case 'i': // dot-less-i
163 modtype = DOT_LESS_I; // dot-less-i
164 plusasc = true; // at the top (not really needed)
167 case 'j': // dot-less-j
168 modtype = DOT_LESS_J; // dot-less-j
169 plusasc = true; // at the top (not really needed)
174 plusasc = true; // at the top (not really needed)
178 plusasc = true; // at the top (not really needed)
181 printf ("Default\n");
182 // unknow accent (or something else)
186 // we demand that third char is a '{' (Lgb)
187 if (contents[2] != '{') return;
189 // special clause for \i{}, \j{} \l{} and \L{}
190 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
191 || modtype == lSLASH || modtype == LSLASH)
192 && contents[3] == '}' ) {
194 case DOT_LESS_I: ic = 'i'; break;
195 case DOT_LESS_J: ic = 'j'; break;
196 case lSLASH: ic = 'l'; break;
197 case LSLASH: ic = 'L'; break;
199 // if this happens something is really wrong
200 lyxerr << "InsetLaTexAccent: weird error." << endl;
203 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
204 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
206 << ", top: " << plusasc
207 << ", bot: " << plusdesc
208 << ", dot: " << remdot
209 << ", mod: " << modtype << endl;
210 // Special case for space
211 } else if (contents[3] == '}') {
217 ic = contents[3]; // i will always be 3 here
219 // ic should now be a alfa-char or '\\'
221 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
222 if (ic == 'i' || ic == 'j')
226 } else if ( (ic == 'i'|| ic == 'j') && contents[4] == '}') {
227 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
228 string temp = contents;
229 temp.erase(3, string::npos);
232 for(string::size_type j = 4;
233 j < contents.length(); ++j)
240 // demand a '}' at the end
241 if (contents[++i] != '}' && contents[++i]) return;
243 // fine, the char is properly decoded now (hopefully)
244 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
246 << ", top: " << plusasc
247 << ", bot: " << plusdesc
248 << ", dot: " << remdot
249 << ", mod: " << modtype << endl;
255 int InsetLatexAccent::ascent(Painter &, LyXFont const & font) const
257 // This function is a bit too simplistix and is just a
258 // "try to make a fit for all accents" approach, to
259 // make it better we need to know what kind of accent is
260 // used and add to max based on that.
264 max = font.ascent('a');
266 max = font.ascent(ic);
268 max += (font.maxAscent() + 3) / 3;
270 max = font.maxAscent() + 4;
275 int InsetLatexAccent::descent(Painter &, LyXFont const & font) const
280 max = font.descent('a');
282 max = font.descent(ic);
286 max = font.maxDescent() + 4;
291 int InsetLatexAccent::width(Painter &, LyXFont const & font) const
294 return font.textWidth(&ic, 1);
296 return font.stringWidth(contents) + 4;
300 int InsetLatexAccent::Lbearing(LyXFont const & font) const
302 return font.lbearing(ic);
306 int InsetLatexAccent::Rbearing(LyXFont const & font) const
308 return font.rbearing(ic);
312 bool InsetLatexAccent::DisplayISO8859_9(Painter & pain, LyXFont const & font,
316 unsigned char tmpic = ic;
321 if (ic == 'c') tmpic = 0xe7;
322 if (ic == 'C') tmpic = 0xc7;
323 if (ic == 's') tmpic = 0xfe;
324 if (ic == 'S') tmpic = 0xde;
328 { if (ic == 'g') tmpic = 0xf0;
329 if (ic == 'G') tmpic = 0xd0;
334 if (ic == 'o') tmpic = 0xf6;
335 if (ic == 'O') tmpic = 0xd6;
336 if (ic == 'u') tmpic = 0xfc;
337 if (ic == 'U') tmpic = 0xdc;
340 case DOT: if (ic == 'I') tmpic = 0xdd; break;
341 case DOT_LESS_I: tmpic = 0xfd; break;
342 default: return false;
345 char ch = char(tmpic);
346 pain.text(x, baseline, ch, font);
347 x += width(pain, font);
355 void InsetLatexAccent::draw(Painter & pain, LyXFont const & font,
356 int baseline, float & x) const
358 if (lyxrc->font_norm == "iso8859-9")
359 if (DisplayISO8859_9(pain, font, baseline, x))
363 // All the manually drawn accents in this function could use an
364 // overhaul. Different ways of drawing (what metrics to use)
365 // should also be considered.
368 int asc = ascent(pain, font);
369 int desc = descent(pain, font);
370 int wid = width(pain, font);
371 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
376 hg = font.maxDescent();
379 if (font.shape() == LyXFont::ITALIC_SHAPE)
380 x2 += (4.0 * hg) / 5.0; // italic
387 float hg35 = float(hg * 3.0) / 5.0;
389 // display with proper accent mark
391 pain.text(int(x), baseline, ic, font);
394 int tmpvar = baseline - font.ascent('i');
396 if (font.shape() == LyXFont::ITALIC_SHAPE)
397 tmpx += (8.0 * hg) / 10.0; // italic
398 lyxerr[Debug::KEY] << "Removing dot." << endl;
399 // remove the dot first
400 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
402 font.ascent('x') - 1,
404 // the five lines below is a simple hack to
405 // make the display of accent 'i' and 'j'
406 // better. It makes the accent be written
407 // closer to the top of the dot-less 'i' or 'j'.
408 char tmpic = ic; // store the ic when we
409 ic = 'x'; // calculates the ascent of
410 asc = ascent(pain, font); // the dot-less version (here: 'x')
411 ic = tmpic; // set the orig ic back
412 y = baseline - asc; // update to new y coord.
414 // now the rest - draw within (x, y, x+wid, y+hg)
416 case ACUTE: // acute 0xB4
419 pain.line(int(x2), int(y + hg),
420 int(x2 + hg35), y + hg35);
422 pain.text(x2 - (font.rbearing(0xB4) - font.lbearing(0xB4)) / 2,
423 baseline - font.ascent(ic) - font.descent(0xB4) - (font.ascent(0xB4) + font.descent(0xB4)) / 2,
428 case GRAVE: // grave 0x60
431 pain.line(int(x2), int(y + hg),
432 int(x2 - hg35), y + hg35);
434 pain.text(x2 - (font.rbearing(0x60) - font.lbearing(0x60)) / 2,
435 baseline - font.ascent(ic) - font.descent(0x60) - (font.ascent(0x60) + font.descent(0x60)) / 2.0,
440 case MACRON: // macron
443 pain.line(int(x2 - wid * 0.4),
448 pain.text(x2 - (font.rbearing(0xAF) - font.lbearing(0xAF)) / 2,
449 baseline - font.ascent(ic) - font.descent(0xAF) - (font.ascent(0xAF) + font.descent(0xAF)),
457 if (hg35 > 2.0) hg35 -= 1.0;
461 xp[0] = int(x2 - 2.0 * hg35);
464 xp[1] = int(x2 - hg35);
465 yp[1] = int(y + hg35);
470 xp[3] = int(x2 + hg35);
471 yp[3] = int(y + hg35);
473 pain.lines(xp, yp, 4);
475 pain.text(x2 - (font.rbearing('~') - font.lbearing('~')) / 2,
476 baseline - font.ascent(ic) - font.descent('~') - (font.ascent('~') + font.descent('~')) / 2,
481 case UNDERBAR: // underbar 0x5F
484 pain.line(int(x2 - wid * 0.4),
489 pain.text(x2 - (font.rbearing(0x5F) - font.lbearing(0x5F)) / 2, baseline,
494 case CEDILLA: // cedilla
503 yp[1] = y + int(hg / 3.0);
505 xp[2] = int(x2 + (hg / 3.0));
506 yp[2] = y + int(hg / 2.0);
508 xp[3] = int(x2 - (hg / 4.0));
511 pain.lines(xp, yp, 4);
513 pain.text(x2 - (font.rbearing(0xB8) - font.lbearing(0xB8)) / 2, baseline,
519 case UNDERDOT: // underdot
522 pain.arc(int(x2), y + hg35,
525 pain.text(x2 - (font.rbearing('.') - font.lbearing('.')) / 2.0,
526 baseline + 3.0 / 2.0 * (font.ascent('.') + font.descent('.')),
535 pain.arc(int(x2), y + hg * 0.5,
540 pain.text(x2 - (font.rbearing('.') - font.lbearing('.')) / 2.0,
541 baseline - font.ascent(ic) - font.descent('.') - (font.ascent('.') + font.descent('.')) / 2,
547 case CIRCLE: // circle
550 pain.arc(int(x2 - (hg / 2.0)),
551 y + (hg / 2.0), hg, hg , 0,
555 tmpf.decSize().decSize();
556 pain.text(x2 - (tmpf.rbearing(0xB0) - tmpf.lbearing(0xB0)) / 2.0,
557 baseline - font.ascent(ic) - tmpf.descent(0xB0) - (tmpf.ascent(0xB0) + tmpf.descent(0xB0)) / 3.0,
564 pain.arc(int(x2 + hg35), y + hg / 2.0,
565 2 * hg, hg, 0, 360 * 32);
570 pain.arc(int(x2 - (hg / 2.0)), y,
578 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
579 xp[1] = int(x2); yp[1] = int(y + hg);
580 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
581 pain.lines(xp, yp, 3);
584 case SPECIAL_CARON: // special caron
587 case 'L': wid = int(4.0 * wid / 5.0); break;
588 case 't': y -= int(hg35 / 2.0); break;
591 xp[0] = int(x + wid);
592 yp[0] = int(y + hg35 + hg);
594 xp[1] = int(x + wid + (hg35 / 2.0));
595 yp[1] = int(y + hg + (hg35 / 2.0));
597 xp[2] = int(x + wid + (hg35 / 2.0));
600 pain.lines(xp, yp, 3);
603 case HUNGARIAN_UMLAUT: // hung. umlaut
606 int xs1[2], xs2[2], ys1[2], ys2[2];
608 xs1[0] = int(x2 - (hg / 2.0));
609 ys1[0] = int(y + hg);
611 xs2[0] = int(x2 + hg35 - (hg / 2.0));
612 ys2[0] = int(y + hg35);
614 xs1[1] = int(x2 + (hg / 2.0));
615 ys1[1] = int(y + hg);
617 xs2[1] = int(x2 + hg35 + (hg / 2.0));
618 ys2[1] = int(y + hg35);
620 pain.segments(xs1, ys1, xs2, ys2, 2);
622 pain.text(x2 - (font.rbearing('´') - font.lbearing('´')),
623 baseline - font.ascent(ic) - font.descent('´') - (font.ascent('´') + font.descent('´')) / 2,
626 baseline - font.ascent(ic) - font.descent('´') - (font.ascent('´') + font.descent('´')) / 2,
631 case UMLAUT: // umlaut
634 float rad = hg / 2.0;
636 pain.point(int(x2 - 4.0 * hg / 7.0),
638 pain.point(int(x2 + 4.0 * hg / 7.0),
641 rad += .5; // this ensures that f.ex. 1.5 will
642 // not be rounded down to .5 and then
643 // converted to int = 0
644 pain.arc(int(x2 - 2.0 * hg / 4.0),
648 pain.arc(int(x2 + 2.0 * hg / 4.0),
650 rad, rad, 0, 360*64);
653 pain.text(x2 - (font.rbearing('¨') - font.lbearing('¨')) / 2,
654 baseline - font.ascent(ic) - font.descent('¨') - ( font.ascent('¨') + font.descent('¨')) / 2,
659 case CIRCUMFLEX: // circumflex
664 xp[0] = int(x2 - hg35); yp[0] = y + int(hg);
665 xp[1] = int(x2); yp[1] = int(y + hg35);
666 xp[2] = int(x2 + hg35); yp[2] = y + int(hg);
667 pain.lines(xp, yp, 3);
670 tmpf.decSize().decSize().decSize();
671 pain.text(x2 - (tmpf.rbearing(0x5E) - tmpf.lbearing(0x5E)) / 2,
672 baseline - font.ascent(ic) - tmpf.descent(0x5E) - (tmpf.ascent(0x5E) + tmpf.descent(0x5E)) / 3.0,
677 case OGONEK: // ogonek
679 // this does probably not look like an ogonek, so
680 // it should certainly be refined
687 yp[1] = y + int(hg35);
689 xp[2] = int(x2 - hg35);
690 yp[2] = y + int(hg / 2.0);
692 xp[3] = int(x2 + hg / 4.0);
695 pain.lines(xp, yp, 4);
704 yp[0] = y + int(3.0 * hg);
706 xp[1] = int(x + float(wid) * 0.75);
709 pain.lines(xp, yp, 2);
712 case DOT_LESS_I: // dotless-i
713 case DOT_LESS_J: // dotless-j
715 // nothing to do for these
720 pain.fillRectangle(int(x + 1),
721 baseline - ascent(pain, font) + 1,
722 width(pain, font) - 2,
724 + descent(pain, font) - 2);
725 pain.rectangle(int(x + 1), baseline - ascent(pain, font) + 1,
726 width(pain, font) - 2,
727 ascent(pain, font) + descent(pain, font) - 2);
728 pain.text(int(x + 2), baseline, contents, font);
730 x += width(pain, font);
734 void InsetLatexAccent::Write(ostream & os) const
736 os << "\\i " << contents << "\n";
740 void InsetLatexAccent::Read(LyXLex & lex)
743 contents = lex.GetString();
748 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/) const
755 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/) const
762 int InsetLatexAccent::Linuxdoc(string & file) const
769 int InsetLatexAccent::DocBook(string & file) const
776 bool InsetLatexAccent::Deletable() const
782 bool InsetLatexAccent::DirectWrite() const
788 Inset * InsetLatexAccent::Clone() const
790 return new InsetLatexAccent(contents);
794 Inset::Code InsetLatexAccent::LyxCode() const
796 return Inset::ACCENT_CODE;
800 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)