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);
367 mi.base.bv = pi.base.bv;
368 mi.base.font = pi.base.font;
373 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
378 hg = font_metrics::maxDescent(font);
379 y = baseline - dim_.asc;
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 pi.pain.text(x, baseline, ic, font);
396 int tmpvar = baseline - font_metrics::ascent('i', font);
398 if (font.shape() == LyXFont::ITALIC_SHAPE)
399 tmpx += int(0.8 * hg); // italic
400 lyxerr[Debug::KEY] << "Removing dot." << endl;
401 // remove the dot first
402 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
403 font_metrics::ascent('i', pi.base.font) -
404 font_metrics::ascent('x', pi.base.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 int asc = ascent(bv, 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 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
421 - font_metrics::lbearing(0xB4, font)) / 2),
422 baseline - font_metrics::ascent(ic, font)
423 - font_metrics::descent(0xB4, font)
424 - (font_metrics::ascent(0xB4, font)
425 + font_metrics::descent(0xB4, font)) / 2,
429 case GRAVE: // grave 0x60
431 pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
432 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
436 case MACRON: // macron
438 pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
439 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
445 pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
446 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
450 case UNDERBAR: // underbar 0x5F
452 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
456 case CEDILLA: // cedilla
458 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
463 case UNDERDOT: // underdot
465 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
466 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
473 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
474 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
479 case CIRCLE: // circle
482 tmpf.decSize().decSize();
483 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
484 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
490 pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
491 int(2 * hg), int(hg), 0, 360 * 32);
496 pi.pain.arc(int(x2 - (hg / 2.0)), y,
497 int(hg), int(hg), 0, -360*32);
504 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
505 xp[1] = int(x2); yp[1] = int(y + hg);
506 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
507 pi.pain.lines(xp, yp, 3);
510 case SPECIAL_CARON: // special caron
513 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
514 case 't': y -= int(hg35 / 2.0); break;
517 xp[0] = int(x + dim_.wid);
518 yp[0] = int(y + hg35 + hg);
520 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
521 yp[1] = int(y + hg + (hg35 / 2.0));
523 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
526 pi.pain.lines(xp, yp, 3);
529 case HUNGARIAN_UMLAUT: // hung. umlaut
531 pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
532 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
534 pi.pain.text(int(x2),
535 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
539 case UMLAUT: // umlaut
541 pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
542 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
546 case CIRCUMFLEX: // circumflex
549 tmpf.decSize().decSize().decSize();
550 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
551 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
555 case OGONEK: // ogonek
557 // this does probably not look like an ogonek, so
558 // it should certainly be refined
565 yp[1] = y + int(hg35);
567 xp[2] = int(x2 - hg35);
568 yp[2] = y + int(hg / 2.0);
570 xp[3] = int(x2 + hg / 4.0);
573 pi.pain.lines(xp, yp, 4);
582 yp[0] = y + int(3.0 * hg);
584 xp[1] = int(x + float(dim_.wid) * 0.75);
587 pi.pain.lines(xp, yp, 2);
590 case DOT_LESS_I: // dotless-i
591 case DOT_LESS_J: // dotless-j
593 // nothing to do for these
598 pi.pain.fillRectangle(x + 1,
599 baseline - dim_.asc + 1, dim_.wid - 2,
600 dim_.asc + dim_.des - 2, backgroundColor());
601 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
602 dim_.wid - 2, dim_.asc + dim_.des - 2);
603 pi.pain.text(x + 2, baseline, contents, font);
608 void InsetLatexAccent::write(Buffer const *, ostream & os) const
610 os << "\\i " << contents << "\n";
614 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
617 contents = lex.getString();
622 int InsetLatexAccent::latex(Buffer const *, ostream & os,
623 LatexRunParams const &) const
630 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
637 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
644 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
651 bool InsetLatexAccent::directWrite() const
657 Inset * InsetLatexAccent::clone(Buffer const &) const
659 return new InsetLatexAccent(contents);
663 // Inset * InsetLatexAccent::clone(Buffer const &, bool) 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)