]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiFontMetrics.C
* qt_helpers.h:
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.C
index d1ed0ecea854108bd61b293bb2edbd171b7ce40b..ecfe48722e07ae3c5bf8d75683f58db5147b9d11 100644 (file)
 
 #include "support/unicode.h"
 
-using lyx::char_type;
-using lyx::docstring;
-
 using std::string;
 
 namespace lyx {
 namespace frontend {
 
-
 GuiFontMetrics::GuiFontMetrics(QFont const & font)
 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
 {
-#ifdef USE_LYX_FONTCACHE
-  for (int i = 0; i != 65536; ++i)
-    widthcache_[i] = -1;
-#endif
 }
 
 
@@ -58,20 +50,6 @@ int GuiFontMetrics::maxDescent() const
 }
 
 
-int GuiFontMetrics::ascent(char_type c) const
-{
-       QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
-       return -r.top();
-}
-
-
-int GuiFontMetrics::descent(char_type c) const
-{
-       QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
-       return r.bottom() + 1;
-}
-
-
 int GuiFontMetrics::lbearing(char_type c) const
 {
        return metrics_.leftBearing(ucs4_to_qchar(c));
@@ -110,19 +88,18 @@ int GuiFontMetrics::width(char_type const * s, size_t ls) const
        // casts in reality.
 
        if (ls == 1 && !smallcaps_shape_) {
-               QChar const c = ucs4_to_qchar(s[0]);
-               return width(c.unicode());
+               return width(s[0]);
        }
 
-       QString ucs2;
-       ucs4_to_qstring(s, ls, ucs2);
-
-       if (smallcaps_shape_)
+       if (smallcaps_shape_) {
+               QString ucs2;
+               ucs4_to_qstring(s, ls, ucs2);
                return smallcapsWidth(ucs2);
+       }
 
        int w = 0;
        for (unsigned int i = 0; i < ls; ++i)
-               w += width(ucs2[i].unicode());
+               w += width(s[i]);
 
        return w;
 }
@@ -178,14 +155,61 @@ void GuiFontMetrics::buttonText(docstring const & str,
        descent = metrics_.descent() + d;
 }
 
-#ifdef USE_LYX_FONTCACHE
-int GuiFontMetrics::width(unsigned short val) const
+#ifndef USE_LYX_FONTCACHE
+
+int GuiFontMetrics::ascent(char_type c) const
 {
-       if (widthcache_[val] == -1)
-               widthcache_[val] = metrics_.width(QChar(val));
-       return widthcache_[val];
+       QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
+       return -r.top();
 }
-#endif
 
+
+int GuiFontMetrics::descent(char_type c) const
+{
+       QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
+       return r.bottom() + 1;
+}
+
+#else
+
+void GuiFontMetrics::fillMetricsCache(char_type c) const
+{
+       QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
+       AscendDescend ad = { -r.top(), r.bottom() + 1};
+       // We could as well compute the width but this is not really
+       // needed for now as it is done directly in width() below.
+       metrics_cache_.insert(c, ad);
+}
+
+
+int GuiFontMetrics::width(char_type c) const
+{
+       if (!width_cache_.contains(c)) {
+               width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
+       }
+
+       return width_cache_.value(c);
 }
+
+
+int GuiFontMetrics::ascent(char_type c) const
+{
+       if (!metrics_cache_.contains(c))
+               fillMetricsCache(c);
+
+       return metrics_cache_.value(c).ascent;
+}
+
+
+int GuiFontMetrics::descent(char_type c) const
+{
+       if (!metrics_cache_.contains(c))
+               fillMetricsCache(c);
+
+       return metrics_cache_.value(c).descent;
 }
+
+#endif
+
+} // frontend
+} // lyx