2 * \file insetlatexaccent.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
13 #include "insetlatexaccent.h"
17 #include "support/lstrings.h"
18 #include "BufferView.h"
19 #include "frontends/Painter.h"
20 #include "frontends/font_metrics.h"
23 #include "metricsinfo.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) {
51 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
56 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
57 contents = trim(contents);
58 if (!contains(contents, "{") && !contains(contents, "}")) {
59 if (contents.length() == 2) {
65 } else if (contents.length() == 3) {
73 } else if (contents.length() == 4 && contents[2] == ' ') {
81 } else if (contents.length() == 4 && contents[2] == '\\'
82 && (contents[3] == 'i' || contents[3] == 'j')) {
93 if (contents[0] != '\\') { // demand that first char is a '\\'
94 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
98 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
104 switch (contents[1]) { // second char should be one of these
106 modtype = ACUTE; // acute
107 plusasc = true; // at the top of character
110 modtype = GRAVE; // grave
111 plusasc = true; // at the top
114 modtype = MACRON; // macron
115 plusasc = true; // at the top
118 modtype = TILDE; // tilde
119 plusasc = true; // at the top
121 case 'b': // underbar
122 modtype = UNDERBAR; // underbar
123 plusdesc = true; // at the bottom
126 modtype = CEDILLA; // cedilla
127 plusdesc = true; // at the bottom
129 case 'd': // underdot
130 modtype = UNDERDOT; // underdot
131 plusdesc = true; // at the bottom
134 modtype = CIRCLE; // circle
135 plusasc = true; // at the top
138 modtype = TIE; // tie
139 plusasc = true; // at the top
142 modtype = BREVE; // breve
143 plusasc = true; // at the top
146 modtype = CARON; // caron
147 plusasc = true; // at the top
149 case 'q': // special caron
150 modtype = SPECIAL_CARON; // special caron
151 plusasc = true; // at the top
153 case 'H': // hungarian umlaut
154 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
155 plusasc = true; // at the top
158 modtype = UMLAUT; // umlaut
159 plusasc = true; // at the top
162 modtype = DOT; // dot
163 plusasc = true; // at the top
165 case '^': // circumflex
166 modtype = CIRCUMFLEX; // circumflex
167 plusasc = true; // at the top
170 modtype = OGONEK; // ogonek
173 case 'i': // dot-less-i
174 modtype = DOT_LESS_I; // dot-less-i
175 plusasc = true; // at the top (not really needed)
178 case 'j': // dot-less-j
179 modtype = DOT_LESS_J; // dot-less-j
180 plusasc = true; // at the top (not really needed)
185 plusasc = true; // at the top (not really needed)
189 plusasc = true; // at the top (not really needed)
192 lyxerr[Debug::KEY] << "Default" << endl;
193 // unknow accent (or something else)
197 // we demand that third char is a '{' (Lgb)
198 if (contents[2] != '{') return;
200 // special clause for \i{}, \j{} \l{} and \L{}
201 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
202 || modtype == lSLASH || modtype == LSLASH)
203 && contents[3] == '}') {
205 case DOT_LESS_I: ic = 'i'; break;
206 case DOT_LESS_J: ic = 'j'; break;
207 case lSLASH: ic = 'l'; break;
208 case LSLASH: ic = 'L'; break;
210 // if this happens something is really wrong
211 lyxerr << "InsetLaTexAccent: weird error." << endl;
214 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
215 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
217 << ", top: " << plusasc
218 << ", bot: " << plusdesc
219 << ", dot: " << remdot
220 << ", mod: " << modtype << endl;
221 // Special case for space
222 } else if (contents[3] == '}') {
228 ic = contents[3]; // i will always be 3 here
230 // ic should now be a alfa-char or '\\'
232 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
233 if (ic == 'i' || ic == 'j')
237 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
238 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
239 string temp = contents;
240 temp.erase(3, string::npos);
243 for (string::size_type j = 4;
244 j < contents.length(); ++j)
251 // demand a '}' at the end
252 if (contents[++i] != '}' && contents[++i]) return;
254 // fine, the char is properly decoded now (hopefully)
255 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
257 << ", top: " << plusasc
258 << ", bot: " << plusdesc
259 << ", dot: " << remdot
260 << ", mod: " << modtype << endl;
266 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
268 LyXFont & font = mi.base.font;
269 // This function is a bit too simplistic and is just a
270 // "try to make a fit for all accents" approach, to
271 // make it better we need to know what kind of accent is
272 // used and add to max based on that.
275 dim.asc = font_metrics::ascent('a', font);
277 dim.asc = font_metrics::ascent(ic, font);
279 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
282 dim.des = font_metrics::descent('a', font);
284 dim.des = font_metrics::descent(ic, font);
288 dim.wid = font_metrics::width(ic, font);
290 dim.asc = font_metrics::maxAscent(font) + 4;
291 dim.des = font_metrics::maxDescent(font) + 4;
292 dim.wid = font_metrics::width(contents, font) + 4;
298 int InsetLatexAccent::lbearing(LyXFont const & font) const
300 return font_metrics::lbearing(ic, font);
304 int InsetLatexAccent::rbearing(LyXFont const & font) const
306 return font_metrics::rbearing(ic, font);
310 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
312 unsigned char tmpic = ic;
317 if (ic == 'c') tmpic = 0xe7;
318 if (ic == 'C') tmpic = 0xc7;
319 if (ic == 's') tmpic = 0xfe;
320 if (ic == 'S') tmpic = 0xde;
324 { if (ic == 'g') tmpic = 0xf0;
325 if (ic == 'G') tmpic = 0xd0;
330 if (ic == 'o') tmpic = 0xf6;
331 if (ic == 'O') tmpic = 0xd6;
332 if (ic == 'u') tmpic = 0xfc;
333 if (ic == 'U') tmpic = 0xdc;
336 case DOT: if (ic == 'I') tmpic = 0xdd; break;
337 case DOT_LESS_I: tmpic = 0xfd; break;
338 default: return false;
341 pi.pain.text(x, y, char(tmpic), pi.base.font);
349 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
351 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
352 if (displayISO8859_9(pi, x, baseline))
356 // All the manually drawn accents in this function could use an
357 // overhaul. Different ways of drawing (what metrics to use)
358 // should also be considered.
360 BufferView * bv = pi.base.bv;
361 LyXFont font = pi.base.font;
362 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
363 font.setLanguage(english_language);
366 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
371 hg = font_metrics::maxDescent(font);
372 y = baseline - dim_.asc;
374 if (font.shape() == LyXFont::ITALIC_SHAPE)
375 x2 += (4.0 * hg) / 5.0; // italic
382 float hg35 = float(hg * 3.0) / 5.0;
384 // display with proper accent mark
386 pi.pain.text(x, baseline, ic, font);
389 int tmpvar = baseline - font_metrics::ascent('i', font);
391 if (font.shape() == LyXFont::ITALIC_SHAPE)
392 tmpx += int(0.8 * hg); // italic
393 lyxerr[Debug::KEY] << "Removing dot." << endl;
394 // remove the dot first
395 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
396 font_metrics::ascent('i', pi.base.font) -
397 font_metrics::ascent('x', pi.base.font) - 1,
399 // the five lines below is a simple hack to
400 // make the display of accent 'i' and 'j'
401 // better. It makes the accent be written
402 // closer to the top of the dot-less 'i' or 'j'.
403 char tmpic = ic; // store the ic when we
404 ic = 'x'; // calculates the ascent of
405 int asc = ascent(bv, font); // the dot-less version (here: 'x')
406 ic = tmpic; // set the orig ic back
407 y = baseline - asc; // update to new y coord.
409 // now the rest - draw within (x, y, x+wid, y+hg)
411 case ACUTE: // acute 0xB4
413 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
414 - font_metrics::lbearing(0xB4, font)) / 2),
415 baseline - font_metrics::ascent(ic, font)
416 - font_metrics::descent(0xB4, font)
417 - (font_metrics::ascent(0xB4, font)
418 + font_metrics::descent(0xB4, font)) / 2,
422 case GRAVE: // grave 0x60
424 pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
425 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
429 case MACRON: // macron
431 pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
432 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
438 pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
439 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
443 case UNDERBAR: // underbar 0x5F
445 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
449 case CEDILLA: // cedilla
451 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
456 case UNDERDOT: // underdot
458 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
459 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
466 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
467 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
472 case CIRCLE: // circle
475 tmpf.decSize().decSize();
476 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
477 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
483 pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
484 int(2 * hg), int(hg), 0, 360 * 32);
489 pi.pain.arc(int(x2 - (hg / 2.0)), y,
490 int(hg), int(hg), 0, -360*32);
497 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
498 xp[1] = int(x2); yp[1] = int(y + hg);
499 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
500 pi.pain.lines(xp, yp, 3);
503 case SPECIAL_CARON: // special caron
506 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
507 case 't': y -= int(hg35 / 2.0); break;
510 xp[0] = int(x + dim_.wid);
511 yp[0] = int(y + hg35 + hg);
513 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
514 yp[1] = int(y + hg + (hg35 / 2.0));
516 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
519 pi.pain.lines(xp, yp, 3);
522 case HUNGARIAN_UMLAUT: // hung. umlaut
524 pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
525 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
527 pi.pain.text(int(x2),
528 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
532 case UMLAUT: // umlaut
534 pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
535 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
539 case CIRCUMFLEX: // circumflex
542 tmpf.decSize().decSize().decSize();
543 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
544 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
548 case OGONEK: // ogonek
550 // this does probably not look like an ogonek, so
551 // it should certainly be refined
558 yp[1] = y + int(hg35);
560 xp[2] = int(x2 - hg35);
561 yp[2] = y + int(hg / 2.0);
563 xp[3] = int(x2 + hg / 4.0);
566 pi.pain.lines(xp, yp, 4);
575 yp[0] = y + int(3.0 * hg);
577 xp[1] = int(x + float(dim_.wid) * 0.75);
580 pi.pain.lines(xp, yp, 2);
583 case DOT_LESS_I: // dotless-i
584 case DOT_LESS_J: // dotless-j
586 // nothing to do for these
591 pi.pain.fillRectangle(x + 1,
592 baseline - dim_.asc + 1, dim_.wid - 2,
593 dim_.asc + dim_.des - 2, backgroundColor());
594 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
595 dim_.wid - 2, dim_.asc + dim_.des - 2);
596 pi.pain.text(x + 2, baseline, contents, font);
601 void InsetLatexAccent::write(Buffer const *, ostream & os) const
603 os << "\\i " << contents << "\n";
607 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
610 contents = lex.getString();
615 int InsetLatexAccent::latex(Buffer const *, ostream & os,
616 LatexRunParams const &) const
623 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
630 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
637 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
644 bool InsetLatexAccent::directWrite() const
650 InsetBase * InsetLatexAccent::clone() const
652 return new InsetLatexAccent(contents);
656 Inset::Code InsetLatexAccent::lyxCode() const
658 return Inset::ACCENT_CODE;
662 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)