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 lyx::support::contains;
27 using lyx::support::trim;
35 /* LatexAccent. Proper handling of accented characters */
36 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
37 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
39 InsetLatexAccent::InsetLatexAccent()
44 InsetLatexAccent::InsetLatexAccent(string const & str)
51 void InsetLatexAccent::checkContents()
52 // check, if we know the modifier and can display it ok on screen
56 if (contents.empty() || contents.length() < 2) {
57 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
62 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
63 contents = trim(contents);
64 if (!contains(contents, "{") && !contains(contents, "}")) {
65 if (contents.length() == 2) {
71 } else if (contents.length() == 3) {
79 } else if (contents.length() == 4 && contents[2] == ' ') {
87 } else if (contents.length() == 4 && contents[2] == '\\'
88 && (contents[3] == 'i' || contents[3] == 'j')) {
99 if (contents[0] != '\\') { // demand that first char is a '\\'
100 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
104 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
110 switch (contents[1]) { // second char should be one of these
112 modtype = ACUTE; // acute
113 plusasc = true; // at the top of character
116 modtype = GRAVE; // grave
117 plusasc = true; // at the top
120 modtype = MACRON; // macron
121 plusasc = true; // at the top
124 modtype = TILDE; // tilde
125 plusasc = true; // at the top
127 case 'b': // underbar
128 modtype = UNDERBAR; // underbar
129 plusdesc = true; // at the bottom
132 modtype = CEDILLA; // cedilla
133 plusdesc = true; // at the bottom
135 case 'd': // underdot
136 modtype = UNDERDOT; // underdot
137 plusdesc = true; // at the bottom
140 modtype = CIRCLE; // circle
141 plusasc = true; // at the top
144 modtype = TIE; // tie
145 plusasc = true; // at the top
148 modtype = BREVE; // breve
149 plusasc = true; // at the top
152 modtype = CARON; // caron
153 plusasc = true; // at the top
155 case 'q': // special caron
156 modtype = SPECIAL_CARON; // special caron
157 plusasc = true; // at the top
159 case 'H': // hungarian umlaut
160 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
161 plusasc = true; // at the top
164 modtype = UMLAUT; // umlaut
165 plusasc = true; // at the top
168 modtype = DOT; // dot
169 plusasc = true; // at the top
171 case '^': // circumflex
172 modtype = CIRCUMFLEX; // circumflex
173 plusasc = true; // at the top
176 modtype = OGONEK; // ogonek
179 case 'i': // dot-less-i
180 modtype = DOT_LESS_I; // dot-less-i
181 plusasc = true; // at the top (not really needed)
184 case 'j': // dot-less-j
185 modtype = DOT_LESS_J; // dot-less-j
186 plusasc = true; // at the top (not really needed)
191 plusasc = true; // at the top (not really needed)
195 plusasc = true; // at the top (not really needed)
198 lyxerr[Debug::KEY] << "Default" << endl;
199 // unknow accent (or something else)
203 // we demand that third char is a '{' (Lgb)
204 if (contents[2] != '{') return;
206 // special clause for \i{}, \j{} \l{} and \L{}
207 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
208 || modtype == lSLASH || modtype == LSLASH)
209 && contents[3] == '}') {
211 case DOT_LESS_I: ic = 'i'; break;
212 case DOT_LESS_J: ic = 'j'; break;
213 case lSLASH: ic = 'l'; break;
214 case LSLASH: ic = 'L'; break;
216 // if this happens something is really wrong
217 lyxerr << "InsetLaTexAccent: weird error." << endl;
220 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
221 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
223 << ", top: " << plusasc
224 << ", bot: " << plusdesc
225 << ", dot: " << remdot
226 << ", mod: " << modtype << endl;
227 // Special case for space
228 } else if (contents[3] == '}') {
234 ic = contents[3]; // i will always be 3 here
236 // ic should now be a alfa-char or '\\'
238 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
239 if (ic == 'i' || ic == 'j')
243 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
244 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
245 string temp = contents;
246 temp.erase(3, string::npos);
249 for (string::size_type j = 4;
250 j < contents.length(); ++j)
257 // demand a '}' at the end
258 if (contents[++i] != '}' && contents[++i]) return;
260 // fine, the char is properly decoded now (hopefully)
261 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
263 << ", top: " << plusasc
264 << ", bot: " << plusdesc
265 << ", dot: " << remdot
266 << ", mod: " << modtype << endl;
272 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
274 LyXFont & font = mi.base.font;
275 // This function is a bit too simplistic and is just a
276 // "try to make a fit for all accents" approach, to
277 // make it better we need to know what kind of accent is
278 // used and add to max based on that.
281 dim.asc = font_metrics::ascent('a', font);
283 dim.asc = font_metrics::ascent(ic, font);
285 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
288 dim.des = font_metrics::descent('a', font);
290 dim.des = font_metrics::descent(ic, font);
294 dim.wid = font_metrics::width(ic, font);
296 dim.asc = font_metrics::maxAscent(font) + 4;
297 dim.des = font_metrics::maxDescent(font) + 4;
298 dim.wid = font_metrics::width(contents, font) + 4;
304 int InsetLatexAccent::lbearing(LyXFont const & font) const
306 return font_metrics::lbearing(ic, font);
310 int InsetLatexAccent::rbearing(LyXFont const & font) const
312 return font_metrics::rbearing(ic, font);
316 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
318 unsigned char tmpic = ic;
323 if (ic == 'c') tmpic = 0xe7;
324 if (ic == 'C') tmpic = 0xc7;
325 if (ic == 's') tmpic = 0xfe;
326 if (ic == 'S') tmpic = 0xde;
330 { if (ic == 'g') tmpic = 0xf0;
331 if (ic == 'G') tmpic = 0xd0;
336 if (ic == 'o') tmpic = 0xf6;
337 if (ic == 'O') tmpic = 0xd6;
338 if (ic == 'u') tmpic = 0xfc;
339 if (ic == 'U') tmpic = 0xdc;
342 case DOT: if (ic == 'I') tmpic = 0xdd; break;
343 case DOT_LESS_I: tmpic = 0xfd; break;
344 default: return false;
347 pi.pain.text(x, y, char(tmpic), pi.base.font);
355 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
357 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
358 if (displayISO8859_9(pi, x, baseline))
362 // All the manually drawn accents in this function could use an
363 // overhaul. Different ways of drawing (what metrics to use)
364 // should also be considered.
366 LyXFont font = pi.base.font;
367 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
368 font.setLanguage(english_language);
371 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
376 hg = font_metrics::maxDescent(font);
377 y = baseline - dim_.asc;
379 if (font.shape() == LyXFont::ITALIC_SHAPE)
380 x2 += (4.0 * hg) / 5.0; // italic
387 float hg35 = float(hg * 3.0) / 5.0;
389 // display with proper accent mark
391 pi.pain.text(x, baseline, ic, font);
394 int tmpvar = baseline - font_metrics::ascent('i', font);
396 if (font.shape() == LyXFont::ITALIC_SHAPE)
397 tmpx += int(0.8 * hg); // italic
398 lyxerr[Debug::KEY] << "Removing dot." << endl;
399 // remove the dot first
400 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
401 font_metrics::ascent('i', pi.base.font) -
402 font_metrics::ascent('x', pi.base.font) - 1,
404 // the five lines below is a simple hack to
405 // make the display of accent 'i' and 'j'
406 // better. It makes the accent be written
407 // closer to the top of the dot-less 'i' or 'j'.
408 char tmpic = ic; // store the ic when we
409 ic = 'x'; // calculates the ascent of
411 int asc = ascent(); // the dot-less version (here: 'x')
412 ic = tmpic; // set the orig ic back
413 y = baseline - asc; // update to new y coord.
415 // now the rest - draw within (x, y, x+wid, y+hg)
417 case ACUTE: // acute 0xB4
419 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
420 - font_metrics::lbearing(0xB4, font)) / 2),
421 baseline - font_metrics::ascent(ic, font)
422 - font_metrics::descent(0xB4, font)
423 - (font_metrics::ascent(0xB4, font)
424 + font_metrics::descent(0xB4, font)) / 2,
428 case GRAVE: // grave 0x60
430 pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
431 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
435 case MACRON: // macron
437 pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
438 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
444 pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
445 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
449 case UNDERBAR: // underbar 0x5F
451 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
455 case CEDILLA: // cedilla
457 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
462 case UNDERDOT: // underdot
464 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
465 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
472 pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
473 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
478 case CIRCLE: // circle
481 tmpf.decSize().decSize();
482 pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
483 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
489 pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
490 int(2 * hg), int(hg), 0, 360 * 32);
495 pi.pain.arc(int(x2 - (hg / 2.0)), y,
496 int(hg), int(hg), 0, -360*32);
503 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
504 xp[1] = int(x2); yp[1] = int(y + hg);
505 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
506 pi.pain.lines(xp, yp, 3);
509 case SPECIAL_CARON: // special caron
512 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
513 case 't': y -= int(hg35 / 2.0); break;
516 xp[0] = int(x + dim_.wid);
517 yp[0] = int(y + hg35 + hg);
519 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
520 yp[1] = int(y + hg + (hg35 / 2.0));
522 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
525 pi.pain.lines(xp, yp, 3);
528 case HUNGARIAN_UMLAUT: // hung. umlaut
530 pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
531 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
533 pi.pain.text(int(x2),
534 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
538 case UMLAUT: // umlaut
540 pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
541 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
545 case CIRCUMFLEX: // circumflex
548 tmpf.decSize().decSize().decSize();
549 pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
550 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
554 case OGONEK: // ogonek
556 // this does probably not look like an ogonek, so
557 // it should certainly be refined
564 yp[1] = y + int(hg35);
566 xp[2] = int(x2 - hg35);
567 yp[2] = y + int(hg / 2.0);
569 xp[3] = int(x2 + hg / 4.0);
572 pi.pain.lines(xp, yp, 4);
581 yp[0] = y + int(3.0 * hg);
583 xp[1] = int(x + float(dim_.wid) * 0.75);
586 pi.pain.lines(xp, yp, 2);
589 case DOT_LESS_I: // dotless-i
590 case DOT_LESS_J: // dotless-j
592 // nothing to do for these
597 pi.pain.fillRectangle(x + 1,
598 baseline - dim_.asc + 1, dim_.wid - 2,
599 dim_.asc + dim_.des - 2, backgroundColor());
600 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
601 dim_.wid - 2, dim_.asc + dim_.des - 2);
602 pi.pain.text(x + 2, baseline, contents, font);
607 void InsetLatexAccent::write(Buffer const &, ostream & os) const
609 os << "\\i " << contents << "\n";
613 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
616 contents = lex.getString();
621 int InsetLatexAccent::latex(Buffer const &, ostream & os,
622 LatexRunParams const &) const
629 int InsetLatexAccent::ascii(Buffer const &, ostream & os, int) const
636 int InsetLatexAccent::linuxdoc(Buffer const &, ostream & os) const
643 int InsetLatexAccent::docbook(Buffer const &, ostream & os, bool) const
650 bool InsetLatexAccent::directWrite() const
656 auto_ptr<InsetBase> InsetLatexAccent::clone() const
658 return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
662 InsetOld::Code InsetLatexAccent::lyxCode() const
664 return InsetOld::ACCENT_CODE;
668 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)