OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ots.h" | 5 #include "ots.h" |
6 | 6 |
7 #include <sys/types.h> | 7 #include <sys/types.h> |
8 #include <zlib.h> | 8 #include <zlib.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <cstdlib> | 11 #include <cstdlib> |
12 #include <cstring> | 12 #include <cstring> |
13 #include <limits> | 13 #include <limits> |
14 #include <map> | 14 #include <map> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "woff2.h" | |
18 | |
17 // The OpenType Font File | 19 // The OpenType Font File |
18 // http://www.microsoft.com/typography/otspec/cmap.htm | 20 // http://www.microsoft.com/typography/otspec/cmap.htm |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 bool g_debug_output = true; | 24 bool g_debug_output = true; |
25 bool g_enable_woff2 = false; | |
23 | 26 |
24 struct OpenTypeTable { | 27 struct OpenTypeTable { |
25 uint32_t tag; | 28 uint32_t tag; |
26 uint32_t chksum; | 29 uint32_t chksum; |
27 uint32_t offset; | 30 uint32_t offset; |
28 uint32_t length; | 31 uint32_t length; |
29 uint32_t uncompressed_length; | 32 uint32_t uncompressed_length; |
30 }; | 33 }; |
31 | 34 |
32 // Round a value up to the nearest multiple of 4. Don't round the value in the | 35 // Round a value up to the nearest multiple of 4. Don't round the value in the |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
395 return OTS_FAILURE(); | 398 return OTS_FAILURE(); |
396 } | 399 } |
397 } | 400 } |
398 if (block_end != Round4(length)) { | 401 if (block_end != Round4(length)) { |
399 return OTS_FAILURE(); | 402 return OTS_FAILURE(); |
400 } | 403 } |
401 | 404 |
402 return ProcessGeneric(header, woff_tag, output, data, length, tables, file); | 405 return ProcessGeneric(header, woff_tag, output, data, length, tables, file); |
403 } | 406 } |
404 | 407 |
408 bool ProcessWOFF2(ots::OpenTypeFile *header, | |
409 ots::OTSStream *output, const uint8_t *data, size_t length) { | |
410 size_t decompressed_size = ots::ComputeWOFF2FinalSize(data, length); | |
411 if (decompressed_size == 0) { | |
412 return OTS_FAILURE(); | |
413 } | |
414 | |
415 std::vector<uint8_t> decompressed_buffer(decompressed_size); | |
Yusuke Sato
2013/04/17 18:38:46
* Do we have to zero-fill the buffer?
* Please def
Kunihiko Sakamoto
2013/04/23 06:36:26
Done.
Yusuke Sato
2013/05/07 06:11:27
std::vector<uint8_t> decompressed_buffer(decompres
Kunihiko Sakamoto
2013/05/07 10:17:23
Oh, got it.
We should zero clear this buffer, beca
| |
416 if (!ots::ConvertWOFF2ToTTF(&decompressed_buffer[0], decompressed_size, | |
417 data, length)) { | |
418 return OTS_FAILURE(); | |
419 } | |
420 return ProcessTTF(header, output, &decompressed_buffer[0], decompressed_size); | |
421 } | |
422 | |
405 bool ProcessGeneric(ots::OpenTypeFile *header, uint32_t signature, | 423 bool ProcessGeneric(ots::OpenTypeFile *header, uint32_t signature, |
406 ots::OTSStream *output, | 424 ots::OTSStream *output, |
407 const uint8_t *data, size_t length, | 425 const uint8_t *data, size_t length, |
408 const std::vector<OpenTypeTable>& tables, | 426 const std::vector<OpenTypeTable>& tables, |
409 ots::Buffer& file) { | 427 ots::Buffer& file) { |
410 const size_t data_offset = file.offset(); | 428 const size_t data_offset = file.offset(); |
411 | 429 |
412 uint32_t uncompressed_sum = 0; | 430 uint32_t uncompressed_sum = 0; |
413 | 431 |
414 for (unsigned i = 0; i < header->num_tables; ++i) { | 432 for (unsigned i = 0; i < header->num_tables; ++i) { |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
676 } | 694 } |
677 | 695 |
678 } // namespace | 696 } // namespace |
679 | 697 |
680 namespace ots { | 698 namespace ots { |
681 | 699 |
682 void DisableDebugOutput() { | 700 void DisableDebugOutput() { |
683 g_debug_output = false; | 701 g_debug_output = false; |
684 } | 702 } |
685 | 703 |
704 void EnableWOFF2() { | |
705 g_enable_woff2 = true; | |
706 } | |
707 | |
686 bool Process(OTSStream *output, const uint8_t *data, size_t length) { | 708 bool Process(OTSStream *output, const uint8_t *data, size_t length) { |
687 OpenTypeFile header; | 709 OpenTypeFile header; |
688 if (length < 4) { | 710 if (length < 4) { |
689 return OTS_FAILURE(); | 711 return OTS_FAILURE(); |
690 } | 712 } |
691 | 713 |
692 bool result; | 714 bool result; |
693 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') { | 715 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') { |
694 result = ProcessWOFF(&header, output, data, length); | 716 result = ProcessWOFF(&header, output, data, length); |
717 } else if (g_enable_woff2 && | |
718 data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && | |
719 data[3] == '2') { | |
720 result = ProcessWOFF2(&header, output, data, length); | |
695 } else { | 721 } else { |
696 result = ProcessTTF(&header, output, data, length); | 722 result = ProcessTTF(&header, output, data, length); |
697 } | 723 } |
698 | 724 |
699 for (unsigned i = 0; ; ++i) { | 725 for (unsigned i = 0; ; ++i) { |
700 if (table_parsers[i].parse == NULL) break; | 726 if (table_parsers[i].parse == NULL) break; |
701 table_parsers[i].free(&header); | 727 table_parsers[i].free(&header); |
702 } | 728 } |
703 return result; | 729 return result; |
704 } | 730 } |
(...skipping 14 matching lines...) Expand all Loading... | |
719 va_start(va, format); | 745 va_start(va, format); |
720 std::vfprintf(stderr, format, va); | 746 std::vfprintf(stderr, format, va); |
721 va_end(va); | 747 va_end(va); |
722 std::fprintf(stderr, "\n"); | 748 std::fprintf(stderr, "\n"); |
723 std::fflush(stderr); | 749 std::fflush(stderr); |
724 } | 750 } |
725 } | 751 } |
726 #endif | 752 #endif |
727 | 753 |
728 } // namespace ots | 754 } // namespace ots |
OLD | NEW |