| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "platform/fonts/FontCustomPlatformData.h" | |
| 33 | |
| 34 #include "platform/LayoutTestSupport.h" | |
| 35 #include "platform/SharedBuffer.h" | |
| 36 #include "platform/fonts/FontPlatformData.h" | |
| 37 #include "platform/fonts/opentype/OpenTypeSanitizer.h" | |
| 38 #include "platform/fonts/opentype/OpenTypeUtilities.h" | |
| 39 #include "wtf/PassOwnPtr.h" | |
| 40 #include "wtf/RefPtr.h" | |
| 41 #include "wtf/text/Base64.h" | |
| 42 | |
| 43 #include <objbase.h> | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 // Creates a unique and unpredictable font name, in order to avoid collisions an
d to | |
| 48 // not allow access from CSS. | |
| 49 String createUniqueFontName() | |
| 50 { | |
| 51 GUID fontUuid; | |
| 52 CoCreateGuid(&fontUuid); | |
| 53 | |
| 54 String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fo
ntUuid)); | |
| 55 ASSERT(fontName.length() < LF_FACESIZE); | |
| 56 return fontName; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 namespace WebCore { | |
| 62 | |
| 63 FontCustomPlatformData::FontCustomPlatformData(HANDLE fontReference, const Strin
g& name) | |
| 64 : m_fontReference(fontReference) | |
| 65 , m_name(name) | |
| 66 { | |
| 67 } | |
| 68 | |
| 69 FontCustomPlatformData::~FontCustomPlatformData() | |
| 70 { | |
| 71 if (m_fontReference) | |
| 72 RemoveFontMemResourceEx(m_fontReference); | |
| 73 } | |
| 74 | |
| 75 FontPlatformData FontCustomPlatformData::fontPlatformData(float size, bool bold,
bool italic, FontOrientation orientation, FontWidthVariant) | |
| 76 { | |
| 77 ASSERT(m_fontReference); | |
| 78 | |
| 79 LOGFONT logFont; | |
| 80 // m_name comes from createUniqueFontName, which, in turn, gets | |
| 81 // it from base64-encoded uuid (128-bit). So, m_name | |
| 82 // can never be longer than LF_FACESIZE (32). | |
| 83 if (m_name.length() + 1 >= LF_FACESIZE) { | |
| 84 ASSERT_NOT_REACHED(); | |
| 85 return FontPlatformData(); | |
| 86 } | |
| 87 unsigned len = m_name.copyTo(logFont.lfFaceName, 0, LF_FACESIZE - 1); | |
| 88 logFont.lfFaceName[len] = '\0'; | |
| 89 | |
| 90 // FIXME: almost identical to FillLogFont in FontCacheWin.cpp. | |
| 91 // Need to refactor. | |
| 92 logFont.lfHeight = -static_cast<int>(size); | |
| 93 logFont.lfWidth = 0; | |
| 94 logFont.lfEscapement = 0; | |
| 95 logFont.lfOrientation = 0; | |
| 96 logFont.lfUnderline = false; | |
| 97 logFont.lfStrikeOut = false; | |
| 98 logFont.lfCharSet = DEFAULT_CHARSET; | |
| 99 logFont.lfOutPrecision = OUT_TT_ONLY_PRECIS; | |
| 100 logFont.lfQuality = isRunningLayoutTest() ? NONANTIALIASED_QUALITY : DEFAULT
_QUALITY; // Honor user's desktop settings. | |
| 101 logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; | |
| 102 logFont.lfItalic = italic; | |
| 103 logFont.lfWeight = bold ? FW_BOLD : FW_DONTCARE; | |
| 104 | |
| 105 HFONT hfont = CreateFontIndirect(&logFont); | |
| 106 return FontPlatformData(hfont, size, orientation); | |
| 107 } | |
| 108 | |
| 109 PassOwnPtr<FontCustomPlatformData> FontCustomPlatformData::create(SharedBuffer*
buffer) | |
| 110 { | |
| 111 ASSERT_ARG(buffer, buffer); | |
| 112 | |
| 113 OpenTypeSanitizer sanitizer(buffer); | |
| 114 RefPtr<SharedBuffer> transcodeBuffer = sanitizer.sanitize(); | |
| 115 if (!transcodeBuffer) | |
| 116 return nullptr; // validation failed. | |
| 117 buffer = transcodeBuffer.get(); | |
| 118 | |
| 119 // Introduce the font to GDI. AddFontMemResourceEx should be used with care,
because it will pollute the process's | |
| 120 // font namespace (Windows has no API for creating an HFONT from data withou
t exposing the font to the | |
| 121 // entire process first). | |
| 122 String fontName = createUniqueFontName(); | |
| 123 HANDLE fontReference = renameAndActivateFont(buffer, fontName); | |
| 124 if (!fontReference) | |
| 125 return nullptr; | |
| 126 | |
| 127 return adoptPtr(new FontCustomPlatformData(fontReference, fontName)); | |
| 128 } | |
| 129 | |
| 130 bool FontCustomPlatformData::supportsFormat(const String& format) | |
| 131 { | |
| 132 return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "o
pentype") || OpenTypeSanitizer::supportsFormat(format); | |
| 133 } | |
| 134 | |
| 135 } // namespace WebCore | |
| OLD | NEW |