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"
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) return;
53 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
54 contents = frontStrip(strip(contents));
55 if (!contains(contents, "{") && !contains(contents, "}")) {
56 if (contents.length() == 2) {
62 } else if (contents.length() == 3) {
70 } else if (contents.length() == 4 && contents[2] == ' ') {
78 } else if (contents.length() == 4 && contents[2] == '\\'
79 && (contents[3] == 'i' || contents[3] == 'j')) {
90 if (contents[0] != '\\') return; // demand that first char is a '\\'
92 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
94 remdot = false; plusasc = false; plusdesc = false;
96 switch (contents[1]) { // second char should be one of these
98 modtype = ACUTE; // acute
99 plusasc = true; // at the top of character
102 modtype = GRAVE; // grave
103 plusasc = true; // at the top
106 modtype = MACRON; // macron
107 plusasc = true; // at the top
110 modtype = TILDE; // tilde
111 plusasc = true; // at the top
113 case 'b': // underbar
114 modtype = UNDERBAR; // underbar
115 plusdesc = true; // at the bottom
118 modtype = CEDILLA; // cedilla
119 plusdesc = true; // at the bottom
121 case 'd': // underdot
122 modtype = UNDERDOT; // underdot
123 plusdesc = true; // at the bottom
126 modtype = CIRCLE; // circle
127 plusasc = true; // at the top
130 modtype = TIE; // tie
131 plusasc = true; // at the top
134 modtype = BREVE; // breve
135 plusasc = true; // at the top
138 modtype = CARON; // caron
139 plusasc = true; // at the top
141 case 'q': // special caron
142 modtype = SPECIAL_CARON; // special caron
143 plusasc = true; // at the top
145 case 'H': // hungarian umlaut
146 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
147 plusasc = true; // at the top
150 modtype = UMLAUT; // umlaut
151 plusasc = true; // at the top
154 modtype = DOT; // dot
155 plusasc = true; // at the top
157 case '^': // circumflex
158 modtype = CIRCUMFLEX; // circumflex
159 plusasc = true; // at the top
162 modtype = OGONEK; // ogonek
165 case 'i': // dot-less-i
166 modtype = DOT_LESS_I; // dot-less-i
167 plusasc = true; // at the top (not really needed)
170 case 'j': // dot-less-j
171 modtype = DOT_LESS_J; // dot-less-j
172 plusasc = true; // at the top (not really needed)
177 plusasc = true; // at the top (not really needed)
181 plusasc = true; // at the top (not really needed)
184 lyxerr[Debug::KEY] << "Default" << endl;
185 // unknow accent (or something else)
189 // we demand that third char is a '{' (Lgb)
190 if (contents[2] != '{') return;
192 // special clause for \i{}, \j{} \l{} and \L{}
193 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
194 || modtype == lSLASH || modtype == LSLASH)
195 && contents[3] == '}') {
197 case DOT_LESS_I: ic = 'i'; break;
198 case DOT_LESS_J: ic = 'j'; break;
199 case lSLASH: ic = 'l'; break;
200 case LSLASH: ic = 'L'; break;
202 // if this happens something is really wrong
203 lyxerr << "InsetLaTexAccent: weird error." << endl;
206 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
207 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
209 << ", top: " << plusasc
210 << ", bot: " << plusdesc
211 << ", dot: " << remdot
212 << ", mod: " << modtype << endl;
213 // Special case for space
214 } else if (contents[3] == '}') {
220 ic = contents[3]; // i will always be 3 here
222 // ic should now be a alfa-char or '\\'
224 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
225 if (ic == 'i' || ic == 'j')
229 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
230 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
231 string temp = contents;
232 temp.erase(3, string::npos);
235 for (string::size_type j = 4;
236 j < contents.length(); ++j)
243 // demand a '}' at the end
244 if (contents[++i] != '}' && contents[++i]) return;
246 // fine, the char is properly decoded now (hopefully)
247 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
249 << ", top: " << plusasc
250 << ", bot: " << plusdesc
251 << ", dot: " << remdot
252 << ", mod: " << modtype << endl;
258 int InsetLatexAccent::ascent(BufferView *, LyXFont const & font) const
260 // This function is a bit too simplistix and is just a
261 // "try to make a fit for all accents" approach, to
262 // make it better we need to know what kind of accent is
263 // used and add to max based on that.
267 max = lyxfont::ascent('a', font);
269 max = lyxfont::ascent(ic, font);
271 max += (lyxfont::maxAscent(font) + 3) / 3;
273 max = lyxfont::maxAscent(font) + 4;
278 int InsetLatexAccent::descent(BufferView *, LyXFont const & font) const
283 max = lyxfont::descent('a', font);
285 max = lyxfont::descent(ic, font);
289 max = lyxfont::maxDescent(font) + 4;
294 int InsetLatexAccent::width(BufferView *, LyXFont const & font) const
297 return lyxfont::width(ic, font);
299 return lyxfont::width(contents, font) + 4;
303 int InsetLatexAccent::Lbearing(LyXFont const & font) const
305 return lyxfont::lbearing(ic, font);
309 int InsetLatexAccent::Rbearing(LyXFont const & font) const
311 return lyxfont::rbearing(ic, font);
315 bool InsetLatexAccent::DisplayISO8859_9(BufferView * bv, LyXFont const & font,
319 unsigned char tmpic = ic;
324 if (ic == 'c') tmpic = 0xe7;
325 if (ic == 'C') tmpic = 0xc7;
326 if (ic == 's') tmpic = 0xfe;
327 if (ic == 'S') tmpic = 0xde;
331 { if (ic == 'g') tmpic = 0xf0;
332 if (ic == 'G') tmpic = 0xd0;
337 if (ic == 'o') tmpic = 0xf6;
338 if (ic == 'O') tmpic = 0xd6;
339 if (ic == 'u') tmpic = 0xfc;
340 if (ic == 'U') tmpic = 0xdc;
343 case DOT: if (ic == 'I') tmpic = 0xdd; break;
344 case DOT_LESS_I: tmpic = 0xfd; break;
345 default: return false;
348 char ch = char(tmpic);
349 bv->painter().text(int(x), baseline, ch, font);
350 x += width(bv, font);
358 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
359 int baseline, float & x, bool) const
361 Painter & pain = bv->painter();
363 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
364 if (DisplayISO8859_9(bv, font0, baseline, x))
368 // All the manually drawn accents in this function could use an
369 // overhaul. Different ways of drawing (what metrics to use)
370 // should also be considered.
373 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
374 font.setLanguage(english_language);
377 int asc = ascent(bv, font);
378 int desc = descent(bv, font);
379 int wid = width(bv, font);
380 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
385 hg = lyxfont::maxDescent(font);
388 if (font.shape() == LyXFont::ITALIC_SHAPE)
389 x2 += (4.0 * hg) / 5.0; // italic
396 float hg35 = float(hg * 3.0) / 5.0;
398 // display with proper accent mark
400 pain.text(int(x), baseline, ic, font);
403 int tmpvar = baseline - lyxfont::ascent('i', font);
405 if (font.shape() == LyXFont::ITALIC_SHAPE)
406 tmpx += (8.0 * hg) / 10.0; // italic
407 lyxerr[Debug::KEY] << "Removing dot." << endl;
408 // remove the dot first
409 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
410 lyxfont::ascent('i', font) -
411 lyxfont::ascent('x', font) - 1,
413 // the five lines below is a simple hack to
414 // make the display of accent 'i' and 'j'
415 // better. It makes the accent be written
416 // closer to the top of the dot-less 'i' or 'j'.
417 char tmpic = ic; // store the ic when we
418 ic = 'x'; // calculates the ascent of
419 asc = ascent(bv, font); // the dot-less version (here: 'x')
420 ic = tmpic; // set the orig ic back
421 y = baseline - asc; // update to new y coord.
423 // now the rest - draw within (x, y, x+wid, y+hg)
425 case ACUTE: // acute 0xB4
427 pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
428 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
432 case GRAVE: // grave 0x60
434 pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
435 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
439 case MACRON: // macron
441 pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
442 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
448 pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
449 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
453 case UNDERBAR: // underbar 0x5F
455 pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
459 case CEDILLA: // cedilla
461 pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
466 case UNDERDOT: // underdot
468 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
469 int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
476 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
477 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
482 case CIRCLE: // circle
485 tmpf.decSize().decSize();
486 pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
487 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
493 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
494 int(2 * hg), int(hg), 0, 360 * 32);
499 pain.arc(int(x2 - (hg / 2.0)), y,
500 int(hg), int(hg), 0, -360*32);
507 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
508 xp[1] = int(x2); yp[1] = int(y + hg);
509 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
510 pain.lines(xp, yp, 3);
513 case SPECIAL_CARON: // special caron
516 case 'L': wid = int(4.0 * wid / 5.0); break;
517 case 't': y -= int(hg35 / 2.0); break;
520 xp[0] = int(x + wid);
521 yp[0] = int(y + hg35 + hg);
523 xp[1] = int(x + wid + (hg35 / 2.0));
524 yp[1] = int(y + hg + (hg35 / 2.0));
526 xp[2] = int(x + wid + (hg35 / 2.0));
529 pain.lines(xp, yp, 3);
532 case HUNGARIAN_UMLAUT: // hung. umlaut
534 pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
535 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
538 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
542 case UMLAUT: // umlaut
544 pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
545 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
549 case CIRCUMFLEX: // circumflex
552 tmpf.decSize().decSize().decSize();
553 pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
554 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
558 case OGONEK: // ogonek
560 // this does probably not look like an ogonek, so
561 // it should certainly be refined
568 yp[1] = y + int(hg35);
570 xp[2] = int(x2 - hg35);
571 yp[2] = y + int(hg / 2.0);
573 xp[3] = int(x2 + hg / 4.0);
576 pain.lines(xp, yp, 4);
585 yp[0] = y + int(3.0 * hg);
587 xp[1] = int(x + float(wid) * 0.75);
590 pain.lines(xp, yp, 2);
593 case DOT_LESS_I: // dotless-i
594 case DOT_LESS_J: // dotless-j
596 // nothing to do for these
601 pain.fillRectangle(int(x + 1),
602 baseline - ascent(bv, font) + 1,
605 + descent(bv, font) - 2);
606 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
608 ascent(bv, font) + descent(bv, font) - 2);
609 pain.text(int(x + 2), baseline, contents, font);
611 x += width(bv, font);
615 void InsetLatexAccent::Write(Buffer const *, ostream & os) const
617 os << "\\i " << contents << "\n";
621 void InsetLatexAccent::Read(Buffer const *, LyXLex & lex)
624 contents = lex.GetString();
629 int InsetLatexAccent::Latex(Buffer const *, ostream & os,
630 bool /*fragile*/, bool/*fs*/) const
637 int InsetLatexAccent::Ascii(Buffer const *, ostream & os, int) const
644 int InsetLatexAccent::Linuxdoc(Buffer const *, ostream & os) const
651 int InsetLatexAccent::DocBook(Buffer const *, ostream & os) const
658 bool InsetLatexAccent::Deletable() const
664 bool InsetLatexAccent::DirectWrite() const
670 Inset * InsetLatexAccent::Clone(Buffer const &) const
672 return new InsetLatexAccent(contents);
676 Inset::Code InsetLatexAccent::LyxCode() const
678 return Inset::ACCENT_CODE;
682 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)