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"
22 #include "frontends/Painter.h"
23 #include "frontends/font_metrics.h"
30 /* LatexAccent. Proper handling of accented characters */
31 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
32 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
34 InsetLatexAccent::InsetLatexAccent()
39 InsetLatexAccent::InsetLatexAccent(string const & str)
46 void InsetLatexAccent::checkContents()
47 // check, if we know the modifier and can display it ok on screen
51 if (contents.empty() || contents.length() < 2) {
52 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
57 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
58 contents = trim(contents);
59 if (!contains(contents, "{") && !contains(contents, "}")) {
60 if (contents.length() == 2) {
66 } else if (contents.length() == 3) {
74 } else if (contents.length() == 4 && contents[2] == ' ') {
82 } else if (contents.length() == 4 && contents[2] == '\\'
83 && (contents[3] == 'i' || contents[3] == 'j')) {
94 if (contents[0] != '\\') { // demand that first char is a '\\'
95 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
99 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
101 remdot = false; plusasc = false; plusdesc = false;
103 switch (contents[1]) { // second char should be one of these
105 modtype = ACUTE; // acute
106 plusasc = true; // at the top of character
109 modtype = GRAVE; // grave
110 plusasc = true; // at the top
113 modtype = MACRON; // macron
114 plusasc = true; // at the top
117 modtype = TILDE; // tilde
118 plusasc = true; // at the top
120 case 'b': // underbar
121 modtype = UNDERBAR; // underbar
122 plusdesc = true; // at the bottom
125 modtype = CEDILLA; // cedilla
126 plusdesc = true; // at the bottom
128 case 'd': // underdot
129 modtype = UNDERDOT; // underdot
130 plusdesc = true; // at the bottom
133 modtype = CIRCLE; // circle
134 plusasc = true; // at the top
137 modtype = TIE; // tie
138 plusasc = true; // at the top
141 modtype = BREVE; // breve
142 plusasc = true; // at the top
145 modtype = CARON; // caron
146 plusasc = true; // at the top
148 case 'q': // special caron
149 modtype = SPECIAL_CARON; // special caron
150 plusasc = true; // at the top
152 case 'H': // hungarian umlaut
153 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
154 plusasc = true; // at the top
157 modtype = UMLAUT; // umlaut
158 plusasc = true; // at the top
161 modtype = DOT; // dot
162 plusasc = true; // at the top
164 case '^': // circumflex
165 modtype = CIRCUMFLEX; // circumflex
166 plusasc = true; // at the top
169 modtype = OGONEK; // ogonek
172 case 'i': // dot-less-i
173 modtype = DOT_LESS_I; // dot-less-i
174 plusasc = true; // at the top (not really needed)
177 case 'j': // dot-less-j
178 modtype = DOT_LESS_J; // dot-less-j
179 plusasc = true; // at the top (not really needed)
184 plusasc = true; // at the top (not really needed)
188 plusasc = true; // at the top (not really needed)
191 lyxerr[Debug::KEY] << "Default" << endl;
192 // unknow accent (or something else)
196 // we demand that third char is a '{' (Lgb)
197 if (contents[2] != '{') return;
199 // special clause for \i{}, \j{} \l{} and \L{}
200 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
201 || modtype == lSLASH || modtype == LSLASH)
202 && contents[3] == '}') {
204 case DOT_LESS_I: ic = 'i'; break;
205 case DOT_LESS_J: ic = 'j'; break;
206 case lSLASH: ic = 'l'; break;
207 case LSLASH: ic = 'L'; break;
209 // if this happens something is really wrong
210 lyxerr << "InsetLaTexAccent: weird error." << endl;
213 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
214 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
216 << ", top: " << plusasc
217 << ", bot: " << plusdesc
218 << ", dot: " << remdot
219 << ", mod: " << modtype << endl;
220 // Special case for space
221 } else if (contents[3] == '}') {
227 ic = contents[3]; // i will always be 3 here
229 // ic should now be a alfa-char or '\\'
231 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
232 if (ic == 'i' || ic == 'j')
236 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
237 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
238 string temp = contents;
239 temp.erase(3, string::npos);
242 for (string::size_type j = 4;
243 j < contents.length(); ++j)
250 // demand a '}' at the end
251 if (contents[++i] != '}' && contents[++i]) return;
253 // fine, the char is properly decoded now (hopefully)
254 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
256 << ", top: " << plusasc
257 << ", bot: " << plusdesc
258 << ", dot: " << remdot
259 << ", mod: " << modtype << endl;
265 int InsetLatexAccent::ascent(BufferView *, LyXFont const & font) const
267 // This function is a bit too simplistix and is just a
268 // "try to make a fit for all accents" approach, to
269 // make it better we need to know what kind of accent is
270 // used and add to max based on that.
274 max = font_metrics::ascent('a', font);
276 max = font_metrics::ascent(ic, font);
278 max += (font_metrics::maxAscent(font) + 3) / 3;
280 max = font_metrics::maxAscent(font) + 4;
285 int InsetLatexAccent::descent(BufferView *, LyXFont const & font) const
290 max = font_metrics::descent('a', font);
292 max = font_metrics::descent(ic, font);
296 max = font_metrics::maxDescent(font) + 4;
301 int InsetLatexAccent::width(BufferView *, LyXFont const & font) const
304 return font_metrics::width(ic, font);
306 return font_metrics::width(contents, font) + 4;
310 int InsetLatexAccent::lbearing(LyXFont const & font) const
312 return font_metrics::lbearing(ic, font);
316 int InsetLatexAccent::rbearing(LyXFont const & font) const
318 return font_metrics::rbearing(ic, font);
322 bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
326 unsigned char tmpic = ic;
331 if (ic == 'c') tmpic = 0xe7;
332 if (ic == 'C') tmpic = 0xc7;
333 if (ic == 's') tmpic = 0xfe;
334 if (ic == 'S') tmpic = 0xde;
338 { if (ic == 'g') tmpic = 0xf0;
339 if (ic == 'G') tmpic = 0xd0;
344 if (ic == 'o') tmpic = 0xf6;
345 if (ic == 'O') tmpic = 0xd6;
346 if (ic == 'u') tmpic = 0xfc;
347 if (ic == 'U') tmpic = 0xdc;
350 case DOT: if (ic == 'I') tmpic = 0xdd; break;
351 case DOT_LESS_I: tmpic = 0xfd; break;
352 default: return false;
355 char ch = char(tmpic);
356 bv->painter().text(int(x), baseline, ch, font);
357 x += width(bv, font);
365 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
366 int baseline, float & x, bool) const
368 Painter & pain = bv->painter();
370 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
371 if (displayISO8859_9(bv, font0, baseline, x))
375 // All the manually drawn accents in this function could use an
376 // overhaul. Different ways of drawing (what metrics to use)
377 // should also be considered.
380 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
381 font.setLanguage(english_language);
384 int asc = ascent(bv, font);
385 int desc = descent(bv, font);
386 int wid = width(bv, font);
387 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
392 hg = font_metrics::maxDescent(font);
395 if (font.shape() == LyXFont::ITALIC_SHAPE)
396 x2 += (4.0 * hg) / 5.0; // italic
403 float hg35 = float(hg * 3.0) / 5.0;
405 // display with proper accent mark
407 pain.text(int(x), baseline, ic, font);
410 int tmpvar = baseline - font_metrics::ascent('i', font);
412 if (font.shape() == LyXFont::ITALIC_SHAPE)
413 tmpx += (8.0 * hg) / 10.0; // italic
414 lyxerr[Debug::KEY] << "Removing dot." << endl;
415 // remove the dot first
416 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
417 font_metrics::ascent('i', font) -
418 font_metrics::ascent('x', font) - 1,
420 // the five lines below is a simple hack to
421 // make the display of accent 'i' and 'j'
422 // better. It makes the accent be written
423 // closer to the top of the dot-less 'i' or 'j'.
424 char tmpic = ic; // store the ic when we
425 ic = 'x'; // calculates the ascent of
426 asc = ascent(bv, font); // the dot-less version (here: 'x')
427 ic = tmpic; // set the orig ic back
428 y = baseline - asc; // update to new y coord.
430 // now the rest - draw within (x, y, x+wid, y+hg)
432 case ACUTE: // acute 0xB4
434 pain.text(int(x2 - (font_metrics::rbearing(0xB4, font) - font_metrics::lbearing(0xB4, font)) / 2),
435 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB4, font) - (font_metrics::ascent(0xB4, font) + font_metrics::descent(0xB4, font)) / 2,
439 case GRAVE: // grave 0x60
441 pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
442 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
446 case MACRON: // macron
448 pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
449 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
455 pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
456 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
460 case UNDERBAR: // underbar 0x5F
462 pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
466 case CEDILLA: // cedilla
468 pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
473 case UNDERDOT: // underdot
475 pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
476 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
483 pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
484 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
489 case CIRCLE: // circle
492 tmpf.decSize().decSize();
493 pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
494 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
500 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
501 int(2 * hg), int(hg), 0, 360 * 32);
506 pain.arc(int(x2 - (hg / 2.0)), y,
507 int(hg), int(hg), 0, -360*32);
514 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
515 xp[1] = int(x2); yp[1] = int(y + hg);
516 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
517 pain.lines(xp, yp, 3);
520 case SPECIAL_CARON: // special caron
523 case 'L': wid = int(4.0 * wid / 5.0); break;
524 case 't': y -= int(hg35 / 2.0); break;
527 xp[0] = int(x + wid);
528 yp[0] = int(y + hg35 + hg);
530 xp[1] = int(x + wid + (hg35 / 2.0));
531 yp[1] = int(y + hg + (hg35 / 2.0));
533 xp[2] = int(x + wid + (hg35 / 2.0));
536 pain.lines(xp, yp, 3);
539 case HUNGARIAN_UMLAUT: // hung. umlaut
541 pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
542 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
545 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
549 case UMLAUT: // umlaut
551 pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
552 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
556 case CIRCUMFLEX: // circumflex
559 tmpf.decSize().decSize().decSize();
560 pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
561 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
565 case OGONEK: // ogonek
567 // this does probably not look like an ogonek, so
568 // it should certainly be refined
575 yp[1] = y + int(hg35);
577 xp[2] = int(x2 - hg35);
578 yp[2] = y + int(hg / 2.0);
580 xp[3] = int(x2 + hg / 4.0);
583 pain.lines(xp, yp, 4);
592 yp[0] = y + int(3.0 * hg);
594 xp[1] = int(x + float(wid) * 0.75);
597 pain.lines(xp, yp, 2);
600 case DOT_LESS_I: // dotless-i
601 case DOT_LESS_J: // dotless-j
603 // nothing to do for these
608 pain.fillRectangle(int(x + 1),
609 baseline - ascent(bv, font) + 1,
612 + descent(bv, font) - 2,
614 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
616 ascent(bv, font) + descent(bv, font) - 2);
617 pain.text(int(x + 2), baseline, contents, font);
619 x += width(bv, font);
623 void InsetLatexAccent::write(Buffer const *, ostream & os) const
625 os << "\\i " << contents << "\n";
629 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
632 contents = lex.getString();
637 int InsetLatexAccent::latex(Buffer const *, ostream & os,
638 bool /*fragile*/, bool/*fs*/) const
645 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
652 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
659 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
666 bool InsetLatexAccent::deletable() const
672 bool InsetLatexAccent::directWrite() const
678 Inset * InsetLatexAccent::clone(Buffer const &, bool) const
680 return new InsetLatexAccent(contents);
684 Inset::Code InsetLatexAccent::lyxCode() const
686 return Inset::ACCENT_CODE;
690 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)