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