]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetlatexaccent.C
Final touch 'inset display()'; fix 'is a bit silly' bug
[lyx.git] / src / insets / insetlatexaccent.C
index f05530fd6b6430327d4bc72fcf5cb1094d7c1d03..bcbd6ed46cb23660b41a6d4f85ba1c1c60db0063 100644 (file)
@@ -5,24 +5,33 @@
  *
  * \author Lars Gullik Bjønnes
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-
 #include "insetlatexaccent.h"
+
 #include "debug.h"
-#include "lyxrc.h"
-#include "support/lstrings.h"
-#include "BufferView.h"
-#include "frontends/Painter.h"
-#include "frontends/font_metrics.h"
 #include "language.h"
+#include "LColor.h"
 #include "lyxlex.h"
+#include "lyxrc.h"
+#include "metricsinfo.h"
+
+#include "frontends/font_metrics.h"
+#include "frontends/Painter.h"
+
+#include "support/lstrings.h"
+
+using lyx::support::contains;
+using lyx::support::trim;
 
-using std::ostream;
 using std::endl;
+using std::string;
+using std::auto_ptr;
+using std::ostream;
+
 
 /* LatexAccent. Proper handling of accented characters */
 /* This part is done by Ivan Schreter, schreter@ccsun.tuke.sk */
@@ -95,7 +104,9 @@ void InsetLatexAccent::checkContents()
 
        lyxerr[Debug::KEY] << "Decode: " << contents << endl;
 
-       remdot = false; plusasc = false; plusdesc = false;
+       remdot = false;
+       plusasc = false;
+       plusdesc = false;
 
        switch (contents[1]) { // second char should be one of these
        case '\'':  // acute
@@ -259,48 +270,35 @@ void InsetLatexAccent::checkContents()
 }
 
 
-int InsetLatexAccent::ascent(BufferView *, LyXFont const & font) const
+void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       // This function is a bit too simplistix and is just a
+       LyXFont & font = mi.base.font;
+       // This function is a bit too simplistic and is just a
        // "try to make a fit for all accents" approach, to
        // make it better we need to know what kind of accent is
        // used and add to max based on that.
-       int max;
        if (candisp) {
                if (ic == ' ')
-                       max = font_metrics::ascent('a', font);
+                       dim.asc = font_metrics::ascent('a', font);
                else
-                       max = font_metrics::ascent(ic, font);
+                       dim.asc = font_metrics::ascent(ic, font);
                if (plusasc)
-                       max += (font_metrics::maxAscent(font) + 3) / 3;
-       } else
-               max = font_metrics::maxAscent(font) + 4;
-       return max;
-}
-
+                       dim.asc += (font_metrics::maxAscent(font) + 3) / 3;
 
-int InsetLatexAccent::descent(BufferView *, LyXFont const & font) const
-{
-       int max;
-       if (candisp) {
                if (ic == ' ')
-                       max = font_metrics::descent('a', font);
+                       dim.des = font_metrics::descent('a', font);
                else
-               max = font_metrics::descent(ic, font);
+                       dim.des = font_metrics::descent(ic, font);
                if (plusdesc)
-               max += 3;
-       } else
-               max = font_metrics::maxDescent(font) + 4;
-       return max;
-}
+                       dim.des += 3;
 
-
-int InsetLatexAccent::width(BufferView *, LyXFont const & font) const
-{
-       if (candisp)
-               return font_metrics::width(ic, font);
-       else
-               return font_metrics::width(contents, font) + 4;
+               dim.wid = font_metrics::width(ic, font);
+       } else {
+               dim.asc = font_metrics::maxAscent(font) + 4;
+               dim.des = font_metrics::maxDescent(font) + 4;
+               dim.wid = font_metrics::width(contents, font) + 4;
+       }
+       dim_ = dim;
 }
 
 
@@ -316,9 +314,7 @@ int InsetLatexAccent::rbearing(LyXFont const & font) const
 }
 
 
-bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
-                                       int baseline,
-                                       float & x) const
+bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const
 {
        unsigned char tmpic = ic;
 
@@ -349,9 +345,7 @@ bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
        default:         return false;
        }
        if (tmpic != ic) {
-               char ch = char(tmpic);
-               bv->painter().text(int(x), baseline, ch, font);
-               x += width(bv, font);
+               pi.pain.text(x, y, char(tmpic), pi.base.font);
                return true;
        }
        else
@@ -359,13 +353,10 @@ bool InsetLatexAccent::displayISO8859_9(BufferView * bv, LyXFont const & font,
 }
 
 
-void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
-                           int baseline, float & x, bool) const
+void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const
 {
-       Painter & pain = bv->painter();
-
        if (lyxrc.font_norm_type == LyXRC::ISO_8859_9)
-               if (displayISO8859_9(bv, font0, baseline, x))
+               if (displayISO8859_9(pi, x, baseline))
                        return;
 
        /* draw it! */
@@ -373,27 +364,24 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
        // overhaul. Different ways of drawing (what metrics to use)
        // should also be considered.
 
-       LyXFont font(font0);
+       LyXFont font = pi.base.font;
        if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
                font.setLanguage(english_language);
 
        if (candisp) {
-               int asc = ascent(bv, font);
-               int desc = descent(bv, font);
-               int wid = width(bv, font);
                float x2 = x + (rbearing(font) - lbearing(font)) / 2.0;
                float hg;
                int y;
                if (plusasc) {
                        // mark at the top
                        hg = font_metrics::maxDescent(font);
-                       y = baseline - asc;
+                       y = baseline - dim_.asc;
 
                        if (font.shape() == LyXFont::ITALIC_SHAPE)
                                x2 += (4.0 * hg) / 5.0; // italic
                } else {
                        // at the bottom
-                       hg = desc;
+                       hg = dim_.des;
                        y = baseline;
                }
 
@@ -401,18 +389,18 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
 
                // display with proper accent mark
                // first the letter
-               pain.text(int(x), baseline, ic, font);
+               pi.pain.text(x, baseline, ic, font);
 
                if (remdot) {
                        int tmpvar = baseline - font_metrics::ascent('i', font);
-                       float tmpx = 0;
+                       int tmpx = 0;
                        if (font.shape() == LyXFont::ITALIC_SHAPE)
-                               tmpx += (8.0 * hg) / 10.0; // italic
+                               tmpx += int(0.8 * hg); // italic
                        lyxerr[Debug::KEY] << "Removing dot." << endl;
                        // remove the dot first
-                       pain.fillRectangle(int(x + tmpx), tmpvar, wid,
-                                          font_metrics::ascent('i', font) -
-                                          font_metrics::ascent('x', font) - 1,
+                       pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid,
+                                          font_metrics::ascent('i', pi.base.font) -
+                                          font_metrics::ascent('x', pi.base.font) - 1,
                                           backgroundColor());
                        // the five lines below is a simple hack to
                        // make the display of accent 'i' and 'j'
@@ -420,7 +408,8 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                        // closer to the top of the dot-less 'i' or 'j'.
                        char tmpic = ic; // store the ic when we
                        ic = 'x';        // calculates the ascent of
-                       asc = ascent(bv, font); // the dot-less version (here: 'x')
+#warning metrics?
+                       int asc = ascent(); // the dot-less version (here: 'x')
                        ic = tmpic;      // set the orig ic back
                        y = baseline - asc; // update to new y coord.
                }
@@ -428,48 +417,52 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                switch (modtype) {
                case ACUTE:     // acute 0xB4
                {
-                       pain.text(int(x2 - (font_metrics::rbearing(0xB4, font) - font_metrics::lbearing(0xB4, font)) / 2),
-                                 baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB4, font) - (font_metrics::ascent(0xB4, font) + font_metrics::descent(0xB4, font)) / 2,
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0xB4, font)
+                                     - font_metrics::lbearing(0xB4, font)) / 2),
+                                 baseline - font_metrics::ascent(ic, font)
+                                                - font_metrics::descent(0xB4, font)
+                                                - (font_metrics::ascent(0xB4, font)
+                                                 + font_metrics::descent(0xB4, font)) / 2,
                                  char(0xB4), font);
                        break;
                }
                case GRAVE:     // grave 0x60
                {
-                       pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0x60, font) - font_metrics::lbearing(0x60, font)) / 2),
                                  int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x60, font) - (font_metrics::ascent(0x60, font) + font_metrics::descent(0x60, font)) / 2.0),
                                  char(0x60), font);
                        break;
                }
                case MACRON:     // macron
                {
-                       pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0xAF, font) - font_metrics::lbearing(0xAF, font)) / 2),
                                  baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xAF, font) - (font_metrics::ascent(0xAF, font) + font_metrics::descent(0xAF, font)),
                                  char(0xAF), font);
                        break;
                }
                case TILDE:     // tilde
                {
-                       pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing('~', font) - font_metrics::lbearing('~', font)) / 2),
                                  baseline - font_metrics::ascent(ic, font) - font_metrics::descent('~', font) - (font_metrics::ascent('~', font) + font_metrics::descent('~', font)) / 2,
                                  '~', font);
                        break;
                }
                case UNDERBAR:     // underbar 0x5F
                {
-                       pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0x5F, font) - font_metrics::lbearing(0x5F, font)) / 2), baseline,
                                  char(0x5F), font);
                        break;
                }
                case CEDILLA:     // cedilla
                {
-                       pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0xB8, font) - font_metrics::lbearing(0xB8, font)) / 2), baseline,
                                  char(0xB8), font);
 
                        break;
                }
                case UNDERDOT:     // underdot
                {
-                       pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
                                  int(baseline + 3.0 / 2.0 * (font_metrics::ascent('.', font) + font_metrics::descent('.', font))),
                                  '.', font);
                        break;
@@ -477,7 +470,7 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
 
                case DOT:    // dot
                {
-                       pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing('.', font) - font_metrics::lbearing('.', font)) / 2.0),
                                  baseline - font_metrics::ascent(ic, font) - font_metrics::descent('.', font) - (font_metrics::ascent('.', font) + font_metrics::descent('.', font)) / 2,
                                  '.', font);
                        break;
@@ -485,23 +478,25 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
 
                case CIRCLE:     // circle
                {
-                       LyXFont tmpf(font);
+                       LyXFont tmpf = font;
                        tmpf.decSize().decSize();
-                       pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0xB0, tmpf) - font_metrics::lbearing(0xB0, tmpf)) / 2.0),
                                  int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0xB0, tmpf) - (font_metrics::ascent(0xB0, tmpf) + font_metrics::descent(0xB0, tmpf)) / 3.0),
                                  char(0xB0), tmpf);
                        break;
                }
                case TIE:     // tie
                {
-                       pain.arc(int(x2 + hg35), int(y + hg / 2.0),
-                                int(2 * hg), int(hg), 0, 360 * 32);
+                       pi.pain.arc(int(x2 + hg35), int(y + hg / 2.0),
+                                   int(2 * hg), int(hg), 0, 360 * 32,
+                                   LColor::foreground);
                        break;
                }
                case BREVE:     // breve
                {
-                       pain.arc(int(x2 - (hg / 2.0)), y,
-                                int(hg), int(hg), 0, -360*32);
+                       pi.pain.arc(int(x2 - (hg / 2.0)), y,
+                                   int(hg), int(hg), 0, -360*32,
+                                   LColor::foreground);
                        break;
                }
                case CARON:    // caron
@@ -511,41 +506,41 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                        xp[0] = int(x2 - hg35); yp[0] = int(y + hg35);
                        xp[1] = int(x2);        yp[1] = int(y + hg);
                        xp[2] = int(x2 + hg35); yp[2] = int(y + hg35);
-                       pain.lines(xp, yp, 3);
+                       pi.pain.lines(xp, yp, 3, LColor::foreground);
                        break;
                }
                case SPECIAL_CARON:    // special caron
                {
                        switch (ic) {
-                       case 'L': wid = int(4.0 * wid / 5.0); break;
+                       case 'L': dim_.wid = int(4.0 * dim_.wid / 5.0); break;
                        case 't': y -= int(hg35 / 2.0); break;
                        }
                        int xp[3], yp[3];
-                       xp[0] = int(x + wid);
+                       xp[0] = int(x + dim_.wid);
                        yp[0] = int(y + hg35 + hg);
 
-                       xp[1] = int(x + wid + (hg35 / 2.0));
+                       xp[1] = int(x + dim_.wid + (hg35 / 2.0));
                        yp[1] = int(y + hg + (hg35 / 2.0));
 
-                       xp[2] = int(x + wid + (hg35 / 2.0));
+                       xp[2] = int(x + dim_.wid + (hg35 / 2.0));
                        yp[2] = y + int(hg);
 
-                       pain.lines(xp, yp, 3);
+                       pi.pain.lines(xp, yp, 3, LColor::foreground);
                        break;
                }
                case HUNGARIAN_UMLAUT:    // hung. umlaut
                {
-                       pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing('´', font) - font_metrics::lbearing('´', font))),
                                  baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
                                  '´', font);
-                       pain.text(int(x2),
+                       pi.pain.text(int(x2),
                                  baseline - font_metrics::ascent(ic, font) - font_metrics::descent('´', font) - (font_metrics::ascent('´', font) + font_metrics::descent('´', font)) / 2,
                                  '´', font);
                        break;
                }
                case UMLAUT:    // umlaut
                {
-                       pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing('¨', font) - font_metrics::lbearing('¨', font)) / 2),
                                  baseline - font_metrics::ascent(ic, font) - font_metrics::descent('¨', font) - (font_metrics::ascent('¨', font) + font_metrics::descent('¨', font)) / 2,
                                  '¨', font);
                        break;
@@ -554,7 +549,7 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                {
                        LyXFont tmpf(font);
                        tmpf.decSize().decSize().decSize();
-                       pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
+                       pi.pain.text(int(x2 - (font_metrics::rbearing(0x5E, tmpf) - font_metrics::lbearing(0x5E, tmpf)) / 2),
                                  int(baseline - font_metrics::ascent(ic, font) - font_metrics::descent(0x5E, tmpf) - (font_metrics::ascent(0x5E, tmpf) + font_metrics::descent(0x5E, tmpf)) / 3.0),
                                  char(0x5E), tmpf);
                        break;
@@ -577,7 +572,7 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                        xp[3] = int(x2 + hg / 4.0);
                        yp[3] = y + int(hg);
 
-                       pain.lines(xp, yp, 4);
+                       pi.pain.lines(xp, yp, 4, LColor::foreground);
                        break;
                }
                case lSLASH:
@@ -588,10 +583,10 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                        xp[0] = int(x);
                        yp[0] = y + int(3.0 * hg);
 
-                       xp[1] = int(x + float(wid) * 0.75);
+                       xp[1] = int(x + float(dim_.wid) * 0.75);
                        yp[1] = y + int(hg);
 
-                       pain.lines(xp, yp, 2);
+                       pi.pain.lines(xp, yp, 2, LColor::foreground);
                        break;
                }
                case DOT_LESS_I: // dotless-i
@@ -602,28 +597,25 @@ void InsetLatexAccent::draw(BufferView * bv, LyXFont const & font0,
                }
                }
        } else {
-               pain.fillRectangle(int(x + 1),
-                                  baseline - ascent(bv, font) + 1,
-                                  width(bv, font) - 2,
-                                  ascent(bv, font)
-                                  + descent(bv, font) - 2,
-                                  backgroundColor());
-               pain.rectangle(int(x + 1), baseline - ascent(bv, font) + 1,
-                              width(bv, font) - 2,
-                              ascent(bv, font) + descent(bv, font) - 2);
-               pain.text(int(x + 2), baseline, contents, font);
+               pi.pain.fillRectangle(x + 1,
+                                     baseline - dim_.asc + 1, dim_.wid - 2,
+                                     dim_.asc + dim_.des - 2,
+                                     backgroundColor());
+               pi.pain.rectangle(x + 1, baseline - dim_.asc + 1,
+                                 dim_.wid - 2, dim_.asc + dim_.des - 2,
+                                 LColor::foreground);
+               pi.pain.text(x + 2, baseline, contents, font);
        }
-       x +=  width(bv, font);
 }
 
 
-void InsetLatexAccent::write(Buffer const *, ostream & os) const
+void InsetLatexAccent::write(Buffer const &, ostream & os) const
 {
        os << "\\i " << contents << "\n";
 }
 
 
-void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
+void InsetLatexAccent::read(Buffer const &, LyXLex & lex)
 {
        lex.eatLine();
        contents = lex.getString();
@@ -631,29 +623,29 @@ void InsetLatexAccent::read(Buffer const *, LyXLex & lex)
 }
 
 
-int InsetLatexAccent::latex(Buffer const *, ostream & os,
-                           bool /*fragile*/, bool/*fs*/) const
+int InsetLatexAccent::latex(Buffer const &, ostream & os,
+                           LatexRunParams const &) const
 {
        os << contents;
        return 0;
 }
 
 
-int InsetLatexAccent::ascii(Buffer const *, ostream & os, int) const
+int InsetLatexAccent::ascii(Buffer const &, ostream & os, int) const
 {
        os << contents;
        return 0;
 }
 
 
-int InsetLatexAccent::linuxdoc(Buffer const *, ostream & os) const
+int InsetLatexAccent::linuxdoc(Buffer const &, ostream & os) const
 {
        os << contents;
        return 0;
 }
 
 
-int InsetLatexAccent::docbook(Buffer const *, ostream & os, bool) const
+int InsetLatexAccent::docbook(Buffer const &, ostream & os, bool) const
 {
        os << contents;
        return 0;
@@ -666,15 +658,15 @@ bool InsetLatexAccent::directWrite() const
 }
 
 
-Inset * InsetLatexAccent::clone(Buffer const &, bool) const
+auto_ptr<InsetBase> InsetLatexAccent::clone() const
 {
-       return new InsetLatexAccent(contents);
+       return auto_ptr<InsetBase>(new InsetLatexAccent(contents));
 }
 
 
-Inset::Code InsetLatexAccent::lyxCode() const
+InsetOld::Code InsetLatexAccent::lyxCode() const
 {
-       return Inset::ACCENT_CODE;
+       return InsetOld::ACCENT_CODE;
 }