javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/qt/library/swtfontcache.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 17 Sep 2010 08:28:21 +0300
changeset 76 4ad59aaee882
parent 21 2a9601315dfc
permissions -rw-r--r--
Revision: v2.2.13 Kit: 201037

/*******************************************************************************
 * Copyright (c) 2009, 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Nokia Corporation - initial implementation
 *******************************************************************************/

#include <QFont>
#include "swtfontcache.h"
#include "swtlog.h"

#define SWTFONTCACHE_GRANULARITY 10

namespace Java { namespace eSWT {

SwtFontCache::SwtFontCache() : mFonts(NULL), mSize(0), mCount(0)
{
    SWT_LOG_FUNC_CALL();
}

SwtFontCache::~SwtFontCache()
{
    SWT_LOG_FUNC_CALL();
        
    for(int i = 0; i < mCount; ++i)
    {
        delete mFonts[i];
        mFonts[i] = NULL;
    }
    delete[] mFonts;
    mFonts = NULL;
}

QFont* SwtFontCache::cache(QFont* aFont)
{
    SWT_LOG_FUNC_CALL();
    
    QFont* cached = findEqual( aFont );
    if( cached )
    {
        delete aFont;
        aFont = NULL;
    }
    else
    {
        add( aFont );
        cached = aFont;
    }
    return cached;
}

bool SwtFontCache::isCached(const QFont* const aFont)
{
    SWT_LOG_FUNC_CALL();
    
    for(int i = 0; i < mCount; ++i)
    {
        if(mFonts[i] == aFont)
        {
            return true;
        }
    }
    return false;
}

QFont* SwtFontCache::findEqual(QFont* aFont)
{
    SWT_LOG_FUNC_CALL();

    if(aFont == NULL)
    {
        return NULL;
    }
    for(int i = 0; i < mCount; ++i)
    {
        if( *(mFonts[i]) == *aFont )
        {
            return mFonts[i];
        }
    }
    return NULL;
}

void SwtFontCache::add(QFont* aFont)
{
    SWT_LOG_FUNC_CALL();

    if(mSize == mCount)
    {
        const int newSize = mSize + SWTFONTCACHE_GRANULARITY;
        QFont** bigger = new QFont* [newSize];
        if(mFonts != NULL)
        {
            ::memcpy( bigger, mFonts, mSize * sizeof(QFont*) );
            delete[] mFonts;
        }
        ::memset( &bigger[mSize], 0, (newSize - mSize) * sizeof(QFont*) );
        mFonts = bigger;
        mSize = newSize;
    }
    mFonts[mCount++] = aFont;
}

}}