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"
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::dimension(BufferView *, LyXFont const & font,
267 Dimension & dim) const
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.a = font_metrics::ascent('a', font);
277 dim.a = font_metrics::ascent(ic, font);
279 dim.a += (font_metrics::maxAscent(font) + 3) / 3;
282 dim.d = font_metrics::descent('a', font);
284 dim.d = font_metrics::descent(ic, font);
288 dim.w = font_metrics::width(ic, font);
290 dim.a = font_metrics::maxAscent(font) + 4;
291 dim.d = font_metrics::maxDescent(font) + 4;
292 dim.w = font_metrics::width(contents, font) + 4;
297 int InsetLatexAccent::lbearing(LyXFont const & font) const
299 return font_metrics::lbearing(ic, font);
303 int InsetLatexAccent::rbearing(LyXFont const & font) const
305 return font_metrics::rbearing(ic, font);
309 bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
313 unsigned char tmpic = ic;
318 if (ic == 'c') tmpic = 0xe7;
319 if (ic == 'C') tmpic = 0xc7;
320 if (ic == 's') tmpic = 0xfe;
321 if (ic == 'S') tmpic = 0xde;
325 { if (ic == 'g') tmpic = 0xf0;
326 if (ic == 'G') tmpic = 0xd0;
331 if (ic == 'o') tmpic = 0xf6;
332 if (ic == 'O') tmpic = 0xd6;
333 if (ic == 'u') tmpic = 0xfc;
334 if (ic == 'U') tmpic = 0xdc;
337 case DOT: if (ic == 'I') tmpic = 0xdd; break;
338 case DOT_LESS_I: tmpic = 0xfd; break;
339 default: return false;
342 char ch = char(tmpic);
343 bv->painter().text(int(x), baseline, ch, font);
344 x += width(bv, font);
352 void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
353 int baseline, float & x) const
355 Painter & pain = bv->painter();
357 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
358 if (displayISO8859_9(bv, font0, baseline, x))
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.
367 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
368 font.setLanguage(english_language);
371 int asc = ascent(bv, font);
372 int desc = descent(bv, font);
373 int wid = width(bv, font);
374 float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
379 hg = font_metrics::maxDescent(font);
382 if (font.shape() == LyXFont::ITALIC_SHAPE)
383 x2 += (4.0 * hg) / 5.0; // italic
390 float hg35 = float(hg * 3.0) / 5.0;
392 // display with proper accent mark
394 pain.text(int(x), baseline, ic, font);
397 int tmpvar = baseline - font_metrics::ascent('i', font);
399 if (font.shape() == LyXFont::ITALIC_SHAPE)
400 tmpx += (8.0 * hg) / 10.0; // italic
401 lyxerr[Debug::KEY] << "Removing dot." << endl;
402 // remove the dot first
403 pain.fillRectangle(int(x + tmpx), tmpvar, wid,
404 font_metrics::ascent('i', font) -
405 font_metrics::ascent('x', font) - 1,
407 // the five lines below is a simple hack to
408 // make the display of accent 'i' and 'j'
409 // better. It makes the accent be written
410 // closer to the top of the dot-less 'i' or 'j'.
411 char tmpic = ic; // store the ic when we
412 ic = 'x'; // calculates the ascent of
413 asc = ascent(bv, font); // the dot-less version (here: 'x')
414 ic = tmpic; // set the orig ic back
415 y = baseline - asc; // update to new y coord.
417 // now the rest - draw within (x, y, x+wid, y+hg)
419 case ACUTE: // acute 0xB4
421 pain.text(int(x2 - (font_metrics::rbearing(0xB4, font) - font_metrics::lbearing(0xB4, font)) / 2),
422 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB4, font) - (font_metrics::ascent(0xB4, font) + font_metrics::descent(0xB4, font)) / 2,
426 case GRAVE: // grave 0x60
428 pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
429 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
433 case MACRON: // macron
435 pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
436 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
442 pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
443 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
447 case UNDERBAR: // underbar 0x5F
449 pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
453 case CEDILLA: // cedilla
455 pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
460 case UNDERDOT: // underdot
462 pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
463 int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
470 pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
471 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
476 case CIRCLE: // circle
479 tmpf.decSize().decSize();
480 pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
481 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
487 pain.arc(int(x2 + hg35), int(y + hg / 2.0),
488 int(2 * hg), int(hg), 0, 360 * 32);
493 pain.arc(int(x2 - (hg / 2.0)), y,
494 int(hg), int(hg), 0, -360*32);
501 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
502 xp[1] = int(x2); yp[1] = int(y + hg);
503 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
504 pain.lines(xp, yp, 3);
507 case SPECIAL_CARON: // special caron
510 case 'L': wid = int(4.0 * wid / 5.0); break;
511 case 't': y -= int(hg35 / 2.0); break;
514 xp[0] = int(x + wid);
515 yp[0] = int(y + hg35 + hg);
517 xp[1] = int(x + wid + (hg35 / 2.0));
518 yp[1] = int(y + hg + (hg35 / 2.0));
520 xp[2] = int(x + wid + (hg35 / 2.0));
523 pain.lines(xp, yp, 3);
526 case HUNGARIAN_UMLAUT: // hung. umlaut
528 pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
529 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
532 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
536 case UMLAUT: // umlaut
538 pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
539 baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
543 case CIRCUMFLEX: // circumflex
546 tmpf.decSize().decSize().decSize();
547 pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
548 int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
552 case OGONEK: // ogonek
554 // this does probably not look like an ogonek, so
555 // it should certainly be refined
562 yp[1] = y + int(hg35);
564 xp[2] = int(x2 - hg35);
565 yp[2] = y + int(hg / 2.0);
567 xp[3] = int(x2 + hg / 4.0);
570 pain.lines(xp, yp, 4);
579 yp[0] = y + int(3.0 * hg);
581 xp[1] = int(x + float(wid) * 0.75);
584 pain.lines(xp, yp, 2);
587 case DOT_LESS_I: // dotless-i
588 case DOT_LESS_J: // dotless-j
590 // nothing to do for these
595 pain.fillRectangle(int(x + 1),
596 baseline - ascent(bv, font) + 1,
599 + descent(bv, font) - 2,
601 pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
603 ascent(bv, font) + descent(bv, font) - 2);
604 pain.text(int(x + 2), baseline, contents, font);
606 x += width(bv, font);
610 void InsetLatexAccent::write(Buffer const *, ostream & os) const
612 os << "\\i " << contents << "\n";
616 void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
619 contents = lex.getString();
624 int InsetLatexAccent::latex(Buffer const *, ostream & os,
625 LatexRunParams const &) const
632 int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
639 int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
646 int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
653 bool InsetLatexAccent::directWrite() const
659 Inset * InsetLatexAccent::clone(Buffer const &) const
661 return new InsetLatexAccent(contents);
665 // Inset * InsetLatexAccent::clone(Buffer const &, bool) const
667 // return new InsetLatexAccent(contents);
671 Inset::Code InsetLatexAccent::lyxCode() const
673 return Inset::ACCENT_CODE;
677 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)