summaryrefslogtreecommitdiff
path: root/vcl
diff options
context:
space:
mode:
authorJan-Marek Glogowski <glogow@fbihome.de>2018-10-04 14:03:07 +0200
committerJan-Marek Glogowski <glogow@fbihome.de>2018-10-06 15:17:52 +0200
commit612339e574293b248c44cc04a4fae0c77a64ad53 (patch)
treeebc9a280800f40e26ef32fa9189845ba74bc9c40 /vcl
parent4435135e68f7d1024875defc3df18de183607048 (diff)
Add a glyph-bound-rect cache to the font cache
This way the font cache can correctly invalidate the cached glyph rects when a font is dropped from the cache. Change-Id: I050866099742334f01cac1b872228a017ddb5e9b Reviewed-on: https://gerrit.libreoffice.org/61371 Tested-by: Jenkins Reviewed-by: Jan-Marek Glogowski <glogow@fbihome.de>
Diffstat (limited to 'vcl')
-rw-r--r--vcl/inc/fontinstance.hxx6
-rw-r--r--vcl/inc/impfontcache.hxx48
-rw-r--r--vcl/source/font/fontcache.cxx38
-rw-r--r--vcl/source/font/fontinstance.cxx13
4 files changed, 98 insertions, 7 deletions
diff --git a/vcl/inc/fontinstance.hxx b/vcl/inc/fontinstance.hxx
index 597c747553ac..b3b67c8a9079 100644
--- a/vcl/inc/fontinstance.hxx
+++ b/vcl/inc/fontinstance.hxx
@@ -25,6 +25,9 @@
#include <rtl/ref.hxx>
#include <salhelper/simplereferenceobject.hxx>
+#include <tools/gen.hxx>
+#include <vcl/vcllayout.hxx>
+
#include <unordered_map>
#include <memory>
@@ -67,6 +70,9 @@ public: // TODO: make data members private
const PhysicalFontFace* GetFontFace() const { return m_pFontFace.get(); }
const ImplFontCache* GetFontCache() const { return mpFontCache; }
+ bool GetCachedGlyphBoundRect(sal_GlyphId, tools::Rectangle &);
+ void CacheGlyphBoundRect(sal_GlyphId nID, tools::Rectangle &);
+
int GetKashidaWidth();
void GetScale(double* nXScale, double* nYScale);
diff --git a/vcl/inc/impfontcache.hxx b/vcl/inc/impfontcache.hxx
index 10a38540e67b..c96994300176 100644
--- a/vcl/inc/impfontcache.hxx
+++ b/vcl/inc/impfontcache.hxx
@@ -21,6 +21,11 @@
#define INCLUDED_VCL_INC_IMPFONTCACHE_HXX
#include <unordered_map>
+#include <boost/functional/hash.hpp>
+
+#include <o3tl/lru_map.hxx>
+#include <tools/gen.hxx>
+#include <vcl/vcllayout.hxx>
#include "fontselect.hxx"
@@ -28,25 +33,53 @@ class Size;
namespace vcl { class Font; }
class PhysicalFontCollection;
-
// TODO: closely couple with PhysicalFontCollection
+struct GlpyhBoundRectCacheKey
+{
+ const LogicalFontInstance* m_pFont;
+ const sal_GlyphId m_nId;
+
+ GlpyhBoundRectCacheKey(const LogicalFontInstance* pFont, sal_GlyphId nID)
+ : m_pFont(pFont), m_nId(nID)
+ {}
+
+ bool operator==(GlpyhBoundRectCacheKey const& aOther) const
+ { return m_pFont == aOther.m_pFont && m_nId == aOther.m_nId; }
+};
+
+struct GlpyhBoundRectCacheHash
+{
+ std::size_t operator()(GlpyhBoundRectCacheKey const& aCache) const
+ {
+ std::size_t seed = 0;
+ boost::hash_combine(seed, aCache.m_pFont);
+ boost::hash_combine(seed, aCache.m_nId);
+ return seed;
+ }
+};
+
+typedef o3tl::lru_map<GlpyhBoundRectCacheKey, tools::Rectangle,
+ GlpyhBoundRectCacheHash> GlpyhBoundRectCache;
+typedef GlpyhBoundRectCache::key_value_pair_t GlpyhBoundRectCachePair;
+
class ImplFontCache
{
private:
- LogicalFontInstance* mpLastHitCacheEntry; ///< keeps the last hit cache entry
-
// cache of recently used font instances
struct IFSD_Equal { bool operator()( const FontSelectPattern&, const FontSelectPattern& ) const; };
struct IFSD_Hash { size_t operator()( const FontSelectPattern& ) const; };
typedef std::unordered_map<FontSelectPattern, rtl::Reference<LogicalFontInstance>, IFSD_Hash, IFSD_Equal> FontInstanceList;
- FontInstanceList maFontInstanceList;
+
+ LogicalFontInstance* mpLastHitCacheEntry; ///< keeps the last hit cache entry
+ FontInstanceList maFontInstanceList;
+ GlpyhBoundRectCache m_aBoundRectCache;
rtl::Reference<LogicalFontInstance> GetFontInstance(PhysicalFontCollection const*, FontSelectPattern&);
public:
- ImplFontCache();
- ~ImplFontCache();
+ ImplFontCache();
+ ~ImplFontCache();
rtl::Reference<LogicalFontInstance> GetFontInstance( PhysicalFontCollection const *,
const vcl::Font&, const Size& rPixelSize, float fExactHeight);
@@ -54,6 +87,9 @@ public:
LogicalFontInstance* pLogicalFont,
int nFallbackLevel, OUString& rMissingCodes );
+ bool GetCachedGlyphBoundRect(LogicalFontInstance *, sal_GlyphId, tools::Rectangle &);
+ void CacheGlyphBoundRect(LogicalFontInstance *, sal_GlyphId, tools::Rectangle &);
+
void Invalidate();
};
diff --git a/vcl/source/font/fontcache.cxx b/vcl/source/font/fontcache.cxx
index 82a6261c274c..0db997fe510f 100644
--- a/vcl/source/font/fontcache.cxx
+++ b/vcl/source/font/fontcache.cxx
@@ -84,11 +84,15 @@ bool ImplFontCache::IFSD_Equal::operator()(const FontSelectPattern& rA, const Fo
}
ImplFontCache::ImplFontCache()
-: mpLastHitCacheEntry( nullptr )
+ : mpLastHitCacheEntry( nullptr )
+ // The cache limit is set by the rough number of characters needed to read your average Asian newspaper.
+ , m_aBoundRectCache(3000)
{}
ImplFontCache::~ImplFontCache()
{
+ for (auto & rLFI : maFontInstanceList)
+ rLFI.second->mpFontCache = nullptr;
}
rtl::Reference<LogicalFontInstance> ImplFontCache::GetFontInstance( PhysicalFontCollection const * pFontList,
@@ -174,6 +178,9 @@ rtl::Reference<LogicalFontInstance> ImplFontCache::GetFontInstance( PhysicalFont
++it_next;
continue;
}
+ m_aBoundRectCache.remove_if([&pFontEntry] (GlpyhBoundRectCachePair const& rPair)
+ { return rPair.first.m_pFont == pFontEntry; } );
+
maFontInstanceList.erase(it_next);
if (mpLastHitCacheEntry == pFontEntry)
mpLastHitCacheEntry = nullptr;
@@ -233,6 +240,35 @@ void ImplFontCache::Invalidate()
for (auto const & pair : maFontInstanceList)
pair.second->mpFontCache = nullptr;
maFontInstanceList.clear();
+ m_aBoundRectCache.clear();
+}
+
+bool ImplFontCache::GetCachedGlyphBoundRect(LogicalFontInstance *pFont, sal_GlyphId nID, tools::Rectangle &rRect)
+{
+ if (!pFont->GetFontCache())
+ return false;
+ assert(pFont->GetFontCache() == this);
+ if (pFont->GetFontCache() != this)
+ return false;
+
+ auto it = m_aBoundRectCache.find({pFont, nID});
+ if (it != m_aBoundRectCache.end())
+ {
+ rRect = it->second;
+ return true;
+ }
+ return false;
+}
+
+void ImplFontCache::CacheGlyphBoundRect(LogicalFontInstance *pFont, sal_GlyphId nID, tools::Rectangle &rRect)
+{
+ if (!pFont->GetFontCache())
+ return;
+ assert(pFont->GetFontCache() == this);
+ if (pFont->GetFontCache() != this)
+ return;
+
+ m_aBoundRectCache.insert({{pFont, nID}, rRect});
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/font/fontinstance.cxx b/vcl/source/font/fontinstance.cxx
index cf80428f61e2..7a889fa65a7c 100644
--- a/vcl/source/font/fontinstance.cxx
+++ b/vcl/source/font/fontinstance.cxx
@@ -147,5 +147,18 @@ void LogicalFontInstance::IgnoreFallbackForUnicode( sal_UCS4 cChar, FontWeight e
mpUnicodeFallbackList->erase( it );
}
+bool LogicalFontInstance::GetCachedGlyphBoundRect(sal_GlyphId nID, tools::Rectangle &rRect)
+{
+ if (!mpFontCache)
+ return false;
+ return mpFontCache->GetCachedGlyphBoundRect(this, nID, rRect);
+}
+
+void LogicalFontInstance::CacheGlyphBoundRect(sal_GlyphId nID, tools::Rectangle &rRect)
+{
+ if (!mpFontCache)
+ return;
+ mpFontCache->CacheGlyphBoundRect(this, nID, rRect);
+}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */