]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt2/qfont_metrics.C
Some string(widget->text()) fixes. Weirdness
[lyx.git] / src / frontends / qt2 / qfont_metrics.C
index 5f3963c386fea0f78b277ee4974c86a08dcd355e..3019577e8232f66d08c443883baa5c9341ceb5d4 100644 (file)
@@ -1,15 +1,16 @@
 /**
  * \file qfont_metrics.C
- * Copyright 1995-2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
  * \author unknown
- * \author John Levon <moz@compsoc.man.ac.uk>
+ * \author John Levon
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
 
-
 #ifdef __GNUG__
 #pragma implementation "frontends/font_metrics.h"
 #endif
 #include "font_metrics.h"
 #include "qfont_loader.h"
 #include "debug.h"
+#include "encoding.h"
+#include "language.h"
 
 #include <qfontmetrics.h>
 #include <qfont.h>
 
+
 namespace {
-       QFontMetrics const & metrics(LyXFont const & f) {
-               return fontloader.metrics(f);
-       }
+
+QFontMetrics const & metrics(LyXFont const & f)
+{
+       return fontloader.metrics(f);
 }
 
+} // namespace anon
+
+
 namespace font_metrics {
+
 int maxAscent(LyXFont const & f)
 {
        return metrics(f).ascent();
@@ -39,21 +46,23 @@ int maxAscent(LyXFont const & f)
 
 int maxDescent(LyXFont const & f)
 {
-       return metrics(f).descent();
+       return metrics(f).descent()+1;
+       // We add 1 as the value returned by QT is different than X
+       // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
 }
 
 
 int ascent(char c, LyXFont const & f)
 {
        QRect r = metrics(f).boundingRect(c);
-       return abs(r.top());
+       return -r.top();
 }
 
 
 int descent(char c, LyXFont const & f)
 {
        QRect r = metrics(f).boundingRect(c);
-       return abs(r.bottom());
+       return r.bottom()+1;
 }
 
 
@@ -65,18 +74,35 @@ int lbearing(char c, LyXFont const & f)
 
 int rbearing(char c, LyXFont const & f)
 {
-       return metrics(f).rightBearing(c);
+       QFontMetrics const & m(metrics(f));
+
+       // Qt rbearing is from the right edge of the char's width().
+       return (m.width(c) - m.rightBearing(c));
 }
 
 
 int width(char const * s, size_t ls, LyXFont const & f)
 {
+       Encoding const * encoding = f.language()->encoding();
+       if (f.isSymbolFont())
+               encoding = encodings.symbol_encoding();
+
+       QString str;
+#if QT_VERSION >= 300
+       str.setLength(ls);
+       for (size_t i = 0; i < ls; ++i)
+               str[i] = QChar(encoding->ucs(s[i]));
+#else
+       for (size_t i = 0; i < ls; ++i)
+               str += QChar(encoding->ucs(s[i]));
+#endif
+
        if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
-               return metrics(f).width(s, ls);
+               return metrics(f).width(str);
        }
 
        // handle small caps ourselves ...
+
        LyXFont smallfont(f);
        smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
 
@@ -86,11 +112,11 @@ int width(char const * s, size_t ls, LyXFont const & f)
        int w = 0;
 
        for (size_t i = 0; i < ls; ++i) {
-               char const c = uppercase(s[i]);
-               if (c != s[i])
-                       w += qsmallm.width(&c, 1);
+               QChar const c = str[i].upper();
+               if (c != str[i])
+                       w += qsmallm.width(c);
                else
-                       w += qm.width(&c, 1);
+                       w += qm.width(c);
        }
        return w;
 }
@@ -107,13 +133,13 @@ int signedWidth(string const & s, LyXFont const & f)
 
 void rectText(string const & str, LyXFont const & f,
        int & w,
-       int & ascent, 
+       int & ascent,
        int & descent)
 {
        QFontMetrics const & m(metrics(f));
+
        static int const d = 2;
+
        w = width(str, f) + d * 2 + 2;
        ascent = m.ascent() + d;
        descent = m.descent() + d;
@@ -122,14 +148,14 @@ void rectText(string const & str, LyXFont const & f,
 
 
 void buttonText(string const & str, LyXFont const & f,
-       int & w, 
-       int & ascent, 
+       int & w,
+       int & ascent,
        int & descent)
 {
        QFontMetrics const & m(metrics(f));
+
        static int const d = 3;
-       
+
        w = width(str, f) + d * 2 + 2;
        ascent = m.ascent() + d;
        descent = m.descent() + d;