2000-09-18 11:07:07 -05:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* $RCSfile: bmcache.cxx,v $
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* $Revision: 1.3 $
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* last change: $Author: rt $ $Date: 2005-09-09 07:05:21 $
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* The Contents of this file are made available subject to
|
|
|
|
* the terms of GNU Lesser General Public License Version 2.1.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* This library is 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
|
|
|
|
* Lesser General Public License for more details.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2005-09-09 01:05:21 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#pragma hdrstop
|
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
#include <limits.h> // LONG_MAX
|
2000-09-18 11:07:07 -05:00
|
|
|
#include <tools/solar.h>
|
2001-09-24 07:41:22 -05:00
|
|
|
#include <goodies/grfmgr.hxx>
|
2000-09-18 11:07:07 -05:00
|
|
|
#include "bmcache.hxx"
|
|
|
|
|
|
|
|
// eine Struktur fuer die Cache-Eintraege
|
2001-09-24 07:41:22 -05:00
|
|
|
struct BitmapCacheEntry
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
const SdPage* pPage;
|
|
|
|
GraphicObject* pGraphicObject;
|
|
|
|
sal_uInt32 nSizeBytes;
|
|
|
|
long nZoom;
|
|
|
|
};
|
2000-09-18 11:07:07 -05:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* Destruktor, loescht die gespeicherten Bitmaps
|
|
|
|
|*
|
|
|
|
\************************************************************************/
|
|
|
|
|
|
|
|
BitmapCache::~BitmapCache()
|
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
for( void* pEntry = aEntries.First(); pEntry; pEntry = aEntries.Next() )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
delete static_cast< BitmapCacheEntry* >( pEntry )->pGraphicObject;
|
|
|
|
delete static_cast< BitmapCacheEntry* >( pEntry );
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* Cache-Eintrag einfuegen
|
|
|
|
|*
|
|
|
|
\************************************************************************/
|
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
void BitmapCache::Add(const SdPage* pPage, const Bitmap& rBmp, long nZoom)
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
BitmapCacheEntry* pEntry = NULL;
|
|
|
|
ULONG nSizeOfBitmap = rBmp.GetSizeBytes();
|
2000-09-18 11:07:07 -05:00
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
if( nSizeOfBitmap < nMaxSize )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
while (nCurSize + nSizeOfBitmap > nMaxSize)
|
|
|
|
{
|
|
|
|
if( aEntries.Count() )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
pEntry = (BitmapCacheEntry*) aEntries.Remove(aEntries.Count() - 1);
|
|
|
|
|
|
|
|
if( pEntry && pEntry->pGraphicObject )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
nCurSize -= pEntry->nSizeBytes;
|
|
|
|
delete pEntry->pGraphicObject;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
2001-09-24 07:41:22 -05:00
|
|
|
|
|
|
|
delete pEntry;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
2001-09-24 07:41:22 -05:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2000-09-18 11:07:07 -05:00
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
pEntry = new BitmapCacheEntry;
|
|
|
|
pEntry->pPage = pPage;
|
|
|
|
pEntry->pGraphicObject = new GraphicObject( rBmp );
|
|
|
|
pEntry->nSizeBytes = nSizeOfBitmap;
|
|
|
|
pEntry->nZoom = nZoom;
|
2000-09-18 11:07:07 -05:00
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
aEntries.Insert( pEntry, (ULONG) 0 );
|
2000-09-18 11:07:07 -05:00
|
|
|
nCurSize += nSizeOfBitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* Cache-Eintrag suchen, Bitmap-Zeiger zurueckgeben (NULL, wenn Bitmap nicht
|
|
|
|
|* im Cache)
|
|
|
|
|* - auf rZoomPercent wird der Zoomfaktor der Bitmap geschrieben
|
|
|
|
|* - abs(nZoomTolerance) gibt die maximal erlaubte Abweichung des
|
|
|
|
|* Zoomfaktors an; ist die Toleranz negativ, so sucht der Cache den
|
|
|
|
|* "best fit", sonst den "first fit",
|
|
|
|
|* - es werden nur Bitmaps mit Zoomfaktoren kleiner oder gleich rZoomPercent
|
|
|
|
|* beruecksichtigt, um ein Verkleinern der Bitmap beim Zeichnen zu vermeiden
|
|
|
|
|*
|
|
|
|
\************************************************************************/
|
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
const GraphicObject* BitmapCache::Get( const SdPage* pPage, long& rZoomPercent, long nZoomTolerancePercent)
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
|
|
|
BitmapCacheEntry* pEntry = NULL;
|
2001-09-24 07:41:22 -05:00
|
|
|
GraphicObject* pGraphicObject = NULL;
|
2000-09-18 11:07:07 -05:00
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
if( nZoomTolerancePercent < 0 )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
// "best fit"-Suche
|
2000-09-18 11:07:07 -05:00
|
|
|
long nTolerance = -nZoomTolerancePercent;
|
|
|
|
BitmapCacheEntry* pBest = NULL;
|
|
|
|
long nBest = LONG_MAX;
|
|
|
|
long nTest = 0L;
|
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
for( ULONG nPos = 0; nPos < aEntries.Count(); nPos++ )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
pEntry = (BitmapCacheEntry*) aEntries.GetObject( nPos );
|
|
|
|
|
|
|
|
if( pEntry->pPage == pPage )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
|
|
|
nTest = rZoomPercent - pEntry->nZoom;
|
2001-09-24 07:41:22 -05:00
|
|
|
|
|
|
|
if( nTest >= 0 && nTest < nBest && nTest <= nTolerance )
|
2000-09-18 11:07:07 -05:00
|
|
|
pBest = pEntry;
|
|
|
|
}
|
|
|
|
}
|
2001-09-24 07:41:22 -05:00
|
|
|
|
2000-09-18 11:07:07 -05:00
|
|
|
pEntry = pBest;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
// "first fit"-suche
|
|
|
|
for( ULONG nPos = 0; nPos < aEntries.Count(); nPos++ )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
pEntry = (BitmapCacheEntry*)aEntries.GetObject( nPos );
|
|
|
|
|
|
|
|
if (pEntry->pPage == pPage && Abs( pEntry->nZoom - rZoomPercent ) <= nZoomTolerancePercent )
|
2000-09-18 11:07:07 -05:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
pEntry = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// was passendes gefunden?
|
2001-09-24 07:41:22 -05:00
|
|
|
if( pEntry )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
pGraphicObject = pEntry->pGraphicObject;
|
|
|
|
aEntries.Remove( pEntry );
|
|
|
|
aEntries.Insert( pEntry, (ULONG) 0 );
|
2000-09-18 11:07:07 -05:00
|
|
|
rZoomPercent = pEntry->nZoom;
|
|
|
|
}
|
2001-09-24 07:41:22 -05:00
|
|
|
|
|
|
|
return pGraphicObject;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
void BitmapCache::Remove( const SdPage* pPage )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
for( ULONG nPos = 0; nPos < aEntries.Count(); )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
BitmapCacheEntry* pCand = (BitmapCacheEntry*) aEntries.GetObject( nPos );
|
2000-09-18 11:07:07 -05:00
|
|
|
|
2001-09-24 07:41:22 -05:00
|
|
|
if( pCand->pPage == pPage )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
2001-09-24 07:41:22 -05:00
|
|
|
pCand = (BitmapCacheEntry*) aEntries.Remove((ULONG)nPos);
|
|
|
|
delete pCand->pGraphicObject;
|
2000-09-18 11:07:07 -05:00
|
|
|
delete pCand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nPos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|