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 #if 0 // the compiler generated version should be ok.
36 InsetLatexAccent::InsetLatexAccent(InsetLatexAccent const & other)
37 : Inset(), contents(other.contents),
38 candisp(other.candisp),
39 modtype(other.modtype),
41 plusasc(other.plusasc),
42 plusdesc(other.plusdesc),
47 InsetLatexAccent::InsetLatexAccent(string const & string)
54 void InsetLatexAccent::checkContents()
55 // check, if we know the modifier and can display it ok on screen
59 if (contents.empty() || contents.length() < 2) return;
62 // Dirty Hack for backward compability. remove in 0.13 (Lgb)
63 contents = frontStrip(strip(contents));
64 if (!contains(contents, "{") && !contains(contents, "}")) {
65 if (contents.length() == 2) {
71 } else if (contents.length() == 3) {
79 } else if (contents.length() == 4 && contents[2] == ' ') {
87 } else if (contents.length() == 4 && contents[2] == '\\'
88 && (contents[3] == 'i' || contents[3] == 'j')) {
99 if (contents[0] != '\\') return; // demand that first char is a '\\'
101 lyxerr.debug() << "Decode: " << contents << endl;
103 remdot = false; plusasc = false; plusdesc = false;
105 switch (contents[1]) { // second char should be one of these
107 modtype = ACUTE; // acute
108 plusasc = true; // at the top of character
111 modtype = GRAVE; // grave
112 plusasc = true; // at the top
115 modtype = MACRON; // macron
116 plusasc = true; // at the top
119 modtype = TILDE; // tilde
120 plusasc = true; // at the top
122 case 'b': // underbar
123 modtype = UNDERBAR; // underbar
124 plusdesc = true; // at the bottom
127 modtype = CEDILLA; // cedilla
128 plusdesc = true; // at the bottom
130 case 'd': // underdot
131 modtype = UNDERDOT; // underdot
132 plusdesc = true; // at the bottom
135 modtype = CIRCLE; // circle
136 plusasc = true; // at the top
139 modtype = TIE; // tie
140 plusasc = true; // at the top
143 modtype = BREVE; // breve
144 plusasc = true; // at the top
147 modtype = CARON; // caron
148 plusasc = true; // at the top
150 case 'q': // special caron
151 modtype = SPECIAL_CARON; // special caron
152 plusasc = true; // at the top
154 case 'H': // hungarian umlaut
155 modtype = HUNGARIAN_UMLAUT; // hungarian umlaut
156 plusasc = true; // at the top
159 modtype = UMLAUT; // umlaut
160 plusasc = true; // at the top
163 modtype = DOT; // dot
164 plusasc = true; // at the top
166 case '^': // circumflex
167 modtype = CIRCUMFLEX; // circumflex
168 plusasc = true; // at the top
171 modtype = OGONEK; // ogonek
174 case 'i': // dot-less-i
175 modtype = DOT_LESS_I; // dot-less-i
176 plusasc = true; // at the top (not really needed)
179 case 'j': // dot-less-j
180 modtype = DOT_LESS_J; // dot-less-j
181 plusasc = true; // at the top (not really needed)
186 plusasc = true; // at the top (not really needed)
190 plusasc = true; // at the top (not really needed)
193 printf ("Default\n");
194 // unknow accent (or something else)
198 // we demand that third char is a '{' (Lgb)
199 if (contents[2] != '{') return;
201 // special clause for \i{}, \j{} \l{} and \L{}
202 if ((modtype == DOT_LESS_I || modtype == DOT_LESS_J
203 || modtype == lSLASH || modtype == LSLASH)
204 && contents[3] == '}' ) {
206 case DOT_LESS_I: ic = 'i'; break;
207 case DOT_LESS_J: ic = 'j'; break;
208 case lSLASH: ic = 'l'; break;
209 case LSLASH: ic = 'L'; break;
211 // if this happens something is really wrong
212 lyxerr << "InsetLaTexAccent: weird error." << endl;
215 //ic = (modtype == DOT_LESS_J ? 'j' : 'i');
216 lyxerr.debug() << "Contents: [" << contents << "]"
218 << ", top: " << plusasc
219 << ", bot: " << plusdesc
220 << ", dot: " << remdot
221 << ", mod: " << modtype << endl;
222 // Special case for space
223 } else if (contents[3] == '}') {
229 ic = contents[3]; // i will always be 3 here
231 // ic should now be a alfa-char or '\\'
233 ic = contents[++i]; // will only allow \<foo>{\i} and \<foo>{\j}
234 if (ic == 'i' || ic == 'j')
238 } else if ( (ic == 'i'|| ic == 'j') && contents[4] == '}') {
239 // Do a rewrite: \<foo>{i} --> \<foo>{\i}
240 string temp = contents;
241 temp.erase(3, string::npos);
244 for(string::size_type j = 4;
245 j < contents.length(); ++j)
252 // demand a '}' at the end
253 if (contents[++i] != '}' && contents[++i]) return;
255 // fine, the char is properly decoded now (hopefully)
256 lyxerr.debug() << "Contents: [" << contents << "]"
258 << ", top: " << plusasc
259 << ", bot: " << plusdesc
260 << ", dot: " << remdot
261 << ", mod: " << modtype << endl;
267 int InsetLatexAccent::Ascent(LyXFont const & font) const
269 // This function is a bit too simplistix and is just a
270 // "try to make a fit for all accents" approach, to
271 // make it better we need to know what kind of accent is
272 // used and add to max based on that.
276 max = font.ascent('a');
278 max = font.ascent(ic);
280 max += (font.maxAscent() + 3) / 3;
282 max = font.maxAscent() + 4;
287 int InsetLatexAccent::Descent(LyXFont const & font) const
292 max = font.descent('a');
294 max = font.descent(ic);
298 max = font.maxDescent() + 4;
303 int InsetLatexAccent::Width(LyXFont const & font) const
306 return font.textWidth(&ic, 1);
308 return font.stringWidth(contents) + 4;
312 int InsetLatexAccent::Lbearing(LyXFont const & font) const
314 return font.lbearing(ic);
318 int InsetLatexAccent::Rbearing(LyXFont const & font) const
320 return font.rbearing(ic);
324 bool InsetLatexAccent::DisplayISO8859_9(LyXFont font,
329 unsigned char tmpic = ic;
334 if (ic == 'c') tmpic = 0xe7;
335 if (ic == 'C') tmpic = 0xc7;
336 if (ic == 's') tmpic = 0xfe;
337 if (ic == 'S') tmpic = 0xde;
341 { if (ic == 'g') tmpic = 0xf0;
342 if (ic == 'G') tmpic = 0xd0;
347 if (ic == 'o') tmpic = 0xf6;
348 if (ic == 'O') tmpic = 0xd6;
349 if (ic == 'u') tmpic = 0xfc;
350 if (ic == 'U') tmpic = 0xdc;
353 case DOT: if (ic == 'I') tmpic = 0xdd; break;
354 case DOT_LESS_I: tmpic = 0xfd; break;
355 default: return false;
358 char ch = char(tmpic);
359 scr.drawText(font, &ch, 1, baseline, int(x));
368 void InsetLatexAccent::Draw(LyXFont font,
373 if (lyxrc->font_norm == "iso8859-9")
374 if (DisplayISO8859_9 (font, scr, baseline, x))
378 // All the manually drawn accents in this function could use an
379 // overhaul. Different ways of drawing (what metrics to use)
380 // should also be considered.
383 int asc = Ascent(font);
384 int desc = Descent(font);
385 int wid = Width(font);
386 float x2 = x + (Rbearing(font) - Lbearing(font)) / 2.0;
392 hg = font.maxDescent();
395 if (font.shape() == LyXFont::ITALIC_SHAPE)
396 x2 += (4.0 * hg) / 5.0; // italic
403 hg35 = float(hg * 3.0) / 5.0;
405 // display with proper accent mark
407 scr.drawText(font, &ic, 1, baseline, int(x));
409 GC pgc = GetAccentGC(font, int((hg + 3.0) / 5.0));
412 int tmpvar = baseline - font.ascent('i');
414 if (font.shape() == LyXFont::ITALIC_SHAPE)
415 tmpx += (8.0 * hg) / 10.0; // italic
416 lyxerr.debug() << "Removing dot." << endl;
417 // remove the dot first
418 scr.fillRectangle(gc_clear, int(x + tmpx),
421 font.ascent('x') - 1);
422 // the five lines below is a simple hack to
423 // make the display of accent 'i' and 'j'
424 // better. It makes the accent be written
425 // closer to the top of the dot-less 'i' or 'j'.
426 char tmpic = ic; // store the ic when we
427 ic = 'x'; // calculates the ascent of
428 asc = Ascent(font); // the dot-less version (here: 'x')
429 ic = tmpic; // set the orig ic back
430 y = baseline - asc; // update to new y coord.
432 // now the rest - draw within (x, y, x+wid, y+hg)
436 scr.drawLine(pgc, int(x2), int(y + hg35),
442 scr.drawLine(pgc, int(x2), int(y + hg35),
446 case MACRON: // macron
449 int(x2 - (3.0 * wid / 7.0)),
450 int(y + (hg / 2.0) + hg35),
451 int(x2 + (3.0 * wid / 7.0)),
452 int(y + (hg / 2.0) + hg35));
457 if (hg35 > 2.0) hg35 -= 1.0;
460 p[0].x = int(x2 - 2.0 * hg35); p[0].y = int(y + hg);
461 p[1].x = int(x2 - hg35); p[1].y = int(y + hg35);
462 p[2].x = int(x2); p[2].y = int(y + hg);
463 p[3].x = int(x2 + hg35); p[3].y = int(y + hg35);
464 scr.drawLines(pgc, p, 4);
467 case UNDERBAR: // underbar
470 int(x2 - (3.0 * wid / 7.0)),
472 int(x2 + (3.0 * wid / 7.0)),
476 case CEDILLA: // cedilla
479 p[0].x = int(x2); p[0].y = y;
480 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
481 p[2].x = int(x2 + (hg / 3.0));
482 p[2].y = y + int(hg / 2.0);
483 p[3].x = int(x2 - (hg / 4.0)); p[3].y = y + int(hg);
484 scr.drawLines(pgc, p, 4);
487 case UNDERDOT: // underdot
490 scr.fillArc(pgc, int(x2), y + (hg / 2.0),
494 case CIRCLE: // circle
496 scr.drawArc(pgc, int(x2 - (hg / 2.0)),
497 y + (hg / 2.0), hg, hg, 0,
504 int(x2), y + (hg / 4.0),
511 int(x2 - (hg / 2.0)), y - (hg / 4.0),
518 p[0].x = int(x2 - hg35); p[0].y = y;
519 p[1].x = int(x2); p[1].y = int(y + hg35);
520 p[2].x = int(x2 + hg35); p[2].y = y;
521 scr.drawLines(pgc, p, 3);
524 case SPECIAL_CARON: // special caron
527 case 'L': wid = int(4.0 * wid / 5.0); break;
528 case 't': y -= int(hg35 / 2.0); break;
531 p[0].x = int(x + wid); p[0].y = int(y + hg35 + hg);
532 p[1].x = int(x + wid + (hg35 / 2.0));
533 p[1].y = int(y + hg + (hg35 / 2.0));
534 p[2].x = int(x + wid + (hg35 / 2.0));
535 p[2].y = y + int(hg);
536 scr.drawLines(pgc, p, 3);
539 case HUNGARIAN_UMLAUT: // hung. umlaut
542 s[0].x1= int(x2 - (hg / 2.0)); s[0].y1 = int(y + hg35);
543 s[0].x2= int(x2 + hg35 - (hg / 2.0)); s[0].y2 = y;
544 s[1].x1= int(x2 + (hg / 2.0));
545 s[1].y1 = int(y + hg35);
546 s[1].x2= int(x2 + hg35 + (hg / 2.0)); s[1].y2 = y;
548 scr.drawSegments(pgc, s, 2);
551 case UMLAUT: // umlaut
553 float rad = hg / 2.0;
556 int(x2 - ((4.0 * hg) / 7.0)),
559 int(x2 + ((4.0 * hg) / 7.0)),
562 rad += .5; // this ensures that f.ex. 1.5 will
563 // not be rounded down to .5 and then
564 // converted to int = 0
565 scr.fillArc(pgc, int(x2 - ((2.0 * hg) / 4.0)),
567 rad, rad, 0, 360*64);
568 scr.fillArc(pgc, int(x2 + ((2.0 * hg) / 4.0)),
570 rad, rad, 0, 360*64);
574 case CIRCUMFLEX: // circumflex
577 p[0].x = int(x2 - hg35); p[0].y = y + int(hg);
578 p[1].x = int(x2); p[1].y = int(y + hg35);
579 p[2].x = int(x2 + hg35); p[2].y = y + int(hg);
580 scr.drawLines(pgc, p, 3);
583 case OGONEK: // ogonek
585 // this does probably not look like an ogonek, so
586 // it should certainly be refined
588 p[0].x = int(x2); p[0].y = y;
589 p[1].x = int(x2); p[1].y = y + int(hg / 3.0);
590 p[2].x = int(x2 - (hg / 3.0));
591 p[2].y = y + int(hg / 2.0);
592 p[3].x = int(x2 + (hg / 4.0)); p[3].y = y + int(hg);
593 scr.drawLines(pgc, p, 4);
601 p[0].y = y + int(3.0 * hg);
602 p[1].x = int(x + float(wid) * 0.75);
603 p[1].y = y + int(hg);
604 scr.drawLines(pgc, p, 2);
607 case DOT_LESS_I: // dotless-i
608 case DOT_LESS_J: // dotless-j
610 // nothing to do for these
615 scr.fillRectangle(gc_lighted,
616 int(x + 1), baseline - Ascent(font) + 1,
618 Ascent(font) + Descent(font) - 2);
620 scr.drawRectangle(gc_lighted,
621 int(x), baseline - Ascent(font),
623 Ascent(font) + Descent(font) - 1);
624 scr.drawString(font, contents, baseline, int(x + 2));
630 void InsetLatexAccent::Write(ostream & os)
632 os << "\\i " << contents << "\n";
636 void InsetLatexAccent::Read(LyXLex & lex)
639 contents = lex.GetString();
644 int InsetLatexAccent::Latex(ostream & os, signed char /*fragile*/)
651 int InsetLatexAccent::Latex(string & file, signed char /*fragile*/)
658 int InsetLatexAccent::Linuxdoc(string & file)
665 int InsetLatexAccent::DocBook(string & file)
672 bool InsetLatexAccent::Deletable() const
678 bool InsetLatexAccent::DirectWrite() const
684 Inset * InsetLatexAccent::Clone() const
686 return new InsetLatexAccent(contents);
690 Inset::Code InsetLatexAccent::LyxCode() const
692 return Inset::ACCENT_CODE;
696 ostream & operator<<(ostream & o, InsetLatexAccent::ACCENT_TYPES at)