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"
30 using support::contains;
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 bool InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
247 LyXFont & font = mi.base.font;
248 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;
276 bool const changed = dim_ != dim;
282 bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
284 unsigned char tmpic = ic;
289 if (ic == 'c') tmpic = '\xe7';
290 if (ic == 'C') tmpic = '\xc7';
291 if (ic == 's') tmpic = '\xfe';
292 if (ic == 'S') tmpic = '\xde';
297 if (ic == 'g') tmpic = '\xf0';
298 if (ic == 'G') tmpic = '\xd0';
303 if (ic == 'o') tmpic = '\xf6';
304 if (ic == 'O') tmpic = '\xd6';
305 if (ic == 'u') tmpic = '\xfc';
306 if (ic == 'U') tmpic = '\xdc';
311 if (ic == 'I') tmpic = '\xdd';
325 pi.pain.text(x, y, char(tmpic), pi.base.font);
330 void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y,
331 char_type accent) const
333 LyXFont const & font = pi.base.font;
334 frontend::FontMetrics const & fm = theFontMetrics(font);
336 x -= fm.center(accent);
338 y -= fm.descent(accent);
339 y -= fm.height(accent) / 2;
340 pi.pain.text(x, y, accent, font);
344 void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
346 if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
347 if (displayISO8859_9(pi, x, baseline))
350 // All the manually drawn accents in this function could use an
351 // overhaul. Different ways of drawing (what metrics to use)
352 // should also be considered.
354 LyXFont font = pi.base.font;
355 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
356 font.setLanguage(english_language);
358 frontend::FontMetrics const & fm = theFontMetrics(font);
361 int x2 = int(x + (fm.rbearing(ic) - fm.lbearing(ic)) / 2);
366 hg = fm.maxDescent();
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 - fm.ascent('i');
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,
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, '\xB4');
411 drawAccent(pi, x2, baseline, 0xB4);
415 //drawAccent(pi, x2, baseline, '\x60');
416 drawAccent(pi, x2, baseline, 0x60);
420 //drawAccent(pi, x2, baseline, '\xAF');
421 drawAccent(pi, x2, baseline, 0xAF);
425 drawAccent(pi, x2, baseline, '~');
429 char_type const underbar = 0x5F; //('\x5F');
430 pi.pain.text(x2 - fm.center(underbar),
431 baseline, underbar, font);
436 char_type const cedilla = 0xB8; //('\xB8');
437 pi.pain.text(x2 - fm.center(cedilla),
438 baseline, cedilla, font);
443 pi.pain.text(x2 - fm.center('.'),
444 int(baseline + 1.5 * fm.height('.')),
449 drawAccent(pi, x2, baseline, '.');
453 //drawAccent(pi, x2, baseline, '\xB0');
454 drawAccent(pi, x2, baseline, 0xB0);
458 pi.pain.arc(int(x2 + hg35), y + hg / 2, 2 * hg, hg, 0, 360 * 32,
463 pi.pain.arc(int(x2 - hg / 2), y, hg, hg, 0, -360*32,
469 xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
470 xp[1] = int(x2); yp[1] = int(y + hg);
471 xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
472 pi.pain.lines(xp, yp, 3, LColor::foreground);
476 case SPECIAL_CARON: {
478 case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
479 case 't': y -= int(hg35 / 2.0); break;
482 xp[0] = int(x + dim_.wid);
483 yp[0] = int(y + hg35 + hg);
485 xp[1] = int(x + dim_.wid + (hg35 / 2.0));
486 yp[1] = int(y + hg + (hg35 / 2.0));
488 xp[2] = int(x + dim_.wid + (hg35 / 2.0));
491 pi.pain.lines(xp, yp, 3, LColor::foreground);
495 case HUNGARIAN_UMLAUT:
496 drawAccent(pi, x2, baseline, 0x02DD);
500 drawAccent(pi, x2, baseline, '"');
504 drawAccent(pi, x2, baseline, '\x5E');
508 // this does probably not look like an ogonek, so
509 // it should certainly be refined
516 yp[1] = y + int(hg35);
518 xp[2] = int(x2 - hg35);
524 pi.pain.lines(xp, yp, 4, LColor::foreground);
533 yp[0] = y + int(3 * hg);
535 xp[1] = int(x + dim_.wid * 0.75);
538 pi.pain.lines(xp, yp, 2, LColor::foreground);
542 case DOT_LESS_I: // dotless-i
543 case DOT_LESS_J: // dotless-j
544 // nothing to do for these
549 pi.pain.fillRectangle(x + 1,
550 baseline - dim_.asc + 1, dim_.wid - 2,
551 dim_.asc + dim_.des - 2,
553 pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
554 dim_.wid - 2, dim_.asc + dim_.des - 2,
556 docstring dcon(contents.begin(), contents.end());
557 pi.pain.text(x + 2, baseline, dcon, font);
562 void InsetLatexAccent::write(Buffer const &, ostream & os) const
564 os << "\\i " << contents << "\n";
568 void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
571 contents = lex.getString();
576 int InsetLatexAccent::latex(Buffer const &, odocstream & os,
577 OutputParams const &) const
579 os << from_ascii(contents);
584 int InsetLatexAccent::plaintext(Buffer const &, odocstream & os,
585 OutputParams const &) const
587 os << from_ascii(contents);
592 int InsetLatexAccent::docbook(Buffer const &, odocstream & os,
593 OutputParams const &) const
596 os << from_ascii(contents);
601 int InsetLatexAccent::textString(Buffer const & buf, odocstream & os,
602 OutputParams const & op) const
604 return plaintext(buf, os, op);
608 bool InsetLatexAccent::directWrite() const
614 InsetBase::Code InsetLatexAccent::lyxCode() const
616 return InsetBase::ACCENT_CODE;
620 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)