| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2009 Torch Mobile, Inc. | |
| 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 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 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 "sky/engine/core/fetch/FontResource.h" | |
| 28 | |
| 29 #include "sky/engine/core/fetch/ResourceClientWalker.h" | |
| 30 #include "sky/engine/platform/SharedBuffer.h" | |
| 31 #include "sky/engine/platform/fonts/FontCustomPlatformData.h" | |
| 32 #include "sky/engine/platform/fonts/FontPlatformData.h" | |
| 33 #include "sky/engine/public/platform/Platform.h" | |
| 34 #include "sky/engine/wtf/CurrentTime.h" | |
| 35 | |
| 36 namespace blink { | |
| 37 | |
| 38 static const double fontLoadWaitLimitSec = 3.0; | |
| 39 | |
| 40 enum FontPackageFormat { | |
| 41 PackageFormatUnknown, | |
| 42 PackageFormatSFNT, | |
| 43 PackageFormatWOFF, | |
| 44 PackageFormatWOFF2, | |
| 45 PackageFormatSVG, | |
| 46 PackageFormatEnumMax | |
| 47 }; | |
| 48 | |
| 49 static FontPackageFormat packageFormatOf(SharedBuffer* buffer) | |
| 50 { | |
| 51 if (buffer->size() < 4) | |
| 52 return PackageFormatUnknown; | |
| 53 | |
| 54 const char* data = buffer->data(); | |
| 55 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') | |
| 56 return PackageFormatWOFF; | |
| 57 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == '2') | |
| 58 return PackageFormatWOFF2; | |
| 59 return PackageFormatSFNT; | |
| 60 } | |
| 61 | |
| 62 static void recordPackageFormatHistogram(FontPackageFormat format) | |
| 63 { | |
| 64 blink::Platform::current()->histogramEnumeration("WebFont.PackageFormat", fo
rmat, PackageFormatEnumMax); | |
| 65 } | |
| 66 | |
| 67 FontResource::FontResource(const ResourceRequest& resourceRequest) | |
| 68 : Resource(resourceRequest, Font) | |
| 69 , m_state(Unloaded) | |
| 70 , m_exceedsFontLoadWaitLimit(false) | |
| 71 , m_corsFailed(false) | |
| 72 , m_fontLoadWaitLimitTimer(this, &FontResource::fontLoadWaitLimitCallback) | |
| 73 { | |
| 74 } | |
| 75 | |
| 76 FontResource::~FontResource() | |
| 77 { | |
| 78 } | |
| 79 | |
| 80 void FontResource::didScheduleLoad() | |
| 81 { | |
| 82 if (m_state == Unloaded) | |
| 83 m_state = LoadScheduled; | |
| 84 } | |
| 85 | |
| 86 void FontResource::didUnscheduleLoad() | |
| 87 { | |
| 88 if (m_state == LoadScheduled) | |
| 89 m_state = Unloaded; | |
| 90 } | |
| 91 | |
| 92 void FontResource::load(ResourceFetcher*, const ResourceLoaderOptions& options) | |
| 93 { | |
| 94 // Don't load the file yet. Wait for an access before triggering the load. | |
| 95 setLoading(true); | |
| 96 m_options = options; | |
| 97 } | |
| 98 | |
| 99 void FontResource::didAddClient(ResourceClient* c) | |
| 100 { | |
| 101 ASSERT(c->resourceClientType() == FontResourceClient::expectedType()); | |
| 102 Resource::didAddClient(c); | |
| 103 if (!isLoading()) | |
| 104 static_cast<FontResourceClient*>(c)->fontLoaded(this); | |
| 105 } | |
| 106 | |
| 107 void FontResource::beginLoadIfNeeded(ResourceFetcher* dl) | |
| 108 { | |
| 109 if (m_state != LoadInitiated) { | |
| 110 m_state = LoadInitiated; | |
| 111 Resource::load(dl, m_options); | |
| 112 m_fontLoadWaitLimitTimer.startOneShot(fontLoadWaitLimitSec, FROM_HERE); | |
| 113 | |
| 114 ResourceClientWalker<FontResourceClient> walker(m_clients); | |
| 115 while (FontResourceClient* client = walker.next()) | |
| 116 client->didStartFontLoad(this); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 bool FontResource::ensureCustomFontData() | |
| 121 { | |
| 122 if (!m_fontData && !errorOccurred() && !isLoading()) { | |
| 123 if (m_data) | |
| 124 m_fontData = FontCustomPlatformData::create(m_data.get()); | |
| 125 | |
| 126 if (m_fontData) { | |
| 127 recordPackageFormatHistogram(packageFormatOf(m_data.get())); | |
| 128 } else { | |
| 129 setStatus(DecodeError); | |
| 130 recordPackageFormatHistogram(PackageFormatUnknown); | |
| 131 } | |
| 132 } | |
| 133 return m_fontData; | |
| 134 } | |
| 135 | |
| 136 FontPlatformData FontResource::platformDataFromCustomData(float size, bool bold,
bool italic, FontOrientation orientation, FontWidthVariant widthVariant) | |
| 137 { | |
| 138 ASSERT(m_fontData); | |
| 139 return m_fontData->fontPlatformData(size, bold, italic, orientation, widthVa
riant); | |
| 140 } | |
| 141 | |
| 142 bool FontResource::isSafeToUnlock() const | |
| 143 { | |
| 144 return m_data->hasOneRef(); | |
| 145 } | |
| 146 | |
| 147 void FontResource::fontLoadWaitLimitCallback(Timer<FontResource>*) | |
| 148 { | |
| 149 if (!isLoading()) | |
| 150 return; | |
| 151 m_exceedsFontLoadWaitLimit = true; | |
| 152 ResourceClientWalker<FontResourceClient> walker(m_clients); | |
| 153 while (FontResourceClient* client = walker.next()) | |
| 154 client->fontLoadWaitLimitExceeded(this); | |
| 155 } | |
| 156 | |
| 157 void FontResource::allClientsRemoved() | |
| 158 { | |
| 159 m_fontData.clear(); | |
| 160 Resource::allClientsRemoved(); | |
| 161 } | |
| 162 | |
| 163 void FontResource::checkNotify() | |
| 164 { | |
| 165 m_fontLoadWaitLimitTimer.stop(); | |
| 166 ResourceClientWalker<FontResourceClient> w(m_clients); | |
| 167 while (FontResourceClient* c = w.next()) | |
| 168 c->fontLoaded(this); | |
| 169 } | |
| 170 | |
| 171 } | |
| OLD | NEW |