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"
21 #include "BufferView.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()
37 InsetLatexAccent::InsetLatexAccent(string const & str)
44 void InsetLatexAccent::checkContents()
45 // check, if we know the modifier and can display it ok on screen
49 if (contents.empty() || contents.length() < 2) return;
52 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
53 contents = frontStrip(strip(contents));
54 if (!contains(contents, "{") && !contains(contents, "}")) {
55 if (contents.length() == 2) {
61 } else if (contents.length() == 3) {
69 } else if (contents.length() == 4 && contents[2] == ' ') {
77 } else if (contents.length() == 4 && contents[2] == '\\'
78 && (contents[3] == 'i' || contents[3] == 'j')) {
89 if (contents[0] != '\\') return; // demand that first char is a '\\'
91 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
93 remdot = false; plusasc = false; plusdesc = false;
95 switch (contents[1]) { // second char should be one of these
97 modtype = ACUTE; // acute
98 plusasc = true; // at the top of character
101 modtype = GRAVE; // grave
102 plusasc = true; // at the top
105 modtype = MACRON; // macron
106 plusasc = true; // at the top
109 modtype = TILDE; // tilde
110 plusasc = true; // at the top
112 case 'b': // underbar
113 modtype = UNDERBAR; // underbar
114 plusdesc = true; // at the bottom
117 modtype = CEDILLA; // cedilla
118 plusdesc = true; // at the bottom
120 case 'd': // underdot
121 modtype = UNDERDOT; // underdot
122 plusdesc = true; // at the bottom
125 modtype = CIRCLE; // circle
126 plusasc = true; // at the top
129 modtype = TIE; // tie
130 plusasc = true; // at the top
133 modtype = BREVE; // breve
134 plusasc = true; // at the top
137 modtype = CARON; // caron
138 plusasc = true; // at the top
140 case 'q': // special caron
141 modtype = SPECIAL_CARON; // special caron
142 plusasc = true; // at the top
144 case 'H': // hungarian umlaut
145 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
146 plusasc = true; // at the top
149 modtype = UMLAUT; // umlaut
150 plusasc = true; // at the top
153 modtype = DOT; // dot
154 plusasc = true; // at the top
156 case '^': // circumflex
157 modtype = CIRCUMFLEX; // circumflex
158 plusasc = true; // at the top
161 modtype = OGONEK; // ogonek
164 case 'i': // dot-less-i
165 modtype = DOT_LESS_I; // dot-less-i
166 plusasc = true; // at the top (not really needed)
169 case 'j': // dot-less-j
170 modtype = DOT_LESS_J; // dot-less-j
171 plusasc = true; // at the top (not really needed)
176 plusasc = true; // at the top (not really needed)
180 plusasc = true; // at the top (not really needed)
183 lyxerr[Debug::KEY] << "Default" << endl;
184 // unknow accent (or something else)
188 // we demand that third char is a '{' (Lgb)
189 if (contents[2] != '{') return;
191 // special clause for \i{}, \j{} \l{} and \L{}
192 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
193 || modtype == lSLASH || modtype == LSLASH)
194 && contents[3] == '}') {
196 case DOT_LESS_I: ic = 'i'; break;
197 case DOT_LESS_J: ic = 'j'; break;
198 case lSLASH: ic = 'l'; break;
199 case LSLASH: ic = 'L'; break;
201 // if this happens something is really wrong
202 lyxerr << "InsetLaTexAccent: weird error." << endl;
205 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
206 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
208 << ", top: " << plusasc
209 << ", bot: " << plusdesc
210 << ", dot: " << remdot
211 << ", mod: " << modtype << endl;
212 // Special case for space
213 } else if (contents[3] == '}') {
219 ic = contents[3]; // i will always be 3 here
221 // ic should now be a alfa-char or '\\'
223 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
224 if (ic == 'i' || ic == 'j')
228 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
229 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
230 string temp = contents;
231 temp.erase(3, string::npos);
234 for (string::size_type j = 4;
235 j < contents.length(); ++j)
242 // demand a '}' at the end
243 if (contents[++i] != '}' && contents[++i]) return;
245 // fine, the char is properly decoded now (hopefully)
246 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
248 << ", top: " << plusasc
249 << ", bot: " << plusdesc
250 << ", dot: " << remdot
251 << ", mod: " << modtype << endl;
257 int InsetLatexAccent::ascent(BufferView *, LyXFont const & font) const
259 // This function is a bit too simplistix and is just a
260 // "try to make a fit for all accents" approach, to
261 // make it better we need to know what kind of accent is
262 // used and add to max based on that.
266 max = lyxfont::ascent('a', font);
268 max = lyxfont::ascent(ic, font);
270 max += (lyxfont::maxAscent(font) + 3) / 3;
272 max = lyxfont::maxAscent(font) + 4;
277 int InsetLatexAccent::descent(BufferView *, LyXFont const & font) const
282 max = lyxfont::descent('a', font);
284 max = lyxfont::descent(ic, font);
288 max = lyxfont::maxDescent(font) + 4;
293 int InsetLatexAccent::width(BufferView *, LyXFont const & font) const
296 return lyxfont::width(ic, font);
298 return lyxfont::width(contents, font) + 4;
302 int InsetLatexAccent::Lbearing(LyXFont const & font) const
304 return lyxfont::lbearing(ic, font);
308 int InsetLatexAccent::Rbearing(LyXFont const & font) const
310 return lyxfont::rbearing(ic, font);
314 bool InsetLatexAccent::DisplayISO8859_9(BufferView * bv, LyXFont const & font,
318 unsigned char tmpic = ic;
323 if (ic == 'c') tmpic = 0xe7;
324 if (ic == 'C') tmpic = 0xc7;
325 if (ic == 's') tmpic = 0xfe;
326 if (ic == 'S') tmpic = 0xde;
330 { if (ic == 'g') tmpic = 0xf0;
331 if (ic == 'G') tmpic = 0xd0;
336 if (ic == 'o') tmpic = 0xf6;
337 if (ic == 'O') tmpic = 0xd6;
338 if (ic == 'u') tmpic = 0xfc;
339 if (ic == 'U') tmpic = 0xdc;
342 case DOT: if (ic == 'I') tmpic = 0xdd; break;
343 case DOT_LESS_I: tmpic = 0xfd; break;
344 default: return false;
347 char ch = char(tmpic);
348 bv->painter().text(int(x), baseline, ch, font);
349 x += width(bv, font);
357 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
358 int baseline, float & x, bool) const
360 Painter & pain = bv->painter();
362 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
363 if (DisplayISO8859_9(bv, font0, baseline, x))
367 // All the manually drawn accents in this function could use an
368 // overhaul. Different ways of drawing (what metrics to use)
369 // should also be considered.
372 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
373 font.setLanguage(english_language);
376 int asc = ascent(bv, font);
377 int desc = descent(bv, font);
378 int wid = width(bv, font);
379 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
384 hg = lyxfont::maxDescent(font);
387 if (font.shape() == LyXFont::ITALIC_SHAPE)
388 x2 += (4.0 * hg) / 5.0; // italic
395 float hg35 = float(hg * 3.0) / 5.0;
397 // display with proper accent mark
399 pain.text(int(x), baseline, ic, font);
402 int tmpvar = baseline - lyxfont::ascent('i', font);
404 if (font.shape() == LyXFont::ITALIC_SHAPE)
405 tmpx += (8.0 * hg) / 10.0; // italic
406 lyxerr[Debug::KEY] << "Removing dot." << endl;
407 // remove the dot first
408 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
409 lyxfont::ascent('i', font) -
410 lyxfont::ascent('x', font) - 1,
412 // the five lines below is a simple hack to
413 // make the display of accent 'i' and 'j'
414 // better. It makes the accent be written
415 // closer to the top of the dot-less 'i' or 'j'.
416 char tmpic = ic; // store the ic when we
417 ic = 'x'; // calculates the ascent of
418 asc = ascent(bv, font); // the dot-less version (here: 'x')
419 ic = tmpic; // set the orig ic back
420 y = baseline - asc; // update to new y coord.
422 // now the rest - draw within (x, y, x+wid, y+hg)
424 case ACUTE: // acute 0xB4
426 pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
427 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
431 case GRAVE: // grave 0x60
433 pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
434 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
438 case MACRON: // macron
440 pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
441 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
447 pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
448 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
452 case UNDERBAR: // underbar 0x5F
454 pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
458 case CEDILLA: // cedilla
460 pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
465 case UNDERDOT: // underdot
467 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
468 int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
475 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
476 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
481 case CIRCLE: // circle
484 tmpf.decSize().decSize();
485 pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
486 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
492 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
493 int(2 * hg), int(hg), 0, 360 * 32);
498 pain.arc(int(x2 - (hg / 2.0)), y,
499 int(hg), int(hg), 0, -360*32);
506 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
507 xp[1] = int(x2); yp[1] = int(y + hg);
508 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
509 pain.lines(xp, yp, 3);
512 case SPECIAL_CARON: // special caron
515 case 'L': wid = int(4.0 * wid / 5.0); break;
516 case 't': y -= int(hg35 / 2.0); break;
519 xp[0] = int(x + wid);
520 yp[0] = int(y + hg35 + hg);
522 xp[1] = int(x + wid + (hg35 / 2.0));
523 yp[1] = int(y + hg + (hg35 / 2.0));
525 xp[2] = int(x + wid + (hg35 / 2.0));
528 pain.lines(xp, yp, 3);
531 case HUNGARIAN_UMLAUT: // hung. umlaut
533 pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
534 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
537 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
541 case UMLAUT: // umlaut
543 pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
544 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
548 case CIRCUMFLEX: // circumflex
551 tmpf.decSize().decSize().decSize();
552 pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
553 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
557 case OGONEK: // ogonek
559 // this does probably not look like an ogonek, so
560 // it should certainly be refined
567 yp[1] = y + int(hg35);
569 xp[2] = int(x2 - hg35);
570 yp[2] = y + int(hg / 2.0);
572 xp[3] = int(x2 + hg / 4.0);
575 pain.lines(xp, yp, 4);
584 yp[0] = y + int(3.0 * hg);
586 xp[1] = int(x + float(wid) * 0.75);
589 pain.lines(xp, yp, 2);
592 case DOT_LESS_I: // dotless-i
593 case DOT_LESS_J: // dotless-j
595 // nothing to do for these
600 pain.fillRectangle(int(x + 1),
601 baseline - ascent(bv, font) + 1,
604 + descent(bv, font) - 2);
605 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
607 ascent(bv, font) + descent(bv, font) - 2);
608 pain.text(int(x + 2), baseline, contents, font);
610 x += width(bv, font);
614 void InsetLatexAccent::Write(Buffer const *, ostream & os) const
616 os << "\\i " << contents << "\n";
620 void InsetLatexAccent::Read(Buffer const *, LyXLex & lex)
623 contents = lex.GetString();
628 int InsetLatexAccent::Latex(Buffer const *, ostream & os,
629 bool /*fragile*/, bool/*fs*/) const
636 int InsetLatexAccent::Ascii(Buffer const *, ostream & os, int) const
643 int InsetLatexAccent::Linuxdoc(Buffer const *, ostream & os) const
650 int InsetLatexAccent::DocBook(Buffer const *, ostream & os) const
657 bool InsetLatexAccent::Deletable() const
663 bool InsetLatexAccent::DirectWrite() const
669 Inset * InsetLatexAccent::Clone(Buffer const &) const
671 return new InsetLatexAccent(contents);
675 Inset::Code InsetLatexAccent::LyxCode() const
677 return Inset::ACCENT_CODE;
681 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)