1/*
2 * Copyright (C) 2007, 2008, 2009, 2010, 2013 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 <ApplicationServices/ApplicationServices.h>
28#include <WebKitSystemInterface/WebKitSystemInterface.h>
29#include <wtf/RetainPtr.h>
30#include <wtf/text/Base64.h>
31#include <wtf/win/GDIObject.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().data(), 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    auto hfont = adoptGDIObject(::CreateFontIndirect(&logFont));
66
67    RetainPtr<CGFontRef> cgFont = adoptCF(CGFontCreateWithPlatformFont(&logFont));
68    return FontPlatformData(WTF::move(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
83std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
84{
85    String fontName = createUniqueFontName();
86    HANDLE fontReference;
87    fontReference = renameAndActivateFont(buffer, fontName);
88    if (!fontReference)
89        return nullptr;
90    return std::make_unique<FontCustomPlatformData>(fontReference, fontName);
91}
92
93bool FontCustomPlatformData::supportsFormat(const String& format)
94{
95    return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "opentype") || equalIgnoringCase(format, "woff");
96}
97
98}
99