oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 1 | /* |
mitz@apple.com | 26b747a | 2008-04-01 05:49:35 +0000 | [diff] [blame] | 2 | * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
yong.li@torchmobile.com | 3e8b9ec | 2009-08-18 19:39:33 +0000 | [diff] [blame] | 3 | * Copyright (C) 2009 Torch Mobile, Inc. |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "CachedFont.h" |
| 29 | |
andersca@apple.com | 0920f1b | 2013-04-05 00:08:09 +0000 | [diff] [blame] | 30 | #include "CachedFontClient.h" |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 31 | #include "CachedResourceClientWalker.h" |
japhet@chromium.org | f8d4369 | 2010-12-10 19:26:15 +0000 | [diff] [blame] | 32 | #include "CachedResourceLoader.h" |
paroga@webkit.org | ffcd45c | 2013-03-25 20:11:47 +0000 | [diff] [blame] | 33 | #include "FontCustomPlatformData.h" |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 34 | #include "FontDescription.h" |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 35 | #include "FontPlatformData.h" |
zimmermann@webkit.org | 42ab4a7 | 2010-12-03 11:14:32 +0000 | [diff] [blame] | 36 | #include "MemoryCache.h" |
paroga@webkit.org | 43bfcfa | 2013-11-05 01:31:43 +0000 | [diff] [blame] | 37 | #include "SharedBuffer.h" |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 38 | #include "TextResourceDecoder.h" |
darin@apple.com | d0e9acb | 2014-03-21 07:12:07 +0000 | [diff] [blame] | 39 | #include "TypedElementDescendantIterator.h" |
paroga@webkit.org | 43bfcfa | 2013-11-05 01:31:43 +0000 | [diff] [blame] | 40 | #include "WOFFFileFormat.h" |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 41 | #include <wtf/Vector.h> |
| 42 | |
zimmermann@webkit.org | 8161594 | 2008-01-28 23:37:04 +0000 | [diff] [blame] | 43 | #if ENABLE(SVG_FONTS) |
zimmermann@webkit.org | 8161594 | 2008-01-28 23:37:04 +0000 | [diff] [blame] | 44 | #include "NodeList.h" |
tonyg@chromium.org | 4b0a55f | 2011-05-12 10:31:51 +0000 | [diff] [blame] | 45 | #include "SVGDocument.h" |
zimmermann@webkit.org | 8161594 | 2008-01-28 23:37:04 +0000 | [diff] [blame] | 46 | #include "SVGElement.h" |
| 47 | #include "SVGFontElement.h" |
| 48 | #include "SVGGElement.h" |
zimmermann@webkit.org | 42ab4a7 | 2010-12-03 11:14:32 +0000 | [diff] [blame] | 49 | #include "SVGNames.h" |
zimmermann@webkit.org | 8161594 | 2008-01-28 23:37:04 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 52 | namespace WebCore { |
| 53 | |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 54 | CachedFont::CachedFont(const ResourceRequest& resourceRequest, SessionID sessionID, Type type) |
| 55 | : CachedResource(resourceRequest, type, sessionID) |
antti@apple.com | b4122c5 | 2008-03-25 19:21:00 +0000 | [diff] [blame] | 56 | , m_loadInitiated(false) |
akling@apple.com | 1f81ba3 | 2014-01-28 01:59:20 +0000 | [diff] [blame] | 57 | , m_hasCreatedFontDataWrappingResource(false) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 58 | { |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | CachedFont::~CachedFont() |
| 62 | { |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 63 | } |
| 64 | |
cdumez@apple.com | 0bb03ec | 2015-01-28 05:53:53 +0000 | [diff] [blame] | 65 | void CachedFont::load(CachedResourceLoader&, const ResourceLoaderOptions& options) |
antti@apple.com | b4122c5 | 2008-03-25 19:21:00 +0000 | [diff] [blame] | 66 | { |
| 67 | // Don't load the file yet. Wait for an access before triggering the load. |
simon.fraser@apple.com | 274aac4 | 2010-05-16 15:43:03 +0000 | [diff] [blame] | 68 | setLoading(true); |
japhet@chromium.org | 42013bd | 2011-09-27 01:29:20 +0000 | [diff] [blame] | 69 | m_options = options; |
antti@apple.com | b4122c5 | 2008-03-25 19:21:00 +0000 | [diff] [blame] | 70 | } |
| 71 | |
antti@apple.com | 317d75c | 2009-06-17 00:38:53 +0000 | [diff] [blame] | 72 | void CachedFont::didAddClient(CachedResourceClient* c) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 73 | { |
japhet@chromium.org | 515a552 | 2011-10-14 19:31:57 +0000 | [diff] [blame] | 74 | ASSERT(c->resourceClientType() == CachedFontClient::expectedType()); |
| 75 | if (!isLoading()) |
japhet@chromium.org | 27e5439 | 2011-10-11 00:41:18 +0000 | [diff] [blame] | 76 | static_cast<CachedFontClient*>(c)->fontLoaded(this); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
darin@apple.com | 6644eab | 2014-11-04 16:47:53 +0000 | [diff] [blame] | 79 | void CachedFont::finishLoading(SharedBuffer* data) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 80 | { |
beidson@apple.com | 4f3559c | 2012-10-11 00:26:39 +0000 | [diff] [blame] | 81 | m_data = data; |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 82 | setEncodedSize(m_data.get() ? m_data->size() : 0); |
simon.fraser@apple.com | 274aac4 | 2010-05-16 15:43:03 +0000 | [diff] [blame] | 83 | setLoading(false); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 84 | checkNotify(); |
| 85 | } |
| 86 | |
cdumez@apple.com | 0bb03ec | 2015-01-28 05:53:53 +0000 | [diff] [blame] | 87 | void CachedFont::beginLoadIfNeeded(CachedResourceLoader& loader) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 88 | { |
| 89 | if (!m_loadInitiated) { |
| 90 | m_loadInitiated = true; |
cdumez@apple.com | 0bb03ec | 2015-01-28 05:53:53 +0000 | [diff] [blame] | 91 | CachedResource::load(loader, m_options); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
mmaxfield@apple.com | d490886 | 2015-01-12 20:55:09 +0000 | [diff] [blame] | 95 | bool CachedFont::ensureCustomFontData(bool, const AtomicString&) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 96 | { |
mmaxfield@apple.com | 0172c04 | 2015-01-17 22:51:47 +0000 | [diff] [blame] | 97 | return ensureCustomFontData(m_data.get()); |
mmaxfield@apple.com | d490886 | 2015-01-12 20:55:09 +0000 | [diff] [blame] | 98 | } |
| 99 | |
mmaxfield@apple.com | 0172c04 | 2015-01-17 22:51:47 +0000 | [diff] [blame] | 100 | bool CachedFont::ensureCustomFontData(SharedBuffer* data) |
mmaxfield@apple.com | d490886 | 2015-01-12 20:55:09 +0000 | [diff] [blame] | 101 | { |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 102 | if (!m_fontCustomPlatformData && !errorOccurred() && !isLoading() && data) { |
mmaxfield@apple.com | 0172c04 | 2015-01-17 22:51:47 +0000 | [diff] [blame] | 103 | RefPtr<SharedBuffer> buffer(data); |
commit-queue@webkit.org | 71d0507 | 2014-11-03 22:27:59 +0000 | [diff] [blame] | 104 | |
darin@apple.com | 6644eab | 2014-11-04 16:47:53 +0000 | [diff] [blame] | 105 | #if (!PLATFORM(MAC) || __MAC_OS_X_VERSION_MIN_REQUIRED <= 1090) && (!PLATFORM(IOS) || __IPHONE_OS_VERSION_MIN_REQUIRED < 80000) |
| 106 | if (isWOFF(buffer.get())) { |
| 107 | Vector<char> convertedFont; |
| 108 | if (!convertWOFFToSfnt(buffer.get(), convertedFont)) |
paroga@webkit.org | 43bfcfa | 2013-11-05 01:31:43 +0000 | [diff] [blame] | 109 | buffer = nullptr; |
mmaxfield@apple.com | d490886 | 2015-01-12 20:55:09 +0000 | [diff] [blame] | 110 | else |
darin@apple.com | 6644eab | 2014-11-04 16:47:53 +0000 | [diff] [blame] | 111 | buffer = SharedBuffer::adoptVector(convertedFont); |
paroga@webkit.org | 43bfcfa | 2013-11-05 01:31:43 +0000 | [diff] [blame] | 112 | } |
mmaxfield@apple.com | d13fba2 | 2014-07-23 00:35:27 +0000 | [diff] [blame] | 113 | #endif |
paroga@webkit.org | 43bfcfa | 2013-11-05 01:31:43 +0000 | [diff] [blame] | 114 | |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 115 | m_fontCustomPlatformData = buffer ? createFontCustomPlatformData(*buffer) : nullptr; |
| 116 | m_hasCreatedFontDataWrappingResource = m_fontCustomPlatformData && (buffer == m_data); |
| 117 | if (!m_fontCustomPlatformData) |
inferno@chromium.org | 28ad8d8 | 2010-11-17 20:11:56 +0000 | [diff] [blame] | 118 | setStatus(DecodeError); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 119 | } |
darin@apple.com | 6644eab | 2014-11-04 16:47:53 +0000 | [diff] [blame] | 120 | |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 121 | return m_fontCustomPlatformData.get(); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 124 | RefPtr<Font> CachedFont::createFont(const FontDescription& fontDescription, const AtomicString&, bool syntheticBold, bool syntheticItalic, bool) |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 125 | { |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 126 | return Font::create(platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 127 | fontDescription.orientation(), fontDescription.widthVariant(), fontDescription.renderingMode()), true, false); |
| 128 | } |
| 129 | |
mitz@apple.com | 15b3d3f | 2012-12-04 17:58:44 +0000 | [diff] [blame] | 130 | FontPlatformData CachedFont::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, FontWidthVariant widthVariant, FontRenderingMode renderingMode) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 131 | { |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 132 | ASSERT(m_fontCustomPlatformData); |
| 133 | return m_fontCustomPlatformData->fontPlatformData(static_cast<int>(size), bold, italic, orientation, widthVariant, renderingMode); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
mitz@apple.com | 9d7e426 | 2008-09-04 18:15:25 +0000 | [diff] [blame] | 136 | void CachedFont::allClientsRemoved() |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 137 | { |
antti@apple.com | 5a8f794 | 2015-01-22 21:57:04 +0000 | [diff] [blame] | 138 | m_fontCustomPlatformData = nullptr; |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void CachedFont::checkNotify() |
| 142 | { |
simon.fraser@apple.com | 274aac4 | 2010-05-16 15:43:03 +0000 | [diff] [blame] | 143 | if (isLoading()) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 144 | return; |
| 145 | |
japhet@chromium.org | dcdb05b | 2011-10-12 00:12:29 +0000 | [diff] [blame] | 146 | CachedResourceClientWalker<CachedFontClient> w(m_clients); |
| 147 | while (CachedFontClient* c = w.next()) |
| 148 | c->fontLoaded(this); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
beidson@apple.com | b9d4b7b | 2013-04-24 22:35:47 +0000 | [diff] [blame] | 151 | bool CachedFont::mayTryReplaceEncodedData() const |
| 152 | { |
akling@apple.com | 1f81ba3 | 2014-01-28 01:59:20 +0000 | [diff] [blame] | 153 | // If a FontCustomPlatformData has ever been constructed to wrap the internal resource buffer then it still might be in use somewhere. |
beidson@apple.com | b9d4b7b | 2013-04-24 22:35:47 +0000 | [diff] [blame] | 154 | // That platform font object might directly reference the encoded data buffer behind this CachedFont, |
| 155 | // so replacing it is unsafe. |
| 156 | |
akling@apple.com | 1f81ba3 | 2014-01-28 01:59:20 +0000 | [diff] [blame] | 157 | return !m_hasCreatedFontDataWrappingResource; |
beidson@apple.com | b9d4b7b | 2013-04-24 22:35:47 +0000 | [diff] [blame] | 158 | } |
| 159 | |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 160 | } |