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 "OpenTypeUtilities.h" 28#include "SharedBuffer.h" 29#include <wtf/RandomNumber.h> 30#include <wtf/text/Base64.h> 31 32namespace WebCore { 33 34static CustomFontCache* g_customFontCache = 0; 35 36void setCustomFontCache(CustomFontCache* cache) 37{ 38 g_customFontCache = cache; 39} 40 41FontCustomPlatformData::~FontCustomPlatformData() 42{ 43 if (g_customFontCache && !m_name.isEmpty()) 44 g_customFontCache->unregisterFont(m_name); 45} 46 47FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontOrientation, FontWidthVariant, 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. 59static String createUniqueFontName() 60{ 61 GUID fontUuid; 62 63 unsigned int* ptr = reinterpret_cast<unsigned int*>(&fontUuid); 64 for (int i = 0; i < sizeof(GUID) / sizeof(int) ; ++i) 65 *(ptr + i) = static_cast<unsigned int>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)); 66 67 String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid)); 68 ASSERT(fontName.length() < LF_FACESIZE); 69 return fontName.replace('/', '_'); 70} 71 72std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer) 73{ 74 String fontName = createUniqueFontName(); 75 76 Vector<char> rewrittenFontData; 77 if (!renameFont(buffer, fontName, rewrittenFontData)) 78 return nullptr; 79 80 RefPtr<SharedBuffer> localBuffer = SharedBuffer::adoptVector(rewrittenFontData); 81 if (!g_customFontCache || !g_customFontCache->registerFont(fontName, localBuffer.get())) 82 return nullptr; 83 84 return std::make_unique<FontCustomPlatformData>(fontName); 85} 86 87bool FontCustomPlatformData::supportsFormat(const String& format) 88{ 89 return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "opentype"); 90} 91 92} 93