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::odocstream;
30 using lyx::support::contains;
31 using lyx::support::trim;
39 /* LatexAccent. Proper handling of accented characters */
40 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
41 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
43 InsetLatexAccent::InsetLatexAccent()
48 InsetLatexAccent::InsetLatexAccent(string const & str)
55 auto_ptr<InsetBase> InsetLatexAccent::doClone() const
57 return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
61 void InsetLatexAccent::checkContents()
62 // check, if we know the modifier and can display it ok on screen
66 if (contents.empty() || contents.length() < 2) {
67 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
71 contents = trim(contents);
72 if (contents[0] != '\\') { // demand that first char is a '\\'
73 lyxerr[Debug::KEY] << "Cannot decode: " << contents << endl;
77 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
83 switch (contents[1]) { // second char should be one of these
85 modtype = ACUTE; // acute
86 plusasc = true; // at the top of character
89 modtype = GRAVE; // grave
90 plusasc = true; // at the top
93 modtype = MACRON; // macron
94 plusasc = true; // at the top
97 modtype = TILDE; // tilde
98 plusasc = true; // at the top
100 case 'b': // underbar
101 modtype = UNDERBAR; // underbar
102 plusdesc = true; // at the bottom
105 modtype = CEDILLA; // cedilla
106 plusdesc = true; // at the bottom
108 case 'd': // underdot
109 modtype = UNDERDOT; // underdot
110 plusdesc = true; // at the bottom
113 modtype = CIRCLE; // circle
114 plusasc = true; // at the top
117 modtype = TIE; // tie
118 plusasc = true; // at the top
121 modtype = BREVE; // breve
122 plusasc = true; // at the top
125 modtype = CARON; // caron
126 plusasc = true; // at the top
128 case 'q': // special caron
129 modtype = SPECIAL_CARON; // special caron
130 plusasc = true; // at the top
132 case 'H': // hungarian umlaut
133 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
134 plusasc = true; // at the top
137 modtype = UMLAUT; // umlaut
138 plusasc = true; // at the top
141 modtype = DOT; // dot
142 plusasc = true; // at the top
144 case '^': // circumflex
145 modtype = CIRCUMFLEX; // circumflex
146 plusasc = true; // at the top
149 modtype = OGONEK; // ogonek
152 case 'i': // dot-less-i
153 modtype = DOT_LESS_I; // dot-less-i
154 plusasc = true; // at the top (not really needed)
157 case 'j': // dot-less-j
158 modtype = DOT_LESS_J; // dot-less-j
159 plusasc = true; // at the top (not really needed)
164 plusasc = true; // at the top (not really needed)
168 plusasc = true; // at the top (not really needed)
171 lyxerr[Debug::KEY] << "Default" << endl;
172 // unknown accent (or something else)
176 // we demand that third char is a '{' (Lgb)
177 if (contents[2] != '{') return;
179 // special clause for \i{}, \j{} \l{} and \L{}
180 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
181 || modtype == lSLASH || modtype == LSLASH)
182 && contents[3] == '}') {
184 case DOT_LESS_I: ic = 'i'; break;
185 case DOT_LESS_J: ic = 'j'; break;
186 case lSLASH: ic = 'l'; break;
187 case LSLASH: ic = 'L'; break;
189 // if this happens something is really wrong
190 lyxerr << "InsetLaTexAccent: weird error." << endl;
193 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
194 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
196 << ", top: " << plusasc
197 << ", bot: " << plusdesc
198 << ", dot: " << remdot
199 << ", mod: " << modtype << endl;
200 // Special case for space
201 } else if (contents[3] == '}') {
207 ic = contents[3]; // i will always be 3 here
209 // ic should now be a alfa-char or '\\'
211 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
212 if (ic == 'i' || ic == 'j')
216 } else if ((ic == 'i'|| ic == 'j') && contents[4] == '}') {
217 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
218 string temp = contents;
219 temp.erase(3, string::npos);
222 for (string::size_type j = 4;
223 j < contents.length(); ++j)
230 // demand a '}' at the end
231 if (contents[++i] != '}' && contents[++i]) return;
233 // fine, the char is properly decoded now (hopefully)
234 lyxerr[Debug::KEY] << "Contents: [" << contents << ']'
236 << ", top: " << plusasc
237 << ", bot: " << plusdesc
238 << ", dot: " << remdot
239 << ", mod: " << modtype << endl;
245 void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
247 LyXFont & font = mi.base.font;
248 lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
250 // This function is a bit too simplistic and is just a
251 // "try to make a fit for all accents" approach, to
252 // make it better we need to know what kind of accent is
253 // used and add to max based on that.
256 dim.asc = fm.ascent('a');
258 dim.asc = fm.ascent(ic);
260 dim.asc += (fm.maxAscent() + 3) / 3;
263 dim.des = fm.descent('a');
265 dim.des = fm.descent(ic);
269 dim.wid = fm.width(ic);
271 dim.asc = fm.maxAscent() + 4;
272 dim.des = fm.maxDescent() + 4;
273 docstring dcon(contents.begin(), contents.end());
274 dim.wid = fm.width(dcon) + 4;
280 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
282 unsigned char tmpic = ic;
287 if (ic == 'c') tmpic = '\xe7';
288 if (ic == 'C') tmpic = '\xc7';
289 if (ic == 's') tmpic = '\xfe';
290 if (ic == 'S') tmpic = '\xde';
295 if (ic == 'g') tmpic = '\xf0';
296 if (ic == 'G') tmpic = '\xd0';
301 if (ic == 'o') tmpic = '\xf6';
302 if (ic == 'O') tmpic = '\xd6';
303 if (ic == 'u') tmpic = '\xfc';
304 if (ic == 'U') tmpic = '\xdc';
309 if (ic == 'I') tmpic = '\xdd';
323 pi.pain.text(x, y, char(tmpic), pi.base.font);
328 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
329 char_type accent) const
331 LyXFont const & font = pi.base.font;
332 lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
334 x -= fm.center(accent);
336 y -= fm.descent(accent);
337 y -= fm.height(accent) / 2;
338 pi.pain.text(x, y, accent, font);
342 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
344 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
345 if (displayISO8859_9(pi, x, baseline))
348 // All the manually drawn accents in this function could use an
349 // overhaul. Different ways of drawing (what metrics to use)
350 // should also be considered.
352 LyXFont font = pi.base.font;
353 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
354 font.setLanguage(english_language);
356 lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
359 int x2 = int(x + (fm.rbearing(ic) - fm.lbearing(ic)) / 2);
364 hg = fm.maxDescent();
365 y = baseline - dim_.asc;
366 if (font.shape() == LyXFont::ITALIC_SHAPE)
367 x2 += int(0.8 * hg); // italic
374 double hg35 = hg * 0.6;
376 // display with proper accent mark
378 pi.pain.text(x, baseline, ic, font);
381 int tmpvar = baseline - fm.ascent('i');
383 if (font.shape() == LyXFont::ITALIC_SHAPE)
384 tmpx += int(0.8 * hg); // italic
385 lyxerr[Debug::KEY] << "Removing dot." << endl;
386 // remove the dot first
387 pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
391 // the five lines below is a simple hack to
392 // make the display of accent 'i' and 'j'
393 // better. It makes the accent be written
394 // closer to the top of the dot-less 'i' or 'j'.
395 char tmpic = ic; // store the ic when we
396 ic = 'x'; // calculates the ascent of
400 int asc = ascent(); // the dot-less version (here: 'x')
401 ic = tmpic; // set the orig ic back
402 y = baseline - asc; // update to new y coord.
405 // now the rest - draw within (x, y, x + wid, y + hg)
408 //drawAccent(pi, x2, baseline, '\xB4');
409 drawAccent(pi, x2, baseline, 0xB4);
413 //drawAccent(pi, x2, baseline, '\x60');
414 drawAccent(pi, x2, baseline, 0x60);
418 //drawAccent(pi, x2, baseline, '\xAF');
419 drawAccent(pi, x2, baseline, 0xAF);
423 drawAccent(pi, x2, baseline, '~');
427 char_type const underbar = 0x5F; //('\x5F');
428 pi.pain.text(x2 - fm.center(underbar),
429 baseline, underbar, font);
434 char_type const cedilla = 0xB8; //('\xB8');
435 pi.pain.text(x2 - fm.center(cedilla),
436 baseline, cedilla, font);
441 pi.pain.text(x2 - fm.center('.'),
442 int(baseline + 1.5 * fm.height('.')),
447 drawAccent(pi, x2, baseline, '.');
451 //drawAccent(pi, x2, baseline, '\xB0');
452 drawAccent(pi, x2, baseline, 0xB0);
456 pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
461 pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
467 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
468 xp[1] = int(x2); yp[1] = int(y + hg);
469 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
470 pi.pain.lines(xp, yp, 3, LColor::foreground);
474 case SPECIAL_CARON: {
476 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
477 case 't': y -= int(hg35 / 2.0); break;
480 xp[0] = int(x + dim_.wid);
481 yp[0] = int(y + hg35 + hg);
483 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
484 yp[1] = int(y + hg + (hg35 / 2.0));
486 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
489 pi.pain.lines(xp, yp, 3, LColor::foreground);
493 case HUNGARIAN_UMLAUT:
494 drawAccent(pi, x2 - fm.center('´'), baseline, '´');
495 drawAccent(pi, x2 + fm.center('´'), baseline, '´');
499 drawAccent(pi, x2, baseline, '"');
503 drawAccent(pi, x2, baseline, '\x5E');
507 // this does probably not look like an ogonek, so
508 // it should certainly be refined
515 yp[1] = y + int(hg35);
517 xp[2] = int(x2 - hg35);
523 pi.pain.lines(xp, yp, 4, LColor::foreground);
532 yp[0] = y + int(3 * hg);
534 xp[1] = int(x + dim_.wid * 0.75);
537 pi.pain.lines(xp, yp, 2, LColor::foreground);
541 case DOT_LESS_I: // dotless-i
542 case DOT_LESS_J: // dotless-j
543 // nothing to do for these
548 pi.pain.fillRectangle(x + 1,
549 baseline - dim_.asc + 1, dim_.wid - 2,
550 dim_.asc + dim_.des - 2,
552 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
553 dim_.wid - 2, dim_.asc + dim_.des - 2,
555 docstring dcon(contents.begin(), contents.end());
556 pi.pain.text(x + 2, baseline, dcon, font);
561 void InsetLatexAccent::write(Buffer const &, ostream & os) const
563 os << "\\i " << contents << "\n";
567 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
570 contents = lex.getString();
575 int InsetLatexAccent::latex(Buffer const &, odocstream & os,
576 OutputParams const &) const
578 os << lyx::from_ascii(contents);
583 int InsetLatexAccent::plaintext(Buffer const &, odocstream & os,
584 OutputParams const &) const
586 os << lyx::from_ascii(contents);
591 int InsetLatexAccent::docbook(Buffer const &, odocstream & os,
592 OutputParams const &) const
595 os << lyx::from_ascii(contents);
600 int InsetLatexAccent::textString(Buffer const & buf, odocstream & os,
601 OutputParams const & op) const
603 return plaintext(buf, os, op);
607 bool InsetLatexAccent::directWrite() const
613 InsetBase::Code InsetLatexAccent::lyxCode() const
615 return InsetBase::ACCENT_CODE;
619 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)