Home | History | Annotate | Download | only in wince
      1 /*
      2  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2009 Torch Mobile Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  *
     20  */
     21 
     22 #include "config.h"
     23 #include "FontCustomPlatformData.h"
     24 
     25 #include "Base64.h"
     26 #include "CachedFont.h"
     27 #include "FontPlatformData.h"
     28 #include <wtf/RandomNumber.h>
     29 
     30 namespace WebCore {
     31 
     32 static CustomFontCache* g_customFontCache = 0;
     33 
     34 bool renameFont(SharedBuffer* fontData, const String& fontName);
     35 
     36 void setCustomFontCache(CustomFontCache* cache)
     37 {
     38     g_customFontCache = cache;
     39 }
     40 
     41 FontCustomPlatformData::~FontCustomPlatformData()
     42 {
     43     if (g_customFontCache && !m_name.isEmpty())
     44         g_customFontCache->unregisterFont(m_name);
     45 }
     46 
     47 FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontRenderingMode renderingMode)
     48 {
     49     FontDescription fontDesc;
     50     fontDesc.setComputedSize(size);
     51     fontDesc.setSpecifiedSize(size);
     52     fontDesc.setItalic(italic);
     53     fontDesc.setWeight(bold ? FontWeightBold : FontWeightNormal);
     54     return FontPlatformData(fontDesc, m_name, false);
     55 }
     56 
     57 // Creates a unique and unpredictable font name, in order to avoid collisions and to
     58 // not allow access from CSS.
     59 static String createUniqueFontName()
     60 {
     61     Vector<char> fontUuid(sizeof(GUID));
     62 
     63     unsigned int* ptr = reinterpret_cast<unsigned int*>(fontUuid.data());
     64     for (int i = 0; i < sizeof(GUID) / sizeof(int) ; ++i)
     65         *(ptr + i) = static_cast<unsigned int>(WTF::randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0));
     66 
     67     Vector<char> fontNameVector;
     68     base64Encode(fontUuid, fontNameVector);
     69     ASSERT(fontNameVector.size() < LF_FACESIZE);
     70     String fontName(fontNameVector.data(), fontNameVector.size());
     71     return fontName.replace('/', '_');
     72 }
     73 
     74 FontCustomPlatformData* createFontCustomPlatformData(const SharedBuffer* buffer)
     75 {
     76     if (g_customFontCache) {
     77         String fontName = createUniqueFontName();
     78         RefPtr<SharedBuffer> localBuffer = SharedBuffer::create(buffer->data(), buffer->size());
     79         if (renameFont(localBuffer.get(), fontName) && g_customFontCache->registerFont(fontName, localBuffer.get()))
     80             return new FontCustomPlatformData(fontName);
     81     }
     82     return 0;
     83 }
     84 
     85 }
     86