[EMAIL PROTECTED] wrote:
Author: younes
Date: Mon Aug 13 15:56:54 2007
New Revision: 19501

URL: http://www.lyx.org/trac/changeset/19501
Log:
Optimisation: don't search twice the hash tables.

I can do that for 1.5.x too if you want Jurgen.

Abdel.


Modified:
    lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.cpp
    lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.h

Modified: lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.cpp
URL: 
http://www.lyx.org/trac/file/lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.cpp?rev=19501
==============================================================================
--- lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.cpp (original)
+++ lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.cpp Mon Aug 13 15:56:54 
2007
@@ -70,23 +70,30 @@
 }
+namespace {
+int const outOfLimitMetric = -10000;
+}
+
+
 int GuiFontMetrics::rbearing(char_type c) const
 {
-       if (!rbearing_cache_.contains(c)) {
-               // Qt rbearing is from the right edge of the char's width().
-               int rb;
-               if (is_utf16(c)) {
-                       QChar sc = ucs4_to_qchar(c);
-                       rb = width(c) - metrics_.rightBearing(sc);
-               } else
-                       // FIXME: QFontMetrics::leftBearingdoes not support the
-                       //        full unicode range. Once it does, we could 
use:
-                       // metrics_.rightBearing(toqstr(docstring(1,c)));
-                       rb = width(c);
-
-               rbearing_cache_.insert(c, rb);
-       }
-       return rbearing_cache_.value(c);
+       int value = rbearing_cache_.value(c, outOfLimitMetric);
+       if (value != outOfLimitMetric)
+               return value;
+
+       // Qt rbearing is from the right edge of the char's width().
+       if (is_utf16(c)) {
+               QChar sc = ucs4_to_qchar(c);
+               value = width(c) - metrics_.rightBearing(sc);
+       } else
+               // FIXME: QFontMetrics::leftBearingdoes not support the
+               //        full unicode range. Once it does, we could use:
+               // metrics_.rightBearing(toqstr(docstring(1,c)));
+               value = width(c);
+
+       rbearing_cache_.insert(c, value);
+
+       return value;
 }
@@ -173,7 +180,8 @@
 }
-void GuiFontMetrics::fillMetricsCache(char_type c) const
+GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
+               char_type c) const
 {
        QRect r;
        if (is_utf16(c))
@@ -185,6 +193,8 @@
        // 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);
+
+       return ad;
 }
@@ -193,32 +203,44 @@
        if (smallcaps_shape_)
                return smallcapsWidth(c);
- if (!width_cache_.contains(c)) {
-               if (is_utf16(c))
-                       width_cache_.insert(c, 
metrics_.width(ucs4_to_qchar(c)));
-               else
-                       width_cache_.insert(c, 
metrics_.width(toqstr(docstring(1,c))));
-       }
-
-       return width_cache_.value(c);
+       int value = width_cache_.value(c, outOfLimitMetric);
+       if (value != outOfLimitMetric)
+               return value;
+
+       if (is_utf16(c))
+               value = metrics_.width(ucs4_to_qchar(c));
+       else
+               value = metrics_.width(toqstr(docstring(1,c)));
+
+       width_cache_.insert(c, value);
+
+       return value;
 }
int GuiFontMetrics::ascent(char_type c) const
 {
-       if (!metrics_cache_.contains(c))
-               fillMetricsCache(c);
-
-       return metrics_cache_.value(c).ascent;
+ static AscendDescend const outOfLimitAD = + {outOfLimitMetric, outOfLimitMetric};
+       AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
+       if (value.ascent != outOfLimitMetric)
+               return value.ascent;
+
+       value = fillMetricsCache(c);
+       return value.ascent;
 }
int GuiFontMetrics::descent(char_type c) const
 {
-       if (!metrics_cache_.contains(c))
-               fillMetricsCache(c);
-
-       return metrics_cache_.value(c).descent;
+ static AscendDescend const outOfLimitAD = + {outOfLimitMetric, outOfLimitMetric};
+       AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
+       if (value.descent != outOfLimitMetric)
+               return value.descent;
+
+       value = fillMetricsCache(c);
+       return value.descent;
 }
} // frontend

Modified: lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.h
URL: 
http://www.lyx.org/trac/file/lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.h?rev=19501
==============================================================================
--- lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.h (original)
+++ lyx-devel/trunk/src/frontends/qt4/GuiFontMetrics.h Mon Aug 13 15:56:54 2007
@@ -74,7 +74,7 @@
        /// Cache of char ascends and descends
        mutable QHash<char_type, AscendDescend> metrics_cache_;
        /// fill in \c metrics_cache_ at specified value.
-       void fillMetricsCache(char_type) const;
+       AscendDescend const fillMetricsCache(char_type) const;
/// Cache of char right bearings
        mutable QHash<char_type, int> rbearing_cache_;




Reply via email to