1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetlatexaccent.h"
21 #include "support/lstrings.h"
25 /* LatexAccent. Proper handling of accented characters */
26 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
27 /* Later modified by Lars G. Bjønnes, larsbj@lyx.org */
29 InsetLatexAccent::InsetLatexAccent()
35 InsetLatexAccent::InsetLatexAccent(InsetLatexAccent const & other)
36 : Inset(), contents(other.contents),
37 candisp(other.candisp),
38 modtype(other.modtype),
40 plusasc(other.plusasc),
41 plusdesc(other.plusdesc),
46 InsetLatexAccent::InsetLatexAccent(string const & string)
53 void InsetLatexAccent::checkContents()
54 // check, if we know the modifier and can display it ok on screen
58 if (contents.empty() || contents.length() < 2) return;
61 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
62 contents = frontStrip(strip(contents));
63 if (!contains(contents, "{") && !contains(contents, "}")) {
64 if (contents.length() == 2) {
70 } else if (contents.length() == 3) {
78 } else if (contents.length() == 4 && contents[2] == ' ') {
86 } else if (contents.length() == 4 && contents[2] == '\\'
87 && (contents[3] == 'i' || contents[3] == 'j')) {
98 if (contents[0] != '\\') return; // demand that first char is a '\\'
100 lyxerr.debug() << "Decode: " << contents << endl;
102 remdot = false; plusasc = false; plusdesc = false;
104 switch (contents[1]) { // second char should be one of these
106 modtype = ACUTE; // acute
107 plusasc = true; // at the top of character
110 modtype = GRAVE; // grave
111 plusasc = true; // at the top
114 modtype = MACRON; // macron
115 plusasc = true; // at the top
118 modtype = TILDE; // tilde
119 plusasc = true; // at the top
121 case 'b': // underbar
122 modtype = UNDERBAR; // underbar
123 plusdesc = true; // at the bottom
126 modtype = CEDILLA; // cedilla
127 plusdesc = true; // at the bottom
129 case 'd': // underdot
130 modtype = UNDERDOT; // underdot
131 plusdesc = true; // at the bottom
134 modtype = CIRCLE; // circle
135 plusasc = true; // at the top
138 modtype = TIE; // tie
139 plusasc = true; // at the top
142 modtype = BREVE; // breve
143 plusasc = true; // at the top
146 modtype = CARON; // caron
147 plusasc = true; // at the top
149 case 'q': // special caron
150 modtype = SPECIAL_CARON; // special caron
151 plusasc = true; // at the top
153 case 'H': // hungarian umlaut
154 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
155 plusasc = true; // at the top
158 modtype = UMLAUT; // umlaut
159 plusasc = true; // at the top
162 modtype = DOT; // dot
163 plusasc = true; // at the top
165 case '^': // circumflex
166 modtype = CIRCUMFLEX; // circumflex
167 plusasc = true; // at the top
170 modtype = OGONEK; // ogonek
173 case 'i': // dot-less-i
174 modtype = DOT_LESS_I; // dot-less-i
175 plusasc = true; // at the top (not really needed)
178 case 'j': // dot-less-j
179 modtype = DOT_LESS_J; // dot-less-j
180 plusasc = true; // at the top (not really needed)
185 plusasc = true; // at the top (not really needed)
189 plusasc = true; // at the top (not really needed)
192 printf ("Default\n");
193 // unknow accent (or something else)
197 // we demand that third char is a '{' (Lgb)
198 if (contents[2] != '{') return;
200 // special clause for \i{}, \j{} \l{} and \L{}
201 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
202 || modtype == lSLASH || modtype == LSLASH)
203 && contents[3] == '}' ) {
205 case DOT_LESS_I: ic = 'i'; break;
206 case DOT_LESS_J: ic = 'j'; break;
207 case lSLASH: ic = 'l'; break;
208 case LSLASH: ic = 'L'; break;
210 // if this happens something is really wrong
211 lyxerr << "InsetLaTexAccent: weird error." << endl;
214 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
215 lyxerr.debug() << "Contents: [" << contents << "]"
217 << ", top: " << plusasc
218 << ", bot: " << plusdesc
219 << ", dot: " << remdot
220 << ", mod: " << modtype << endl;
221 // Special case for space
222 } else if (contents[3] == '}') {
228 ic = contents[3]; // i will always be 3 here
230 // ic should now be a alfa-char or '\\'
232 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
233 if (ic == 'i' || ic == 'j')
237 } else if ( (ic == 'i'|| ic == 'j') && contents[4] == '}') {
238 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
239 string temp = contents;
240 temp.erase(3, string::npos);
243 for(string::size_type j = 4;
244 j < contents.length(); ++j)
251 // demand a '}' at the end
252 if (contents[++i] != '}' && contents[++i]) return;
254 // fine, the char is properly decoded now (hopefully)
255 lyxerr.debug() << "Contents: [" << contents << "]"
257 << ", top: " << plusasc
258 << ", bot: " << plusdesc
259 << ", dot: " << remdot
260 << ", mod: " << modtype << endl;
266 int InsetLatexAccent::Ascent(LyXFont const & font) const
268 // This function is a bit too simplistix and is just a
269 // "try to make a fit for all accents" approach, to
270 // make it better we need to know what kind of accent is
271 // used and add to max based on that.
275 max = font.ascent('a');
277 max = font.ascent(ic);
279 max += (font.maxAscent() + 3) / 3;
281 max = font.maxAscent() + 4;
286 int InsetLatexAccent::Descent(LyXFont const & font) const
291 max = font.descent('a');
293 max = font.descent(ic);
297 max = font.maxDescent() + 4;
302 int InsetLatexAccent::Width(LyXFont const & font) const
305 return font.textWidth(&ic, 1);
307 return font.stringWidth(contents) + 4;
311 int InsetLatexAccent::Lbearing(LyXFont const & font) const
313 return font.lbearing(ic);
317 int InsetLatexAccent::Rbearing(LyXFont const & font) const
319 return font.rbearing(ic);
323 bool InsetLatexAccent::DisplayISO8859_9(LyXFont font,
328 unsigned char tmpic = ic;
333 if (ic == 'c') tmpic = 0xe7;
334 if (ic == 'C') tmpic = 0xc7;
335 if (ic == 's') tmpic = 0xfe;
336 if (ic == 'S') tmpic = 0xde;
340 { if (ic == 'g') tmpic = 0xf0;
341 if (ic == 'G') tmpic = 0xd0;
346 if (ic == 'o') tmpic = 0xf6;
347 if (ic == 'O') tmpic = 0xd6;
348 if (ic == 'u') tmpic = 0xfc;
349 if (ic == 'U') tmpic = 0xdc;
352 case DOT: if (ic == 'I') tmpic = 0xdd; break;
353 case DOT_LESS_I: tmpic = 0xfd; break;
354 default: return false;
357 char ch = char(tmpic);
358 scr.drawText(font, &ch, 1, baseline, int(x));
367 void InsetLatexAccent::Draw(LyXFont font,
372 if (lyxrc->font_norm == "iso8859-9")
373 if (DisplayISO8859_9 (font, scr, baseline, x))
377 // All the manually drawn accents in this function could use an
378 // overhaul. Different ways of drawing (what metrics to use)
379 // should also be considered.
382 int asc = Ascent(font);
383 int desc = Descent(font);
384 int wid = Width(font);
385 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
391 hg = font.maxDescent();
394 if (font.shape() == LyXFont::ITALIC_SHAPE)
395 x2 += (4.0 * hg) / 5.0; // italic
402 hg35 = float(hg * 3.0) / 5.0;
404 // display with proper accent mark
406 scr.drawText(font, &ic, 1, baseline, int(x));
408 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
411 int tmpvar = baseline - font.ascent('i');
413 if (font.shape() == LyXFont::ITALIC_SHAPE)
414 tmpx += (8.0 * hg) / 10.0; // italic
415 lyxerr.debug() << "Removing dot." << endl;
416 // remove the dot first
417 scr.fillRectangle(gc_clear, int(x + tmpx),
420 font.ascent('x') - 1);
422 // now the rest - draw within (x, y, x+wid, y+hg)
426 scr.drawLine(pgc, int(x2), int(y + hg35),
432 scr.drawLine(pgc, int(x2), int(y + hg35),
436 case MACRON: // macron
439 int(x2 - (3.0 * wid / 7.0)),
440 int(y + (hg / 2.0) + hg35),
441 int(x2 + (3.0 * wid / 7.0)),
442 int(y + (hg / 2.0) + hg35));
447 if (hg35 > 2.0) hg35 -= 1.0;
450 p[0].x = int(x2 - 2.0 * hg35); p[0].y = int(y + hg);
451 p[1].x = int(x2 - hg35); p[1].y = int(y + hg35);
452 p[2].x = int(x2); p[2].y = int(y + hg);
453 p[3].x = int(x2 + hg35); p[3].y = int(y + hg35);
454 scr.drawLines(pgc, p, 4);
457 case UNDERBAR: // underbar
460 int(x2 - (3.0 * wid / 7.0)),
462 int(x2 + (3.0 * wid / 7.0)),
466 case CEDILLA: // cedilla
469 p[0].x = int(x2); p[0].y = y;
470 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
471 p[2].x = int(x2 + (hg / 3.0));
472 p[2].y = y + int(hg / 2.0);
473 p[3].x = int(x2 - (hg / 4.0)); p[3].y = y + int(hg);
474 scr.drawLines(pgc, p, 4);
477 case UNDERDOT: // underdot
480 scr.fillArc(pgc, int(x2), y + (hg / 2.0),
484 case CIRCLE: // circle
486 scr.drawArc(pgc, int(x2 - (hg / 2.0)),
487 y + (hg / 2.0), hg, hg, 0,
494 int(x2), y + (hg / 4.0),
501 int(x2 - (hg / 2.0)), y - (hg / 4.0),
508 p[0].x = int(x2 - hg35); p[0].y = y;
509 p[1].x = int(x2); p[1].y = int(y + hg35);
510 p[2].x = int(x2 + hg35); p[2].y = y;
511 scr.drawLines(pgc, p, 3);
514 case SPECIAL_CARON: // special caron
517 case 'L': wid = int(4.0 * wid / 5.0); break;
518 case 't': y -= int(hg35 / 2.0); break;
521 p[0].x = int(x + wid); p[0].y = int(y + hg35 + hg);
522 p[1].x = int(x + wid + (hg35 / 2.0));
523 p[1].y = int(y + hg + (hg35 / 2.0));
524 p[2].x = int(x + wid + (hg35 / 2.0));
525 p[2].y = y + int(hg);
526 scr.drawLines(pgc, p, 3);
529 case HUNGARIAN_UMLAUT: // hung. umlaut
532 s[0].x1= int(x2 - (hg / 2.0)); s[0].y1 = int(y + hg35);
533 s[0].x2= int(x2 + hg35 - (hg / 2.0)); s[0].y2 = y;
534 s[1].x1= int(x2 + (hg / 2.0));
535 s[1].y1 = int(y + hg35);
536 s[1].x2= int(x2 + hg35 + (hg / 2.0)); s[1].y2 = y;
538 scr.drawSegments(pgc, s, 2);
541 case UMLAUT: // umlaut
546 asc / 5.0; // if (remdot) -> i or j
547 float rad = hg / 2.0;
550 int(x2 - ((4.0 * hg) / 7.0)),
553 int(x2 + ((4.0 * hg) / 7.0)),
556 rad += .5; // this ensures that f.ex. 1.5 will
557 // not be rounded down to .5 and then
558 // converted to int = 0
559 scr.fillArc(pgc, int(x2 - ((2.0 * hg) / 4.0)),
561 rad, rad, 0, 360*64);
562 scr.fillArc(pgc, int(x2 + ((2.0 * hg) / 4.0)),
564 rad, rad, 0, 360*64);
568 case CIRCUMFLEX: // circumflex
571 p[0].x = int(x2 - hg35); p[0].y = y + int(hg);
572 p[1].x = int(x2); p[1].y = int(y + hg35);
573 p[2].x = int(x2 + hg35); p[2].y = y + int(hg);
574 scr.drawLines(pgc, p, 3);
577 case OGONEK: // ogonek
579 // this does probably not look like an ogonek, so
580 // it should certainly be refined
582 p[0].x = int(x2); p[0].y = y;
583 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
584 p[2].x = int(x2 - (hg / 3.0));
585 p[2].y = y + int(hg / 2.0);
586 p[3].x = int(x2 + (hg / 4.0)); p[3].y = y + int(hg);
587 scr.drawLines(pgc, p, 4);
595 p[0].y = y + int(3.0 * hg);
596 p[1].x = int(x + float(wid) * 0.75);
597 p[1].y = y + int(hg);
598 scr.drawLines(pgc, p, 2);
601 case DOT_LESS_I: // dotless-i
602 case DOT_LESS_J: // dotless-j
604 // nothing to do for these
609 scr.fillRectangle(gc_lighted,
610 int(x + 1), baseline - Ascent(font) + 1,
612 Ascent(font) + Descent(font) - 2);
614 scr.drawRectangle(gc_lighted,
615 int(x), baseline - Ascent(font),
617 Ascent(font) + Descent(font) - 1);
618 scr.drawString(font, contents, baseline, int(x + 2));
624 void InsetLatexAccent::Write(ostream & os)
626 os << "\\i " << contents << "\n";
630 void InsetLatexAccent::Read(LyXLex & lex)
633 contents = lex.GetString();
638 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/)
645 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/)
652 int InsetLatexAccent::Linuxdoc(string & file)
659 int InsetLatexAccent::DocBook(string & file)
666 bool InsetLatexAccent::Deletable() const
672 bool InsetLatexAccent::DirectWrite() const
678 Inset * InsetLatexAccent::Clone() const
680 return new InsetLatexAccent(contents);
684 Inset::Code InsetLatexAccent::LyxCode() const
686 return Inset::ACCENT_CODE;
690 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)