OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 17 matching lines...) Expand all Loading... | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "platform/fonts/opentype/OpenTypeSanitizer.h" | 32 #include "platform/fonts/opentype/OpenTypeSanitizer.h" |
33 | 33 |
34 #include "RuntimeEnabledFeatures.h" | 34 #include "RuntimeEnabledFeatures.h" |
35 #include "platform/SharedBuffer.h" | 35 #include "platform/SharedBuffer.h" |
36 #include "opentype-sanitiser.h" | 36 #include "opentype-sanitiser.h" |
37 #include "ots-memory-stream.h" | 37 #include "ots-memory-stream.h" |
38 #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.
| |
39 #include "third_party/brotli/src/woff2/woff2_dec.h" | |
40 #include "wtf/Vector.h" | |
38 | 41 |
39 namespace WebCore { | 42 namespace WebCore { |
40 | 43 |
41 PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() | 44 // This is the largest web font size which we'll try to transcode. |
45 static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB | |
46 | |
47 static bool isWoff2(SharedBuffer* buffer) | |
42 { | 48 { |
43 if (!m_buffer) | 49 static const size_t signatureSize = 4; |
44 return nullptr; | 50 if (buffer->size() < signatureSize) |
51 return false; | |
45 | 52 |
46 // This is the largest web font size which we'll try to transcode. | 53 const char* signature = buffer->data(); |
47 static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB | 54 return signature[0] == 'w' && signature[1] == 'O' && signature[2] == 'F' && signature[3] == '2'; |
48 if (m_buffer->size() > maxWebFontSize) | 55 } |
49 return nullptr; | |
50 | 56 |
51 if (RuntimeEnabledFeatures::woff2Enabled()) | 57 static PassRefPtr<SharedBuffer> transcode(const uint8_t* targetData, size_t targ etDataSize) |
52 ots::EnableWOFF2(); | 58 { |
53 | |
54 // A transcoded font is usually smaller than an original font. | 59 // A transcoded font is usually smaller than an original font. |
55 // However, it can be slightly bigger than the original one due to | 60 // However, it can be slightly bigger than the original one due to |
56 // name table replacement and/or padding for glyf table. | 61 // name table replacement and/or padding for glyf table. |
57 // | 62 // |
58 // With WOFF fonts, however, we'll be decompressing, so the result can be | 63 // With WOFF fonts, however, we'll be decompressing, so the result can be |
59 // much larger than the original. | 64 // much larger than the original. |
60 | 65 |
61 ots::ExpandingMemoryStream output(m_buffer->size(), maxWebFontSize); | 66 ots::ExpandingMemoryStream output(targetDataSize, maxWebFontSize); |
62 if (!ots::Process(&output, reinterpret_cast<const uint8_t*>(m_buffer->data() ), m_buffer->size())) | 67 if (!ots::Process(&output, targetData, targetDataSize)) |
63 return nullptr; | 68 return nullptr; |
64 | 69 |
65 const size_t transcodeLen = output.Tell(); | 70 const size_t transcodeLen = output.Tell(); |
66 return SharedBuffer::create(static_cast<unsigned char*>(output.get()), trans codeLen); | 71 return SharedBuffer::create(static_cast<unsigned char*>(output.get()), trans codeLen); |
67 } | 72 } |
68 | 73 |
74 PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() | |
75 { | |
76 if (!m_buffer) | |
77 return nullptr; | |
78 | |
79 if (m_buffer->size() > maxWebFontSize) | |
80 return nullptr; | |
81 | |
82 const uint8_t* originalData = reinterpret_cast<const uint8_t*>(m_buffer->dat a()); | |
83 size_t originalDataSize = m_buffer->size(); | |
84 if (RuntimeEnabledFeatures::woff2Enabled() && isWoff2(m_buffer)) { | |
85 size_t woff2Size = woff2::ComputeWOFF2FinalSize(originalData, originalDa taSize); | |
86 if (woff2Size > maxWebFontSize) | |
87 return nullptr; | |
88 | |
89 Vector<uint8_t> woff2Output(woff2Size); | |
90 if (!woff2::ConvertWOFF2ToTTF(woff2Output.data(), woff2Size, originalDat a, originalDataSize)) | |
91 return nullptr; | |
92 return transcode(woff2Output.data(), woff2Size); | |
93 } | |
94 return transcode(originalData, originalDataSize); | |
95 } | |
96 | |
69 bool OpenTypeSanitizer::supportsFormat(const String& format) | 97 bool OpenTypeSanitizer::supportsFormat(const String& format) |
70 { | 98 { |
71 return equalIgnoringCase(format, "woff") | 99 return equalIgnoringCase(format, "woff") |
72 || (RuntimeEnabledFeatures::woff2Enabled() && equalIgnoringCase(format, "woff2")); | 100 || (RuntimeEnabledFeatures::woff2Enabled() && equalIgnoringCase(format, "woff2")); |
73 } | 101 } |
74 | 102 |
75 } // namespace WebCore | 103 } // namespace WebCore |
OLD | NEW |