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"
16 #include "dimension.h"
18 #include "support/lstrings.h"
19 #include "BufferView.h"
20 #include "frontends/Painter.h"
21 #include "frontends/font_metrics.h"
24 #include "metricsinfo.h"
30 /* LatexAccent. Proper handling of accented characters */
31 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
32 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
34 InsetLatexAccent::InsetLatexAccent()
39 InsetLatexAccent::InsetLatexAccent(string const & str)
46 void InsetLatexAccent::checkContents()
47 // check, if we know the modifier and can display it ok on screen
51 if (contents.empty() || contents.length() < 2) {
52 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
57 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
58 contents = trim(contents);
59 if (!contains(contents, "{") && !contains(contents, "}")) {
60 if (contents.length() == 2) {
66 } else if (contents.length() == 3) {
74 } else if (contents.length() == 4 && contents[2] == ' ') {
82 } else if (contents.length() == 4 && contents[2] == '\\'
83 && (contents[3] == 'i' || contents[3] == 'j')) {
94 if (contents[0] != '\\') { // demand that first char is a '\\'
95 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
99 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
105 switch (contents[1]) { // second char should be one of these
107 modtype = ACUTE; // acute
108 plusasc = true; // at the top of character
111 modtype = GRAVE; // grave
112 plusasc = true; // at the top
115 modtype = MACRON; // macron
116 plusasc = true; // at the top
119 modtype = TILDE; // tilde
120 plusasc = true; // at the top
122 case 'b': // underbar
123 modtype = UNDERBAR; // underbar
124 plusdesc = true; // at the bottom
127 modtype = CEDILLA; // cedilla
128 plusdesc = true; // at the bottom
130 case 'd': // underdot
131 modtype = UNDERDOT; // underdot
132 plusdesc = true; // at the bottom
135 modtype = CIRCLE; // circle
136 plusasc = true; // at the top
139 modtype = TIE; // tie
140 plusasc = true; // at the top
143 modtype = BREVE; // breve
144 plusasc = true; // at the top
147 modtype = CARON; // caron
148 plusasc = true; // at the top
150 case 'q': // special caron
151 modtype = SPECIAL_CARON; // special caron
152 plusasc = true; // at the top
154 case 'H': // hungarian umlaut
155 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
156 plusasc = true; // at the top
159 modtype = UMLAUT; // umlaut
160 plusasc = true; // at the top
163 modtype = DOT; // dot
164 plusasc = true; // at the top
166 case '^': // circumflex
167 modtype = CIRCUMFLEX; // circumflex
168 plusasc = true; // at the top
171 modtype = OGONEK; // ogonek
174 case 'i': // dot-less-i
175 modtype = DOT_LESS_I; // dot-less-i
176 plusasc = true; // at the top (not really needed)
179 case 'j': // dot-less-j
180 modtype = DOT_LESS_J; // dot-less-j
181 plusasc = true; // at the top (not really needed)
186 plusasc = true; // at the top (not really needed)
190 plusasc = true; // at the top (not really needed)
193 lyxerr[Debug::KEY] << "Default" << endl;
194 // unknow accent (or something else)
198 // we demand that third char is a '{' (Lgb)
199 if (contents[2] != '{') return;
201 // special clause for \i{}, \j{} \l{} and \L{}
202 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
203 || modtype == lSLASH || modtype == LSLASH)
204 && contents[3] == '}') {
206 case DOT_LESS_I: ic = 'i'; break;
207 case DOT_LESS_J: ic = 'j'; break;
208 case lSLASH: ic = 'l'; break;
209 case LSLASH: ic = 'L'; break;
211 // if this happens something is really wrong
212 lyxerr << "InsetLaTexAccent: weird error." << endl;
215 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
216 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
218 << ", top: " << plusasc
219 << ", bot: " << plusdesc
220 << ", dot: " << remdot
221 << ", mod: " << modtype << endl;
222 // Special case for space
223 } else if (contents[3] == '}') {
229 ic = contents[3]; // i will always be 3 here
231 // ic should now be a alfa-char or '\\'
233 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
234 if (ic == 'i' || ic == 'j')
238 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
239 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
240 string temp = contents;
241 temp.erase(3, string::npos);
244 for (string::size_type j = 4;
245 j < contents.length(); ++j)
252 // demand a '}' at the end
253 if (contents[++i] != '}' && contents[++i]) return;
255 // fine, the char is properly decoded now (hopefully)
256 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
258 << ", top: " << plusasc
259 << ", bot: " << plusdesc
260 << ", dot: " << remdot
261 << ", mod: " << modtype << endl;
267 void InsetLatexAccent::dimension(BufferView *, LyXFont const & font,
268 Dimension & dim) const
270 // This function is a bit too simplistic and is just a
271 // "try to make a fit for all accents" approach, to
272 // make it better we need to know what kind of accent is
273 // used and add to max based on that.
276 dim.asc = font_metrics::ascent('a', font);
278 dim.asc = font_metrics::ascent(ic, font);
280 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
283 dim.des = font_metrics::descent('a', font);
285 dim.des = font_metrics::descent(ic, font);
289 dim.wid = font_metrics::width(ic, font);
291 dim.asc = font_metrics::maxAscent(font) + 4;
292 dim.des = font_metrics::maxDescent(font) + 4;
293 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 dimension(bv, font, dim);
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
408 int asc = ascent(bv, font); // 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 Inset * InsetLatexAccent::clone(Buffer const &) const
655 return new InsetLatexAccent(contents);
659 // Inset * InsetLatexAccent::clone(Buffer const &, bool) const
661 // return new InsetLatexAccent(contents);
665 Inset::Code InsetLatexAccent::lyxCode() const
667 return Inset::ACCENT_CODE;
671 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)