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()
38 InsetLatexAccent::InsetLatexAccent(string const & string)
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(Painter &, 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(Painter &, 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(Painter &, 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(Painter & pain, 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 pain.text(int(x), baseline, ch, font);
350 x += width(pain, font);
358 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font,
359 int baseline, float & x, bool) const
361 Painter & pain = bv->painter();
363 if (lyxrc.font_norm == "iso8859-9")
364 if (DisplayISO8859_9(pain, font, 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 int asc = ascent(pain, font);
374 int desc = descent(pain, font);
375 int wid = width(pain, font);
376 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
381 hg = lyxfont::maxDescent(font);
384 if (font.shape() == LyXFont::ITALIC_SHAPE)
385 x2 += (4.0 * hg) / 5.0; // italic
392 float hg35 = float(hg * 3.0) / 5.0;
394 // display with proper accent mark
396 pain.text(int(x), baseline, ic, font);
399 int tmpvar = baseline - lyxfont::ascent('i', font);
401 if (font.shape() == LyXFont::ITALIC_SHAPE)
402 tmpx += (8.0 * hg) / 10.0; // italic
403 lyxerr[Debug::KEY] << "Removing dot." << endl;
404 // remove the dot first
405 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
406 lyxfont::ascent('i', font) -
407 lyxfont::ascent('x', font) - 1,
409 // the five lines below is a simple hack to
410 // make the display of accent 'i' and 'j'
411 // better. It makes the accent be written
412 // closer to the top of the dot-less 'i' or 'j'.
413 char tmpic = ic; // store the ic when we
414 ic = 'x'; // calculates the ascent of
415 asc = ascent(pain, font); // the dot-less version (here: 'x')
416 ic = tmpic; // set the orig ic back
417 y = baseline - asc; // update to new y coord.
419 // now the rest - draw within (x, y, x+wid, y+hg)
421 case ACUTE: // acute 0xB4
423 pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
424 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
428 case GRAVE: // grave 0x60
430 pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
431 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
435 case MACRON: // macron
437 pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
438 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
444 pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
445 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
449 case UNDERBAR: // underbar 0x5F
451 pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
455 case CEDILLA: // cedilla
457 pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
462 case UNDERDOT: // underdot
464 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
465 int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
472 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
473 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
478 case CIRCLE: // circle
481 tmpf.decSize().decSize();
482 pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
483 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
489 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
490 int(2 * hg), int(hg), 0, 360 * 32);
495 pain.arc(int(x2 - (hg / 2.0)), y,
496 int(hg), int(hg), 0, -360*32);
503 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
504 xp[1] = int(x2); yp[1] = int(y + hg);
505 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
506 pain.lines(xp, yp, 3);
509 case SPECIAL_CARON: // special caron
512 case 'L': wid = int(4.0 * wid / 5.0); break;
513 case 't': y -= int(hg35 / 2.0); break;
516 xp[0] = int(x + wid);
517 yp[0] = int(y + hg35 + hg);
519 xp[1] = int(x + wid + (hg35 / 2.0));
520 yp[1] = int(y + hg + (hg35 / 2.0));
522 xp[2] = int(x + wid + (hg35 / 2.0));
525 pain.lines(xp, yp, 3);
528 case HUNGARIAN_UMLAUT: // hung. umlaut
530 pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
531 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
534 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
538 case UMLAUT: // umlaut
540 pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
541 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
545 case CIRCUMFLEX: // circumflex
548 tmpf.decSize().decSize().decSize();
549 pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
550 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
554 case OGONEK: // ogonek
556 // this does probably not look like an ogonek, so
557 // it should certainly be refined
564 yp[1] = y + int(hg35);
566 xp[2] = int(x2 - hg35);
567 yp[2] = y + int(hg / 2.0);
569 xp[3] = int(x2 + hg / 4.0);
572 pain.lines(xp, yp, 4);
581 yp[0] = y + int(3.0 * hg);
583 xp[1] = int(x + float(wid) * 0.75);
586 pain.lines(xp, yp, 2);
589 case DOT_LESS_I: // dotless-i
590 case DOT_LESS_J: // dotless-j
592 // nothing to do for these
597 pain.fillRectangle(int(x + 1),
598 baseline - ascent(pain, font) + 1,
599 width(pain, font) - 2,
601 + descent(pain, font) - 2);
602 pain.rectangle(int(x + 1), baseline - ascent(pain, font) + 1,
603 width(pain, font) - 2,
604 ascent(pain, font) + descent(pain, font) - 2);
605 pain.text(int(x + 2), baseline, contents, font);
607 x += width(pain, font);
611 void InsetLatexAccent::Write(Buffer const *, ostream & os) const
613 os << "\\i " << contents << "\n";
617 void InsetLatexAccent::Read(Buffer const *, LyXLex & lex)
620 contents = lex.GetString();
625 int InsetLatexAccent::Latex(Buffer const *, ostream & os,
626 bool /*fragile*/, bool/*fs*/) const
633 int InsetLatexAccent::Ascii(Buffer const *, ostream & os) const
640 int InsetLatexAccent::Linuxdoc(Buffer const *, ostream & os) const
647 int InsetLatexAccent::DocBook(Buffer const *, ostream & os) const
654 bool InsetLatexAccent::Deletable() const
660 bool InsetLatexAccent::DirectWrite() const
666 Inset * InsetLatexAccent::Clone() const
668 return new InsetLatexAccent(contents);
672 Inset::Code InsetLatexAccent::LyxCode() const
674 return Inset::ACCENT_CODE;
678 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)