javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/qt/library/swtfontcache.cpp
changeset 21 2a9601315dfc
child 67 63b81d807542
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     Nokia Corporation - initial implementation
       
    10  *******************************************************************************/
       
    11 
       
    12 #include <QFont>
       
    13 #include "swtfontcache.h"
       
    14 #include "swtlog.h"
       
    15 
       
    16 #define SWTFONTCACHE_GRANULARITY 10
       
    17 
       
    18 namespace Java { namespace eSWT {
       
    19 
       
    20 SwtFontCache::SwtFontCache() : mFonts(NULL), mSize(0), mCount(0)
       
    21 {
       
    22     SWT_LOG_JNI_CALL();
       
    23 }
       
    24 
       
    25 SwtFontCache::~SwtFontCache()
       
    26 {
       
    27     SWT_LOG_JNI_CALL();
       
    28         
       
    29     for(int i = 0; i < mCount; ++i)
       
    30     {
       
    31         delete mFonts[i];
       
    32         mFonts[i] = NULL;
       
    33     }
       
    34     delete[] mFonts;
       
    35     mFonts = NULL;
       
    36 }
       
    37 
       
    38 QFont* SwtFontCache::cache(QFont* aFont)
       
    39 {
       
    40     SWT_LOG_JNI_CALL();
       
    41     
       
    42     QFont* cached = findEqual( aFont );
       
    43     if( cached )
       
    44     {
       
    45         delete aFont;
       
    46         aFont = NULL;
       
    47     }
       
    48     else
       
    49     {
       
    50         add( aFont );
       
    51         cached = aFont;
       
    52     }
       
    53     return cached;
       
    54 }
       
    55 
       
    56 bool SwtFontCache::isCached(const QFont* const aFont)
       
    57 {
       
    58     SWT_LOG_JNI_CALL();
       
    59     
       
    60     for(int i = 0; i < mCount; ++i)
       
    61     {
       
    62         if(mFonts[i] == aFont)
       
    63         {
       
    64             return true;
       
    65         }
       
    66     }
       
    67     return false;
       
    68 }
       
    69 
       
    70 QFont* SwtFontCache::findEqual(QFont* aFont)
       
    71 {
       
    72     SWT_LOG_JNI_CALL();
       
    73 
       
    74     if(aFont == NULL)
       
    75     {
       
    76         return NULL;
       
    77     }
       
    78     for(int i = 0; i < mCount; ++i)
       
    79     {
       
    80         if( *(mFonts[i]) == *aFont )
       
    81         {
       
    82             return mFonts[i];
       
    83         }
       
    84     }
       
    85     return NULL;
       
    86 }
       
    87 
       
    88 void SwtFontCache::add(QFont* aFont)
       
    89 {
       
    90     SWT_LOG_JNI_CALL();
       
    91 
       
    92     if(mSize == mCount)
       
    93     {
       
    94         const int newSize = mSize + SWTFONTCACHE_GRANULARITY;
       
    95         QFont** bigger = new QFont* [newSize];
       
    96         if(mFonts != NULL)
       
    97         {
       
    98             ::memcpy( bigger, mFonts, mSize * sizeof(QFont*) );
       
    99             delete[] mFonts;
       
   100         }
       
   101         ::memset( &bigger[mSize], 0, (newSize - mSize) * sizeof(QFont*) );
       
   102         mFonts = bigger;
       
   103         mSize = newSize;
       
   104     }
       
   105     mFonts[mCount++] = aFont;
       
   106 }
       
   107 
       
   108 }}
       
   109