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 "platform/RuntimeEnabledFeatures.h" | 34 #include "platform/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_dec.h" |
| 39 #include "wtf/Vector.h" |
38 | 40 |
39 namespace blink { | 41 namespace blink { |
40 | 42 |
41 PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() | 43 // This is the largest web font size which we'll try to transcode. |
| 44 static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB |
| 45 |
| 46 static bool isWoff2(SharedBuffer* buffer) |
42 { | 47 { |
43 if (!m_buffer) | 48 static const size_t signatureSize = 4; |
44 return nullptr; | 49 if (buffer->size() < signatureSize) |
| 50 return false; |
45 | 51 |
46 // This is the largest web font size which we'll try to transcode. | 52 const char* signature = buffer->data(); |
47 static const size_t maxWebFontSize = 30 * 1024 * 1024; // 30 MB | 53 return signature[0] == 'w' && signature[1] == 'O' && signature[2] == 'F' &&
signature[3] == '2'; |
48 if (m_buffer->size() > maxWebFontSize) | 54 } |
49 return nullptr; | |
50 | 55 |
51 if (RuntimeEnabledFeatures::woff2Enabled()) | 56 static PassRefPtr<SharedBuffer> transcode(const uint8_t* targetData, size_t targ
etDataSize) |
52 ots::EnableWOFF2(); | 57 { |
53 | |
54 // A transcoded font is usually smaller than an original font. | 58 // A transcoded font is usually smaller than an original font. |
55 // However, it can be slightly bigger than the original one due to | 59 // However, it can be slightly bigger than the original one due to |
56 // name table replacement and/or padding for glyf table. | 60 // name table replacement and/or padding for glyf table. |
57 // | 61 // |
58 // With WOFF fonts, however, we'll be decompressing, so the result can be | 62 // With WOFF fonts, however, we'll be decompressing, so the result can be |
59 // much larger than the original. | 63 // much larger than the original. |
60 | 64 |
61 ots::ExpandingMemoryStream output(m_buffer->size(), maxWebFontSize); | 65 ots::ExpandingMemoryStream output(targetDataSize, maxWebFontSize); |
62 if (!ots::Process(&output, reinterpret_cast<const uint8_t*>(m_buffer->data()
), m_buffer->size())) | 66 if (!ots::Process(&output, targetData, targetDataSize)) |
63 return nullptr; | 67 return nullptr; |
64 | 68 |
65 const size_t transcodeLen = output.Tell(); | 69 const size_t transcodeLen = output.Tell(); |
66 return SharedBuffer::create(static_cast<unsigned char*>(output.get()), trans
codeLen); | 70 return SharedBuffer::create(static_cast<unsigned char*>(output.get()), trans
codeLen); |
67 } | 71 } |
68 | 72 |
| 73 PassRefPtr<SharedBuffer> OpenTypeSanitizer::sanitize() |
| 74 { |
| 75 if (!m_buffer) |
| 76 return nullptr; |
| 77 |
| 78 if (m_buffer->size() > maxWebFontSize) |
| 79 return nullptr; |
| 80 |
| 81 if (RuntimeEnabledFeatures::woff2Enabled()) |
| 82 ots::EnableWOFF2(); |
| 83 |
| 84 const uint8_t* originalData = reinterpret_cast<const uint8_t*>(m_buffer->dat
a()); |
| 85 size_t originalDataSize = m_buffer->size(); |
| 86 if (RuntimeEnabledFeatures::woff2Enabled() && isWoff2(m_buffer)) { |
| 87 size_t woff2Size = woff2::ComputeWOFF2FinalSize(originalData, originalDa
taSize); |
| 88 if (woff2Size > maxWebFontSize) |
| 89 return nullptr; |
| 90 |
| 91 Vector<uint8_t> woff2Output(woff2Size); |
| 92 if (!woff2::ConvertWOFF2ToTTF(woff2Output.data(), woff2Size, originalDat
a, originalDataSize)) |
| 93 return nullptr; |
| 94 return transcode(woff2Output.data(), woff2Size); |
| 95 } |
| 96 return transcode(originalData, originalDataSize); |
| 97 } |
| 98 |
69 bool OpenTypeSanitizer::supportsFormat(const String& format) | 99 bool OpenTypeSanitizer::supportsFormat(const String& format) |
70 { | 100 { |
71 return equalIgnoringCase(format, "woff") | 101 return equalIgnoringCase(format, "woff") |
72 || (RuntimeEnabledFeatures::woff2Enabled() && equalIgnoringCase(format,
"woff2")); | 102 || (RuntimeEnabledFeatures::woff2Enabled() && equalIgnoringCase(format,
"woff2")); |
73 } | 103 } |
74 | 104 |
75 } // namespace blink | 105 } // namespace blink |
OLD | NEW |