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"
19 #include "metricsinfo.h"
21 #include "frontends/font_metrics.h"
22 #include "frontends/Painter.h"
24 #include "support/lstrings.h"
26 using namespace lyx::support;
33 /* LatexAccent. Proper handling of accented characters */
34 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
35 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
37 InsetLatexAccent::InsetLatexAccent()
42 InsetLatexAccent::InsetLatexAccent(string const & str)
49 void InsetLatexAccent::checkContents()
50 // check, if we know the modifier and can display it ok on screen
54 if (contents.empty() || contents.length() < 2) {
55 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
60 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
61 contents = trim(contents);
62 if (!contains(contents, "{") && !contains(contents, "}")) {
63 if (contents.length() == 2) {
69 } else if (contents.length() == 3) {
77 } else if (contents.length() == 4 && contents[2] == ' ') {
85 } else if (contents.length() == 4 && contents[2] == '\\'
86 && (contents[3] == 'i' || contents[3] == 'j')) {
97 if (contents[0] != '\\') { // demand that first char is a '\\'
98 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
102 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
108 switch (contents[1]) { // second char should be one of these
110 modtype = ACUTE; // acute
111 plusasc = true; // at the top of character
114 modtype = GRAVE; // grave
115 plusasc = true; // at the top
118 modtype = MACRON; // macron
119 plusasc = true; // at the top
122 modtype = TILDE; // tilde
123 plusasc = true; // at the top
125 case 'b': // underbar
126 modtype = UNDERBAR; // underbar
127 plusdesc = true; // at the bottom
130 modtype = CEDILLA; // cedilla
131 plusdesc = true; // at the bottom
133 case 'd': // underdot
134 modtype = UNDERDOT; // underdot
135 plusdesc = true; // at the bottom
138 modtype = CIRCLE; // circle
139 plusasc = true; // at the top
142 modtype = TIE; // tie
143 plusasc = true; // at the top
146 modtype = BREVE; // breve
147 plusasc = true; // at the top
150 modtype = CARON; // caron
151 plusasc = true; // at the top
153 case 'q': // special caron
154 modtype = SPECIAL_CARON; // special caron
155 plusasc = true; // at the top
157 case 'H': // hungarian umlaut
158 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
159 plusasc = true; // at the top
162 modtype = UMLAUT; // umlaut
163 plusasc = true; // at the top
166 modtype = DOT; // dot
167 plusasc = true; // at the top
169 case '^': // circumflex
170 modtype = CIRCUMFLEX; // circumflex
171 plusasc = true; // at the top
174 modtype = OGONEK; // ogonek
177 case 'i': // dot-less-i
178 modtype = DOT_LESS_I; // dot-less-i
179 plusasc = true; // at the top (not really needed)
182 case 'j': // dot-less-j
183 modtype = DOT_LESS_J; // dot-less-j
184 plusasc = true; // at the top (not really needed)
189 plusasc = true; // at the top (not really needed)
193 plusasc = true; // at the top (not really needed)
196 lyxerr[Debug::KEY] << "Default" << endl;
197 // unknow accent (or something else)
201 // we demand that third char is a '{' (Lgb)
202 if (contents[2] != '{') return;
204 // special clause for \i{}, \j{} \l{} and \L{}
205 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
206 || modtype == lSLASH || modtype == LSLASH)
207 && contents[3] == '}') {
209 case DOT_LESS_I: ic = 'i'; break;
210 case DOT_LESS_J: ic = 'j'; break;
211 case lSLASH: ic = 'l'; break;
212 case LSLASH: ic = 'L'; break;
214 // if this happens something is really wrong
215 lyxerr << "InsetLaTexAccent: weird error." << endl;
218 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
219 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
221 << ", top: " << plusasc
222 << ", bot: " << plusdesc
223 << ", dot: " << remdot
224 << ", mod: " << modtype << endl;
225 // Special case for space
226 } else if (contents[3] == '}') {
232 ic = contents[3]; // i will always be 3 here
234 // ic should now be a alfa-char or '\\'
236 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
237 if (ic == 'i' || ic == 'j')
241 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
242 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
243 string temp = contents;
244 temp.erase(3, string::npos);
247 for (string::size_type j = 4;
248 j < contents.length(); ++j)
255 // demand a '}' at the end
256 if (contents[++i] != '}' && contents[++i]) return;
258 // fine, the char is properly decoded now (hopefully)
259 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
261 << ", top: " << plusasc
262 << ", bot: " << plusdesc
263 << ", dot: " << remdot
264 << ", mod: " << modtype << endl;
270 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
272 LyXFont & font = mi.base.font;
273 // This function is a bit too simplistic and is just a
274 // "try to make a fit for all accents" approach, to
275 // make it better we need to know what kind of accent is
276 // used and add to max based on that.
279 dim.asc = font_metrics::ascent('a', font);
281 dim.asc = font_metrics::ascent(ic, font);
283 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
286 dim.des = font_metrics::descent('a', font);
288 dim.des = font_metrics::descent(ic, font);
292 dim.wid = font_metrics::width(ic, font);
294 dim.asc = font_metrics::maxAscent(font) + 4;
295 dim.des = font_metrics::maxDescent(font) + 4;
296 dim.wid = font_metrics::width(contents, font) + 4;
302 int InsetLatexAccent::lbearing(LyXFont const & font) const
304 return font_metrics::lbearing(ic, font);
308 int InsetLatexAccent::rbearing(LyXFont const & font) const
310 return font_metrics::rbearing(ic, font);
314 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
316 unsigned char tmpic = ic;
321 if (ic == 'c') tmpic = 0xe7;
322 if (ic == 'C') tmpic = 0xc7;
323 if (ic == 's') tmpic = 0xfe;
324 if (ic == 'S') tmpic = 0xde;
328 { if (ic == 'g') tmpic = 0xf0;
329 if (ic == 'G') tmpic = 0xd0;
334 if (ic == 'o') tmpic = 0xf6;
335 if (ic == 'O') tmpic = 0xd6;
336 if (ic == 'u') tmpic = 0xfc;
337 if (ic == 'U') tmpic = 0xdc;
340 case DOT: if (ic == 'I') tmpic = 0xdd; break;
341 case DOT_LESS_I: tmpic = 0xfd; break;
342 default: return false;
345 pi.pain.text(x, y, char(tmpic), pi.base.font);
353 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
355 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
356 if (displayISO8859_9(pi, x, baseline))
360 // All the manually drawn accents in this function could use an
361 // overhaul. Different ways of drawing (what metrics to use)
362 // should also be considered.
364 LyXFont font = pi.base.font;
365 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
366 font.setLanguage(english_language);
369 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
374 hg = font_metrics::maxDescent(font);
375 y = baseline - dim_.asc;
377 if (font.shape() == LyXFont::ITALIC_SHAPE)
378 x2 += (4.0 * hg) / 5.0; // italic
385 float hg35 = float(hg * 3.0) / 5.0;
387 // display with proper accent mark
389 pi.pain.text(x, baseline, ic, font);
392 int tmpvar = baseline - font_metrics::ascent('i', font);
394 if (font.shape() == LyXFont::ITALIC_SHAPE)
395 tmpx += int(0.8 * hg); // italic
396 lyxerr[Debug::KEY] << "Removing dot." << endl;
397 // remove the dot first
398 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
399 font_metrics::ascent('i', pi.base.font) -
400 font_metrics::ascent('x', pi.base.font) - 1,
402 // the five lines below is a simple hack to
403 // make the display of accent 'i' and 'j'
404 // better. It makes the accent be written
405 // closer to the top of the dot-less 'i' or 'j'.
406 char tmpic = ic; // store the ic when we
407 ic = 'x'; // calculates the ascent of
409 int asc = ascent(); // the dot-less version (here: 'x')
410 ic = tmpic; // set the orig ic back
411 y = baseline - asc; // update to new y coord.
413 // now the rest - draw within (x, y, x+wid, y+hg)
415 case ACUTE: // acute 0xB4
417 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
418 - font_metrics::lbearing(0xB4, font)) / 2),
419 baseline - font_metrics::ascent(ic, font)
420 - font_metrics::descent(0xB4, font)
421 - (font_metrics::ascent(0xB4, font)
422 + font_metrics::descent(0xB4, font)) / 2,
426 case GRAVE: // grave 0x60
428 pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
429 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
433 case MACRON: // macron
435 pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
436 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
442 pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
443 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
447 case UNDERBAR: // underbar 0x5F
449 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
453 case CEDILLA: // cedilla
455 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
460 case UNDERDOT: // underdot
462 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
463 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
470 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
471 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
476 case CIRCLE: // circle
479 tmpf.decSize().decSize();
480 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
481 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
487 pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
488 int(2 * hg), int(hg), 0, 360 * 32);
493 pi.pain.arc(int(x2 - (hg / 2.0)), y,
494 int(hg), int(hg), 0, -360*32);
501 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
502 xp[1] = int(x2); yp[1] = int(y + hg);
503 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
504 pi.pain.lines(xp, yp, 3);
507 case SPECIAL_CARON: // special caron
510 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
511 case 't': y -= int(hg35 / 2.0); break;
514 xp[0] = int(x + dim_.wid);
515 yp[0] = int(y + hg35 + hg);
517 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
518 yp[1] = int(y + hg + (hg35 / 2.0));
520 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
523 pi.pain.lines(xp, yp, 3);
526 case HUNGARIAN_UMLAUT: // hung. umlaut
528 pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
529 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
531 pi.pain.text(int(x2),
532 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
536 case UMLAUT: // umlaut
538 pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
539 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
543 case CIRCUMFLEX: // circumflex
546 tmpf.decSize().decSize().decSize();
547 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
548 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
552 case OGONEK: // ogonek
554 // this does probably not look like an ogonek, so
555 // it should certainly be refined
562 yp[1] = y + int(hg35);
564 xp[2] = int(x2 - hg35);
565 yp[2] = y + int(hg / 2.0);
567 xp[3] = int(x2 + hg / 4.0);
570 pi.pain.lines(xp, yp, 4);
579 yp[0] = y + int(3.0 * hg);
581 xp[1] = int(x + float(dim_.wid) * 0.75);
584 pi.pain.lines(xp, yp, 2);
587 case DOT_LESS_I: // dotless-i
588 case DOT_LESS_J: // dotless-j
590 // nothing to do for these
595 pi.pain.fillRectangle(x + 1,
596 baseline - dim_.asc + 1, dim_.wid - 2,
597 dim_.asc + dim_.des - 2, backgroundColor());
598 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
599 dim_.wid - 2, dim_.asc + dim_.des - 2);
600 pi.pain.text(x + 2, baseline, contents, font);
605 void InsetLatexAccent::write(Buffer const &, ostream & os) const
607 os << "\\i " << contents << "\n";
611 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
614 contents = lex.getString();
619 int InsetLatexAccent::latex(Buffer const &, ostream & os,
620 LatexRunParams const &) const
627 int InsetLatexAccent::ascii(Buffer const &, ostream & os, int) const
634 int InsetLatexAccent::linuxdoc(Buffer const &, ostream & os) const
641 int InsetLatexAccent::docbook(Buffer const &, ostream & os, bool) const
648 bool InsetLatexAccent::directWrite() const
654 auto_ptr<InsetBase> InsetLatexAccent::clone() const
656 return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
660 InsetOld::Code InsetLatexAccent::lyxCode() const
662 return InsetOld::ACCENT_CODE;
666 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)