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"
29 using lyx::support::contains;
30 using lyx::support::trim;
38 /* LatexAccent. Proper handling of accented characters */
39 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
40 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
42 InsetLatexAccent::InsetLatexAccent()
47 InsetLatexAccent::InsetLatexAccent(string const & str)
54 auto_ptr<InsetBase> InsetLatexAccent::doClone() const
56 return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
60 void InsetLatexAccent::checkContents()
61 // check, if we know the modifier and can display it ok on screen
65 if (contents.empty() || contents.length() < 2) {
66 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
70 contents = trim(contents);
71 if (contents[0] != '\\') { // demand that first char is a '\\'
72 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
76 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
82 switch (contents[1]) { // second char should be one of these
84 modtype = ACUTE; // acute
85 plusasc = true; // at the top of character
88 modtype = GRAVE; // grave
89 plusasc = true; // at the top
92 modtype = MACRON; // macron
93 plusasc = true; // at the top
96 modtype = TILDE; // tilde
97 plusasc = true; // at the top
100 modtype = UNDERBAR; // underbar
101 plusdesc = true; // at the bottom
104 modtype = CEDILLA; // cedilla
105 plusdesc = true; // at the bottom
107 case 'd': // underdot
108 modtype = UNDERDOT; // underdot
109 plusdesc = true; // at the bottom
112 modtype = CIRCLE; // circle
113 plusasc = true; // at the top
116 modtype = TIE; // tie
117 plusasc = true; // at the top
120 modtype = BREVE; // breve
121 plusasc = true; // at the top
124 modtype = CARON; // caron
125 plusasc = true; // at the top
127 case 'q': // special caron
128 modtype = SPECIAL_CARON; // special caron
129 plusasc = true; // at the top
131 case 'H': // hungarian umlaut
132 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
133 plusasc = true; // at the top
136 modtype = UMLAUT; // umlaut
137 plusasc = true; // at the top
140 modtype = DOT; // dot
141 plusasc = true; // at the top
143 case '^': // circumflex
144 modtype = CIRCUMFLEX; // circumflex
145 plusasc = true; // at the top
148 modtype = OGONEK; // ogonek
151 case 'i': // dot-less-i
152 modtype = DOT_LESS_I; // dot-less-i
153 plusasc = true; // at the top (not really needed)
156 case 'j': // dot-less-j
157 modtype = DOT_LESS_J; // dot-less-j
158 plusasc = true; // at the top (not really needed)
163 plusasc = true; // at the top (not really needed)
167 plusasc = true; // at the top (not really needed)
170 lyxerr[Debug::KEY] << "Default" << endl;
171 // unknown accent (or something else)
175 // we demand that third char is a '{' (Lgb)
176 if (contents[2] != '{') return;
178 // special clause for \i{}, \j{} \l{} and \L{}
179 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
180 || modtype == lSLASH || modtype == LSLASH)
181 && contents[3] == '}') {
183 case DOT_LESS_I: ic = 'i'; break;
184 case DOT_LESS_J: ic = 'j'; break;
185 case lSLASH: ic = 'l'; break;
186 case LSLASH: ic = 'L'; break;
188 // if this happens something is really wrong
189 lyxerr << "InsetLaTexAccent: weird error." << endl;
192 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
193 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
195 << ", top: " << plusasc
196 << ", bot: " << plusdesc
197 << ", dot: " << remdot
198 << ", mod: " << modtype << endl;
199 // Special case for space
200 } else if (contents[3] == '}') {
206 ic = contents[3]; // i will always be 3 here
208 // ic should now be a alfa-char or '\\'
210 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
211 if (ic == 'i' || ic == 'j')
215 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
216 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
217 string temp = contents;
218 temp.erase(3, string::npos);
221 for (string::size_type j = 4;
222 j < contents.length(); ++j)
229 // demand a '}' at the end
230 if (contents[++i] != '}' && contents[++i]) return;
232 // fine, the char is properly decoded now (hopefully)
233 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
235 << ", top: " << plusasc
236 << ", bot: " << plusdesc
237 << ", dot: " << remdot
238 << ", mod: " << modtype << endl;
244 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
246 LyXFont & font = mi.base.font;
247 // This function is a bit too simplistic and is just a
248 // "try to make a fit for all accents" approach, to
249 // make it better we need to know what kind of accent is
250 // used and add to max based on that.
253 dim.asc = font_metrics::ascent('a', font);
255 dim.asc = font_metrics::ascent(ic, font);
257 dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
260 dim.des = font_metrics::descent('a', font);
262 dim.des = font_metrics::descent(ic, font);
266 dim.wid = font_metrics::width(ic, font);
268 dim.asc = font_metrics::maxAscent(font) + 4;
269 dim.des = font_metrics::maxDescent(font) + 4;
270 docstring dcon(contents.begin(), contents.end());
271 dim.wid = font_metrics::width(dcon, font) + 4;
277 int InsetLatexAccent::lbearing(LyXFont const & font) const
279 return font_metrics::lbearing(ic, font);
283 int InsetLatexAccent::rbearing(LyXFont const & font) const
285 return font_metrics::rbearing(ic, font);
289 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
291 unsigned char tmpic = ic;
296 if (ic == 'c') tmpic = '\xe7';
297 if (ic == 'C') tmpic = '\xc7';
298 if (ic == 's') tmpic = '\xfe';
299 if (ic == 'S') tmpic = '\xde';
304 if (ic == 'g') tmpic = '\xf0';
305 if (ic == 'G') tmpic = '\xd0';
310 if (ic == 'o') tmpic = '\xf6';
311 if (ic == 'O') tmpic = '\xd6';
312 if (ic == 'u') tmpic = '\xfc';
313 if (ic == 'U') tmpic = '\xdc';
318 if (ic == 'I') tmpic = '\xdd';
332 pi.pain.text(x, y, char(tmpic), pi.base.font);
337 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
338 char_type accent) const
340 LyXFont const & font = pi.base.font;
341 x -= font_metrics::center(accent, font);
342 y -= font_metrics::ascent(ic, font);
343 y -= font_metrics::descent(accent, font);
344 y -= font_metrics::height(accent, font) / 2;
345 pi.pain.text(x, y, accent, 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))
355 // All the manually drawn accents in this function could use an
356 // overhaul. Different ways of drawing (what metrics to use)
357 // should also be considered.
359 LyXFont font = pi.base.font;
360 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
361 font.setLanguage(english_language);
364 int x2 = int(x + (rbearing(font) - lbearing(font)) / 2);
369 hg = font_metrics::maxDescent(font);
370 y = baseline - dim_.asc;
371 if (font.shape() == LyXFont::ITALIC_SHAPE)
372 x2 += int(0.8 * hg); // italic
379 double hg35 = hg * 0.6;
381 // display with proper accent mark
383 pi.pain.text(x, baseline, ic, font);
386 int tmpvar = baseline - font_metrics::ascent('i', font);
388 if (font.shape() == LyXFont::ITALIC_SHAPE)
389 tmpx += int(0.8 * hg); // italic
390 lyxerr[Debug::KEY] << "Removing dot." << endl;
391 // remove the dot first
392 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
393 font_metrics::ascent('i', pi.base.font) -
394 font_metrics::ascent('x', pi.base.font) - 1,
396 // the five lines below is a simple hack to
397 // make the display of accent 'i' and 'j'
398 // better. It makes the accent be written
399 // closer to the top of the dot-less 'i' or 'j'.
400 char tmpic = ic; // store the ic when we
401 ic = 'x'; // calculates the ascent of
405 int asc = ascent(); // the dot-less version (here: 'x')
406 ic = tmpic; // set the orig ic back
407 y = baseline - asc; // update to new y coord.
410 // now the rest - draw within (x, y, x + wid, y + hg)
413 //drawAccent(pi, x2, baseline, '\xB4');
414 drawAccent(pi, x2, baseline, 0xB4);
418 //drawAccent(pi, x2, baseline, '\x60');
419 drawAccent(pi, x2, baseline, 0x60);
423 //drawAccent(pi, x2, baseline, '\xAF');
424 drawAccent(pi, x2, baseline, 0xAF);
428 drawAccent(pi, x2, baseline, '~');
432 char_type const underbar = 0x5F; //('\x5F');
433 pi.pain.text(x2 - font_metrics::center(underbar, font),
434 baseline, underbar, font);
439 char_type const cedilla = 0xB8; //('\xB8');
440 pi.pain.text(x2 - font_metrics::center(cedilla, font),
441 baseline, cedilla, font);
446 pi.pain.text(x2 - font_metrics::center('.', font),
447 int(baseline + 1.5 * font_metrics::height('.', font)),
452 drawAccent(pi, x2, baseline, '.');
456 //drawAccent(pi, x2, baseline, '\xB0');
457 drawAccent(pi, x2, baseline, 0xB0);
461 pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
466 pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
472 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
473 xp[1] = int(x2); yp[1] = int(y + hg);
474 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
475 pi.pain.lines(xp, yp, 3, LColor::foreground);
479 case SPECIAL_CARON: {
481 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
482 case 't': y -= int(hg35 / 2.0); break;
485 xp[0] = int(x + dim_.wid);
486 yp[0] = int(y + hg35 + hg);
488 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
489 yp[1] = int(y + hg + (hg35 / 2.0));
491 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
494 pi.pain.lines(xp, yp, 3, LColor::foreground);
498 case HUNGARIAN_UMLAUT:
499 drawAccent(pi, x2 - font_metrics::center('´', font), baseline, '´');
500 drawAccent(pi, x2 + font_metrics::center('´', font), baseline, '´');
504 drawAccent(pi, x2, baseline, '"');
508 drawAccent(pi, x2, baseline, '\x5E');
512 // this does probably not look like an ogonek, so
513 // it should certainly be refined
520 yp[1] = y + int(hg35);
522 xp[2] = int(x2 - hg35);
528 pi.pain.lines(xp, yp, 4, LColor::foreground);
537 yp[0] = y + int(3 * hg);
539 xp[1] = int(x + dim_.wid * 0.75);
542 pi.pain.lines(xp, yp, 2, LColor::foreground);
546 case DOT_LESS_I: // dotless-i
547 case DOT_LESS_J: // dotless-j
548 // nothing to do for these
553 pi.pain.fillRectangle(x + 1,
554 baseline - dim_.asc + 1, dim_.wid - 2,
555 dim_.asc + dim_.des - 2,
557 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
558 dim_.wid - 2, dim_.asc + dim_.des - 2,
560 docstring dcon(contents.begin(), contents.end());
561 pi.pain.text(x + 2, baseline, dcon, font);
566 void InsetLatexAccent::write(Buffer const &, ostream & os) const
568 os << "\\i " << contents << "\n";
572 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
575 contents = lex.getString();
580 int InsetLatexAccent::latex(Buffer const &, ostream & os,
581 OutputParams const &) const
588 int InsetLatexAccent::plaintext(Buffer const &, ostream & os,
589 OutputParams const &) const
596 int InsetLatexAccent::docbook(Buffer const &, ostream & os,
597 OutputParams const &) const
604 int InsetLatexAccent::textString(Buffer const & buf, ostream & os,
605 OutputParams const & op) const
607 return plaintext(buf, os, op);
611 bool InsetLatexAccent::directWrite() const
617 InsetBase::Code InsetLatexAccent::lyxCode() const
619 return InsetBase::ACCENT_CODE;
623 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)