From d3979e798cd9c85041bb5e6158002384a8cddb8c Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Wed, 20 Mar 2019 10:56:16 +0100 Subject: [PATCH] Cache the value of GuiFontMetrics::lbearing(). This seems to be necessary on windows, where math editing can get very slow. Note that other methods like rbearing already use a cache. In the future all these caches for single characters shall be unified. --- src/frontends/qt4/GuiFontMetrics.cpp | 31 ++++++++++++++++++---------- src/frontends/qt4/GuiFontMetrics.h | 2 ++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/frontends/qt4/GuiFontMetrics.cpp b/src/frontends/qt4/GuiFontMetrics.cpp index b57f514aa6..5f70c8d705 100644 --- a/src/frontends/qt4/GuiFontMetrics.cpp +++ b/src/frontends/qt4/GuiFontMetrics.cpp @@ -148,20 +148,29 @@ int GuiFontMetrics::strikeoutPos() const } -int GuiFontMetrics::lbearing(char_type c) const -{ - if (!is_utf16(c)) - // FIXME: QFontMetrics::leftBearing does not support the - // full unicode range. Once it does, we could use: - //return metrics_.leftBearing(toqstr(docstring(1, c))); - return 0; - - return metrics_.leftBearing(ucs4_to_qchar(c)); +namespace { +int const outOfLimitMetric = -10000; } -namespace { -int const outOfLimitMetric = -10000; +int GuiFontMetrics::lbearing(char_type c) const +{ + int value = lbearing_cache_.value(c, outOfLimitMetric); + if (value != outOfLimitMetric) + return value; + + if (is_utf16(c)) + value = metrics_.leftBearing(ucs4_to_qchar(c)); + else { + // FIXME: QFontMetrics::leftBearing does not support the + // full unicode range. Once it does, we could use: + // metrics_.leftBearing(toqstr(docstring(1, c))); + value = 0; + } + + lbearing_cache_.insert(c, value); + + return value; } diff --git a/src/frontends/qt4/GuiFontMetrics.h b/src/frontends/qt4/GuiFontMetrics.h index 6d50d35425..44897c459e 100644 --- a/src/frontends/qt4/GuiFontMetrics.h +++ b/src/frontends/qt4/GuiFontMetrics.h @@ -103,6 +103,8 @@ private: /// fill in \c metrics_cache_ at specified value. AscendDescend const fillMetricsCache(char_type) const; + /// Cache of char leftt bearings + mutable QHash lbearing_cache_; /// Cache of char right bearings mutable QHash rbearing_cache_;