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/FontMetrics.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 lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
249 // This function is a bit too simplistic and is just a
250 // "try to make a fit for all accents" approach, to
251 // make it better we need to know what kind of accent is
252 // used and add to max based on that.
255 dim.asc = fm.ascent('a');
257 dim.asc = fm.ascent(ic);
259 dim.asc += (fm.maxAscent() + 3) / 3;
262 dim.des = fm.descent('a');
264 dim.des = fm.descent(ic);
268 dim.wid = fm.width(ic);
270 dim.asc = fm.maxAscent() + 4;
271 dim.des = fm.maxDescent() + 4;
272 docstring dcon(contents.begin(), contents.end());
273 dim.wid = fm.width(dcon) + 4;
279 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
281 unsigned char tmpic = ic;
286 if (ic == 'c') tmpic = '\xe7';
287 if (ic == 'C') tmpic = '\xc7';
288 if (ic == 's') tmpic = '\xfe';
289 if (ic == 'S') tmpic = '\xde';
294 if (ic == 'g') tmpic = '\xf0';
295 if (ic == 'G') tmpic = '\xd0';
300 if (ic == 'o') tmpic = '\xf6';
301 if (ic == 'O') tmpic = '\xd6';
302 if (ic == 'u') tmpic = '\xfc';
303 if (ic == 'U') tmpic = '\xdc';
308 if (ic == 'I') tmpic = '\xdd';
322 pi.pain.text(x, y, char(tmpic), pi.base.font);
327 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
328 char_type accent) const
330 LyXFont const & font = pi.base.font;
331 lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
333 x -= fm.center(accent);
335 y -= fm.descent(accent);
336 y -= fm.height(accent) / 2;
337 pi.pain.text(x, y, accent, font);
341 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
343 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
344 if (displayISO8859_9(pi, x, baseline))
347 // All the manually drawn accents in this function could use an
348 // overhaul. Different ways of drawing (what metrics to use)
349 // should also be considered.
351 LyXFont font = pi.base.font;
352 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
353 font.setLanguage(english_language);
355 lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
358 int x2 = int(x + (fm.rbearing(ic) - fm.lbearing(ic)) / 2);
363 hg = fm.maxDescent();
364 y = baseline - dim_.asc;
365 if (font.shape() == LyXFont::ITALIC_SHAPE)
366 x2 += int(0.8 * hg); // italic
373 double hg35 = hg * 0.6;
375 // display with proper accent mark
377 pi.pain.text(x, baseline, ic, font);
380 int tmpvar = baseline - fm.ascent('i');
382 if (font.shape() == LyXFont::ITALIC_SHAPE)
383 tmpx += int(0.8 * hg); // italic
384 lyxerr[Debug::KEY] << "Removing dot." << endl;
385 // remove the dot first
386 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
390 // the five lines below is a simple hack to
391 // make the display of accent 'i' and 'j'
392 // better. It makes the accent be written
393 // closer to the top of the dot-less 'i' or 'j'.
394 char tmpic = ic; // store the ic when we
395 ic = 'x'; // calculates the ascent of
399 int asc = ascent(); // the dot-less version (here: 'x')
400 ic = tmpic; // set the orig ic back
401 y = baseline - asc; // update to new y coord.
404 // now the rest - draw within (x, y, x + wid, y + hg)
407 //drawAccent(pi, x2, baseline, '\xB4');
408 drawAccent(pi, x2, baseline, 0xB4);
412 //drawAccent(pi, x2, baseline, '\x60');
413 drawAccent(pi, x2, baseline, 0x60);
417 //drawAccent(pi, x2, baseline, '\xAF');
418 drawAccent(pi, x2, baseline, 0xAF);
422 drawAccent(pi, x2, baseline, '~');
426 char_type const underbar = 0x5F; //('\x5F');
427 pi.pain.text(x2 - fm.center(underbar),
428 baseline, underbar, font);
433 char_type const cedilla = 0xB8; //('\xB8');
434 pi.pain.text(x2 - fm.center(cedilla),
435 baseline, cedilla, font);
440 pi.pain.text(x2 - fm.center('.'),
441 int(baseline + 1.5 * fm.height('.')),
446 drawAccent(pi, x2, baseline, '.');
450 //drawAccent(pi, x2, baseline, '\xB0');
451 drawAccent(pi, x2, baseline, 0xB0);
455 pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
460 pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
466 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
467 xp[1] = int(x2); yp[1] = int(y + hg);
468 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
469 pi.pain.lines(xp, yp, 3, LColor::foreground);
473 case SPECIAL_CARON: {
475 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
476 case 't': y -= int(hg35 / 2.0); break;
479 xp[0] = int(x + dim_.wid);
480 yp[0] = int(y + hg35 + hg);
482 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
483 yp[1] = int(y + hg + (hg35 / 2.0));
485 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
488 pi.pain.lines(xp, yp, 3, LColor::foreground);
492 case HUNGARIAN_UMLAUT:
493 drawAccent(pi, x2 - fm.center('´'), baseline, '´');
494 drawAccent(pi, x2 + fm.center('´'), baseline, '´');
498 drawAccent(pi, x2, baseline, '"');
502 drawAccent(pi, x2, baseline, '\x5E');
506 // this does probably not look like an ogonek, so
507 // it should certainly be refined
514 yp[1] = y + int(hg35);
516 xp[2] = int(x2 - hg35);
522 pi.pain.lines(xp, yp, 4, LColor::foreground);
531 yp[0] = y + int(3 * hg);
533 xp[1] = int(x + dim_.wid * 0.75);
536 pi.pain.lines(xp, yp, 2, LColor::foreground);
540 case DOT_LESS_I: // dotless-i
541 case DOT_LESS_J: // dotless-j
542 // nothing to do for these
547 pi.pain.fillRectangle(x + 1,
548 baseline - dim_.asc + 1, dim_.wid - 2,
549 dim_.asc + dim_.des - 2,
551 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
552 dim_.wid - 2, dim_.asc + dim_.des - 2,
554 docstring dcon(contents.begin(), contents.end());
555 pi.pain.text(x + 2, baseline, dcon, font);
560 void InsetLatexAccent::write(Buffer const &, ostream & os) const
562 os << "\\i " << contents << "\n";
566 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
569 contents = lex.getString();
574 int InsetLatexAccent::latex(Buffer const &, ostream & os,
575 OutputParams const &) const
582 int InsetLatexAccent::plaintext(Buffer const &, lyx::odocstream & os,
583 OutputParams const &) const
585 os << lyx::from_ascii(contents);
590 int InsetLatexAccent::docbook(Buffer const &, ostream & os,
591 OutputParams const &) const
598 int InsetLatexAccent::textString(Buffer const & buf, lyx::odocstream & os,
599 OutputParams const & op) const
601 return plaintext(buf, os, op);
605 bool InsetLatexAccent::directWrite() const
611 InsetBase::Code InsetLatexAccent::lyxCode() const
613 return InsetBase::ACCENT_CODE;
617 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)