1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetlatexaccent.h"
20 #include "support/lstrings.h"
21 #include "BufferView.h"
29 /* LatexAccent. Proper handling of accented characters */
30 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
31 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
33 InsetLatexAccent::InsetLatexAccent()
38 InsetLatexAccent::InsetLatexAccent(string const & str)
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) {
51 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
56 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
57 contents = frontStrip(strip(contents));
58 if (!contains(contents, "{") && !contains(contents, "}")) {
59 if (contents.length() == 2) {
65 } else if (contents.length() == 3) {
73 } else if (contents.length() == 4 && contents[2] == ' ') {
81 } else if (contents.length() == 4 && contents[2] == '\\'
82 && (contents[3] == 'i' || contents[3] == 'j')) {
93 if (contents[0] != '\\') { // demand that first char is a '\\'
94 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
98 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
100 remdot = false; plusasc = false; plusdesc = false;
102 switch (contents[1]) { // second char should be one of these
104 modtype = ACUTE; // acute
105 plusasc = true; // at the top of character
108 modtype = GRAVE; // grave
109 plusasc = true; // at the top
112 modtype = MACRON; // macron
113 plusasc = true; // at the top
116 modtype = TILDE; // tilde
117 plusasc = true; // at the top
119 case 'b': // underbar
120 modtype = UNDERBAR; // underbar
121 plusdesc = true; // at the bottom
124 modtype = CEDILLA; // cedilla
125 plusdesc = true; // at the bottom
127 case 'd': // underdot
128 modtype = UNDERDOT; // underdot
129 plusdesc = true; // at the bottom
132 modtype = CIRCLE; // circle
133 plusasc = true; // at the top
136 modtype = TIE; // tie
137 plusasc = true; // at the top
140 modtype = BREVE; // breve
141 plusasc = true; // at the top
144 modtype = CARON; // caron
145 plusasc = true; // at the top
147 case 'q': // special caron
148 modtype = SPECIAL_CARON; // special caron
149 plusasc = true; // at the top
151 case 'H': // hungarian umlaut
152 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
153 plusasc = true; // at the top
156 modtype = UMLAUT; // umlaut
157 plusasc = true; // at the top
160 modtype = DOT; // dot
161 plusasc = true; // at the top
163 case '^': // circumflex
164 modtype = CIRCUMFLEX; // circumflex
165 plusasc = true; // at the top
168 modtype = OGONEK; // ogonek
171 case 'i': // dot-less-i
172 modtype = DOT_LESS_I; // dot-less-i
173 plusasc = true; // at the top (not really needed)
176 case 'j': // dot-less-j
177 modtype = DOT_LESS_J; // dot-less-j
178 plusasc = true; // at the top (not really needed)
183 plusasc = true; // at the top (not really needed)
187 plusasc = true; // at the top (not really needed)
190 lyxerr[Debug::KEY] << "Default" << endl;
191 // unknow accent (or something else)
195 // we demand that third char is a '{' (Lgb)
196 if (contents[2] != '{') return;
198 // special clause for \i{}, \j{} \l{} and \L{}
199 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
200 || modtype == lSLASH || modtype == LSLASH)
201 && contents[3] == '}') {
203 case DOT_LESS_I: ic = 'i'; break;
204 case DOT_LESS_J: ic = 'j'; break;
205 case lSLASH: ic = 'l'; break;
206 case LSLASH: ic = 'L'; break;
208 // if this happens something is really wrong
209 lyxerr << "InsetLaTexAccent: weird error." << endl;
212 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
213 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
215 << ", top: " << plusasc
216 << ", bot: " << plusdesc
217 << ", dot: " << remdot
218 << ", mod: " << modtype << endl;
219 // Special case for space
220 } else if (contents[3] == '}') {
226 ic = contents[3]; // i will always be 3 here
228 // ic should now be a alfa-char or '\\'
230 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
231 if (ic == 'i' || ic == 'j')
235 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
236 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
237 string temp = contents;
238 temp.erase(3, string::npos);
241 for (string::size_type j = 4;
242 j < contents.length(); ++j)
249 // demand a '}' at the end
250 if (contents[++i] != '}' && contents[++i]) return;
252 // fine, the char is properly decoded now (hopefully)
253 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
255 << ", top: " << plusasc
256 << ", bot: " << plusdesc
257 << ", dot: " << remdot
258 << ", mod: " << modtype << endl;
264 int InsetLatexAccent::ascent(BufferView *, LyXFont const & font) const
266 // This function is a bit too simplistix and is just a
267 // "try to make a fit for all accents" approach, to
268 // make it better we need to know what kind of accent is
269 // used and add to max based on that.
273 max = lyxfont::ascent('a', font);
275 max = lyxfont::ascent(ic, font);
277 max += (lyxfont::maxAscent(font) + 3) / 3;
279 max = lyxfont::maxAscent(font) + 4;
284 int InsetLatexAccent::descent(BufferView *, LyXFont const & font) const
289 max = lyxfont::descent('a', font);
291 max = lyxfont::descent(ic, font);
295 max = lyxfont::maxDescent(font) + 4;
300 int InsetLatexAccent::width(BufferView *, LyXFont const & font) const
303 return lyxfont::width(ic, font);
305 return lyxfont::width(contents, font) + 4;
309 int InsetLatexAccent::lbearing(LyXFont const & font) const
311 return lyxfont::lbearing(ic, font);
315 int InsetLatexAccent::rbearing(LyXFont const & font) const
317 return lyxfont::rbearing(ic, font);
321 bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
325 unsigned char tmpic = ic;
330 if (ic == 'c') tmpic = 0xe7;
331 if (ic == 'C') tmpic = 0xc7;
332 if (ic == 's') tmpic = 0xfe;
333 if (ic == 'S') tmpic = 0xde;
337 { if (ic == 'g') tmpic = 0xf0;
338 if (ic == 'G') tmpic = 0xd0;
343 if (ic == 'o') tmpic = 0xf6;
344 if (ic == 'O') tmpic = 0xd6;
345 if (ic == 'u') tmpic = 0xfc;
346 if (ic == 'U') tmpic = 0xdc;
349 case DOT: if (ic == 'I') tmpic = 0xdd; break;
350 case DOT_LESS_I: tmpic = 0xfd; break;
351 default: return false;
354 char ch = char(tmpic);
355 bv->painter().text(int(x), baseline, ch, font);
356 x += width(bv, font);
364 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
365 int baseline, float & x, bool) const
367 Painter & pain = bv->painter();
369 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
370 if (displayISO8859_9(bv, font0, baseline, x))
374 // All the manually drawn accents in this function could use an
375 // overhaul. Different ways of drawing (what metrics to use)
376 // should also be considered.
379 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
380 font.setLanguage(english_language);
383 int asc = ascent(bv, font);
384 int desc = descent(bv, font);
385 int wid = width(bv, font);
386 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
391 hg = lyxfont::maxDescent(font);
394 if (font.shape() == LyXFont::ITALIC_SHAPE)
395 x2 += (4.0 * hg) / 5.0; // italic
402 float hg35 = float(hg * 3.0) / 5.0;
404 // display with proper accent mark
406 pain.text(int(x), baseline, ic, font);
409 int tmpvar = baseline - lyxfont::ascent('i', font);
411 if (font.shape() == LyXFont::ITALIC_SHAPE)
412 tmpx += (8.0 * hg) / 10.0; // italic
413 lyxerr[Debug::KEY] << "Removing dot." << endl;
414 // remove the dot first
415 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
416 lyxfont::ascent('i', font) -
417 lyxfont::ascent('x', font) - 1,
419 // the five lines below is a simple hack to
420 // make the display of accent 'i' and 'j'
421 // better. It makes the accent be written
422 // closer to the top of the dot-less 'i' or 'j'.
423 char tmpic = ic; // store the ic when we
424 ic = 'x'; // calculates the ascent of
425 asc = ascent(bv, font); // the dot-less version (here: 'x')
426 ic = tmpic; // set the orig ic back
427 y = baseline - asc; // update to new y coord.
429 // now the rest - draw within (x, y, x+wid, y+hg)
431 case ACUTE: // acute 0xB4
433 pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
434 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
438 case GRAVE: // grave 0x60
440 pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
441 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
445 case MACRON: // macron
447 pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
448 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
454 pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
455 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
459 case UNDERBAR: // underbar 0x5F
461 pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
465 case CEDILLA: // cedilla
467 pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
472 case UNDERDOT: // underdot
474 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
475 int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
482 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
483 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
488 case CIRCLE: // circle
491 tmpf.decSize().decSize();
492 pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
493 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
499 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
500 int(2 * hg), int(hg), 0, 360 * 32);
505 pain.arc(int(x2 - (hg / 2.0)), y,
506 int(hg), int(hg), 0, -360*32);
513 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
514 xp[1] = int(x2); yp[1] = int(y + hg);
515 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
516 pain.lines(xp, yp, 3);
519 case SPECIAL_CARON: // special caron
522 case 'L': wid = int(4.0 * wid / 5.0); break;
523 case 't': y -= int(hg35 / 2.0); break;
526 xp[0] = int(x + wid);
527 yp[0] = int(y + hg35 + hg);
529 xp[1] = int(x + wid + (hg35 / 2.0));
530 yp[1] = int(y + hg + (hg35 / 2.0));
532 xp[2] = int(x + wid + (hg35 / 2.0));
535 pain.lines(xp, yp, 3);
538 case HUNGARIAN_UMLAUT: // hung. umlaut
540 pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
541 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
544 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
548 case UMLAUT: // umlaut
550 pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
551 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
555 case CIRCUMFLEX: // circumflex
558 tmpf.decSize().decSize().decSize();
559 pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
560 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
564 case OGONEK: // ogonek
566 // this does probably not look like an ogonek, so
567 // it should certainly be refined
574 yp[1] = y + int(hg35);
576 xp[2] = int(x2 - hg35);
577 yp[2] = y + int(hg / 2.0);
579 xp[3] = int(x2 + hg / 4.0);
582 pain.lines(xp, yp, 4);
591 yp[0] = y + int(3.0 * hg);
593 xp[1] = int(x + float(wid) * 0.75);
596 pain.lines(xp, yp, 2);
599 case DOT_LESS_I: // dotless-i
600 case DOT_LESS_J: // dotless-j
602 // nothing to do for these
607 pain.fillRectangle(int(x + 1),
608 baseline - ascent(bv, font) + 1,
611 + descent(bv, font) - 2,
613 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
615 ascent(bv, font) + descent(bv, font) - 2);
616 pain.text(int(x + 2), baseline, contents, font);
618 x += width(bv, font);
622 void InsetLatexAccent::write(Buffer const *, ostream & os) const
624 os << "\\i " << contents << "\n";
628 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
631 contents = lex.getString();
636 int InsetLatexAccent::latex(Buffer const *, ostream & os,
637 bool /*fragile*/, bool/*fs*/) const
644 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
651 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
658 int InsetLatexAccent::docBook(Buffer const *, ostream & os) const
665 bool InsetLatexAccent::deletable() const
671 bool InsetLatexAccent::directWrite() const
677 Inset * InsetLatexAccent::clone(Buffer const &, bool) const
679 return new InsetLatexAccent(contents);
683 Inset::Code InsetLatexAccent::lyxCode() const
685 return Inset::ACCENT_CODE;
689 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)