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"
20 #include "metricsinfo.h"
22 #include "frontends/font_metrics.h"
23 #include "frontends/Painter.h"
25 #include "support/lstrings.h"
27 using lyx::support::contains;
28 using lyx::support::trim;
36 /* LatexAccent. Proper handling of accented characters */
37 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
38 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
40 InsetLatexAccent::InsetLatexAccent()
45 InsetLatexAccent::InsetLatexAccent(string const & str)
52 void InsetLatexAccent::checkContents()
53 // check, if we know the modifier and can display it ok on screen
57 if (contents.empty() || contents.length() < 2) {
58 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
63 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
64 contents = trim(contents);
65 if (!contains(contents, "{") && !contains(contents, "}")) {
66 if (contents.length() == 2) {
72 } else if (contents.length() == 3) {
80 } else if (contents.length() == 4 && contents[2] == ' ') {
88 } else if (contents.length() == 4 && contents[2] == '\\'
89 && (contents[3] == 'i' || contents[3] == 'j')) {
100 if (contents[0] != '\\') { // demand that first char is a '\\'
101 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
105 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
111 switch (contents[1]) { // second char should be one of these
113 modtype = ACUTE; // acute
114 plusasc = true; // at the top of character
117 modtype = GRAVE; // grave
118 plusasc = true; // at the top
121 modtype = MACRON; // macron
122 plusasc = true; // at the top
125 modtype = TILDE; // tilde
126 plusasc = true; // at the top
128 case 'b': // underbar
129 modtype = UNDERBAR; // underbar
130 plusdesc = true; // at the bottom
133 modtype = CEDILLA; // cedilla
134 plusdesc = true; // at the bottom
136 case 'd': // underdot
137 modtype = UNDERDOT; // underdot
138 plusdesc = true; // at the bottom
141 modtype = CIRCLE; // circle
142 plusasc = true; // at the top
145 modtype = TIE; // tie
146 plusasc = true; // at the top
149 modtype = BREVE; // breve
150 plusasc = true; // at the top
153 modtype = CARON; // caron
154 plusasc = true; // at the top
156 case 'q': // special caron
157 modtype = SPECIAL_CARON; // special caron
158 plusasc = true; // at the top
160 case 'H': // hungarian umlaut
161 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
162 plusasc = true; // at the top
165 modtype = UMLAUT; // umlaut
166 plusasc = true; // at the top
169 modtype = DOT; // dot
170 plusasc = true; // at the top
172 case '^': // circumflex
173 modtype = CIRCUMFLEX; // circumflex
174 plusasc = true; // at the top
177 modtype = OGONEK; // ogonek
180 case 'i': // dot-less-i
181 modtype = DOT_LESS_I; // dot-less-i
182 plusasc = true; // at the top (not really needed)
185 case 'j': // dot-less-j
186 modtype = DOT_LESS_J; // dot-less-j
187 plusasc = true; // at the top (not really needed)
192 plusasc = true; // at the top (not really needed)
196 plusasc = true; // at the top (not really needed)
199 lyxerr[Debug::KEY] << "Default" << endl;
200 // unknow accent (or something else)
204 // we demand that third char is a '{' (Lgb)
205 if (contents[2] != '{') return;
207 // special clause for \i{}, \j{} \l{} and \L{}
208 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
209 || modtype == lSLASH || modtype == LSLASH)
210 && contents[3] == '}') {
212 case DOT_LESS_I: ic = 'i'; break;
213 case DOT_LESS_J: ic = 'j'; break;
214 case lSLASH: ic = 'l'; break;
215 case LSLASH: ic = 'L'; break;
217 // if this happens something is really wrong
218 lyxerr << "InsetLaTexAccent: weird error." << endl;
221 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
222 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
224 << ", top: " << plusasc
225 << ", bot: " << plusdesc
226 << ", dot: " << remdot
227 << ", mod: " << modtype << endl;
228 // Special case for space
229 } else if (contents[3] == '}') {
235 ic = contents[3]; // i will always be 3 here
237 // ic should now be a alfa-char or '\\'
239 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
240 if (ic == 'i' || ic == 'j')
244 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
245 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
246 string temp = contents;
247 temp.erase(3, string::npos);
250 for (string::size_type j = 4;
251 j < contents.length(); ++j)
258 // demand a '}' at the end
259 if (contents[++i] != '}' && contents[++i]) return;
261 // fine, the char is properly decoded now (hopefully)
262 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
264 << ", top: " << plusasc
265 << ", bot: " << plusdesc
266 << ", dot: " << remdot
267 << ", mod: " << modtype << endl;
273 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
275 LyXFont & font = mi.base.font;
276 // This function is a bit too simplistic and is just a
277 // "try to make a fit for all accents" approach, to
278 // make it better we need to know what kind of accent is
279 // used and add to max based on that.
282 dim.asc = font_metrics::ascent('a', font);
284 dim.asc = font_metrics::ascent(ic, font);
286 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
289 dim.des = font_metrics::descent('a', font);
291 dim.des = font_metrics::descent(ic, font);
295 dim.wid = font_metrics::width(ic, font);
297 dim.asc = font_metrics::maxAscent(font) + 4;
298 dim.des = font_metrics::maxDescent(font) + 4;
299 dim.wid = font_metrics::width(contents, font) + 4;
305 int InsetLatexAccent::lbearing(LyXFont const & font) const
307 return font_metrics::lbearing(ic, font);
311 int InsetLatexAccent::rbearing(LyXFont const & font) const
313 return font_metrics::rbearing(ic, font);
317 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
319 unsigned char tmpic = ic;
324 if (ic == 'c') tmpic = 0xe7;
325 if (ic == 'C') tmpic = 0xc7;
326 if (ic == 's') tmpic = 0xfe;
327 if (ic == 'S') tmpic = 0xde;
331 { if (ic == 'g') tmpic = 0xf0;
332 if (ic == 'G') tmpic = 0xd0;
337 if (ic == 'o') tmpic = 0xf6;
338 if (ic == 'O') tmpic = 0xd6;
339 if (ic == 'u') tmpic = 0xfc;
340 if (ic == 'U') tmpic = 0xdc;
343 case DOT: if (ic == 'I') tmpic = 0xdd; break;
344 case DOT_LESS_I: tmpic = 0xfd; break;
345 default: return false;
348 pi.pain.text(x, y, char(tmpic), pi.base.font);
356 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
358 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
359 if (displayISO8859_9(pi, x, baseline))
363 // All the manually drawn accents in this function could use an
364 // overhaul. Different ways of drawing (what metrics to use)
365 // should also be considered.
367 LyXFont font = pi.base.font;
368 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
369 font.setLanguage(english_language);
372 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
377 hg = font_metrics::maxDescent(font);
378 y = baseline - dim_.asc;
380 if (font.shape() == LyXFont::ITALIC_SHAPE)
381 x2 += (4.0 * hg) / 5.0; // italic
388 float hg35 = float(hg * 3.0) / 5.0;
390 // display with proper accent mark
392 pi.pain.text(x, baseline, ic, font);
395 int tmpvar = baseline - font_metrics::ascent('i', font);
397 if (font.shape() == LyXFont::ITALIC_SHAPE)
398 tmpx += int(0.8 * hg); // italic
399 lyxerr[Debug::KEY] << "Removing dot." << endl;
400 // remove the dot first
401 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
402 font_metrics::ascent('i', pi.base.font) -
403 font_metrics::ascent('x', pi.base.font) - 1,
405 // the five lines below is a simple hack to
406 // make the display of accent 'i' and 'j'
407 // better. It makes the accent be written
408 // closer to the top of the dot-less 'i' or 'j'.
409 char tmpic = ic; // store the ic when we
410 ic = 'x'; // calculates the ascent of
412 int asc = ascent(); // 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,
497 pi.pain.arc(int(x2 - (hg / 2.0)), y,
498 int(hg), int(hg), 0, -360*32,
506 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
507 xp[1] = int(x2); yp[1] = int(y + hg);
508 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
509 pi.pain.lines(xp, yp, 3, LColor::foreground);
512 case SPECIAL_CARON: // special caron
515 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
516 case 't': y -= int(hg35 / 2.0); break;
519 xp[0] = int(x + dim_.wid);
520 yp[0] = int(y + hg35 + hg);
522 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
523 yp[1] = int(y + hg + (hg35 / 2.0));
525 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
528 pi.pain.lines(xp, yp, 3, LColor::foreground);
531 case HUNGARIAN_UMLAUT: // hung. umlaut
533 pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
534 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
536 pi.pain.text(int(x2),
537 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
541 case UMLAUT: // umlaut
543 pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
544 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
548 case CIRCUMFLEX: // circumflex
551 tmpf.decSize().decSize().decSize();
552 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
553 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
557 case OGONEK: // ogonek
559 // this does probably not look like an ogonek, so
560 // it should certainly be refined
567 yp[1] = y + int(hg35);
569 xp[2] = int(x2 - hg35);
570 yp[2] = y + int(hg / 2.0);
572 xp[3] = int(x2 + hg / 4.0);
575 pi.pain.lines(xp, yp, 4, LColor::foreground);
584 yp[0] = y + int(3.0 * hg);
586 xp[1] = int(x + float(dim_.wid) * 0.75);
589 pi.pain.lines(xp, yp, 2, LColor::foreground);
592 case DOT_LESS_I: // dotless-i
593 case DOT_LESS_J: // dotless-j
595 // nothing to do for these
600 pi.pain.fillRectangle(x + 1,
601 baseline - dim_.asc + 1, dim_.wid - 2,
602 dim_.asc + dim_.des - 2,
604 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
605 dim_.wid - 2, dim_.asc + dim_.des - 2,
607 pi.pain.text(x + 2, baseline, contents, font);
612 void InsetLatexAccent::write(Buffer const &, ostream & os) const
614 os << "\\i " << contents << "\n";
618 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
621 contents = lex.getString();
626 int InsetLatexAccent::latex(Buffer const &, ostream & os,
627 LatexRunParams const &) const
634 int InsetLatexAccent::ascii(Buffer const &, ostream & os, int) const
641 int InsetLatexAccent::linuxdoc(Buffer const &, ostream & os) const
648 int InsetLatexAccent::docbook(Buffer const &, ostream & os, bool) const
655 bool InsetLatexAccent::directWrite() const
661 auto_ptr<InsetBase> InsetLatexAccent::clone() const
663 return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
667 InsetOld::Code InsetLatexAccent::lyxCode() const
669 return InsetOld::ACCENT_CODE;
673 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)