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 auto_ptr<InsetBase> InsetLatexAccent::doClone() const
54 return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
58 void InsetLatexAccent::checkContents()
59 // check, if we know the modifier and can display it ok on screen
63 if (contents.empty() || contents.length() < 2) {
64 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
68 contents = trim(contents);
69 if (contents[0] != '\\') { // demand that first char is a '\\'
70 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
74 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
80 switch (contents[1]) { // second char should be one of these
82 modtype = ACUTE; // acute
83 plusasc = true; // at the top of character
86 modtype = GRAVE; // grave
87 plusasc = true; // at the top
90 modtype = MACRON; // macron
91 plusasc = true; // at the top
94 modtype = TILDE; // tilde
95 plusasc = true; // at the top
98 modtype = UNDERBAR; // underbar
99 plusdesc = true; // at the bottom
102 modtype = CEDILLA; // cedilla
103 plusdesc = true; // at the bottom
105 case 'd': // underdot
106 modtype = UNDERDOT; // underdot
107 plusdesc = true; // at the bottom
110 modtype = CIRCLE; // circle
111 plusasc = true; // at the top
114 modtype = TIE; // tie
115 plusasc = true; // at the top
118 modtype = BREVE; // breve
119 plusasc = true; // at the top
122 modtype = CARON; // caron
123 plusasc = true; // at the top
125 case 'q': // special caron
126 modtype = SPECIAL_CARON; // special caron
127 plusasc = true; // at the top
129 case 'H': // hungarian umlaut
130 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
131 plusasc = true; // at the top
134 modtype = UMLAUT; // umlaut
135 plusasc = true; // at the top
138 modtype = DOT; // dot
139 plusasc = true; // at the top
141 case '^': // circumflex
142 modtype = CIRCUMFLEX; // circumflex
143 plusasc = true; // at the top
146 modtype = OGONEK; // ogonek
149 case 'i': // dot-less-i
150 modtype = DOT_LESS_I; // dot-less-i
151 plusasc = true; // at the top (not really needed)
154 case 'j': // dot-less-j
155 modtype = DOT_LESS_J; // dot-less-j
156 plusasc = true; // at the top (not really needed)
161 plusasc = true; // at the top (not really needed)
165 plusasc = true; // at the top (not really needed)
168 lyxerr[Debug::KEY] << "Default" << endl;
169 // unknow accent (or something else)
173 // we demand that third char is a '{' (Lgb)
174 if (contents[2] != '{') return;
176 // special clause for \i{}, \j{} \l{} and \L{}
177 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
178 || modtype == lSLASH || modtype == LSLASH)
179 && contents[3] == '}') {
181 case DOT_LESS_I: ic = 'i'; break;
182 case DOT_LESS_J: ic = 'j'; break;
183 case lSLASH: ic = 'l'; break;
184 case LSLASH: ic = 'L'; break;
186 // if this happens something is really wrong
187 lyxerr << "InsetLaTexAccent: weird error." << endl;
190 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
191 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
193 << ", top: " << plusasc
194 << ", bot: " << plusdesc
195 << ", dot: " << remdot
196 << ", mod: " << modtype << endl;
197 // Special case for space
198 } else if (contents[3] == '}') {
204 ic = contents[3]; // i will always be 3 here
206 // ic should now be a alfa-char or '\\'
208 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
209 if (ic == 'i' || ic == 'j')
213 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
214 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
215 string temp = contents;
216 temp.erase(3, string::npos);
219 for (string::size_type j = 4;
220 j < contents.length(); ++j)
227 // demand a '}' at the end
228 if (contents[++i] != '}' && contents[++i]) return;
230 // fine, the char is properly decoded now (hopefully)
231 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
233 << ", top: " << plusasc
234 << ", bot: " << plusdesc
235 << ", dot: " << remdot
236 << ", mod: " << modtype << endl;
242 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
244 LyXFont & font = mi.base.font;
245 // This function is a bit too simplistic and is just a
246 // "try to make a fit for all accents" approach, to
247 // make it better we need to know what kind of accent is
248 // used and add to max based on that.
251 dim.asc = font_metrics::ascent('a', font);
253 dim.asc = font_metrics::ascent(ic, font);
255 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
258 dim.des = font_metrics::descent('a', font);
260 dim.des = font_metrics::descent(ic, font);
264 dim.wid = font_metrics::width(ic, font);
266 dim.asc = font_metrics::maxAscent(font) + 4;
267 dim.des = font_metrics::maxDescent(font) + 4;
268 dim.wid = font_metrics::width(contents, font) + 4;
274 int InsetLatexAccent::lbearing(LyXFont const & font) const
276 return font_metrics::lbearing(ic, font);
280 int InsetLatexAccent::rbearing(LyXFont const & font) const
282 return font_metrics::rbearing(ic, font);
286 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
288 unsigned char tmpic = ic;
293 if (ic == 'c') tmpic = 0xe7;
294 if (ic == 'C') tmpic = 0xc7;
295 if (ic == 's') tmpic = 0xfe;
296 if (ic == 'S') tmpic = 0xde;
301 if (ic == 'g') tmpic = 0xf0;
302 if (ic == 'G') tmpic = 0xd0;
307 if (ic == 'o') tmpic = 0xf6;
308 if (ic == 'O') tmpic = 0xd6;
309 if (ic == 'u') tmpic = 0xfc;
310 if (ic == 'U') tmpic = 0xdc;
315 if (ic == 'I') tmpic = 0xdd;
329 pi.pain.text(x, y, char(tmpic), pi.base.font);
334 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
337 LyXFont const & font = pi.base.font;
338 x -= font_metrics::center(accent, font);
339 y -= font_metrics::ascent(ic, font);
340 y -= font_metrics::descent(accent, font);
341 y -= font_metrics::height(accent, font) / 2;
342 pi.pain.text(x, y, accent, font);
346 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
348 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
349 if (displayISO8859_9(pi, x, baseline))
352 // All the manually drawn accents in this function could use an
353 // overhaul. Different ways of drawing (what metrics to use)
354 // should also be considered.
356 LyXFont font = pi.base.font;
357 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
358 font.setLanguage(english_language);
361 int x2 = int(x + (rbearing(font) - lbearing(font)) / 2);
366 hg = font_metrics::maxDescent(font);
367 y = baseline - dim_.asc;
368 if (font.shape() == LyXFont::ITALIC_SHAPE)
369 x2 += int(0.8 * hg); // italic
376 double hg35 = hg * 0.6;
378 // display with proper accent mark
380 pi.pain.text(x, baseline, ic, font);
383 int tmpvar = baseline - font_metrics::ascent('i', font);
385 if (font.shape() == LyXFont::ITALIC_SHAPE)
386 tmpx += int(0.8 * hg); // italic
387 lyxerr[Debug::KEY] << "Removing dot." << endl;
388 // remove the dot first
389 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
390 font_metrics::ascent('i', pi.base.font) -
391 font_metrics::ascent('x', pi.base.font) - 1,
393 // the five lines below is a simple hack to
394 // make the display of accent 'i' and 'j'
395 // better. It makes the accent be written
396 // closer to the top of the dot-less 'i' or 'j'.
397 char tmpic = ic; // store the ic when we
398 ic = 'x'; // calculates the ascent of
402 int asc = ascent(); // the dot-less version (here: 'x')
403 ic = tmpic; // set the orig ic back
404 y = baseline - asc; // update to new y coord.
407 // now the rest - draw within (x, y, x + wid, y + hg)
410 drawAccent(pi, x2, baseline, char(0xB4));
414 drawAccent(pi, x2, baseline, char(0x60));
418 drawAccent(pi, x2, baseline, char(0xAF));
422 drawAccent(pi, x2, baseline, '~');
426 char const underbar(0x5F);
427 pi.pain.text(x2 - font_metrics::center(underbar, font),
428 baseline, underbar, font);
433 char const cedilla(0xB8);
434 pi.pain.text(x2 - font_metrics::center(cedilla, font),
435 baseline, cedilla, font);
440 pi.pain.text(x2 - font_metrics::center('.', font),
441 int(baseline + 1.5 * font_metrics::height('.', font)),
446 drawAccent(pi, x2, baseline, '.');
450 drawAccent(pi, x2, baseline, char(0xB0));
454 pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
459 pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
465 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
466 xp[1] = int(x2); yp[1] = int(y + hg);
467 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
468 pi.pain.lines(xp, yp, 3, LColor::foreground);
472 case SPECIAL_CARON: {
474 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
475 case 't': y -= int(hg35 / 2.0); break;
478 xp[0] = int(x + dim_.wid);
479 yp[0] = int(y + hg35 + hg);
481 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
482 yp[1] = int(y + hg + (hg35 / 2.0));
484 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
487 pi.pain.lines(xp, yp, 3, LColor::foreground);
491 case HUNGARIAN_UMLAUT:
492 drawAccent(pi, x2 - font_metrics::center('´', font), baseline, '´');
493 drawAccent(pi, x2 + font_metrics::center('´', font), baseline, '´');
497 drawAccent(pi, x2, baseline, '"');
501 drawAccent(pi, x2, baseline, 0x5E);
505 // this does probably not look like an ogonek, so
506 // it should certainly be refined
513 yp[1] = y + int(hg35);
515 xp[2] = int(x2 - hg35);
521 pi.pain.lines(xp, yp, 4, LColor::foreground);
530 yp[0] = y + int(3 * hg);
532 xp[1] = int(x + dim_.wid * 0.75);
535 pi.pain.lines(xp, yp, 2, LColor::foreground);
539 case DOT_LESS_I: // dotless-i
540 case DOT_LESS_J: // dotless-j
541 // nothing to do for these
546 pi.pain.fillRectangle(x + 1,
547 baseline - dim_.asc + 1, dim_.wid - 2,
548 dim_.asc + dim_.des - 2,
550 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
551 dim_.wid - 2, dim_.asc + dim_.des - 2,
553 pi.pain.text(x + 2, baseline, contents, font);
558 void InsetLatexAccent::write(Buffer const &, ostream & os) const
560 os << "\\i " << contents << "\n";
564 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
567 contents = lex.getString();
572 int InsetLatexAccent::latex(Buffer const &, ostream & os,
573 OutputParams const &) const
580 int InsetLatexAccent::plaintext(Buffer const &, ostream & os,
581 OutputParams const &) const
588 int InsetLatexAccent::linuxdoc(Buffer const &, ostream & os,
589 OutputParams const &) const
596 int InsetLatexAccent::docbook(Buffer const &, ostream & os,
597 OutputParams const &) const
604 bool InsetLatexAccent::directWrite() const
610 InsetBase::Code InsetLatexAccent::lyxCode() const
612 return InsetBase::ACCENT_CODE;
616 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)