1/*
2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "FontCustomPlatformData.h"
23
24#include "FontPlatformData.h"
25#include "OpenTypeUtilities.h"
26#include "SharedBuffer.h"
27#include "WOFFFileFormat.h"
28#include <ApplicationServices/ApplicationServices.h>
29#include <WebKitSystemInterface/WebKitSystemInterface.h>
30#include <wtf/RetainPtr.h>
31#include <wtf/text/Base64.h>
32
33namespace WebCore {
34
35using namespace std;
36
37FontCustomPlatformData::~FontCustomPlatformData()
38{
39    if (m_fontReference)
40            RemoveFontMemResourceEx(m_fontReference);
41}
42
43FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontOrientation, FontWidthVariant, FontRenderingMode renderingMode)
44{
45    ASSERT(m_fontReference);
46
47    LOGFONT& logFont = *static_cast<LOGFONT*>(malloc(sizeof(LOGFONT)));
48    memcpy(logFont.lfFaceName, m_name.charactersWithNullTermination(), sizeof(logFont.lfFaceName[0]) * std::min<size_t>(static_cast<size_t>(LF_FACESIZE), 1 + m_name.length()));
49
50    logFont.lfHeight = -size;
51    if (renderingMode == NormalRenderingMode)
52        logFont.lfHeight *= 32;
53    logFont.lfWidth = 0;
54    logFont.lfEscapement = 0;
55    logFont.lfOrientation = 0;
56    logFont.lfUnderline = false;
57    logFont.lfStrikeOut = false;
58    logFont.lfCharSet = DEFAULT_CHARSET;
59    logFont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
60    logFont.lfQuality = CLEARTYPE_QUALITY;
61    logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
62    logFont.lfItalic = italic;
63    logFont.lfWeight = bold ? 700 : 400;
64
65    HFONT hfont = CreateFontIndirect(&logFont);
66
67    RetainPtr<CGFontRef> cgFont = adoptCF(CGFontCreateWithPlatformFont(&logFont));
68    return FontPlatformData(hfont, cgFont.get(), size, bold, italic, renderingMode == AlternateRenderingMode);
69}
70
71// Creates a unique and unpredictable font name, in order to avoid collisions and to
72// not allow access from CSS.
73static String createUniqueFontName()
74{
75    GUID fontUuid;
76    CoCreateGuid(&fontUuid);
77
78    String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid));
79    ASSERT(fontName.length() < LF_FACESIZE);
80    return fontName;
81}
82
83FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
84{
85    ASSERT_ARG(buffer, buffer);
86
87    RefPtr<SharedBuffer> sfntBuffer;
88    if (isWOFF(buffer)) {
89        Vector<char> sfnt;
90        if (!convertWOFFToSfnt(buffer, sfnt))
91            return 0;
92
93        sfntBuffer = SharedBuffer::adoptVector(sfnt);
94        buffer = sfntBuffer.get();
95    }
96
97    String fontName = createUniqueFontName();
98    HANDLE fontReference;
99    fontReference = renameAndActivateFont(buffer, fontName);
100    if (!fontReference)
101        return 0;
102    return new FontCustomPlatformData(fontReference, fontName);
103}
104
105bool FontCustomPlatformData::supportsFormat(const String& format)
106{
107    return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "opentype") || equalIgnoringCase(format, "woff");
108}
109
110}
111