Chromium Code Reviews| Index: Source/platform/fonts/opentype/OpenTypeSanitizer.cpp |
| diff --git a/Source/platform/fonts/opentype/OpenTypeSanitizer.cpp b/Source/platform/fonts/opentype/OpenTypeSanitizer.cpp |
| index 0b1d007efcf01fad3237d6dc2a57d771b31af374..d6c5cd0e4c3cd68ce1adff39cbb459fc014f59b7 100644 |
| --- a/Source/platform/fonts/opentype/OpenTypeSanitizer.cpp |
| +++ b/Source/platform/fonts/opentype/OpenTypeSanitizer.cpp |
| @@ -35,22 +35,27 @@ |
| #include "platform/SharedBuffer.h" |
| #include "opentype-sanitiser.h" |
| #include "ots-memory-stream.h" |
| +#include "third_party/brotli/src/woff2/woff2_common.h" |
|
Kunihiko Sakamoto
2014/06/06 09:41:26
Is this necessary?
bashi
2014/06/06 09:52:15
That was leftover. Removed.
|
| +#include "third_party/brotli/src/woff2/woff2_dec.h" |
| +#include "wtf/Vector.h" |
| namespace WebCore { |
| -PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() |
| -{ |
| - if (!m_buffer) |
| - return nullptr; |
| +// This is the largest web font size which we'll try to transcode. |
| +static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB |
| - // This is the largest web font size which we'll try to transcode. |
| - static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB |
| - if (m_buffer->size() > maxWebFontSize) |
| - return nullptr; |
| +static bool isWoff2(SharedBuffer* buffer) |
| +{ |
| + static const size_t signatureSize = 4; |
| + if (buffer->size() < signatureSize) |
| + return false; |
| - if (RuntimeEnabledFeatures::woff2Enabled()) |
| - ots::EnableWOFF2(); |
| + const char* signature = buffer->data(); |
| + return signature[0] == 'w' && signature[1] == 'O' && signature[2] == 'F' && signature[3] == '2'; |
| +} |
| +static PassRefPtr<SharedBuffer> transcode(const uint8_t* targetData, size_t targetDataSize) |
| +{ |
| // A transcoded font is usually smaller than an original font. |
| // However, it can be slightly bigger than the original one due to |
| // name table replacement and/or padding for glyf table. |
| @@ -58,14 +63,37 @@ PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() |
| // With WOFF fonts, however, we'll be decompressing, so the result can be |
| // much larger than the original. |
| - ots::ExpandingMemoryStream output(m_buffer->size(), maxWebFontSize); |
| - if (!ots::Process(&output, reinterpret_cast<const uint8_t*>(m_buffer->data()), m_buffer->size())) |
| + ots::ExpandingMemoryStream output(targetDataSize, maxWebFontSize); |
| + if (!ots::Process(&output, targetData, targetDataSize)) |
| return nullptr; |
| const size_t transcodeLen = output.Tell(); |
| return SharedBuffer::create(static_cast<unsigned char*>(output.get()), transcodeLen); |
| } |
| +PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() |
| +{ |
| + if (!m_buffer) |
| + return nullptr; |
| + |
| + if (m_buffer->size() > maxWebFontSize) |
| + return nullptr; |
| + |
| + const uint8_t* originalData = reinterpret_cast<const uint8_t*>(m_buffer->data()); |
| + size_t originalDataSize = m_buffer->size(); |
| + if (RuntimeEnabledFeatures::woff2Enabled() && isWoff2(m_buffer)) { |
| + size_t woff2Size = woff2::ComputeWOFF2FinalSize(originalData, originalDataSize); |
| + if (woff2Size > maxWebFontSize) |
| + return nullptr; |
| + |
| + Vector<uint8_t> woff2Output(woff2Size); |
| + if (!woff2::ConvertWOFF2ToTTF(woff2Output.data(), woff2Size, originalData, originalDataSize)) |
| + return nullptr; |
| + return transcode(woff2Output.data(), woff2Size); |
| + } |
| + return transcode(originalData, originalDataSize); |
| +} |
| + |
| bool OpenTypeSanitizer::supportsFormat(const String& format) |
| { |
| return equalIgnoringCase(format, "woff") |