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 & font,
358 int baseline, float & x, bool) const
360 Painter & pain = bv->painter();
362 if (lyxrc.font_norm == "iso8859-9")
363 if (DisplayISO8859_9(bv, font, 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 int asc = ascent(bv, font);
373 int desc = descent(bv, font);
374 int wid = width(bv, font);
375 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
380 hg = lyxfont::maxDescent(font);
383 if (font.shape() == LyXFont::ITALIC_SHAPE)
384 x2 += (4.0 * hg) / 5.0; // italic
391 float hg35 = float(hg * 3.0) / 5.0;
393 // display with proper accent mark
395 pain.text(int(x), baseline, ic, font);
398 int tmpvar = baseline - lyxfont::ascent('i', font);
400 if (font.shape() == LyXFont::ITALIC_SHAPE)
401 tmpx += (8.0 * hg) / 10.0; // italic
402 lyxerr[Debug::KEY] << "Removing dot." << endl;
403 // remove the dot first
404 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
405 lyxfont::ascent('i', font) -
406 lyxfont::ascent('x', font) - 1,
408 // the five lines below is a simple hack to
409 // make the display of accent 'i' and 'j'
410 // better. It makes the accent be written
411 // closer to the top of the dot-less 'i' or 'j'.
412 char tmpic = ic; // store the ic when we
413 ic = 'x'; // calculates the ascent of
414 asc = ascent(bv, font); // the dot-less version (here: 'x')
415 ic = tmpic; // set the orig ic back
416 y = baseline - asc; // update to new y coord.
418 // now the rest - draw within (x, y, x+wid, y+hg)
420 case ACUTE: // acute 0xB4
422 pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
423 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
427 case GRAVE: // grave 0x60
429 pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
430 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
434 case MACRON: // macron
436 pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
437 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
443 pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
444 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
448 case UNDERBAR: // underbar 0x5F
450 pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
454 case CEDILLA: // cedilla
456 pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
461 case UNDERDOT: // underdot
463 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
464 int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
471 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
472 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
477 case CIRCLE: // circle
480 tmpf.decSize().decSize();
481 pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
482 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
488 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
489 int(2 * hg), int(hg), 0, 360 * 32);
494 pain.arc(int(x2 - (hg / 2.0)), y,
495 int(hg), int(hg), 0, -360*32);
502 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
503 xp[1] = int(x2); yp[1] = int(y + hg);
504 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
505 pain.lines(xp, yp, 3);
508 case SPECIAL_CARON: // special caron
511 case 'L': wid = int(4.0 * wid / 5.0); break;
512 case 't': y -= int(hg35 / 2.0); break;
515 xp[0] = int(x + wid);
516 yp[0] = int(y + hg35 + hg);
518 xp[1] = int(x + wid + (hg35 / 2.0));
519 yp[1] = int(y + hg + (hg35 / 2.0));
521 xp[2] = int(x + wid + (hg35 / 2.0));
524 pain.lines(xp, yp, 3);
527 case HUNGARIAN_UMLAUT: // hung. umlaut
529 pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
530 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
533 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
537 case UMLAUT: // umlaut
539 pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
540 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
544 case CIRCUMFLEX: // circumflex
547 tmpf.decSize().decSize().decSize();
548 pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
549 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
553 case OGONEK: // ogonek
555 // this does probably not look like an ogonek, so
556 // it should certainly be refined
563 yp[1] = y + int(hg35);
565 xp[2] = int(x2 - hg35);
566 yp[2] = y + int(hg / 2.0);
568 xp[3] = int(x2 + hg / 4.0);
571 pain.lines(xp, yp, 4);
580 yp[0] = y + int(3.0 * hg);
582 xp[1] = int(x + float(wid) * 0.75);
585 pain.lines(xp, yp, 2);
588 case DOT_LESS_I: // dotless-i
589 case DOT_LESS_J: // dotless-j
591 // nothing to do for these
596 pain.fillRectangle(int(x + 1),
597 baseline - ascent(bv, font) + 1,
600 + descent(bv, font) - 2);
601 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
603 ascent(bv, font) + descent(bv, font) - 2);
604 pain.text(int(x + 2), baseline, contents, font);
606 x += width(bv, font);
610 void InsetLatexAccent::Write(Buffer const *, ostream & os) const
612 os << "\\i " << contents << "\n";
616 void InsetLatexAccent::Read(Buffer const *, LyXLex & lex)
619 contents = lex.GetString();
624 int InsetLatexAccent::Latex(Buffer const *, ostream & os,
625 bool /*fragile*/, bool/*fs*/) const
632 int InsetLatexAccent::Ascii(Buffer const *, ostream & os) const
639 int InsetLatexAccent::Linuxdoc(Buffer const *, ostream & os) const
646 int InsetLatexAccent::DocBook(Buffer const *, ostream & os) const
653 bool InsetLatexAccent::Deletable() const
659 bool InsetLatexAccent::DirectWrite() const
665 Inset * InsetLatexAccent::Clone() const
667 return new InsetLatexAccent(contents);
671 Inset::Code InsetLatexAccent::LyxCode() const
673 return Inset::ACCENT_CODE;
677 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)