1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 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(string const & string)
42 void InsetLatexAccent::checkContents()
43 // check, if we know the modifier and can display it ok on screen
47 if (contents.empty() || contents.length() < 2) return;
50 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
51 contents = frontStrip(strip(contents));
52 if (!contains(contents, "{") && !contains(contents, "}")) {
53 if (contents.length() == 2) {
59 } else if (contents.length() == 3) {
67 } else if (contents.length() == 4 && contents[2] == ' ') {
75 } else if (contents.length() == 4 && contents[2] == '\\'
76 && (contents[3] == 'i' || contents[3] == 'j')) {
87 if (contents[0] != '\\') return; // demand that first char is a '\\'
89 lyxerr[Debug::KEY] << "Decode: " << contents << endl;
91 remdot = false; plusasc = false; plusdesc = false;
93 switch (contents[1]) { // second char should be one of these
95 modtype = ACUTE; // acute
96 plusasc = true; // at the top of character
99 modtype = GRAVE; // grave
100 plusasc = true; // at the top
103 modtype = MACRON; // macron
104 plusasc = true; // at the top
107 modtype = TILDE; // tilde
108 plusasc = true; // at the top
110 case 'b': // underbar
111 modtype = UNDERBAR; // underbar
112 plusdesc = true; // at the bottom
115 modtype = CEDILLA; // cedilla
116 plusdesc = true; // at the bottom
118 case 'd': // underdot
119 modtype = UNDERDOT; // underdot
120 plusdesc = true; // at the bottom
123 modtype = CIRCLE; // circle
124 plusasc = true; // at the top
127 modtype = TIE; // tie
128 plusasc = true; // at the top
131 modtype = BREVE; // breve
132 plusasc = true; // at the top
135 modtype = CARON; // caron
136 plusasc = true; // at the top
138 case 'q': // special caron
139 modtype = SPECIAL_CARON; // special caron
140 plusasc = true; // at the top
142 case 'H': // hungarian umlaut
143 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
144 plusasc = true; // at the top
147 modtype = UMLAUT; // umlaut
148 plusasc = true; // at the top
151 modtype = DOT; // dot
152 plusasc = true; // at the top
154 case '^': // circumflex
155 modtype = CIRCUMFLEX; // circumflex
156 plusasc = true; // at the top
159 modtype = OGONEK; // ogonek
162 case 'i': // dot-less-i
163 modtype = DOT_LESS_I; // dot-less-i
164 plusasc = true; // at the top (not really needed)
167 case 'j': // dot-less-j
168 modtype = DOT_LESS_J; // dot-less-j
169 plusasc = true; // at the top (not really needed)
174 plusasc = true; // at the top (not really needed)
178 plusasc = true; // at the top (not really needed)
181 printf ("Default\n");
182 // unknow accent (or something else)
186 // we demand that third char is a '{' (Lgb)
187 if (contents[2] != '{') return;
189 // special clause for \i{}, \j{} \l{} and \L{}
190 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
191 || modtype == lSLASH || modtype == LSLASH)
192 && contents[3] == '}' ) {
194 case DOT_LESS_I: ic = 'i'; break;
195 case DOT_LESS_J: ic = 'j'; break;
196 case lSLASH: ic = 'l'; break;
197 case LSLASH: ic = 'L'; break;
199 // if this happens something is really wrong
200 lyxerr << "InsetLaTexAccent: weird error." << endl;
203 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
204 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
206 << ", top: " << plusasc
207 << ", bot: " << plusdesc
208 << ", dot: " << remdot
209 << ", mod: " << modtype << endl;
210 // Special case for space
211 } else if (contents[3] == '}') {
217 ic = contents[3]; // i will always be 3 here
219 // ic should now be a alfa-char or '\\'
221 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
222 if (ic == 'i' || ic == 'j')
226 } else if ( (ic == 'i'|| ic == 'j') && contents[4] == '}') {
227 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
228 string temp = contents;
229 temp.erase(3, string::npos);
232 for(string::size_type j = 4;
233 j < contents.length(); ++j)
240 // demand a '}' at the end
241 if (contents[++i] != '}' && contents[++i]) return;
243 // fine, the char is properly decoded now (hopefully)
244 lyxerr[Debug::KEY] << "Contents: [" << contents << "]"
246 << ", top: " << plusasc
247 << ", bot: " << plusdesc
248 << ", dot: " << remdot
249 << ", mod: " << modtype << endl;
255 int InsetLatexAccent::Ascent(LyXFont const & font) const
257 // This function is a bit too simplistix and is just a
258 // "try to make a fit for all accents" approach, to
259 // make it better we need to know what kind of accent is
260 // used and add to max based on that.
264 max = font.ascent('a');
266 max = font.ascent(ic);
268 max += (font.maxAscent() + 3) / 3;
270 max = font.maxAscent() + 4;
275 int InsetLatexAccent::Descent(LyXFont const & font) const
280 max = font.descent('a');
282 max = font.descent(ic);
286 max = font.maxDescent() + 4;
291 int InsetLatexAccent::Width(LyXFont const & font) const
294 return font.textWidth(&ic, 1);
296 return font.stringWidth(contents) + 4;
300 int InsetLatexAccent::Lbearing(LyXFont const & font) const
302 return font.lbearing(ic);
306 int InsetLatexAccent::Rbearing(LyXFont const & font) const
308 return font.rbearing(ic);
312 bool InsetLatexAccent::DisplayISO8859_9(LyXFont font,
317 unsigned char tmpic = ic;
322 if (ic == 'c') tmpic = 0xe7;
323 if (ic == 'C') tmpic = 0xc7;
324 if (ic == 's') tmpic = 0xfe;
325 if (ic == 'S') tmpic = 0xde;
329 { if (ic == 'g') tmpic = 0xf0;
330 if (ic == 'G') tmpic = 0xd0;
335 if (ic == 'o') tmpic = 0xf6;
336 if (ic == 'O') tmpic = 0xd6;
337 if (ic == 'u') tmpic = 0xfc;
338 if (ic == 'U') tmpic = 0xdc;
341 case DOT: if (ic == 'I') tmpic = 0xdd; break;
342 case DOT_LESS_I: tmpic = 0xfd; break;
343 default: return false;
346 char ch = char(tmpic);
347 scr.drawText(font, &ch, 1, baseline, int(x));
356 void InsetLatexAccent::Draw(LyXFont font,
361 if (lyxrc->font_norm == "iso8859-9")
362 if (DisplayISO8859_9 (font, scr, baseline, x))
366 // All the manually drawn accents in this function could use an
367 // overhaul. Different ways of drawing (what metrics to use)
368 // should also be considered.
371 int asc = Ascent(font);
372 int desc = Descent(font);
373 int wid = Width(font);
374 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
379 hg = font.maxDescent();
382 if (font.shape() == LyXFont::ITALIC_SHAPE)
383 x2 += (4.0 * hg) / 5.0; // italic
390 float hg35 = float(hg * 3.0) / 5.0;
392 // display with proper accent mark
394 scr.drawText(font, &ic, 1, baseline, int(x));
396 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
399 int tmpvar = baseline - font.ascent('i');
401 if (font.shape() == LyXFont::ITALIC_SHAPE)
402 tmpx += (8.0 * hg) / 10.0; // italic
403 lyxerr[Debug::KEY] << "Removing dot." << endl;
404 // remove the dot first
405 scr.fillRectangle(gc_clear, int(x + tmpx),
408 font.ascent('x') - 1);
409 // the five lines below is a simple hack to
410 // make the display of accent 'i' and 'j'
411 // better. It makes the accent be written
412 // closer to the top of the dot-less 'i' or 'j'.
413 char tmpic = ic; // store the ic when we
414 ic = 'x'; // calculates the ascent of
415 asc = Ascent(font); // the dot-less version (here: 'x')
416 ic = tmpic; // set the orig ic back
417 y = baseline - asc; // update to new y coord.
419 // now the rest - draw within (x, y, x+wid, y+hg)
423 scr.drawLine(pgc, int(x2), int(y + hg),
424 int(x2 + hg35), y + hg35);
429 scr.drawLine(pgc, int(x2), int(y + hg),
430 int(x2 - hg35), y + hg35);
433 case MACRON: // macron
444 if (hg35 > 2.0) hg35 -= 1.0;
447 p[0].x = int(x2 - 2.0 * hg35); p[0].y = int(y + hg);
448 p[1].x = int(x2 - hg35); p[1].y = int(y + hg35);
449 p[2].x = int(x2); p[2].y = int(y + hg);
450 p[3].x = int(x2 + hg35); p[3].y = int(y + hg35);
451 scr.drawLines(pgc, p, 4);
454 case UNDERBAR: // underbar
463 case CEDILLA: // cedilla
467 p[0].x = int(x2); p[0].y = y;
468 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
469 p[2].x = int(x2 + (hg / 3.0));
470 p[2].y = y + int(hg / 2.0);
471 p[3].x = int(x2 - (hg / 4.0)); p[3].y = y + int(hg);
472 scr.drawLines(pgc, p, 4);
476 // x2 - hg / 4.0, y + hg);
477 // and then we need a circle thingie.
481 case UNDERDOT: // underdot
484 scr.fillArc(pgc, int(x2), y + hg35,
487 //scr.drawText(font, "·", 1, y + 2 * hg, x2);
494 scr.fillArc(pgc, int(x2), y + hg * 0.5,
496 (hg + 3.0)/5.0, 0, 360*64);
498 //scr.drawText(font, "·", 1, y + 2.0 * hg, x2);
502 case CIRCLE: // circle
505 scr.drawArc(pgc, int(x2 - (hg / 2.0)),
506 y + (hg / 2.0), hg, hg, 0,
509 //scr.drawText(font, "°", 1, y + 2.0 * hg, x2);
516 int(x2 + hg35), y + (hg / 2.0),
517 2 * hg, hg, 0, 360*32);
523 int(x2 - (hg / 2.0)), y,
530 p[0].x = int(x2 - hg35); p[0].y = int(y + hg35);
531 p[1].x = int(x2); p[1].y = int(y + hg);
532 p[2].x = int(x2 + hg35); p[2].y = int(y + hg35);
533 scr.drawLines(pgc, p, 3);
536 case SPECIAL_CARON: // special caron
539 case 'L': wid = int(4.0 * wid / 5.0); break;
540 case 't': y -= int(hg35 / 2.0); break;
543 p[0].x = int(x + wid); p[0].y = int(y + hg35 + hg);
544 p[1].x = int(x + wid + (hg35 / 2.0));
545 p[1].y = int(y + hg + (hg35 / 2.0));
546 p[2].x = int(x + wid + (hg35 / 2.0));
547 p[2].y = y + int(hg);
548 scr.drawLines(pgc, p, 3);
551 case HUNGARIAN_UMLAUT: // hung. umlaut
554 s[0].x1= int(x2 - (hg / 2.0)); s[0].y1 = int(y + hg);
555 s[0].x2 = int(x2 + hg35 - (hg / 2.0));
556 s[0].y2 = int(y + hg35);
557 s[1].x1 = int(x2 + (hg / 2.0));
558 s[1].y1 = int(y + hg);
559 s[1].x2 = int(x2 + hg35 + (hg / 2.0));
560 s[1].y2 = int(y + hg35);
562 scr.drawSegments(pgc, s, 2);
565 case UMLAUT: // umlaut
567 float rad = hg / 2.0;
570 int(x2 - ((4.0 * hg) / 7.0)),
573 int(x2 + ((4.0 * hg) / 7.0)),
576 rad += .5; // this ensures that f.ex. 1.5 will
577 // not be rounded down to .5 and then
578 // converted to int = 0
579 scr.fillArc(pgc, int(x2 - ((2.0 * hg) / 4.0)),
581 rad, rad, 0, 360*64);
582 scr.fillArc(pgc, int(x2 + ((2.0 * hg) / 4.0)),
584 rad, rad, 0, 360*64);
586 //scr.drawText(font, "¨", 1, y + hg, x2);
589 case CIRCUMFLEX: // circumflex
592 p[0].x = int(x2 - hg35); p[0].y = y + int(hg);
593 p[1].x = int(x2); p[1].y = int(y + hg35);
594 p[2].x = int(x2 + hg35); p[2].y = y + int(hg);
595 scr.drawLines(pgc, p, 3);
598 case OGONEK: // ogonek
600 // this does probably not look like an ogonek, so
601 // it should certainly be refined
603 p[0].x = int(x2); p[0].y = y;
604 p[1].x = int(x2); p[1].y = y + int(hg35);
605 p[2].x = int(x2 - hg35);
606 p[2].y = y + int(hg / 2.0);
607 p[3].x = int(x2 + (hg / 4.0)); p[3].y = y + int(hg);
608 scr.drawLines(pgc, p, 4);
616 p[0].y = y + int(3.0 * hg);
617 p[1].x = int(x + float(wid) * 0.75);
618 p[1].y = y + int(hg);
619 scr.drawLines(pgc, p, 2);
622 case DOT_LESS_I: // dotless-i
623 case DOT_LESS_J: // dotless-j
625 // nothing to do for these
630 scr.fillRectangle(gc_lighted,
631 int(x + 1), baseline - Ascent(font) + 1,
633 Ascent(font) + Descent(font) - 2);
635 scr.drawRectangle(gc_lighted,
636 int(x), baseline - Ascent(font),
638 Ascent(font) + Descent(font) - 1);
639 scr.drawString(font, contents, baseline, int(x + 2));
645 void InsetLatexAccent::Write(ostream & os)
647 os << "\\i " << contents << "\n";
651 void InsetLatexAccent::Read(LyXLex & lex)
654 contents = lex.GetString();
659 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/)
666 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/)
673 int InsetLatexAccent::Linuxdoc(string & file)
680 int InsetLatexAccent::DocBook(string & file)
687 bool InsetLatexAccent::Deletable() const
693 bool InsetLatexAccent::DirectWrite() const
699 Inset * InsetLatexAccent::Clone() const
701 return new InsetLatexAccent(contents);
705 Inset::Code InsetLatexAccent::LyxCode() const
707 return Inset::ACCENT_CODE;
711 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)