| 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 "head.h" | 5 #include "head.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 // head - Font Header | 9 // head - Font Header |
| 10 // http://www.microsoft.com/opentype/otspec/head.htm | 10 // http://www.microsoft.com/typography/otspec/head.htm |
| 11 |
| 12 #define TABLE_NAME "head" |
| 11 | 13 |
| 12 namespace ots { | 14 namespace ots { |
| 13 | 15 |
| 14 bool ots_head_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 16 bool ots_head_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
| 15 Buffer table(data, length); | 17 Buffer table(data, length); |
| 16 file->head = new OpenTypeHEAD; | 18 file->head = new OpenTypeHEAD; |
| 17 | 19 |
| 18 uint32_t version = 0; | 20 uint32_t version = 0; |
| 19 if (!table.ReadU32(&version) || | 21 if (!table.ReadU32(&version) || |
| 20 !table.ReadU32(&file->head->revision)) { | 22 !table.ReadU32(&file->head->revision)) { |
| 21 return OTS_FAILURE(); | 23 return OTS_FAILURE_MSG("Failed to read head header"); |
| 22 } | 24 } |
| 23 | 25 |
| 24 if (version >> 16 != 1) { | 26 if (version >> 16 != 1) { |
| 25 return OTS_FAILURE(); | 27 return OTS_FAILURE_MSG("Bad head table version of %d", version); |
| 26 } | 28 } |
| 27 | 29 |
| 28 // Skip the checksum adjustment | 30 // Skip the checksum adjustment |
| 29 if (!table.Skip(4)) { | 31 if (!table.Skip(4)) { |
| 30 return OTS_FAILURE(); | 32 return OTS_FAILURE_MSG("Failed to read checksum"); |
| 31 } | 33 } |
| 32 | 34 |
| 33 uint32_t magic; | 35 uint32_t magic; |
| 34 if (!table.ReadTag(&magic) || | 36 if (!table.ReadTag(&magic) || |
| 35 std::memcmp(&magic, "\x5F\x0F\x3C\xF5", 4)) { | 37 std::memcmp(&magic, "\x5F\x0F\x3C\xF5", 4)) { |
| 36 return OTS_FAILURE(); | 38 return OTS_FAILURE_MSG("Failed to read font magic number"); |
| 37 } | 39 } |
| 38 | 40 |
| 39 if (!table.ReadU16(&file->head->flags)) { | 41 if (!table.ReadU16(&file->head->flags)) { |
| 40 return OTS_FAILURE(); | 42 return OTS_FAILURE_MSG("Failed to read head flags"); |
| 41 } | 43 } |
| 42 | 44 |
| 43 // We allow bits 0..4, 11..13 | 45 // We allow bits 0..4, 11..13 |
| 44 file->head->flags &= 0x381f; | 46 file->head->flags &= 0x381f; |
| 45 | 47 |
| 46 if (!table.ReadU16(&file->head->ppem)) { | 48 if (!table.ReadU16(&file->head->ppem)) { |
| 47 return OTS_FAILURE(); | 49 return OTS_FAILURE_MSG("Failed to read pixels per em"); |
| 48 } | 50 } |
| 49 | 51 |
| 50 // ppem must be in range | 52 // ppem must be in range |
| 51 if (file->head->ppem < 16 || | 53 if (file->head->ppem < 16 || |
| 52 file->head->ppem > 16384) { | 54 file->head->ppem > 16384) { |
| 53 return OTS_FAILURE(); | 55 return OTS_FAILURE_MSG("Bad ppm of %d", file->head->ppem); |
| 54 } | 56 } |
| 55 | 57 |
| 56 // ppem must be a power of two | 58 // ppem must be a power of two |
| 57 #if 0 | 59 #if 0 |
| 58 // We don't call ots_failure() for now since lots of TrueType fonts are | 60 // We don't call ots_failure() for now since lots of TrueType fonts are |
| 59 // not following this rule. Putting OTS_WARNING here is too noisy. | 61 // not following this rule. Putting OTS_WARNING here is too noisy. |
| 60 if ((file->head->ppem - 1) & file->head->ppem) { | 62 if ((file->head->ppem - 1) & file->head->ppem) { |
| 61 return OTS_FAILURE(); | 63 return OTS_FAILURE_MSG("ppm not a power of two: %d", file->head->ppem); |
| 62 } | 64 } |
| 63 #endif | 65 #endif |
| 64 | 66 |
| 65 if (!table.ReadR64(&file->head->created) || | 67 if (!table.ReadR64(&file->head->created) || |
| 66 !table.ReadR64(&file->head->modified)) { | 68 !table.ReadR64(&file->head->modified)) { |
| 67 return OTS_FAILURE(); | 69 return OTS_FAILURE_MSG("Can't read font dates"); |
| 68 } | 70 } |
| 69 | 71 |
| 70 if (!table.ReadS16(&file->head->xmin) || | 72 if (!table.ReadS16(&file->head->xmin) || |
| 71 !table.ReadS16(&file->head->ymin) || | 73 !table.ReadS16(&file->head->ymin) || |
| 72 !table.ReadS16(&file->head->xmax) || | 74 !table.ReadS16(&file->head->xmax) || |
| 73 !table.ReadS16(&file->head->ymax)) { | 75 !table.ReadS16(&file->head->ymax)) { |
| 74 return OTS_FAILURE(); | 76 return OTS_FAILURE_MSG("Failed to read font bounding box"); |
| 75 } | 77 } |
| 76 | 78 |
| 77 if (file->head->xmin > file->head->xmax) { | 79 if (file->head->xmin > file->head->xmax) { |
| 78 return OTS_FAILURE(); | 80 return OTS_FAILURE_MSG("Bad x dimension in the font bounding box (%d, %d)",
file->head->xmin, file->head->xmax); |
| 79 } | 81 } |
| 80 if (file->head->ymin > file->head->ymax) { | 82 if (file->head->ymin > file->head->ymax) { |
| 81 return OTS_FAILURE(); | 83 return OTS_FAILURE_MSG("Bad y dimension in the font bounding box (%d, %d)",
file->head->ymin, file->head->ymax); |
| 82 } | 84 } |
| 83 | 85 |
| 84 if (!table.ReadU16(&file->head->mac_style)) { | 86 if (!table.ReadU16(&file->head->mac_style)) { |
| 85 return OTS_FAILURE(); | 87 return OTS_FAILURE_MSG("Failed to read font style"); |
| 86 } | 88 } |
| 87 | 89 |
| 88 // We allow bits 0..6 | 90 // We allow bits 0..6 |
| 89 file->head->mac_style &= 0x7f; | 91 file->head->mac_style &= 0x7f; |
| 90 | 92 |
| 91 if (!table.ReadU16(&file->head->min_ppem)) { | 93 if (!table.ReadU16(&file->head->min_ppem)) { |
| 92 return OTS_FAILURE(); | 94 return OTS_FAILURE_MSG("Failed to read font minimum ppm"); |
| 93 } | 95 } |
| 94 | 96 |
| 95 // We don't care about the font direction hint | 97 // We don't care about the font direction hint |
| 96 if (!table.Skip(2)) { | 98 if (!table.Skip(2)) { |
| 97 return OTS_FAILURE(); | 99 return OTS_FAILURE_MSG("Failed to skip font direction hint"); |
| 98 } | 100 } |
| 99 | 101 |
| 100 if (!table.ReadS16(&file->head->index_to_loc_format)) { | 102 if (!table.ReadS16(&file->head->index_to_loc_format)) { |
| 101 return OTS_FAILURE(); | 103 return OTS_FAILURE_MSG("Failed to read index to loc format"); |
| 102 } | 104 } |
| 103 if (file->head->index_to_loc_format < 0 || | 105 if (file->head->index_to_loc_format < 0 || |
| 104 file->head->index_to_loc_format > 1) { | 106 file->head->index_to_loc_format > 1) { |
| 105 return OTS_FAILURE(); | 107 return OTS_FAILURE_MSG("Bad index to loc format %d", file->head->index_to_lo
c_format); |
| 106 } | 108 } |
| 107 | 109 |
| 108 int16_t glyph_data_format; | 110 int16_t glyph_data_format; |
| 109 if (!table.ReadS16(&glyph_data_format) || | 111 if (!table.ReadS16(&glyph_data_format) || |
| 110 glyph_data_format) { | 112 glyph_data_format) { |
| 111 return OTS_FAILURE(); | 113 return OTS_FAILURE_MSG("Failed to read glyph data format"); |
| 112 } | 114 } |
| 113 | 115 |
| 114 return true; | 116 return true; |
| 115 } | 117 } |
| 116 | 118 |
| 117 bool ots_head_should_serialise(OpenTypeFile *file) { | 119 bool ots_head_should_serialise(OpenTypeFile *file) { |
| 118 return file->head != NULL; | 120 return file->head != NULL; |
| 119 } | 121 } |
| 120 | 122 |
| 121 bool ots_head_serialise(OTSStream *out, OpenTypeFile *file) { | 123 bool ots_head_serialise(OTSStream *out, OpenTypeFile *file) { |
| 122 if (!out->WriteU32(0x00010000) || | 124 if (!out->WriteU32(0x00010000) || |
| 123 !out->WriteU32(file->head->revision) || | 125 !out->WriteU32(file->head->revision) || |
| 124 !out->WriteU32(0) || // check sum not filled in yet | 126 !out->WriteU32(0) || // check sum not filled in yet |
| 125 !out->WriteU32(0x5F0F3CF5) || | 127 !out->WriteU32(0x5F0F3CF5) || |
| 126 !out->WriteU16(file->head->flags) || | 128 !out->WriteU16(file->head->flags) || |
| 127 !out->WriteU16(file->head->ppem) || | 129 !out->WriteU16(file->head->ppem) || |
| 128 !out->WriteR64(file->head->created) || | 130 !out->WriteR64(file->head->created) || |
| 129 !out->WriteR64(file->head->modified) || | 131 !out->WriteR64(file->head->modified) || |
| 130 !out->WriteS16(file->head->xmin) || | 132 !out->WriteS16(file->head->xmin) || |
| 131 !out->WriteS16(file->head->ymin) || | 133 !out->WriteS16(file->head->ymin) || |
| 132 !out->WriteS16(file->head->xmax) || | 134 !out->WriteS16(file->head->xmax) || |
| 133 !out->WriteS16(file->head->ymax) || | 135 !out->WriteS16(file->head->ymax) || |
| 134 !out->WriteU16(file->head->mac_style) || | 136 !out->WriteU16(file->head->mac_style) || |
| 135 !out->WriteU16(file->head->min_ppem) || | 137 !out->WriteU16(file->head->min_ppem) || |
| 136 !out->WriteS16(2) || | 138 !out->WriteS16(2) || |
| 137 !out->WriteS16(file->head->index_to_loc_format) || | 139 !out->WriteS16(file->head->index_to_loc_format) || |
| 138 !out->WriteS16(0)) { | 140 !out->WriteS16(0)) { |
| 139 return OTS_FAILURE(); | 141 return OTS_FAILURE_MSG("Failed to write head table"); |
| 140 } | 142 } |
| 141 | 143 |
| 142 return true; | 144 return true; |
| 143 } | 145 } |
| 144 | 146 |
| 145 void ots_head_free(OpenTypeFile *file) { | 147 void ots_head_free(OpenTypeFile *file) { |
| 146 delete file->head; | 148 delete file->head; |
| 147 } | 149 } |
| 148 | 150 |
| 149 } // namespace | 151 } // namespace |
| 152 |
| 153 #undef TABLE_NAME |
| OLD | NEW |