blob: 8f3e8883f75e659bd1c5f77b9377a6a76d541c97 [file] [log] [blame]
oliverd6ae4962007-10-12 14:55:24 +00001/*
mitz@apple.com26b747a2008-04-01 05:49:35 +00002 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
yong.li@torchmobile.com3e8b9ec2009-08-18 19:39:33 +00003 * Copyright (C) 2009 Torch Mobile, Inc.
oliverd6ae4962007-10-12 14:55:24 +00004 *
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.com92047332014-03-15 04:08:27 +000014 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
oliverd6ae4962007-10-12 14:55:24 +000015 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mjs@apple.com92047332014-03-15 04:08:27 +000017 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
oliverd6ae4962007-10-12 14:55:24 +000018 * 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.com0920f1b2013-04-05 00:08:09 +000030#include "CachedFontClient.h"
oliverd6ae4962007-10-12 14:55:24 +000031#include "CachedResourceClientWalker.h"
japhet@chromium.orgf8d43692010-12-10 19:26:15 +000032#include "CachedResourceLoader.h"
paroga@webkit.orgffcd45c2013-03-25 20:11:47 +000033#include "FontCustomPlatformData.h"
mmaxfield@apple.comaf535632014-11-18 20:04:38 +000034#include "FontDescription.h"
oliverd6ae4962007-10-12 14:55:24 +000035#include "FontPlatformData.h"
zimmermann@webkit.org42ab4a72010-12-03 11:14:32 +000036#include "MemoryCache.h"
paroga@webkit.org43bfcfa2013-11-05 01:31:43 +000037#include "SharedBuffer.h"
oliverd6ae4962007-10-12 14:55:24 +000038#include "TextResourceDecoder.h"
darin@apple.comd0e9acb2014-03-21 07:12:07 +000039#include "TypedElementDescendantIterator.h"
paroga@webkit.org43bfcfa2013-11-05 01:31:43 +000040#include "WOFFFileFormat.h"
oliverd6ae4962007-10-12 14:55:24 +000041#include <wtf/Vector.h>
42
zimmermann@webkit.org81615942008-01-28 23:37:04 +000043#if ENABLE(SVG_FONTS)
zimmermann@webkit.org81615942008-01-28 23:37:04 +000044#include "NodeList.h"
tonyg@chromium.org4b0a55f2011-05-12 10:31:51 +000045#include "SVGDocument.h"
zimmermann@webkit.org81615942008-01-28 23:37:04 +000046#include "SVGElement.h"
47#include "SVGFontElement.h"
48#include "SVGGElement.h"
zimmermann@webkit.org42ab4a72010-12-03 11:14:32 +000049#include "SVGNames.h"
zimmermann@webkit.org81615942008-01-28 23:37:04 +000050#endif
51
oliverd6ae4962007-10-12 14:55:24 +000052namespace WebCore {
53
mmaxfield@apple.comaf535632014-11-18 20:04:38 +000054CachedFont::CachedFont(const ResourceRequest& resourceRequest, SessionID sessionID, Type type)
55 : CachedResource(resourceRequest, type, sessionID)
antti@apple.comb4122c52008-03-25 19:21:00 +000056 , m_loadInitiated(false)
akling@apple.com1f81ba32014-01-28 01:59:20 +000057 , m_hasCreatedFontDataWrappingResource(false)
oliverd6ae4962007-10-12 14:55:24 +000058{
oliverd6ae4962007-10-12 14:55:24 +000059}
60
61CachedFont::~CachedFont()
62{
oliverd6ae4962007-10-12 14:55:24 +000063}
64
cdumez@apple.com0bb03ec2015-01-28 05:53:53 +000065void CachedFont::load(CachedResourceLoader&, const ResourceLoaderOptions& options)
antti@apple.comb4122c52008-03-25 19:21:00 +000066{
67 // Don't load the file yet. Wait for an access before triggering the load.
simon.fraser@apple.com274aac42010-05-16 15:43:03 +000068 setLoading(true);
japhet@chromium.org42013bd2011-09-27 01:29:20 +000069 m_options = options;
antti@apple.comb4122c52008-03-25 19:21:00 +000070}
71
antti@apple.com317d75c2009-06-17 00:38:53 +000072void CachedFont::didAddClient(CachedResourceClient* c)
oliverd6ae4962007-10-12 14:55:24 +000073{
japhet@chromium.org515a5522011-10-14 19:31:57 +000074 ASSERT(c->resourceClientType() == CachedFontClient::expectedType());
75 if (!isLoading())
japhet@chromium.org27e54392011-10-11 00:41:18 +000076 static_cast<CachedFontClient*>(c)->fontLoaded(this);
oliverd6ae4962007-10-12 14:55:24 +000077}
78
darin@apple.com6644eab2014-11-04 16:47:53 +000079void CachedFont::finishLoading(SharedBuffer* data)
oliverd6ae4962007-10-12 14:55:24 +000080{
beidson@apple.com4f3559c2012-10-11 00:26:39 +000081 m_data = data;
oliverd6ae4962007-10-12 14:55:24 +000082 setEncodedSize(m_data.get() ? m_data->size() : 0);
simon.fraser@apple.com274aac42010-05-16 15:43:03 +000083 setLoading(false);
oliverd6ae4962007-10-12 14:55:24 +000084 checkNotify();
85}
86
cdumez@apple.com0bb03ec2015-01-28 05:53:53 +000087void CachedFont::beginLoadIfNeeded(CachedResourceLoader& loader)
oliverd6ae4962007-10-12 14:55:24 +000088{
89 if (!m_loadInitiated) {
90 m_loadInitiated = true;
cdumez@apple.com0bb03ec2015-01-28 05:53:53 +000091 CachedResource::load(loader, m_options);
oliverd6ae4962007-10-12 14:55:24 +000092 }
93}
94
mmaxfield@apple.comd4908862015-01-12 20:55:09 +000095bool CachedFont::ensureCustomFontData(bool, const AtomicString&)
oliverd6ae4962007-10-12 14:55:24 +000096{
mmaxfield@apple.com0172c042015-01-17 22:51:47 +000097 return ensureCustomFontData(m_data.get());
mmaxfield@apple.comd4908862015-01-12 20:55:09 +000098}
99
mmaxfield@apple.com0172c042015-01-17 22:51:47 +0000100bool CachedFont::ensureCustomFontData(SharedBuffer* data)
mmaxfield@apple.comd4908862015-01-12 20:55:09 +0000101{
antti@apple.com5a8f7942015-01-22 21:57:04 +0000102 if (!m_fontCustomPlatformData && !errorOccurred() && !isLoading() && data) {
mmaxfield@apple.com0172c042015-01-17 22:51:47 +0000103 RefPtr<SharedBuffer> buffer(data);
commit-queue@webkit.org71d05072014-11-03 22:27:59 +0000104
darin@apple.com6644eab2014-11-04 16:47:53 +0000105#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.org43bfcfa2013-11-05 01:31:43 +0000109 buffer = nullptr;
mmaxfield@apple.comd4908862015-01-12 20:55:09 +0000110 else
darin@apple.com6644eab2014-11-04 16:47:53 +0000111 buffer = SharedBuffer::adoptVector(convertedFont);
paroga@webkit.org43bfcfa2013-11-05 01:31:43 +0000112 }
mmaxfield@apple.comd13fba22014-07-23 00:35:27 +0000113#endif
paroga@webkit.org43bfcfa2013-11-05 01:31:43 +0000114
antti@apple.com5a8f7942015-01-22 21:57:04 +0000115 m_fontCustomPlatformData = buffer ? createFontCustomPlatformData(*buffer) : nullptr;
116 m_hasCreatedFontDataWrappingResource = m_fontCustomPlatformData && (buffer == m_data);
117 if (!m_fontCustomPlatformData)
inferno@chromium.org28ad8d82010-11-17 20:11:56 +0000118 setStatus(DecodeError);
oliverd6ae4962007-10-12 14:55:24 +0000119 }
darin@apple.com6644eab2014-11-04 16:47:53 +0000120
antti@apple.com5a8f7942015-01-22 21:57:04 +0000121 return m_fontCustomPlatformData.get();
oliverd6ae4962007-10-12 14:55:24 +0000122}
123
antti@apple.com5a8f7942015-01-22 21:57:04 +0000124RefPtr<Font> CachedFont::createFont(const FontDescription& fontDescription, const AtomicString&, bool syntheticBold, bool syntheticItalic, bool)
mmaxfield@apple.comaf535632014-11-18 20:04:38 +0000125{
antti@apple.com5a8f7942015-01-22 21:57:04 +0000126 return Font::create(platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic,
mmaxfield@apple.comaf535632014-11-18 20:04:38 +0000127 fontDescription.orientation(), fontDescription.widthVariant(), fontDescription.renderingMode()), true, false);
128}
129
mitz@apple.com15b3d3f2012-12-04 17:58:44 +0000130FontPlatformData CachedFont::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, FontWidthVariant widthVariant, FontRenderingMode renderingMode)
oliverd6ae4962007-10-12 14:55:24 +0000131{
antti@apple.com5a8f7942015-01-22 21:57:04 +0000132 ASSERT(m_fontCustomPlatformData);
133 return m_fontCustomPlatformData->fontPlatformData(static_cast<int>(size), bold, italic, orientation, widthVariant, renderingMode);
oliverd6ae4962007-10-12 14:55:24 +0000134}
135
mitz@apple.com9d7e4262008-09-04 18:15:25 +0000136void CachedFont::allClientsRemoved()
oliverd6ae4962007-10-12 14:55:24 +0000137{
antti@apple.com5a8f7942015-01-22 21:57:04 +0000138 m_fontCustomPlatformData = nullptr;
oliverd6ae4962007-10-12 14:55:24 +0000139}
140
141void CachedFont::checkNotify()
142{
simon.fraser@apple.com274aac42010-05-16 15:43:03 +0000143 if (isLoading())
oliverd6ae4962007-10-12 14:55:24 +0000144 return;
145
japhet@chromium.orgdcdb05b2011-10-12 00:12:29 +0000146 CachedResourceClientWalker<CachedFontClient> w(m_clients);
147 while (CachedFontClient* c = w.next())
148 c->fontLoaded(this);
oliverd6ae4962007-10-12 14:55:24 +0000149}
150
beidson@apple.comb9d4b7b2013-04-24 22:35:47 +0000151bool CachedFont::mayTryReplaceEncodedData() const
152{
akling@apple.com1f81ba32014-01-28 01:59:20 +0000153 // If a FontCustomPlatformData has ever been constructed to wrap the internal resource buffer then it still might be in use somewhere.
beidson@apple.comb9d4b7b2013-04-24 22:35:47 +0000154 // That platform font object might directly reference the encoded data buffer behind this CachedFont,
155 // so replacing it is unsafe.
156
akling@apple.com1f81ba32014-01-28 01:59:20 +0000157 return !m_hasCreatedFontDataWrappingResource;
beidson@apple.comb9d4b7b2013-04-24 22:35:47 +0000158}
159
oliverd6ae4962007-10-12 14:55:24 +0000160}