[Date Prev][Date Next][Thread Prev][Thread Next][Thread Index]

[XaraXtreme-dev] Kerning pairs cache update and header changes



I have implemented caching for kerning pairs, so text reformatting 
operations no longer call Pango/FreeType.

I have also made some changes to reduce header dependencies. 
Currently, changing implementation details of the metrics or kerning 
caches causes the whole world to be recompiled because fontbase.h is 
indirectly included by camtypes.h (via filters.h) and hence ends up in 
the big lumb of precompiled headers on which everything depends.

I have factored out the tiny part of fontbase.h that is actually 
needed by filters.h and put it in a separate header file fontclass.h, 
so changes to fontbase.h no longer affect the world.

To further reduce dependencies I have tightened the Kernel/OIL 
interface by hiding the cache classes behind facade routines in 
OILFontMan, so the Kernel no longer depends on the cache classes. That 
allows them to be factored out into an implementation-specific header 
file fontimpl.h.

Find attached below a patch and the two new header files (as usual, 
they will need their EOL and Id properties set up).

Kernel/fontman.cpp:
calls the facade routines in OILFontMan instead of directly accessing 
the cache classes

Kernel/filters.h:
includes fontclass.h instead of fontbase.h

wxOil/fontbase.h:
global and implementation-specific declarations removed

wxOil/fontbase.cpp:
implemented facade routines and kerning pairs caching

wxOil/fontclass.h:
new file - global declarations

wxOil/fontimpl.h:
new file - implementation-specific declarations

Martin
Index: Kernel/fontman.cpp
===================================================================
--- Kernel/fontman.cpp	(Revision 852)
+++ Kernel/fontman.cpp	(Arbeitskopie)
@@ -1306,7 +1306,6 @@
 
 BOOL FontManager::FindClosestFontFullTry(FontClass Class, String_64 *pTypeFaceName, ENUMLOGFONT *pEnumLogFont)
 {
-	PORTNOTETRACE("text","FontManager::FindClosestFontFullTry - do nothing");
 	ERROR2IF(pEnumLogFont==NULL, FALSE, "FindClosestTry called with pEnumLogFont==NULL.");
 
 	OUTLINETEXTMETRIC *pOutlineTextMetric;
@@ -1346,7 +1345,7 @@
 
 void FontManager::InvalidateCharMetrics()
 {
-	FontMetricsCache::InvalidateCharMetrics();
+	OILFontMan::InvalidateCharMetrics();
 }
 
 /********************************************************************************************
@@ -1365,7 +1364,7 @@
 
 BOOL FontManager::GetCharMetrics(CNativeDC* pDC, WCHAR ch, CharDescription& FontDesc, CharMetrics* pCharMetrics)
 {
-	return FontMetricsCache::GetCharMetrics(pDC, ch, FontDesc, pCharMetrics);
+	return OILFontMan::GetCharMetrics(pDC, ch, FontDesc, pCharMetrics);
 }
 
 /********************************************************************************************
@@ -1384,7 +1383,7 @@
 MILLIPOINT FontManager::GetCharsKerning(CNativeDC* pDC, WCHAR chLeft, WCHAR chRight, CharDescription& FontDesc)
 {
 	// TRACEUSER("wuerthne", _T("FontManager::GetCharsKerning called\n") );
-	return FontKerningPairsCache::GetCharsKerning(pDC, chLeft, chRight, FontDesc);
+	return OILFontMan::GetCharsKerning(pDC, chLeft, chRight, FontDesc);
 }
 
 /********************************************************************************************
Index: Kernel/filters.h
===================================================================
--- Kernel/filters.h	(Revision 852)
+++ Kernel/filters.h	(Arbeitskopie)
@@ -105,7 +105,7 @@
 #include "attr.h"
 #include "attrmgr.h"
 #include "txtattr.h"
-#include "fontbase.h"
+#include "fontclass.h"
 #include <limits.h>
 #include "oilfltrs.h"
 
Index: wxOil/fontbase.h
===================================================================
--- wxOil/fontbase.h	(Revision 852)
+++ wxOil/fontbase.h	(Arbeitskopie)
@@ -107,6 +107,7 @@
 #ifndef INC_FONTBASE
 #define INC_FONTBASE
 
+#include "fontclass.h"
 #include "pathtype.h"
 #include "txtattr.h"
 #include "ccpanose.h"
@@ -124,38 +125,7 @@
 #endif
 
 /********************************************************************************************
-	The types of fonts the OIL font manager recognises so far
-********************************************************************************************/
 
-typedef enum FontClass {
-	FC_UNDEFINED = 0,
-	FC_RASTER,
-	FC_DEVICE,
-	FC_TRUETYPE,
-	FC_ATM,
-	FC_FREETYPE,
-
-	// Add further font classes before this
-	FC_ILLEGAL
-};
-
-// The kernel likes dealing with LOGFONT and ENUMLOGFONT structures, so we do it this favour
-
-#if !defined(__WXMSW__)
-struct LOGFONT
-{
-	String_64 FaceName;
-};
-typedef LOGFONT		   *PLOGFONT, *LPLOGFONT;
-
-struct ENUMLOGFONT
-{
-	LOGFONT elfLogFont;
-};
-#endif
-
-/********************************************************************************************
-
 >	class FontBase : public CCObject
 	   		
 	Author:		Mike_Kenny (Xara Group Ltd) <camelotdev@xxxxxxxx>
@@ -185,172 +155,7 @@
 };
 
 /********************************************************************************************
->	class FontMetricsCacheEntry : public CC_CLASS_MEMDUMP
-	   		
-	Author:		Ed_Cornes (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	14/1/96
-	Purpose:	Holds a cache of char widths, ascent, descent and em for a particular font.
-********************************************************************************************/
 
-class FontMetricsCacheEntry: public CC_CLASS_MEMDUMP
-{
-	CC_DECLARE_MEMDUMP(FontMetricsCacheEntry);
-	
-public:
-	FontMetricsCacheEntry();
-
-	BOOL CacheFontMetrics(wxDC* pDC, CharDescription FontDesc, MILLIPOINT DefaultHeight, INT32 DesignSize);
-
-	void CheckCharWidthsSameAsABCWidths(wxDC* pDC, CharDescription FontDesc);	// debug test
-
-	inline static BOOL CharInCacheRange(WCHAR ch) { return (ch>=FIRSTCHAR && ch<=LASTCHAR); }
-
-	inline INT32 GetCharWidthFromCache(WCHAR ch) { return pCharWidths[ch-FIRSTCHAR]; }
-
-	inline MILLIPOINT      GetFontEmWidth() { return FontEmWidth; }
-	inline MILLIPOINT      GetFontAscent()  { return FontAscent; }
-	inline MILLIPOINT      GetFontDescent() { return FontDescent; }
-	inline CharDescription GetFontDesc()    { return FontDesc; }
-
-	inline void SetFontEmWidth(MILLIPOINT NewFontEmWidth) { FontEmWidth = NewFontEmWidth; }
-	inline void SetFontAscent( MILLIPOINT NewFontAscent)  { FontAscent  = NewFontAscent; }
-	inline void SetFontDescent(MILLIPOINT NewFontDescent) { FontDescent = NewFontDescent; }
-	inline void SetFontDesc(CharDescription NewFontDesc)  { FontDesc    = NewFontDesc; }
-
-protected:
-	// this seems the only way to define constants in a class assigning values
-	enum CharRange { FIRSTCHAR=32, LASTCHAR=127, NUMCHARS=(LASTCHAR-FIRSTCHAR+1) };
-
-	MILLIPOINT pCharWidths[NUMCHARS];
-	MILLIPOINT FontEmWidth;
-	MILLIPOINT FontAscent;
-	MILLIPOINT FontDescent;
-	CharDescription FontDesc;
-};
-
-/********************************************************************************************
->	class FontMetricsCache : public CC_CLASS_MEMDUMP
-	   		
-	Author:		Ed_Cornes (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	14/1/96
-	Purpose:	Holds a cache of char widths, ascent, descent and em for a particular font.
-********************************************************************************************/
-
-class FontMetricsCache: public CC_CLASS_MEMDUMP
-{
-	CC_DECLARE_MEMDUMP(FontMetricsCache);
-	
-public:
-	static void InvalidateCharMetrics();
-	static BOOL GetCharMetrics(wxDC* pDC, WCHAR ch, CharDescription& FontDesc, CharMetrics* pCharMetrics);
-
-protected:
-	// this seems the only way to define constants in a class assigning values
-	enum CacheInfo { NUMENTRIES=3 };
-
-	static FontMetricsCacheEntry mpFontMetricsData[NUMENTRIES];
-};
-
-
-/********************************************************************************************
-
->	class CharOutlineCache : public CC_CLASS_MEMDUMP
-	   		
-	Author:		Mike_Kenny (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	14/9/95
-	Purpose:	A cache for a character outline. This cache can only be accessed via the
-				OILFontMan character outline calls
-
-********************************************************************************************/
-
-class CharOutlineCache : public CC_CLASS_MEMDUMP
-{
-	CC_DECLARE_MEMDUMP(CharOutlineCache);
-	
-	friend class OILFontMan;
-	
-	private:
-		#if _DEBUG
-		static void Dump();
-		#endif
-
-		static DocCoord CacheCoords[OILFONTLIMIT];
-		static PathVerb CacheVerbs[OILFONTLIMIT];
-		static UINT32 CacheSize;
-		static DocCoord LastMoveTo;
-};
-
-// Forward decleration
-struct MillipointKerningPair;
-
-/********************************************************************************************
->	class FontKerningPairsCacheEntry : public CC_CLASS_MEMDUMP
-	   		
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
-	Purpose:	Holds a cache of kerning pairs for a font
-********************************************************************************************/
-
-class FontKerningPairsCacheEntry : public CC_CLASS_MEMDUMP
-{
-	CC_DECLARE_MEMDUMP(FontKerningPairsCacheEntry);
-	
-public:
-	FontKerningPairsCacheEntry();		// constructor
-	~FontKerningPairsCacheEntry();		// destructor
-
-	bool CacheFontKerns(wxDC* pDC, CharDescription FontDesc, MILLIPOINT DefaultHeight, INT32 DesignSize);
-	MILLIPOINT GetCharsKerning(WCHAR chLeft, WCHAR chRight);
-	inline CharDescription GetFontDesc()					{ return FontDesc; }
-	inline void SetFontDesc(CharDescription NewFontDesc)	{ FontDesc = NewFontDesc; }
-
-#ifdef _DEBUG
-public:
-	void Dump();
-#endif//_DEBUG
-
-protected:
-	CharDescription FontDesc;
-
-protected: // Kerning data
-	MillipointKerningPair*	pKernPairs;
-	INT32						KernCount;
-};
-
-
-/********************************************************************************************
-
->	class FontKerningPairsCache : public CC_CLASS_MEMDUMP
-	   		
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
-	Purpose:	A cache for the kerning pairs for a font
-
-********************************************************************************************/
-
-class FontKerningPairsCache : public CC_CLASS_MEMDUMP
-{
-	CC_DECLARE_MEMDUMP(FontKerningPairsCache);
-	
-public:
-	static void	InvalidateKerningPairsCache();
-	static MILLIPOINT GetCharsKerning(wxDC* pDC, WCHAR chLeft, WCHAR chRight,
-														CharDescription& FontDesc);
-
-#ifdef _DEBUG
-public:
-	static void Dump();
-#endif//_DEBUG
-
-protected:
-	// this seems the only way to define constants in a class assigning values
-	enum CacheInfo { NUMENTRIES=3 };
-
-	static FontKerningPairsCacheEntry mpFontKerningPairsCacheData[NUMENTRIES];
-};
-
-/********************************************************************************************
-
 >	class OILFontMan : public CCObject
 	   		
 	Author:		Mike_Kenny (Xara Group Ltd) <camelotdev@xxxxxxxx>
@@ -375,6 +180,10 @@
 		static void FindClosestFont();
 		static FontBase* CreateNewFont(FontClass Class, String_64* pFontName);
 		static OUTLINETEXTMETRIC *GetOutlineTextMetric(FontClass Class, LOGFONT *pLogFont);
+		static void InvalidateCharMetrics();
+		static BOOL GetCharMetrics(wxDC* pDC, WCHAR ch, CharDescription& FontDesc, CharMetrics* pCharMetrics);
+		static MILLIPOINT GetCharsKerning(wxDC* pDC, WCHAR chLeft, WCHAR chRight,
+										  CharDescription& FontDesc);
 
 	public:
 		// Character cache related functions - other font manglers can use this
Index: wxOil/fontbase.cpp
===================================================================
--- wxOil/fontbase.cpp	(Revision 852)
+++ wxOil/fontbase.cpp	(Arbeitskopie)
@@ -112,6 +112,8 @@
 #include "pathtype.h"
 #include "txtattr.h"
 #include "app.h"
+
+#include "fontimpl.h"       // private definitions
 //#include "mike.h"
 
 DECLARE_SOURCE( "$Revision$" );
@@ -139,7 +141,7 @@
 
 // define some font metrics/kerning pairs cache statics
 FontMetricsCacheEntry		FontMetricsCache::mpFontMetricsData[FontMetricsCache::NUMENTRIES];
-FontKerningPairsCacheEntry	FontKerningPairsCache::mpFontKerningPairsCacheData[FontKerningPairsCache::NUMENTRIES];
+FontKerningPairsCacheEntry	FontKerningPairsCache::m_FontKerningPairsCacheData[FontKerningPairsCache::NUMENTRIES];
 
 
 /********************************************************************************************
@@ -539,7 +541,46 @@
 }
 
 /********************************************************************************************
+>	OILFontMan::InvalidateCharMetrics()
 
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
+	Purpose:	Facade routine for FontMetricsCache::InvalidateCharMetrics
+
+********************************************************************************************/
+void OILFontMan::InvalidateCharMetrics()
+{
+	FontMetricsCache::InvalidateCharMetrics();
+}
+
+/********************************************************************************************
+>	BOOL GetCharMetrics(wxDC* pDC, WCHAR ch, CharDescription& FontDesc, CharMetrics* pCharMetrics)
+
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
+	Purpose:	Facade routine for FontMetricsCache::GetCharMetrics
+
+********************************************************************************************/
+BOOL OILFontMan::GetCharMetrics(wxDC* pDC, WCHAR ch, CharDescription& FontDesc, CharMetrics* pCharMetrics)
+{
+	return FontMetricsCache::GetCharMetrics(pDC, ch, FontDesc, pCharMetrics);
+}
+
+/********************************************************************************************
+>	MILLIPOINT GetCharsKerning(wxDC* pDC, WCHAR chLeft, WCHAR chRight, CharDescription& FontDesc)
+
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
+	Purpose:	Facade routine for FontKerningPairsCache::GetCharsKerning
+
+********************************************************************************************/
+MILLIPOINT OILFontMan::GetCharsKerning(wxDC* pDC, WCHAR chLeft, WCHAR chRight, CharDescription& FontDesc)
+{
+	return FontKerningPairsCache::GetCharsKerning(pDC, chLeft, chRight, FontDesc);
+}
+
+/********************************************************************************************
+
 >	static UINT32 OILFontMan::GetOutlineCache(DocCoord** pCoords, PathVerb** pVerbs);
 
 	Author:		Mike_Kenny (Xara Group Ltd) <camelotdev@xxxxxxxx>
@@ -1126,87 +1167,36 @@
 	 			FontDesc	= a font description
 	Returns:	Kern in millipoints (or zero if no kern or error)
 	Purpose:	Get the kern between two chars of a font
-	Note:		So far, the kerning values are not cached
+	Note:		The kern pairs are cached (see FontKerningPairsCacheEntry::GetCharsKerning)
 ********************************************************************************************/
 
 MILLIPOINT FontKerningPairsCache::GetCharsKerning(wxDC* pDC, WCHAR chLeft, WCHAR chRight,
-																	CharDescription& FontDesc)
+												  CharDescription& FontDesc)
 {
-	PORTNOTE("text", "caching of kern pairs removed");
-#ifndef EXCLUDE_FROM_XARALX
-	ERROR2IF(         pDC==NULL,FALSE,"FontKerningPairsCache::GetCharsKerning() - pDC==NULL");
-	ERROR2IF(FontDesc.GetCharCode()!=FONTEMCHAR,FALSE,
-			"FontKerningPairsCache::GetCharsKerning() - FontDesc char should be 'FONTEMCHAR'");
-
-	// find if font is in cache (and if so which entry)
+	// TRACEUSER("wuerthne", _T("GetCharsKerning %04x %04x"), chLeft, chRight);
+	ERROR2IF(pDC==NULL, 0, "FontKerningPairsCache::GetCharsKerning() - pDC==NULL");
+	ERROR2IF(FontDesc.GetCharCode() != FONTEMCHAR, FALSE,
+		"FontKerningPairsCache::GetCharsKerning() - FontDesc char should be 'FONTEMCHAR'");
+#ifndef DISABLE_TEXT_RENDERING
+	// find out whether the font is in the cache (and if so which entry)
 	INT32 CacheEntry = 0;
-	while (CacheEntry<NUMENTRIES && mpFontKerningPairsCacheData[CacheEntry].GetFontDesc()!=FontDesc)
-		CacheEntry +=1;
+	while (CacheEntry < NUMENTRIES && m_FontKerningPairsCacheData[CacheEntry].GetFontDesc() != FontDesc)
+		CacheEntry++;
 
-	// if font not in cache, recache it (requires the DC to be prepared, then either/both is
-	// done before restoring the DC)
-	if (CacheEntry>=NUMENTRIES)
+	if (CacheEntry >= NUMENTRIES)
 	{
-		// get design size of font, and default heigh
-		INT32 DesignSize    = TextManager::GetDesignSize(pDC);
-		INT32 DefaultHeight = TextManager::GetDefaultHeight();
-
-		// first, if ATM fonts, ensure accurate widths returned
-		// this should be a virtual function - but class structure does not facilitate such things!
-//-		CachedFontItem* pItem = FONTMANAGER->GetFont(FontDesc.GetTypefaceHandle());
-//-		if (pItem==NULL || pItem->IsCorrupt())
-//-			return FALSE;
-//-		if (pItem->GetFontClass()==FC_ATM)
-//-			ATMFontMan::ForceExactWidth();
-
-		// get a LogFont, create a font and select it into the DC
-		LOGFONT	CharLogFont;
-		if (TextManager::GetLogFontFromCharDescriptor(pDC, FontDesc, &CharLogFont, DesignSize) == false)
-			return 0;
-		wxFont font;
-		font.CreateFontIndirect(&CharLogFont);
-		wxFont* pOldFont = pDC->SelectObject(&font);
-		ERROR2IF(pOldFont==NULL,FALSE,"FontKerningPairsCache::GetCharsKerning() - SelectObject() failed");
-
-		// if font not in cache, cache its kerns throwing out a random entry
+		// TRACEUSER("wuerthne", _T("did not find cache entry, so allocate new one"));
+		// the font is not in the cache, so throw out a random cache entry and prepare
+		// the cache entry so we can start caching the kerning data for the new font
 		CacheEntry = rand() % NUMENTRIES;
-		if (mpFontKerningPairsCacheData[CacheEntry].CacheFontKerns(pDC, FontDesc, DefaultHeight, DesignSize) == false)
-			return 0;
-
-		// restore old font
-		pDC->SelectObject(pOldFont);
+		m_FontKerningPairsCacheData[CacheEntry].Reinitialise(FontDesc);
 	}
-
-#ifdef _DEBUG
-//	TRACE( _T("State of kerning cache after update...\n"));
-//	Dump();
-#endif /*_DEBUG */
-
-	return mpFontKerningPairsCacheData[CacheEntry].GetCharsKerning(chLeft, chRight);
+	return m_FontKerningPairsCacheData[CacheEntry].GetCharsKerning(chLeft, chRight);
 #else
-#ifndef DISABLE_TEXT_RENDERING
-	return FTFontMan::GetCharsKerning(FontDesc, chLeft, chRight);
-#else
 	return 0;
 #endif
-#endif
 }
 
-
-/********************************************************************************************
->	static void FontKerningPairsCache::InvalidateCharMetrics()
-
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
-	Purpose:	Invalidate the FontKerningPairsCache
-********************************************************************************************/
-void FontKerningPairsCache::InvalidateKerningPairsCache()
-{
-	CharDescription emptyCharDesc(0, 0, 0, 0);
-	for (INT32 i=0; i<NUMENTRIES; ++i)
-		mpFontKerningPairsCacheData[i].SetFontDesc( emptyCharDesc );
-}
-
 #ifdef _DEBUG
 /********************************************************************************************
 
@@ -1223,7 +1213,7 @@
 	for (INT32 i=0; i<NUMENTRIES; ++i)
 	{
 		TRACE( _T("Entry %d:\n"), i);
-		mpFontKerningPairsCacheData[i].Dump();
+		m_FontKerningPairsCacheData[i].Dump();
 	}
 	TRACE( _T("<<< Font kerning data end <<<\n"));
 }
@@ -1235,8 +1225,8 @@
 /********************************************************************************************
 >	FontKerningPairsCacheEntry::FontKerningPairsCacheEntry()
 
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
 	Purpose:	Default constructor
 
 ********************************************************************************************/
@@ -1244,142 +1234,80 @@
 {
 	CharDescription emptyCharDesc(0, 0, 0, 0);
 	FontDesc = emptyCharDesc;
-	KernCount = 0;
-	pKernPairs = 0;
+	m_pPairsCacheMap = NULL;
 }
 
 /********************************************************************************************
 >	FontKerningPairsCacheEntry::FontKerningPairsCacheEntry()
 
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
-	Purpose:	Default destructor
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
+	Purpose:	Destructor
 
 ********************************************************************************************/
 FontKerningPairsCacheEntry::~FontKerningPairsCacheEntry()
 {
-	delete[] pKernPairs;
+	if (m_pPairsCacheMap) delete m_pPairsCacheMap;
 }
 
 /********************************************************************************************
+>	FontKerningPairsCacheEntry::Reinitialise(CharDescription& NewFontDesc)
 
->	INT32 CompareKernsForSort( const void *arg1, const void *arg2 )
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
+	Inputs:		NewFontDesc = a font descriptor specifying the font to which this cache
+							  entry should refer from now on
+	Purpose:	Clears the cache entry and initialises it for a new font
 
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	25/10/2000
-	Purpose:	Compare function for FontKerningPairsChaceEntry::CacheFontKerns 
-
 ********************************************************************************************/
-INT32 CompareKernsFunc( const void *arg1, const void *arg2 )
+void FontKerningPairsCacheEntry::Reinitialise(CharDescription& NewFontDesc)
 {
-	MillipointKerningPair *p1, *p2;
-	p1 = (MillipointKerningPair *)arg1;
-	p2 = (MillipointKerningPair *)arg2;
-
-	if ((p1->wFirst < p2->wFirst) || (p1->wFirst == p2->wFirst && p1->wSecond < p2->wSecond))
-		return -1;	// arg1 less than arg2
-	else if (p1->wFirst == p2->wFirst && p1->wSecond == p2->wSecond)
-		return 0;	// arg1 equivalent to arg2
-	else
-		return 1;	// arg1 greater than arg2
+	FontDesc = NewFontDesc;
+	if (m_pPairsCacheMap) delete m_pPairsCacheMap;
+	m_pPairsCacheMap = new std::map<UINT32,INT32>;
+	// we do not check whether the new has succeeded - in case it has not,
+	// we will not fall over later but caching will be disabled for this entry
 }
 
 /********************************************************************************************
->	bool FontKerningPairsCacheEntry::CacheFontKerns(wxDC* pDC, CharDescription FontDesc,
-												MILLIPOINT DefaultHeight, INT32 DesignSize)
+>	FontKerningPairsCacheEntry::GetCharsKerning(WCHAR chLeft, WCHAR chRight)
 
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
-	Inputs:		pDC          - DC with design size font selected
-				FontDesc     - descriptor of font which is being cached
-				DefaultHeght - default height of char (ie size of font for which char widths are cached)
-				DesignSize   - size of font selected in DC (in Logical units - pixels)
-	Returns:	false if fails
-	Purpose:	Refill the font cache entry
-********************************************************************************************/
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	20/04/2006
+	Inputs:		chLeft		= a character code of the left char of a kerning pair
+				chRight		= a character code of the right char of a kerning pair
+	Returns:	Kern in millipoints (or zero if no kern or error)
+	Purpose:	Gets the kerning for a character pair (and caches it if not cached yet)
+	Note:		The cache uses an STL map, which means logarithmic access time. Maybe
+				a hash_map could do better.
 
-bool FontKerningPairsCacheEntry::CacheFontKerns(wxDC* pDC, CharDescription FontDesc,
-												MILLIPOINT DefaultHeight, INT32 DesignSize)
-{
-	delete[] pKernPairs;	// remove any old kern data
-
-#ifndef EXCLUDE_FROM_XARALX
-	KernCount = TextManager::GetKernCount(pDC);
-
-	// update cache tag
-	SetFontDesc(FontDesc);
-
-	if (KernCount)
-	{
-		pKernPairs = new MillipointKerningPair[KernCount];
-		if (!pKernPairs) return false;
-		if (TextManager::FillKernArray(pDC, pKernPairs, KernCount))
-		{
-			for (INT32 c = 0; c < KernCount; ++c)
-				pKernPairs[c].iKernAmount = MulDiv(pKernPairs[c].iKernAmount, DefaultHeight, DesignSize);
-
-			// Sort array (this allows us to do a binary search on the array
-			// when finding kerns).  Note: The kern data from windows appears to
-			// be sorted already but I am not sure that this is always the case
-			// so I am sorting it again to make sure (not too bad as qsort
-			// does not do too much harm to a sorted array).
-			qsort(pKernPairs, KernCount, sizeof(pKernPairs[0]), CompareKernsFunc);
-
-			return true;
-		}
-		else
-		{
-			KernCount = 0;
-			return false;
-		}
-	}
-#endif
-	pKernPairs = 0;
-	return true; // No kern data so kern cache is valid
-}
-
-/********************************************************************************************
->	MILLIPOINT FontKerningPairsCacheEntry::GetCharsKerning(WCHAR chLeft, WCHAR chRight)
-
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
-	Inputs:		chLeft			- The left char of a kern pair
-				chRight			- The right char of a kern pair
-	Returns:	The kern in millipoints (or zero if none)
-	Purpose:	Performs a binary search of the sorted kern data and returns the kern
-				for a given pair a chars (or zero if there is no kern)
-
-				Algorithm from Knuth Vol III section 6.2.1 algorithm B
 ********************************************************************************************/
 MILLIPOINT FontKerningPairsCacheEntry::GetCharsKerning(WCHAR chLeft, WCHAR chRight)
 {
-	MillipointKerningPair K;	// The key we are looking for
-
-	K.wFirst		= chLeft;
-	K.wSecond		= chRight;
-	K.iKernAmount	= 0;		// Not used
-
-	INT32 i, t;
-
-	INT32 l = 0;											// B1
-	INT32 u = KernCount - 1;
-
-	while (u >= l)
+	UINT32 key;
+	// we cache the kerning for characters in the range 0x0 - 0xFFFF only
+	// we also need the map set up - if it could not be allocated, we simply bypass the cache
+	if (chLeft > 0x10000 || chRight > 0x10000 || m_pPairsCacheMap == NULL)
 	{
-		/* At this point, if the kern is in the table, Kl <= K <= Ku */
-		
-		i = (l + u) / 2;								// B2
+		// TRACEUSER("wuerthne", _T("Bypass cache"));
+		return FTFontMan::GetCharsKerning(FontDesc, chLeft, chRight);
+	}
 
-		t = CompareKernsFunc(&K, &(pKernPairs[i]));		// B3
-
-		if (t < 0)
-			u = i - 1;									// B4
-		else if (t > 0)
-			l = i + 1;									// B5
-		else if (t == 0)
-			return pKernPairs[i].iKernAmount;
+	key = (chLeft << 16) | chRight;
+	std::map<UINT32,INT32>::iterator it = m_pPairsCacheMap->find(key);
+	if (it != m_pPairsCacheMap->end()) {
+		// we found a cached entry
+		// TRACEUSER("wuerthne", _T("kern pair found"));
+		return it->second;
 	}
-	return 0;
+	else {
+		// we did not find an entry, so get the kerning from the underlying font system
+		INT32 kerning = FTFontMan::GetCharsKerning(FontDesc, chLeft, chRight);
+		// and cache it
+		// TRACEUSER("wuerthne", _T("cache kern pair"));
+		(*m_pPairsCacheMap)[key] = kerning;
+		return kerning;
+	}
 }
 
 #if _DEBUG
@@ -1388,21 +1316,20 @@
 
 >	void FontKerningPairsCacheEntry::Dump()
 
-	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
-	Created:	16/10/2000
+	Author:		Martin Wuerthner <xara@xxxxxxxxxxxxxxx>
+	Created:	21/04/2006
 	Purpose:	Dumps the class data
-	See also:	FontKerningPairs::Dump()
 
 ********************************************************************************************/
 void FontKerningPairsCacheEntry::Dump()
 {
-	for (INT32 c = 0; c < KernCount; ++c)
-	{
-		TRACE( _T("%3d:\t%c\t%c\t%d\n"),
-				c,
-				pKernPairs[c].wFirst,
-				pKernPairs[c].wSecond,
-				pKernPairs[c].iKernAmount);
+	// dump the map
+	if (m_pPairsCacheMap != NULL) {
+		for (std::map<UINT32,INT32>::iterator it = m_pPairsCacheMap->begin(); it != m_pPairsCacheMap->end(); ++it)
+		{
+			UINT32 key = (*it).first;
+			TRACE(_T("cached kern pair %04x/%04x = %d"), key >> 16, key & 0xffff, (*it).second);
+		}
 	}
 }
 
// $Id$
/* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
================================XARAHEADERSTART===========================
 
               Xara LX, a vector drawing and manipulation program.
                    Copyright (C) 1993-2006 Xara Group Ltd.
       Copyright on certain contributions may be held in joint with their
              respective authors. See AUTHORS file for details.

LICENSE TO USE AND MODIFY SOFTWARE
----------------------------------

This file is part of Xara LX.

Xara LX is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation.

Xara LX and its component source files are distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with Xara LX (see the file GPL in the root directory of the
distribution); if not, write to the Free Software Foundation, Inc., 51
Franklin St, Fifth Floor, Boston, MA  02110-1301 USA


ADDITIONAL RIGHTS
-----------------

Conditional upon your continuing compliance with the GNU General Public
License described above, Xara Group Ltd grants to you certain additional
rights. 

The additional rights are to use, modify, and distribute the software
together with the wxWidgets library, the wxXtra library, and the "CDraw"
library and any other such library that any version of Xara LX relased
by Xara Group Ltd requires in order to compile and execute, including
the static linking of that library to XaraLX. In the case of the
"CDraw" library, you may satisfy obligation under the GNU General Public
License to provide source code by providing a binary copy of the library
concerned and a copy of the license accompanying it.

Nothing in this section restricts any of the rights you have under
the GNU General Public License.


SCOPE OF LICENSE
----------------

This license applies to this program (XaraLX) and its constituent source
files only, and does not necessarily apply to other Xara products which may
in part share the same code base, and are subject to their own licensing
terms.

This license does not apply to files in the wxXtra directory, which
are built into a separate library, and are subject to the wxWindows
license contained within that directory in the file "WXXTRA-LICENSE".

This license does not apply to the binary libraries (if any) within
the "libs" directory, which are subject to a separate license contained
within that directory in the file "LIBS-LICENSE".


ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
----------------------------------------------

Subject to the terms of the GNU Public License (see above), you are
free to do whatever you like with your modifications. However, you may
(at your option) wish contribute them to Xara's source tree. You can
find details of how to do this at:
  http://www.xaraxtreme.org/developers/

Prior to contributing your modifications, you will need to complete our
contributor agreement. This can be found at:
  http://www.xaraxtreme.org/developers/contribute/

Please note that Xara will not accept modifications which modify any of
the text between the start and end of this header (marked
XARAHEADERSTART and XARAHEADEREND).


MARKS
-----

Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
designs are registered or unregistered trademarks, design-marks, and/or
service marks of Xara Group Ltd. All rights in these marks are reserved.


      Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
                        http://www.xara.com/

=================================XARAHEADEREND============================
 */
// Declares the known font classes

/*
*/

#ifndef INC_FONTCLASS
#define INC_FONTCLASS

/********************************************************************************************
	The types of fonts the OIL font manager recognises so far
********************************************************************************************/

typedef enum FontClass {
	FC_UNDEFINED = 0,
	FC_RASTER,
	FC_DEVICE,
	FC_TRUETYPE,
	FC_ATM,
	FC_FREETYPE,

	// Add further font classes before this
	FC_ILLEGAL
};

// The kernel likes dealing with LOGFONT and ENUMLOGFONT structures, so we do it this favour

#if !defined(__WXMSW__)
struct LOGFONT
{
	String_64 FaceName;
};
typedef LOGFONT		   *PLOGFONT, *LPLOGFONT;

struct ENUMLOGFONT
{
	LOGFONT elfLogFont;
};
#endif

#endif
// $Id$
/* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
================================XARAHEADERSTART===========================
 
               Xara LX, a vector drawing and manipulation program.
                    Copyright (C) 1993-2006 Xara Group Ltd.
       Copyright on certain contributions may be held in joint with their
              respective authors. See AUTHORS file for details.

LICENSE TO USE AND MODIFY SOFTWARE
----------------------------------

This file is part of Xara LX.

Xara LX is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation.

Xara LX and its component source files are distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with Xara LX (see the file GPL in the root directory of the
distribution); if not, write to the Free Software Foundation, Inc., 51
Franklin St, Fifth Floor, Boston, MA  02110-1301 USA


ADDITIONAL RIGHTS
-----------------

Conditional upon your continuing compliance with the GNU General Public
License described above, Xara Group Ltd grants to you certain additional
rights. 

The additional rights are to use, modify, and distribute the software
together with the wxWidgets library, the wxXtra library, and the "CDraw"
library and any other such library that any version of Xara LX relased
by Xara Group Ltd requires in order to compile and execute, including
the static linking of that library to XaraLX. In the case of the
"CDraw" library, you may satisfy obligation under the GNU General Public
License to provide source code by providing a binary copy of the library
concerned and a copy of the license accompanying it.

Nothing in this section restricts any of the rights you have under
the GNU General Public License.


SCOPE OF LICENSE
----------------

This license applies to this program (XaraLX) and its constituent source
files only, and does not necessarily apply to other Xara products which may
in part share the same code base, and are subject to their own licensing
terms.

This license does not apply to files in the wxXtra directory, which
are built into a separate library, and are subject to the wxWindows
license contained within that directory in the file "WXXTRA-LICENSE".

This license does not apply to the binary libraries (if any) within
the "libs" directory, which are subject to a separate license contained
within that directory in the file "LIBS-LICENSE".


ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
----------------------------------------------

Subject to the terms of the GNU Public License (see above), you are
free to do whatever you like with your modifications. However, you may
(at your option) wish contribute them to Xara's source tree. You can
find details of how to do this at:
  http://www.xaraxtreme.org/developers/

Prior to contributing your modifications, you will need to complete our
contributor agreement. This can be found at:
  http://www.xaraxtreme.org/developers/contribute/

Please note that Xara will not accept modifications which modify any of
the text between the start and end of this header (marked
XARAHEADERSTART and XARAHEADEREND).


MARKS
-----

Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
designs are registered or unregistered trademarks, design-marks, and/or
service marks of Xara Group Ltd. All rights in these marks are reserved.


      Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
                        http://www.xara.com/

=================================XARAHEADEREND============================
 */

// Implementation-specific definitions for fontbase.cpp. Not meant to
// included by any other file.

#include "fontbase.h"
#include <map>

/********************************************************************************************
>	class FontMetricsCacheEntry : public CC_CLASS_MEMDUMP
	   		
	Author:		Ed_Cornes (Xara Group Ltd) <camelotdev@xxxxxxxx>
	Created:	14/1/96
	Purpose:	Holds a cache of char widths, ascent, descent and em for a particular font.
********************************************************************************************/

class FontMetricsCacheEntry: public CC_CLASS_MEMDUMP
{
	CC_DECLARE_MEMDUMP(FontMetricsCacheEntry);
	
public:
	FontMetricsCacheEntry();

	BOOL CacheFontMetrics(wxDC* pDC, CharDescription FontDesc, MILLIPOINT DefaultHeight, INT32 DesignSize);

	void CheckCharWidthsSameAsABCWidths(wxDC* pDC, CharDescription FontDesc);	// debug test

	inline static BOOL CharInCacheRange(WCHAR ch) { return (ch>=FIRSTCHAR && ch<=LASTCHAR); }

	inline INT32 GetCharWidthFromCache(WCHAR ch) { return pCharWidths[ch-FIRSTCHAR]; }

	inline MILLIPOINT      GetFontEmWidth() { return FontEmWidth; }
	inline MILLIPOINT      GetFontAscent()  { return FontAscent; }
	inline MILLIPOINT      GetFontDescent() { return FontDescent; }
	inline CharDescription GetFontDesc()    { return FontDesc; }

	inline void SetFontEmWidth(MILLIPOINT NewFontEmWidth) { FontEmWidth = NewFontEmWidth; }
	inline void SetFontAscent( MILLIPOINT NewFontAscent)  { FontAscent  = NewFontAscent; }
	inline void SetFontDescent(MILLIPOINT NewFontDescent) { FontDescent = NewFontDescent; }
	inline void SetFontDesc(CharDescription NewFontDesc)  { FontDesc    = NewFontDesc; }

protected:
	// this seems the only way to define constants in a class assigning values
	enum CharRange { FIRSTCHAR=32, LASTCHAR=127, NUMCHARS=(LASTCHAR-FIRSTCHAR+1) };

	MILLIPOINT pCharWidths[NUMCHARS];
	MILLIPOINT FontEmWidth;
	MILLIPOINT FontAscent;
	MILLIPOINT FontDescent;
	CharDescription FontDesc;
};

/********************************************************************************************
>	class FontMetricsCache : public CC_CLASS_MEMDUMP
	   		
	Author:		Ed_Cornes (Xara Group Ltd) <camelotdev@xxxxxxxx>
	Created:	14/1/96
	Purpose:	Holds a cache of char widths, ascent, descent and em for a particular font.
********************************************************************************************/

class FontMetricsCache: public CC_CLASS_MEMDUMP
{
	CC_DECLARE_MEMDUMP(FontMetricsCache);
	
public:
	static void InvalidateCharMetrics();
	static BOOL GetCharMetrics(wxDC* pDC, WCHAR ch, CharDescription& FontDesc, CharMetrics* pCharMetrics);

protected:
	// this seems the only way to define constants in a class assigning values
	enum CacheInfo { NUMENTRIES=3 };

	static FontMetricsCacheEntry mpFontMetricsData[NUMENTRIES];
};


/********************************************************************************************

>	class CharOutlineCache : public CC_CLASS_MEMDUMP
	   		
	Author:		Mike_Kenny (Xara Group Ltd) <camelotdev@xxxxxxxx>
	Created:	14/9/95
	Purpose:	A cache for a character outline. This cache can only be accessed via the
				OILFontMan character outline calls

********************************************************************************************/

class CharOutlineCache : public CC_CLASS_MEMDUMP
{
	CC_DECLARE_MEMDUMP(CharOutlineCache);
	
	friend class OILFontMan;
	
	private:
		#if _DEBUG
		static void Dump();
		#endif

		static DocCoord CacheCoords[OILFONTLIMIT];
		static PathVerb CacheVerbs[OILFONTLIMIT];
		static UINT32 CacheSize;
		static DocCoord LastMoveTo;
};

// Forward decleration
struct MillipointKerningPair;

/********************************************************************************************
>	class FontKerningPairsCacheEntry : public CC_CLASS_MEMDUMP
	   		
	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
	Created:	16/10/2000
	Purpose:	Holds a cache of kerning pairs for a font
********************************************************************************************/

class FontKerningPairsCacheEntry : public CC_CLASS_MEMDUMP
{
	CC_DECLARE_MEMDUMP(FontKerningPairsCacheEntry);
	
public:
	FontKerningPairsCacheEntry();		// constructor
	~FontKerningPairsCacheEntry();		// destructor

	void Reinitialise(CharDescription& NewFontDesc);

	MILLIPOINT GetCharsKerning(WCHAR chLeft, WCHAR chRight);
	inline CharDescription GetFontDesc()					{ return FontDesc; }
	inline void SetFontDesc(CharDescription NewFontDesc)	{ FontDesc = NewFontDesc; }

#ifdef _DEBUG
public:
	void Dump();
#endif //_DEBUG

protected:
	CharDescription FontDesc;

protected: // Kerning data
	std::map<UINT32,INT32>* m_pPairsCacheMap;

private:
	// declare private copy constructor and assignment operator to prevent
	// copy/assignment operations - the methods are not implemented
    FontKerningPairsCacheEntry(const FontKerningPairsCacheEntry&);
	FontKerningPairsCacheEntry& operator=(const FontKerningPairsCacheEntry&);
};


/********************************************************************************************

>	class FontKerningPairsCache : public CC_CLASS_MEMDUMP
	   		
	Author:		Jonathan_Payne (Xara Group Ltd) <camelotdev@xxxxxxxx>
	Created:	16/10/2000
	Purpose:	A cache for the kerning pairs for a font

********************************************************************************************/

class FontKerningPairsCache : public CC_CLASS_MEMDUMP
{
	CC_DECLARE_MEMDUMP(FontKerningPairsCache);
	
public:
	static MILLIPOINT GetCharsKerning(wxDC* pDC, WCHAR chLeft, WCHAR chRight,
									  CharDescription& FontDesc);

#ifdef _DEBUG
public:
	static void Dump();
#endif//_DEBUG

protected:
	// this seems the only way to define constants in a class assigning values
	enum CacheInfo { NUMENTRIES=3 };

	static FontKerningPairsCacheEntry m_FontKerningPairsCacheData[NUMENTRIES];
};