xref: /aoo41x/main/vcl/source/glyphs/glyphcache.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <stdlib.h>
33*cdf0e10cSrcweir #include <math.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <gcach_ftyp.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <vcl/svapp.hxx>
38*cdf0e10cSrcweir #include <vcl/bitmap.hxx>
39*cdf0e10cSrcweir #include <vcl/salbtype.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <outfont.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
44*cdf0e10cSrcweir #include <graphite_features.hxx>
45*cdf0e10cSrcweir #endif
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <rtl/ustring.hxx>		// used only for string=>hashvalue
48*cdf0e10cSrcweir #include <osl/file.hxx>
49*cdf0e10cSrcweir #include <tools/debug.hxx>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir // =======================================================================
52*cdf0e10cSrcweir // GlyphCache
53*cdf0e10cSrcweir // =======================================================================
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir static GlyphCache* pInstance = NULL;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir GlyphCache::GlyphCache( GlyphCachePeer& rPeer )
58*cdf0e10cSrcweir :   mrPeer( rPeer ),
59*cdf0e10cSrcweir     mnMaxSize( 1500000 ),
60*cdf0e10cSrcweir     mnBytesUsed(sizeof(GlyphCache)),
61*cdf0e10cSrcweir     mnLruIndex(0),
62*cdf0e10cSrcweir     mnGlyphCount(0),
63*cdf0e10cSrcweir     mpCurrentGCFont(NULL),
64*cdf0e10cSrcweir     mpFtManager(NULL)
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir     pInstance = this;
67*cdf0e10cSrcweir     mpFtManager = new FreetypeManager;
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir // -----------------------------------------------------------------------
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir GlyphCache::~GlyphCache()
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     InvalidateAllGlyphs();
75*cdf0e10cSrcweir     if( mpFtManager )
76*cdf0e10cSrcweir         delete mpFtManager;
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir // -----------------------------------------------------------------------
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir void GlyphCache::InvalidateAllGlyphs()
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     // an application about to exit can omit garbage collecting the heap
84*cdf0e10cSrcweir     // since it makes things slower and introduces risks if the heap was not perfect
85*cdf0e10cSrcweir     // for debugging, for memory grinding or leak checking the env allows to force GC
86*cdf0e10cSrcweir     const char* pEnv = getenv( "SAL_FORCE_GC_ON_EXIT" );
87*cdf0e10cSrcweir     if( pEnv && (*pEnv != '0') )
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         // uncache of all glyph shapes and metrics
90*cdf0e10cSrcweir         for( FontList::iterator it = maFontList.begin(); it != maFontList.end(); ++it )
91*cdf0e10cSrcweir             delete const_cast<ServerFont*>( it->second );
92*cdf0e10cSrcweir         maFontList.clear();
93*cdf0e10cSrcweir         mpCurrentGCFont = NULL;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir // -----------------------------------------------------------------------
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir inline
100*cdf0e10cSrcweir size_t GlyphCache::IFSD_Hash::operator()( const ImplFontSelectData& rFontSelData ) const
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir     // TODO: is it worth to improve this hash function?
103*cdf0e10cSrcweir     sal_IntPtr nFontId = reinterpret_cast<sal_IntPtr>( rFontSelData.mpFontData );
104*cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
105*cdf0e10cSrcweir     if (rFontSelData.maTargetName.Search(grutils::GrFeatureParser::FEAT_PREFIX)
106*cdf0e10cSrcweir         != STRING_NOTFOUND)
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         rtl::OString aFeatName = rtl::OUStringToOString( rFontSelData.maTargetName, RTL_TEXTENCODING_UTF8 );
109*cdf0e10cSrcweir         nFontId ^= aFeatName.hashCode();
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir #endif
112*cdf0e10cSrcweir     size_t nHash = nFontId << 8;
113*cdf0e10cSrcweir     nHash   += rFontSelData.mnHeight;
114*cdf0e10cSrcweir     nHash   += rFontSelData.mnOrientation;
115*cdf0e10cSrcweir     nHash   += rFontSelData.mbVertical;
116*cdf0e10cSrcweir     nHash   += rFontSelData.meItalic;
117*cdf0e10cSrcweir     nHash   += rFontSelData.meWeight;
118*cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
119*cdf0e10cSrcweir     nHash   += rFontSelData.meLanguage;
120*cdf0e10cSrcweir #endif
121*cdf0e10cSrcweir     return nHash;
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir // -----------------------------------------------------------------------
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir bool GlyphCache::IFSD_Equal::operator()( const ImplFontSelectData& rA, const ImplFontSelectData& rB) const
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir     // check font ids
129*cdf0e10cSrcweir     sal_IntPtr nFontIdA = reinterpret_cast<sal_IntPtr>( rA.mpFontData );
130*cdf0e10cSrcweir     sal_IntPtr nFontIdB = reinterpret_cast<sal_IntPtr>( rB.mpFontData );
131*cdf0e10cSrcweir     if( nFontIdA != nFontIdB )
132*cdf0e10cSrcweir         return false;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     // compare with the requested metrics
135*cdf0e10cSrcweir     if( (rA.mnHeight         != rB.mnHeight)
136*cdf0e10cSrcweir     ||  (rA.mnOrientation    != rB.mnOrientation)
137*cdf0e10cSrcweir     ||  (rA.mbVertical       != rB.mbVertical)
138*cdf0e10cSrcweir     ||  (rA.mbNonAntialiased != rB.mbNonAntialiased) )
139*cdf0e10cSrcweir         return false;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     if( (rA.meItalic != rB.meItalic)
142*cdf0e10cSrcweir     ||  (rA.meWeight != rB.meWeight) )
143*cdf0e10cSrcweir         return false;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	// NOTE: ignoring meFamily deliberately
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     // compare with the requested width, allow default width
148*cdf0e10cSrcweir     if( (rA.mnWidth != rB.mnWidth)
149*cdf0e10cSrcweir     && ((rA.mnHeight != rB.mnWidth) || (rA.mnWidth != 0)) )
150*cdf0e10cSrcweir         return false;
151*cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
152*cdf0e10cSrcweir    if (rA.meLanguage != rB.meLanguage)
153*cdf0e10cSrcweir         return false;
154*cdf0e10cSrcweir    // check for features
155*cdf0e10cSrcweir    if ((rA.maTargetName.Search(grutils::GrFeatureParser::FEAT_PREFIX)
156*cdf0e10cSrcweir         != STRING_NOTFOUND ||
157*cdf0e10cSrcweir         rB.maTargetName.Search(grutils::GrFeatureParser::FEAT_PREFIX)
158*cdf0e10cSrcweir         != STRING_NOTFOUND) && rA.maTargetName != rB.maTargetName)
159*cdf0e10cSrcweir         return false;
160*cdf0e10cSrcweir #endif
161*cdf0e10cSrcweir     return true;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir // -----------------------------------------------------------------------
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir GlyphCache& GlyphCache::GetInstance()
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir 	return *pInstance;
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir // -----------------------------------------------------------------------
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir void GlyphCache::LoadFonts()
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir     if( const char* pFontPath = ::getenv( "SAL_FONTPATH_PRIVATE" ) )
176*cdf0e10cSrcweir         AddFontPath( String::CreateFromAscii( pFontPath ) );
177*cdf0e10cSrcweir     const String& rFontPath = Application::GetFontPath();
178*cdf0e10cSrcweir     if( rFontPath.Len() > 0 )
179*cdf0e10cSrcweir         AddFontPath( rFontPath );
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir // -----------------------------------------------------------------------
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir void GlyphCache::ClearFontPath()
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     if( mpFtManager )
187*cdf0e10cSrcweir         mpFtManager->ClearFontList();
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir // -----------------------------------------------------------------------
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir void GlyphCache::AddFontPath( const String& rFontPath )
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir     if( !mpFtManager )
195*cdf0e10cSrcweir         return;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     for( xub_StrLen nBreaker1 = 0, nBreaker2 = 0; nBreaker2 != STRING_LEN; nBreaker1 = nBreaker2 + 1 )
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         nBreaker2 = rFontPath.Search( ';', nBreaker1 );
200*cdf0e10cSrcweir         if( nBreaker2 == STRING_NOTFOUND )
201*cdf0e10cSrcweir             nBreaker2 = STRING_LEN;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         ::rtl::OUString aUrlName;
204*cdf0e10cSrcweir         osl::FileBase::getFileURLFromSystemPath( rFontPath.Copy( nBreaker1, nBreaker2 ), aUrlName );
205*cdf0e10cSrcweir         mpFtManager->AddFontDir( aUrlName );
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir // -----------------------------------------------------------------------
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir void GlyphCache::AddFontFile( const rtl::OString& rNormalizedName, int nFaceNum,
212*cdf0e10cSrcweir     sal_IntPtr nFontId, const ImplDevFontAttributes& rDFA, const ExtraKernInfo* pExtraKern )
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir     if( mpFtManager )
215*cdf0e10cSrcweir         mpFtManager->AddFontFile( rNormalizedName, nFaceNum, nFontId, rDFA, pExtraKern );
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir // -----------------------------------------------------------------------
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir void GlyphCache::AnnounceFonts( ImplDevFontList* pList ) const
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir     if( mpFtManager )
223*cdf0e10cSrcweir         mpFtManager->AnnounceFonts( pList );
224*cdf0e10cSrcweir     // VirtDevServerFont::AnnounceFonts( pList );
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir // -----------------------------------------------------------------------
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir ServerFont* GlyphCache::CacheFont( const ImplFontSelectData& rFontSelData )
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     // a serverfont request has pFontData
232*cdf0e10cSrcweir     if( rFontSelData.mpFontData == NULL )
233*cdf0e10cSrcweir         return NULL;
234*cdf0e10cSrcweir     // a serverfont request has a fontid > 0
235*cdf0e10cSrcweir     sal_IntPtr nFontId = rFontSelData.mpFontData->GetFontId();
236*cdf0e10cSrcweir     if( nFontId <= 0 )
237*cdf0e10cSrcweir         return NULL;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     // the FontList's key mpFontData member is reinterpreted as font id
240*cdf0e10cSrcweir     ImplFontSelectData aFontSelData = rFontSelData;
241*cdf0e10cSrcweir     aFontSelData.mpFontData = reinterpret_cast<ImplFontData*>( nFontId );
242*cdf0e10cSrcweir     FontList::iterator it = maFontList.find( aFontSelData );
243*cdf0e10cSrcweir     if( it != maFontList.end() )
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir         ServerFont* pFound = it->second;
246*cdf0e10cSrcweir         if( pFound )
247*cdf0e10cSrcweir             pFound->AddRef();
248*cdf0e10cSrcweir         return pFound;
249*cdf0e10cSrcweir     }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     // font not cached yet => create new font item
252*cdf0e10cSrcweir     ServerFont* pNew = NULL;
253*cdf0e10cSrcweir     if( mpFtManager )
254*cdf0e10cSrcweir         pNew = mpFtManager->CreateFont( aFontSelData );
255*cdf0e10cSrcweir     // TODO: pNew = VirtDevServerFont::CreateFont( aFontSelData );
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     if( pNew )
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         maFontList[ aFontSelData ] = pNew;
260*cdf0e10cSrcweir         mnBytesUsed += pNew->GetByteCount();
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         // enable garbage collection for new font
263*cdf0e10cSrcweir         if( !mpCurrentGCFont )
264*cdf0e10cSrcweir         {
265*cdf0e10cSrcweir             mpCurrentGCFont = pNew;
266*cdf0e10cSrcweir             pNew->mpNextGCFont = pNew;
267*cdf0e10cSrcweir             pNew->mpPrevGCFont = pNew;
268*cdf0e10cSrcweir         }
269*cdf0e10cSrcweir         else
270*cdf0e10cSrcweir         {
271*cdf0e10cSrcweir             pNew->mpNextGCFont = mpCurrentGCFont;
272*cdf0e10cSrcweir             pNew->mpPrevGCFont = mpCurrentGCFont->mpPrevGCFont;
273*cdf0e10cSrcweir             pNew->mpPrevGCFont->mpNextGCFont = pNew;
274*cdf0e10cSrcweir             mpCurrentGCFont->mpPrevGCFont = pNew;
275*cdf0e10cSrcweir         }
276*cdf0e10cSrcweir     }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir     return pNew;
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir // -----------------------------------------------------------------------
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir void GlyphCache::UncacheFont( ServerFont& rServerFont )
284*cdf0e10cSrcweir {
285*cdf0e10cSrcweir     // the interface for rServerFont must be const because a
286*cdf0e10cSrcweir     // user who wants to release it only got const ServerFonts.
287*cdf0e10cSrcweir     // The caching algorithm needs a non-const object
288*cdf0e10cSrcweir     ServerFont* pFont = const_cast<ServerFont*>( &rServerFont );
289*cdf0e10cSrcweir     if( (pFont->Release() <= 0)
290*cdf0e10cSrcweir     &&  (mnMaxSize <= (mnBytesUsed + mrPeer.GetByteCount())) )
291*cdf0e10cSrcweir     {
292*cdf0e10cSrcweir         mpCurrentGCFont = pFont;
293*cdf0e10cSrcweir         GarbageCollect();
294*cdf0e10cSrcweir     }
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir // -----------------------------------------------------------------------
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir sal_uLong GlyphCache::CalcByteCount() const
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir     sal_uLong nCacheSize = sizeof(*this);
302*cdf0e10cSrcweir     for( FontList::const_iterator it = maFontList.begin(); it != maFontList.end(); ++it )
303*cdf0e10cSrcweir     {
304*cdf0e10cSrcweir         const ServerFont* pSF = it->second;
305*cdf0e10cSrcweir         if( pSF )
306*cdf0e10cSrcweir             nCacheSize += pSF->GetByteCount();
307*cdf0e10cSrcweir     }
308*cdf0e10cSrcweir     // TODO: also account something for hashtable management
309*cdf0e10cSrcweir     return nCacheSize;
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir // -----------------------------------------------------------------------
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir void GlyphCache::GarbageCollect()
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir     // when current GC font has been destroyed get another one
317*cdf0e10cSrcweir     if( !mpCurrentGCFont )
318*cdf0e10cSrcweir     {
319*cdf0e10cSrcweir         FontList::iterator it = maFontList.begin();
320*cdf0e10cSrcweir         if( it != maFontList.end() )
321*cdf0e10cSrcweir             mpCurrentGCFont = it->second;
322*cdf0e10cSrcweir     }
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir     // unless there is no other font to collect
325*cdf0e10cSrcweir     if( !mpCurrentGCFont )
326*cdf0e10cSrcweir         return;
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir     // prepare advance to next font for garbage collection
329*cdf0e10cSrcweir     ServerFont* const pServerFont = mpCurrentGCFont;
330*cdf0e10cSrcweir     mpCurrentGCFont = pServerFont->mpNextGCFont;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir     if( (pServerFont == mpCurrentGCFont)    // no other fonts
333*cdf0e10cSrcweir     ||  (pServerFont->GetRefCount() > 0) )  // font still used
334*cdf0e10cSrcweir     {
335*cdf0e10cSrcweir         // try to garbage collect at least a few bytes
336*cdf0e10cSrcweir         pServerFont->GarbageCollect( mnLruIndex - mnGlyphCount/2 );
337*cdf0e10cSrcweir     }
338*cdf0e10cSrcweir     else // current GC font is unreferenced
339*cdf0e10cSrcweir     {
340*cdf0e10cSrcweir         DBG_ASSERT( (pServerFont->GetRefCount() == 0),
341*cdf0e10cSrcweir             "GlyphCache::GC detected RefCount underflow" );
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir         // free all pServerFont related data
344*cdf0e10cSrcweir         pServerFont->GarbageCollect( mnLruIndex+0x10000000 );
345*cdf0e10cSrcweir         if( pServerFont == mpCurrentGCFont )
346*cdf0e10cSrcweir             mpCurrentGCFont = NULL;
347*cdf0e10cSrcweir 	const ImplFontSelectData& rIFSD = pServerFont->GetFontSelData();
348*cdf0e10cSrcweir         maFontList.erase( rIFSD );
349*cdf0e10cSrcweir         mrPeer.RemovingFont( *pServerFont );
350*cdf0e10cSrcweir         mnBytesUsed -= pServerFont->GetByteCount();
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir         // remove font from list of garbage collected fonts
353*cdf0e10cSrcweir         if( pServerFont->mpPrevGCFont )
354*cdf0e10cSrcweir             pServerFont->mpPrevGCFont->mpNextGCFont = pServerFont->mpNextGCFont;
355*cdf0e10cSrcweir         if( pServerFont->mpNextGCFont )
356*cdf0e10cSrcweir             pServerFont->mpNextGCFont->mpPrevGCFont = pServerFont->mpPrevGCFont;
357*cdf0e10cSrcweir         if( pServerFont == mpCurrentGCFont )
358*cdf0e10cSrcweir             mpCurrentGCFont = NULL;
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir         delete pServerFont;
361*cdf0e10cSrcweir     }
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir // -----------------------------------------------------------------------
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir inline void GlyphCache::UsingGlyph( ServerFont&, GlyphData& rGlyphData )
367*cdf0e10cSrcweir {
368*cdf0e10cSrcweir     rGlyphData.SetLruValue( mnLruIndex++ );
369*cdf0e10cSrcweir }
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir // -----------------------------------------------------------------------
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir inline void GlyphCache::AddedGlyph( ServerFont& rServerFont, GlyphData& rGlyphData )
374*cdf0e10cSrcweir {
375*cdf0e10cSrcweir     ++mnGlyphCount;
376*cdf0e10cSrcweir     mnBytesUsed += sizeof( rGlyphData );
377*cdf0e10cSrcweir     UsingGlyph( rServerFont, rGlyphData );
378*cdf0e10cSrcweir     GrowNotify();
379*cdf0e10cSrcweir }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir // -----------------------------------------------------------------------
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir void GlyphCache::GrowNotify()
384*cdf0e10cSrcweir {
385*cdf0e10cSrcweir     if( (mnBytesUsed + mrPeer.GetByteCount()) > mnMaxSize )
386*cdf0e10cSrcweir         GarbageCollect();
387*cdf0e10cSrcweir }
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir // -----------------------------------------------------------------------
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir inline void GlyphCache::RemovingGlyph( ServerFont& rSF, GlyphData& rGD, int nGlyphIndex )
392*cdf0e10cSrcweir {
393*cdf0e10cSrcweir     mrPeer.RemovingGlyph( rSF, rGD, nGlyphIndex );
394*cdf0e10cSrcweir     mnBytesUsed -= sizeof( GlyphData );
395*cdf0e10cSrcweir     --mnGlyphCount;
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir // =======================================================================
399*cdf0e10cSrcweir // ServerFont
400*cdf0e10cSrcweir // =======================================================================
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir ServerFont::ServerFont( const ImplFontSelectData& rFSD )
403*cdf0e10cSrcweir :   maGlyphList( 0),
404*cdf0e10cSrcweir     maFontSelData(rFSD),
405*cdf0e10cSrcweir     mnExtInfo(0),
406*cdf0e10cSrcweir     mnRefCount(1),
407*cdf0e10cSrcweir     mnBytesUsed( sizeof(ServerFont) ),
408*cdf0e10cSrcweir     mpPrevGCFont( NULL ),
409*cdf0e10cSrcweir     mpNextGCFont( NULL ),
410*cdf0e10cSrcweir     mnCos( 0x10000),
411*cdf0e10cSrcweir     mnSin( 0 ),
412*cdf0e10cSrcweir     mnZWJ( 0 ),
413*cdf0e10cSrcweir     mnZWNJ( 0 ),
414*cdf0e10cSrcweir     mbCollectedZW( false )
415*cdf0e10cSrcweir {
416*cdf0e10cSrcweir     // TODO: move update of mpFontEntry into FontEntry class when
417*cdf0e10cSrcweir     // it becomes reponsible for the ServerFont instantiation
418*cdf0e10cSrcweir     ((ImplServerFontEntry*)rFSD.mpFontEntry)->SetServerFont( this );
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir     if( rFSD.mnOrientation != 0 )
421*cdf0e10cSrcweir     {
422*cdf0e10cSrcweir         const double dRad = rFSD.mnOrientation * ( F_2PI / 3600.0 );
423*cdf0e10cSrcweir         mnCos = static_cast<long>( 0x10000 * cos( dRad ) + 0.5 );
424*cdf0e10cSrcweir         mnSin = static_cast<long>( 0x10000 * sin( dRad ) + 0.5 );
425*cdf0e10cSrcweir     }
426*cdf0e10cSrcweir }
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir // -----------------------------------------------------------------------
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir ServerFont::~ServerFont()
431*cdf0e10cSrcweir {
432*cdf0e10cSrcweir     ReleaseFromGarbageCollect();
433*cdf0e10cSrcweir }
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir // -----------------------------------------------------------------------
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir void ServerFont::ReleaseFromGarbageCollect()
438*cdf0e10cSrcweir {
439*cdf0e10cSrcweir    // remove from GC list
440*cdf0e10cSrcweir     ServerFont* pPrev = mpPrevGCFont;
441*cdf0e10cSrcweir     ServerFont* pNext = mpNextGCFont;
442*cdf0e10cSrcweir     if( pPrev ) pPrev->mpNextGCFont = pNext;
443*cdf0e10cSrcweir     if( pNext ) pNext->mpPrevGCFont = pPrev;
444*cdf0e10cSrcweir     mpPrevGCFont = NULL;
445*cdf0e10cSrcweir     mpNextGCFont = NULL;
446*cdf0e10cSrcweir }
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir // -----------------------------------------------------------------------
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir long ServerFont::Release() const
451*cdf0e10cSrcweir {
452*cdf0e10cSrcweir     DBG_ASSERT( mnRefCount > 0, "ServerFont: RefCount underflow" );
453*cdf0e10cSrcweir     return --mnRefCount;
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir // -----------------------------------------------------------------------
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir GlyphData& ServerFont::GetGlyphData( int nGlyphIndex )
459*cdf0e10cSrcweir {
460*cdf0e10cSrcweir     // usually the GlyphData is cached
461*cdf0e10cSrcweir     GlyphList::iterator it = maGlyphList.find( nGlyphIndex );
462*cdf0e10cSrcweir     if( it != maGlyphList.end() ) {
463*cdf0e10cSrcweir         GlyphData& rGlyphData = it->second;
464*cdf0e10cSrcweir         GlyphCache::GetInstance().UsingGlyph( *this, rGlyphData );
465*cdf0e10cSrcweir         return rGlyphData;
466*cdf0e10cSrcweir     }
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir     // sometimes not => we need to create and initialize it ourselves
469*cdf0e10cSrcweir     GlyphData& rGlyphData = maGlyphList[ nGlyphIndex ];
470*cdf0e10cSrcweir     mnBytesUsed += sizeof( GlyphData );
471*cdf0e10cSrcweir     InitGlyphData( nGlyphIndex, rGlyphData );
472*cdf0e10cSrcweir     GlyphCache::GetInstance().AddedGlyph( *this, rGlyphData );
473*cdf0e10cSrcweir     return rGlyphData;
474*cdf0e10cSrcweir }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir // -----------------------------------------------------------------------
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir void ServerFont::GarbageCollect( long nMinLruIndex )
479*cdf0e10cSrcweir {
480*cdf0e10cSrcweir     GlyphList::iterator it_next = maGlyphList.begin();
481*cdf0e10cSrcweir     while( it_next != maGlyphList.end() )
482*cdf0e10cSrcweir     {
483*cdf0e10cSrcweir         GlyphList::iterator it = it_next++;
484*cdf0e10cSrcweir         GlyphData& rGD = it->second;
485*cdf0e10cSrcweir         if( (nMinLruIndex - rGD.GetLruValue()) > 0 )
486*cdf0e10cSrcweir         {
487*cdf0e10cSrcweir             OSL_ASSERT( mnBytesUsed >= sizeof(GlyphData) );
488*cdf0e10cSrcweir             mnBytesUsed -= sizeof( GlyphData );
489*cdf0e10cSrcweir             GlyphCache::GetInstance().RemovingGlyph( *this, rGD, it->first );
490*cdf0e10cSrcweir             maGlyphList.erase( it );
491*cdf0e10cSrcweir             it_next = maGlyphList.begin();
492*cdf0e10cSrcweir         }
493*cdf0e10cSrcweir     }
494*cdf0e10cSrcweir }
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir // -----------------------------------------------------------------------
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir Point ServerFont::TransformPoint( const Point& rPoint ) const
499*cdf0e10cSrcweir {
500*cdf0e10cSrcweir     if( mnCos == 0x10000 )
501*cdf0e10cSrcweir         return rPoint;
502*cdf0e10cSrcweir     // TODO: use 32x32=>64bit intermediate
503*cdf0e10cSrcweir     const double dCos = mnCos * (1.0 / 0x10000);
504*cdf0e10cSrcweir     const double dSin = mnSin * (1.0 / 0x10000);
505*cdf0e10cSrcweir     long nX = (long)(rPoint.X() * dCos + rPoint.Y() * dSin);
506*cdf0e10cSrcweir     long nY = (long)(rPoint.Y() * dCos - rPoint.X() * dSin);
507*cdf0e10cSrcweir     return Point( nX, nY );
508*cdf0e10cSrcweir }
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir bool ServerFont::IsGlyphInvisible( int nGlyphIndex )
511*cdf0e10cSrcweir {
512*cdf0e10cSrcweir     if (!mbCollectedZW)
513*cdf0e10cSrcweir     {
514*cdf0e10cSrcweir         mnZWJ = GetGlyphIndex( 0x200D );
515*cdf0e10cSrcweir         mnZWNJ = GetGlyphIndex( 0x200C );
516*cdf0e10cSrcweir         mbCollectedZW = true;
517*cdf0e10cSrcweir     }
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir     if( !nGlyphIndex ) // don't hide the NotDef glyph
520*cdf0e10cSrcweir         return false;
521*cdf0e10cSrcweir     if( (nGlyphIndex == mnZWNJ) || (nGlyphIndex == mnZWJ) )
522*cdf0e10cSrcweir         return true;
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir     return false;
525*cdf0e10cSrcweir }
526*cdf0e10cSrcweir 
527*cdf0e10cSrcweir // =======================================================================
528*cdf0e10cSrcweir 
529*cdf0e10cSrcweir ImplServerFontEntry::ImplServerFontEntry( ImplFontSelectData& rFSD )
530*cdf0e10cSrcweir :   ImplFontEntry( rFSD )
531*cdf0e10cSrcweir ,   mpServerFont( NULL )
532*cdf0e10cSrcweir ,   mbGotFontOptions( false )
533*cdf0e10cSrcweir ,   mbValidFontOptions( false )
534*cdf0e10cSrcweir {}
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir // -----------------------------------------------------------------------
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir ImplServerFontEntry::~ImplServerFontEntry()
539*cdf0e10cSrcweir {
540*cdf0e10cSrcweir     // TODO: remove the ServerFont here instead of in the GlyphCache
541*cdf0e10cSrcweir }
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir // =======================================================================
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir ExtraKernInfo::ExtraKernInfo( sal_IntPtr nFontId )
546*cdf0e10cSrcweir :   mbInitialized( false ),
547*cdf0e10cSrcweir     mnFontId( nFontId ),
548*cdf0e10cSrcweir     maUnicodeKernPairs( 0 )
549*cdf0e10cSrcweir {}
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir //--------------------------------------------------------------------------
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir bool ExtraKernInfo::HasKernPairs() const
554*cdf0e10cSrcweir {
555*cdf0e10cSrcweir     if( !mbInitialized )
556*cdf0e10cSrcweir         Initialize();
557*cdf0e10cSrcweir     return !maUnicodeKernPairs.empty();
558*cdf0e10cSrcweir }
559*cdf0e10cSrcweir 
560*cdf0e10cSrcweir //--------------------------------------------------------------------------
561*cdf0e10cSrcweir 
562*cdf0e10cSrcweir int ExtraKernInfo::GetUnscaledKernPairs( ImplKernPairData** ppKernPairs ) const
563*cdf0e10cSrcweir {
564*cdf0e10cSrcweir     if( !mbInitialized )
565*cdf0e10cSrcweir         Initialize();
566*cdf0e10cSrcweir 
567*cdf0e10cSrcweir     // return early if no kerning available
568*cdf0e10cSrcweir     if( maUnicodeKernPairs.empty() )
569*cdf0e10cSrcweir         return 0;
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir     // allocate kern pair table
572*cdf0e10cSrcweir     int nKernCount = maUnicodeKernPairs.size();
573*cdf0e10cSrcweir     *ppKernPairs = new ImplKernPairData[ nKernCount ];
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir     // fill in unicode kern pairs with the kern value scaled to the font width
576*cdf0e10cSrcweir     ImplKernPairData* pKernData = *ppKernPairs;
577*cdf0e10cSrcweir     UnicodeKernPairs::const_iterator it = maUnicodeKernPairs.begin();
578*cdf0e10cSrcweir     for(; it != maUnicodeKernPairs.end(); ++it )
579*cdf0e10cSrcweir         *(pKernData++) = *it;
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir     return nKernCount;
582*cdf0e10cSrcweir }
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir //--------------------------------------------------------------------------
585*cdf0e10cSrcweir 
586*cdf0e10cSrcweir int ExtraKernInfo::GetUnscaledKernValue( sal_Unicode cLeft, sal_Unicode cRight ) const
587*cdf0e10cSrcweir {
588*cdf0e10cSrcweir     if( !mbInitialized )
589*cdf0e10cSrcweir         Initialize();
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir     if( maUnicodeKernPairs.empty() )
592*cdf0e10cSrcweir         return 0;
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir     ImplKernPairData aKernPair = { cLeft, cRight, 0 };
595*cdf0e10cSrcweir     UnicodeKernPairs::const_iterator it = maUnicodeKernPairs.find( aKernPair );
596*cdf0e10cSrcweir     if( it == maUnicodeKernPairs.end() )
597*cdf0e10cSrcweir         return 0;
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir     int nUnscaledValue = (*it).mnKern;
600*cdf0e10cSrcweir     return nUnscaledValue;
601*cdf0e10cSrcweir }
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir // =======================================================================
604*cdf0e10cSrcweir 
605