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"
27 /* LatexAccent. Proper handling of accented characters */
28 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
29 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
31 InsetLatexAccent::InsetLatexAccent()
37 InsetLatexAccent::InsetLatexAccent(string const & string)
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(Painter &, 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(Painter &, 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(Painter &, 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(Painter & pain, 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 pain.text(int(x), baseline, ch, font);
349 x += width(pain, font);
357 void InsetLatexAccent::draw(Painter & pain, LyXFont const & font,
358 int baseline, float & x) const
360 if (lyxrc.font_norm == "iso8859-9")
361 if (DisplayISO8859_9(pain, font, baseline, x))
365 // All the manually drawn accents in this function could use an
366 // overhaul. Different ways of drawing (what metrics to use)
367 // should also be considered.
370 int asc = ascent(pain, font);
371 int desc = descent(pain, font);
372 int wid = width(pain, font);
373 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
378 hg = lyxfont::maxDescent(font);
381 if (font.shape() == LyXFont::ITALIC_SHAPE)
382 x2 += (4.0 * hg) / 5.0; // italic
389 float hg35 = float(hg * 3.0) / 5.0;
391 // display with proper accent mark
393 pain.text(int(x), baseline, ic, font);
396 int tmpvar = baseline - lyxfont::ascent('i', font);
398 if (font.shape() == LyXFont::ITALIC_SHAPE)
399 tmpx += (8.0 * hg) / 10.0; // italic
400 lyxerr[Debug::KEY] << "Removing dot." << endl;
401 // remove the dot first
402 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
403 lyxfont::ascent('i', font) -
404 lyxfont::ascent('x', font) - 1,
406 // the five lines below is a simple hack to
407 // make the display of accent 'i' and 'j'
408 // better. It makes the accent be written
409 // closer to the top of the dot-less 'i' or 'j'.
410 char tmpic = ic; // store the ic when we
411 ic = 'x'; // calculates the ascent of
412 asc = ascent(pain, font); // the dot-less version (here: 'x')
413 ic = tmpic; // set the orig ic back
414 y = baseline - asc; // update to new y coord.
416 // now the rest - draw within (x, y, x+wid, y+hg)
418 case ACUTE: // acute 0xB4
420 pain.text(int(x2 - (lyxfont::rbearing(0xB4, font) - lyxfont::lbearing(0xB4, font)) / 2),
421 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB4, font) - (lyxfont::ascent(0xB4, font) + lyxfont::descent(0xB4, font)) / 2,
425 case GRAVE: // grave 0x60
427 pain.text(int(x2 - (lyxfont::rbearing(0x60, font) - lyxfont::lbearing(0x60, font)) / 2),
428 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x60, font) - (lyxfont::ascent(0x60, font) + lyxfont::descent(0x60, font)) / 2.0),
432 case MACRON: // macron
434 pain.text(int(x2 - (lyxfont::rbearing(0xAF, font) - lyxfont::lbearing(0xAF, font)) / 2),
435 baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xAF, font) - (lyxfont::ascent(0xAF, font) + lyxfont::descent(0xAF, font)),
441 pain.text(int(x2 - (lyxfont::rbearing('~', font) - lyxfont::lbearing('~', font)) / 2),
442 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('~', font) - (lyxfont::ascent('~', font) + lyxfont::descent('~', font)) / 2,
446 case UNDERBAR: // underbar 0x5F
448 pain.text(int(x2 - (lyxfont::rbearing(0x5F, font) - lyxfont::lbearing(0x5F, font)) / 2), baseline,
452 case CEDILLA: // cedilla
454 pain.text(int(x2 - (lyxfont::rbearing(0xB8, font) - lyxfont::lbearing(0xB8, font)) / 2), baseline,
459 case UNDERDOT: // underdot
461 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
462 int(baseline + 3.0 / 2.0 * (lyxfont::ascent('.', font) + lyxfont::descent('.', font))),
469 pain.text(int(x2 - (lyxfont::rbearing('.', font) - lyxfont::lbearing('.', font)) / 2.0),
470 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('.', font) - (lyxfont::ascent('.', font) + lyxfont::descent('.', font)) / 2,
475 case CIRCLE: // circle
478 tmpf.decSize().decSize();
479 pain.text(int(x2 - (lyxfont::rbearing(0xB0, tmpf) - lyxfont::lbearing(0xB0, tmpf)) / 2.0),
480 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0xB0, tmpf) - (lyxfont::ascent(0xB0, tmpf) + lyxfont::descent(0xB0, tmpf)) / 3.0),
486 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
487 int(2 * hg), int(hg), 0, 360 * 32);
492 pain.arc(int(x2 - (hg / 2.0)), y,
493 int(hg), int(hg), 0, -360*32);
500 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
501 xp[1] = int(x2); yp[1] = int(y + hg);
502 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
503 pain.lines(xp, yp, 3);
506 case SPECIAL_CARON: // special caron
509 case 'L': wid = int(4.0 * wid / 5.0); break;
510 case 't': y -= int(hg35 / 2.0); break;
513 xp[0] = int(x + wid);
514 yp[0] = int(y + hg35 + hg);
516 xp[1] = int(x + wid + (hg35 / 2.0));
517 yp[1] = int(y + hg + (hg35 / 2.0));
519 xp[2] = int(x + wid + (hg35 / 2.0));
522 pain.lines(xp, yp, 3);
525 case HUNGARIAN_UMLAUT: // hung. umlaut
527 pain.text(int(x2 - (lyxfont::rbearing('´', font) - lyxfont::lbearing('´', font))),
528 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
531 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('´', font) - (lyxfont::ascent('´', font) + lyxfont::descent('´', font)) / 2,
535 case UMLAUT: // umlaut
537 pain.text(int(x2 - (lyxfont::rbearing('¨', font) - lyxfont::lbearing('¨', font)) / 2),
538 baseline - lyxfont::ascent(ic, font) - lyxfont::descent('¨', font) - ( lyxfont::ascent('¨', font) + lyxfont::descent('¨', font)) / 2,
542 case CIRCUMFLEX: // circumflex
545 tmpf.decSize().decSize().decSize();
546 pain.text(int(x2 - (lyxfont::rbearing(0x5E, tmpf) - lyxfont::lbearing(0x5E, tmpf)) / 2),
547 int(baseline - lyxfont::ascent(ic, font) - lyxfont::descent(0x5E, tmpf) - (lyxfont::ascent(0x5E, tmpf) + lyxfont::descent(0x5E, tmpf)) / 3.0),
551 case OGONEK: // ogonek
553 // this does probably not look like an ogonek, so
554 // it should certainly be refined
561 yp[1] = y + int(hg35);
563 xp[2] = int(x2 - hg35);
564 yp[2] = y + int(hg / 2.0);
566 xp[3] = int(x2 + hg / 4.0);
569 pain.lines(xp, yp, 4);
578 yp[0] = y + int(3.0 * hg);
580 xp[1] = int(x + float(wid) * 0.75);
583 pain.lines(xp, yp, 2);
586 case DOT_LESS_I: // dotless-i
587 case DOT_LESS_J: // dotless-j
589 // nothing to do for these
594 pain.fillRectangle(int(x + 1),
595 baseline - ascent(pain, font) + 1,
596 width(pain, font) - 2,
598 + descent(pain, font) - 2);
599 pain.rectangle(int(x + 1), baseline - ascent(pain, font) + 1,
600 width(pain, font) - 2,
601 ascent(pain, font) + descent(pain, font) - 2);
602 pain.text(int(x + 2), baseline, contents, font);
604 x += width(pain, font);
608 void InsetLatexAccent::Write(ostream & os) const
610 os << "\\i " << contents << "\n";
614 void InsetLatexAccent::Read(LyXLex & lex)
617 contents = lex.GetString();
622 int InsetLatexAccent::Latex(ostream & os,
623 bool /*fragile*/, bool/*fs*/) const
630 int InsetLatexAccent::Ascii(ostream & os) const
637 int InsetLatexAccent::Linuxdoc(ostream & os) const
644 int InsetLatexAccent::DocBook(ostream & os) const
651 bool InsetLatexAccent::Deletable() const
657 bool InsetLatexAccent::DirectWrite() const
663 Inset * InsetLatexAccent::Clone() const
665 return new InsetLatexAccent(contents);
669 Inset::Code InsetLatexAccent::LyxCode() const
671 return Inset::ACCENT_CODE;
675 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)