Optimisation: don't search twice the hash tables.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@19501 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-08-13 13:56:54 +00:00
parent bc0a64efc3
commit 484dd46b35
2 changed files with 51 additions and 29 deletions

View File

@ -70,23 +70,30 @@ int GuiFontMetrics::lbearing(char_type c) const
} }
namespace {
int const outOfLimitMetric = -10000;
}
int GuiFontMetrics::rbearing(char_type c) const int GuiFontMetrics::rbearing(char_type c) const
{ {
if (!rbearing_cache_.contains(c)) { int value = rbearing_cache_.value(c, outOfLimitMetric);
// Qt rbearing is from the right edge of the char's width(). if (value != outOfLimitMetric)
int rb; return value;
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); // Qt rbearing is from the right edge of the char's width().
} if (is_utf16(c)) {
return rbearing_cache_.value(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 @@ Dimension const GuiFontMetrics::dimension(char_type c) const
} }
void GuiFontMetrics::fillMetricsCache(char_type c) const GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
char_type c) const
{ {
QRect r; QRect r;
if (is_utf16(c)) if (is_utf16(c))
@ -185,6 +193,8 @@ void GuiFontMetrics::fillMetricsCache(char_type c) const
// We could as well compute the width but this is not really // We could as well compute the width but this is not really
// needed for now as it is done directly in width() below. // needed for now as it is done directly in width() below.
metrics_cache_.insert(c, ad); metrics_cache_.insert(c, ad);
return ad;
} }
@ -193,32 +203,44 @@ int GuiFontMetrics::width(char_type c) const
if (smallcaps_shape_) if (smallcaps_shape_)
return smallcapsWidth(c); return smallcapsWidth(c);
if (!width_cache_.contains(c)) { int value = width_cache_.value(c, outOfLimitMetric);
if (is_utf16(c)) if (value != outOfLimitMetric)
width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c))); return value;
else
width_cache_.insert(c, metrics_.width(toqstr(docstring(1,c))));
}
return width_cache_.value(c); 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 int GuiFontMetrics::ascent(char_type c) const
{ {
if (!metrics_cache_.contains(c)) static AscendDescend const outOfLimitAD =
fillMetricsCache(c); {outOfLimitMetric, outOfLimitMetric};
AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
if (value.ascent != outOfLimitMetric)
return value.ascent;
return metrics_cache_.value(c).ascent; value = fillMetricsCache(c);
return value.ascent;
} }
int GuiFontMetrics::descent(char_type c) const int GuiFontMetrics::descent(char_type c) const
{ {
if (!metrics_cache_.contains(c)) static AscendDescend const outOfLimitAD =
fillMetricsCache(c); {outOfLimitMetric, outOfLimitMetric};
AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
if (value.descent != outOfLimitMetric)
return value.descent;
return metrics_cache_.value(c).descent; value = fillMetricsCache(c);
return value.descent;
} }
} // frontend } // frontend

View File

@ -74,7 +74,7 @@ private:
/// Cache of char ascends and descends /// Cache of char ascends and descends
mutable QHash<char_type, AscendDescend> metrics_cache_; mutable QHash<char_type, AscendDescend> metrics_cache_;
/// fill in \c metrics_cache_ at specified value. /// fill in \c metrics_cache_ at specified value.
void fillMetricsCache(char_type) const; AscendDescend const fillMetricsCache(char_type) const;
/// Cache of char right bearings /// Cache of char right bearings
mutable QHash<char_type, int> rbearing_cache_; mutable QHash<char_type, int> rbearing_cache_;