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) {
- metrics_cache_[i].width = -1000;
- metrics_cache_[i].ascent = -1000;
- metrics_cache_[i].descent = -1000;
- }
-#endif
}
#else
-void GuiFontMetrics::fillCache(unsigned short val) const
+void GuiFontMetrics::fillMetricsCache(char_type c) const
{
- QRect const & r = metrics_.boundingRect(QChar(val));
- metrics_cache_[val].descent = r.bottom() + 1;
- metrics_cache_[val].ascent = -r.top();
+ 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_[val].width = metrics_.width(QChar(val));
+ metrics_cache_.insert(c, ad);
}
int GuiFontMetrics::width(char_type c) const
{
- unsigned short val = static_cast<unsigned short>(c);
- if (metrics_cache_[val].width == -1000)
- metrics_cache_[val].width = metrics_.width(QChar(val));
+ if (!width_cache_.contains(c)) {
+ width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
+ }
- return metrics_cache_[val].width;
+ return width_cache_.value(c);
}
int GuiFontMetrics::ascent(char_type c) const
{
- unsigned short val = static_cast<unsigned short>(c);
- if (metrics_cache_[val].ascent == -1000)
- fillCache(val);
+ if (!metrics_cache_.contains(c))
+ fillMetricsCache(c);
- return metrics_cache_[val].ascent;
+ return metrics_cache_.value(c).ascent;
}
int GuiFontMetrics::descent(char_type c) const
{
- unsigned short val = static_cast<unsigned short>(c);
- if (metrics_cache_[val].descent == -1000)
- fillCache(val);
+ if (!metrics_cache_.contains(c))
+ fillMetricsCache(c);
- return metrics_cache_[val].descent;
+ return metrics_cache_.value(c).descent;
}
#endif