From 5f4e6d9a7c829a51321c0ff612d33cb27b9b6333 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Fri, 1 Dec 2006 17:23:58 +0000 Subject: [PATCH] * GuiFontMetrics - add constants instead of hard-coded values. - add assertions and comments for out of range values. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16126 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/GuiFontMetrics.C | 34 ++++++++++++++++++++++++------ src/frontends/qt4/GuiFontMetrics.h | 4 +++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/frontends/qt4/GuiFontMetrics.C b/src/frontends/qt4/GuiFontMetrics.C index a10381096f..d915a6f94e 100644 --- a/src/frontends/qt4/GuiFontMetrics.C +++ b/src/frontends/qt4/GuiFontMetrics.C @@ -24,15 +24,20 @@ using std::string; namespace lyx { namespace frontend { +namespace { +// Used for checking initialisation state of the C-ish metrics table. +const int BadMetrics = -1000; +} + GuiFontMetrics::GuiFontMetrics(QFont const & font) : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false) { #ifdef USE_LYX_FONTCACHE - for (int i = 0; i != 65536; ++i) { - metrics_cache_[i].width = -1000; - metrics_cache_[i].ascent = -1000; - metrics_cache_[i].descent = -1000; + for (int i = 0; i != MaxCharType; ++i) { + metrics_cache_[i].width = BadMetrics; + metrics_cache_[i].ascent = BadMetrics; + metrics_cache_[i].descent = BadMetrics; } #endif } @@ -193,8 +198,13 @@ void GuiFontMetrics::fillCache(unsigned short val) const int GuiFontMetrics::width(char_type c) const { + // FIXME: The following cast is not a real conversion but it work + // for the ucs2 subrange of unicode. Instead of an assertion we should + // give the metrics of some special characters that indicates that + // its display is not supported. + BOOST_ASSERT(c < MaxCharType); unsigned short val = static_cast(c); - if (metrics_cache_[val].width == -1000) + if (metrics_cache_[val].width == BadMetrics) metrics_cache_[val].width = metrics_.width(QChar(val)); return metrics_cache_[val].width; @@ -203,8 +213,13 @@ int GuiFontMetrics::width(char_type c) const int GuiFontMetrics::ascent(char_type c) const { + // FIXME: The following cast is not a real conversion but it work + // for the ucs2 subrange of unicode. Instead of an assertion we should + // give the metrics of some special characters that indicates that + // its display is not supported. + BOOST_ASSERT(c < MaxCharType); unsigned short val = static_cast(c); - if (metrics_cache_[val].ascent == -1000) + if (metrics_cache_[val].ascent == BadMetrics) fillCache(val); return metrics_cache_[val].ascent; @@ -213,8 +228,13 @@ int GuiFontMetrics::ascent(char_type c) const int GuiFontMetrics::descent(char_type c) const { + // FIXME: The following cast is not a real conversion but it work + // for the ucs2 subrange of unicode. Instead of an assertion we should + // give the metrics of some special characters that indicates that + // its display is not supported. + BOOST_ASSERT(c < MaxCharType); unsigned short val = static_cast(c); - if (metrics_cache_[val].descent == -1000) + if (metrics_cache_[val].descent == BadMetrics) fillCache(val); return metrics_cache_[val].descent; diff --git a/src/frontends/qt4/GuiFontMetrics.h b/src/frontends/qt4/GuiFontMetrics.h index d310741cc3..f3c87adc28 100644 --- a/src/frontends/qt4/GuiFontMetrics.h +++ b/src/frontends/qt4/GuiFontMetrics.h @@ -27,6 +27,8 @@ namespace lyx { namespace frontend { +size_t const MaxCharType = 65536; + struct CharMetrics { int width; @@ -88,7 +90,7 @@ private: * this turns out to be too much, we can switch to a \c QHash based * solution. **/ - mutable CharMetrics metrics_cache_[65536]; + mutable CharMetrics metrics_cache_[MaxCharType]; #endif // USE_LYX_FONTCACHE }; -- 2.39.2