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"
25 using namespace lyx::support;
31 /* LatexAccent. Proper handling of accented characters */
32 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
33 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
35 InsetLatexAccent::InsetLatexAccent()
40 InsetLatexAccent::InsetLatexAccent(string const & str)
47 void InsetLatexAccent::checkContents()
48 // check, if we know the modifier and can display it ok on screen
52 if (contents.empty() || contents.length() < 2) {
53 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
58 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
59 contents = trim(contents);
60 if (!contains(contents, "{") && !contains(contents, "}")) {
61 if (contents.length() == 2) {
67 } else if (contents.length() == 3) {
75 } else if (contents.length() == 4 && contents[2] == ' ') {
83 } else if (contents.length() == 4 && contents[2] == '\\'
84 && (contents[3] == 'i' || contents[3] == 'j')) {
95 if (contents[0] != '\\') { // demand that first char is a '\\'
96 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
100 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
106 switch (contents[1]) { // second char should be one of these
108 modtype = ACUTE; // acute
109 plusasc = true; // at the top of character
112 modtype = GRAVE; // grave
113 plusasc = true; // at the top
116 modtype = MACRON; // macron
117 plusasc = true; // at the top
120 modtype = TILDE; // tilde
121 plusasc = true; // at the top
123 case 'b': // underbar
124 modtype = UNDERBAR; // underbar
125 plusdesc = true; // at the bottom
128 modtype = CEDILLA; // cedilla
129 plusdesc = true; // at the bottom
131 case 'd': // underdot
132 modtype = UNDERDOT; // underdot
133 plusdesc = true; // at the bottom
136 modtype = CIRCLE; // circle
137 plusasc = true; // at the top
140 modtype = TIE; // tie
141 plusasc = true; // at the top
144 modtype = BREVE; // breve
145 plusasc = true; // at the top
148 modtype = CARON; // caron
149 plusasc = true; // at the top
151 case 'q': // special caron
152 modtype = SPECIAL_CARON; // special caron
153 plusasc = true; // at the top
155 case 'H': // hungarian umlaut
156 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
157 plusasc = true; // at the top
160 modtype = UMLAUT; // umlaut
161 plusasc = true; // at the top
164 modtype = DOT; // dot
165 plusasc = true; // at the top
167 case '^': // circumflex
168 modtype = CIRCUMFLEX; // circumflex
169 plusasc = true; // at the top
172 modtype = OGONEK; // ogonek
175 case 'i': // dot-less-i
176 modtype = DOT_LESS_I; // dot-less-i
177 plusasc = true; // at the top (not really needed)
180 case 'j': // dot-less-j
181 modtype = DOT_LESS_J; // dot-less-j
182 plusasc = true; // at the top (not really needed)
187 plusasc = true; // at the top (not really needed)
191 plusasc = true; // at the top (not really needed)
194 lyxerr[Debug::KEY] << "Default" << endl;
195 // unknow accent (or something else)
199 // we demand that third char is a '{' (Lgb)
200 if (contents[2] != '{') return;
202 // special clause for \i{}, \j{} \l{} and \L{}
203 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
204 || modtype == lSLASH || modtype == LSLASH)
205 && contents[3] == '}') {
207 case DOT_LESS_I: ic = 'i'; break;
208 case DOT_LESS_J: ic = 'j'; break;
209 case lSLASH: ic = 'l'; break;
210 case LSLASH: ic = 'L'; break;
212 // if this happens something is really wrong
213 lyxerr << "InsetLaTexAccent: weird error." << endl;
216 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
217 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
219 << ", top: " << plusasc
220 << ", bot: " << plusdesc
221 << ", dot: " << remdot
222 << ", mod: " << modtype << endl;
223 // Special case for space
224 } else if (contents[3] == '}') {
230 ic = contents[3]; // i will always be 3 here
232 // ic should now be a alfa-char or '\\'
234 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
235 if (ic == 'i' || ic == 'j')
239 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
240 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
241 string temp = contents;
242 temp.erase(3, string::npos);
245 for (string::size_type j = 4;
246 j < contents.length(); ++j)
253 // demand a '}' at the end
254 if (contents[++i] != '}' && contents[++i]) return;
256 // fine, the char is properly decoded now (hopefully)
257 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
259 << ", top: " << plusasc
260 << ", bot: " << plusdesc
261 << ", dot: " << remdot
262 << ", mod: " << modtype << endl;
268 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
270 LyXFont & font = mi.base.font;
271 // This function is a bit too simplistic and is just a
272 // "try to make a fit for all accents" approach, to
273 // make it better we need to know what kind of accent is
274 // used and add to max based on that.
277 dim.asc = font_metrics::ascent('a', font);
279 dim.asc = font_metrics::ascent(ic, font);
281 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
284 dim.des = font_metrics::descent('a', font);
286 dim.des = font_metrics::descent(ic, font);
290 dim.wid = font_metrics::width(ic, font);
292 dim.asc = font_metrics::maxAscent(font) + 4;
293 dim.des = font_metrics::maxDescent(font) + 4;
294 dim.wid = font_metrics::width(contents, font) + 4;
300 int InsetLatexAccent::lbearing(LyXFont const & font) const
302 return font_metrics::lbearing(ic, font);
306 int InsetLatexAccent::rbearing(LyXFont const & font) const
308 return font_metrics::rbearing(ic, font);
312 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
314 unsigned char tmpic = ic;
319 if (ic == 'c') tmpic = 0xe7;
320 if (ic == 'C') tmpic = 0xc7;
321 if (ic == 's') tmpic = 0xfe;
322 if (ic == 'S') tmpic = 0xde;
326 { if (ic == 'g') tmpic = 0xf0;
327 if (ic == 'G') tmpic = 0xd0;
332 if (ic == 'o') tmpic = 0xf6;
333 if (ic == 'O') tmpic = 0xd6;
334 if (ic == 'u') tmpic = 0xfc;
335 if (ic == 'U') tmpic = 0xdc;
338 case DOT: if (ic == 'I') tmpic = 0xdd; break;
339 case DOT_LESS_I: tmpic = 0xfd; break;
340 default: return false;
343 pi.pain.text(x, y, char(tmpic), pi.base.font);
351 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
353 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
354 if (displayISO8859_9(pi, x, baseline))
358 // All the manually drawn accents in this function could use an
359 // overhaul. Different ways of drawing (what metrics to use)
360 // should also be considered.
362 BufferView * bv = pi.base.bv;
363 LyXFont font = pi.base.font;
364 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
365 font.setLanguage(english_language);
368 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
373 hg = font_metrics::maxDescent(font);
374 y = baseline - dim_.asc;
376 if (font.shape() == LyXFont::ITALIC_SHAPE)
377 x2 += (4.0 * hg) / 5.0; // italic
384 float hg35 = float(hg * 3.0) / 5.0;
386 // display with proper accent mark
388 pi.pain.text(x, baseline, ic, font);
391 int tmpvar = baseline - font_metrics::ascent('i', font);
393 if (font.shape() == LyXFont::ITALIC_SHAPE)
394 tmpx += int(0.8 * hg); // italic
395 lyxerr[Debug::KEY] << "Removing dot." << endl;
396 // remove the dot first
397 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
398 font_metrics::ascent('i', pi.base.font) -
399 font_metrics::ascent('x', pi.base.font) - 1,
401 // the five lines below is a simple hack to
402 // make the display of accent 'i' and 'j'
403 // better. It makes the accent be written
404 // closer to the top of the dot-less 'i' or 'j'.
405 char tmpic = ic; // store the ic when we
406 ic = 'x'; // calculates the ascent of
408 int asc = ascent(); // the dot-less version (here: 'x')
409 ic = tmpic; // set the orig ic back
410 y = baseline - asc; // update to new y coord.
412 // now the rest - draw within (x, y, x+wid, y+hg)
414 case ACUTE: // acute 0xB4
416 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
417 - font_metrics::lbearing(0xB4, font)) / 2),
418 baseline - font_metrics::ascent(ic, font)
419 - font_metrics::descent(0xB4, font)
420 - (font_metrics::ascent(0xB4, font)
421 + font_metrics::descent(0xB4, font)) / 2,
425 case GRAVE: // grave 0x60
427 pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
428 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
432 case MACRON: // macron
434 pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
435 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
441 pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
442 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
446 case UNDERBAR: // underbar 0x5F
448 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
452 case CEDILLA: // cedilla
454 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
459 case UNDERDOT: // underdot
461 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
462 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
469 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
470 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
475 case CIRCLE: // circle
478 tmpf.decSize().decSize();
479 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
480 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
486 pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
487 int(2 * hg), int(hg), 0, 360 * 32);
492 pi.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 pi.pain.lines(xp, yp, 3);
506 case SPECIAL_CARON: // special caron
509 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
510 case 't': y -= int(hg35 / 2.0); break;
513 xp[0] = int(x + dim_.wid);
514 yp[0] = int(y + hg35 + hg);
516 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
517 yp[1] = int(y + hg + (hg35 / 2.0));
519 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
522 pi.pain.lines(xp, yp, 3);
525 case HUNGARIAN_UMLAUT: // hung. umlaut
527 pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
528 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
530 pi.pain.text(int(x2),
531 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
535 case UMLAUT: // umlaut
537 pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
538 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
542 case CIRCUMFLEX: // circumflex
545 tmpf.decSize().decSize().decSize();
546 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
547 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::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 pi.pain.lines(xp, yp, 4);
578 yp[0] = y + int(3.0 * hg);
580 xp[1] = int(x + float(dim_.wid) * 0.75);
583 pi.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 pi.pain.fillRectangle(x + 1,
595 baseline - dim_.asc + 1, dim_.wid - 2,
596 dim_.asc + dim_.des - 2, backgroundColor());
597 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
598 dim_.wid - 2, dim_.asc + dim_.des - 2);
599 pi.pain.text(x + 2, baseline, contents, font);
604 void InsetLatexAccent::write(Buffer const *, ostream & os) const
606 os << "\\i " << contents << "\n";
610 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
613 contents = lex.getString();
618 int InsetLatexAccent::latex(Buffer const *, ostream & os,
619 LatexRunParams const &) const
626 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
633 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
640 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
647 bool InsetLatexAccent::directWrite() const
653 InsetBase * InsetLatexAccent::clone() const
655 return new InsetLatexAccent(contents);
659 Inset::Code InsetLatexAccent::lyxCode() const
661 return Inset::ACCENT_CODE;
665 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)