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 "CachedFont.h" 26#include "FontPlatformData.h" 27#include "SharedBuffer.h" 28#include <wtf/RandomNumber.h> 29#include <wtf/text/Base64.h> 30 31namespace WebCore { 32 33static CustomFontCache* g_customFontCache = 0; 34 35bool renameFont(SharedBuffer* fontData, const String& fontName); 36 37void setCustomFontCache(CustomFontCache* cache) 38{ 39 g_customFontCache = cache; 40} 41 42FontCustomPlatformData::~FontCustomPlatformData() 43{ 44 if (g_customFontCache && !m_name.isEmpty()) 45 g_customFontCache->unregisterFont(m_name); 46} 47 48FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontOrientation, FontWidthVariant, FontRenderingMode renderingMode) 49{ 50 FontDescription fontDesc; 51 fontDesc.setComputedSize(size); 52 fontDesc.setSpecifiedSize(size); 53 fontDesc.setItalic(italic); 54 fontDesc.setWeight(bold ? FontWeightBold : FontWeightNormal); 55 return FontPlatformData(fontDesc, m_name, false); 56} 57 58// Creates a unique and unpredictable font name, in order to avoid collisions and to 59// not allow access from CSS. 60static String createUniqueFontName() 61{ 62 GUID fontUuid; 63 64 unsigned int* ptr = reinterpret_cast<unsigned int*>(&fontUuid); 65 for (int i = 0; i < sizeof(GUID) / sizeof(int) ; ++i) 66 *(ptr + i) = static_cast<unsigned int>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)); 67 68 String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid)); 69 ASSERT(fontName.length() < LF_FACESIZE); 70 return fontName.replace('/', '_'); 71} 72 73FontCustomPlatformData* createFontCustomPlatformData(const SharedBuffer* buffer) 74{ 75 if (g_customFontCache) { 76 String fontName = createUniqueFontName(); 77 RefPtr<SharedBuffer> localBuffer = SharedBuffer::create(buffer->data(), buffer->size()); 78 if (renameFont(localBuffer.get(), fontName) && g_customFontCache->registerFont(fontName, localBuffer.get())) 79 return new FontCustomPlatformData(fontName); 80 } 81 return 0; 82} 83 84bool FontCustomPlatformData::supportsFormat(const String& format) 85{ 86 return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "opentype"); 87} 88 89} 90