gfx/ots/src/vdmx.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "vdmx.h"
michael@0 6
michael@0 7 // VDMX - Vertical Device Metrics
michael@0 8 // http://www.microsoft.com/typography/otspec/vdmx.htm
michael@0 9
michael@0 10 #define TABLE_NAME "VDMX"
michael@0 11
michael@0 12 #define DROP_THIS_TABLE \
michael@0 13 do { \
michael@0 14 delete file->vdmx; \
michael@0 15 file->vdmx = 0; \
michael@0 16 OTS_FAILURE_MSG("Table discarded"); \
michael@0 17 } while (0)
michael@0 18
michael@0 19 namespace ots {
michael@0 20
michael@0 21 bool ots_vdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
michael@0 22 Buffer table(data, length);
michael@0 23 file->vdmx = new OpenTypeVDMX;
michael@0 24 OpenTypeVDMX * const vdmx = file->vdmx;
michael@0 25
michael@0 26 if (!table.ReadU16(&vdmx->version) ||
michael@0 27 !table.ReadU16(&vdmx->num_recs) ||
michael@0 28 !table.ReadU16(&vdmx->num_ratios)) {
michael@0 29 return OTS_FAILURE_MSG("Failed to read table header");
michael@0 30 }
michael@0 31
michael@0 32 if (vdmx->version > 1) {
michael@0 33 OTS_WARNING("bad version: %u", vdmx->version);
michael@0 34 DROP_THIS_TABLE;
michael@0 35 return true; // continue transcoding
michael@0 36 }
michael@0 37
michael@0 38 vdmx->rat_ranges.reserve(vdmx->num_ratios);
michael@0 39 for (unsigned i = 0; i < vdmx->num_ratios; ++i) {
michael@0 40 OpenTypeVDMXRatioRecord rec;
michael@0 41
michael@0 42 if (!table.ReadU8(&rec.charset) ||
michael@0 43 !table.ReadU8(&rec.x_ratio) ||
michael@0 44 !table.ReadU8(&rec.y_start_ratio) ||
michael@0 45 !table.ReadU8(&rec.y_end_ratio)) {
michael@0 46 return OTS_FAILURE_MSG("Failed to read ratio header %d", i);
michael@0 47 }
michael@0 48
michael@0 49 if (rec.charset > 1) {
michael@0 50 OTS_WARNING("bad charset: %u", rec.charset);
michael@0 51 DROP_THIS_TABLE;
michael@0 52 return true;
michael@0 53 }
michael@0 54
michael@0 55 if (rec.y_start_ratio > rec.y_end_ratio) {
michael@0 56 OTS_WARNING("bad y ratio");
michael@0 57 DROP_THIS_TABLE;
michael@0 58 return true;
michael@0 59 }
michael@0 60
michael@0 61 // All values set to zero signal the default grouping to use;
michael@0 62 // if present, this must be the last Ratio group in the table.
michael@0 63 if ((i < vdmx->num_ratios - 1u) &&
michael@0 64 (rec.x_ratio == 0) &&
michael@0 65 (rec.y_start_ratio == 0) &&
michael@0 66 (rec.y_end_ratio == 0)) {
michael@0 67 // workaround for fonts which have 2 or more {0, 0, 0} terminators.
michael@0 68 OTS_WARNING("superfluous terminator found");
michael@0 69 DROP_THIS_TABLE;
michael@0 70 return true;
michael@0 71 }
michael@0 72
michael@0 73 vdmx->rat_ranges.push_back(rec);
michael@0 74 }
michael@0 75
michael@0 76 vdmx->offsets.reserve(vdmx->num_ratios);
michael@0 77 const size_t current_offset = table.offset();
michael@0 78 // current_offset is less than (2 bytes * 3) + (4 bytes * USHRT_MAX) = 256k.
michael@0 79 for (unsigned i = 0; i < vdmx->num_ratios; ++i) {
michael@0 80 uint16_t offset;
michael@0 81 if (!table.ReadU16(&offset)) {
michael@0 82 return OTS_FAILURE_MSG("Failed to read ratio offset %d", i);
michael@0 83 }
michael@0 84 if (current_offset + offset >= length) { // thus doesn't overflow.
michael@0 85 return OTS_FAILURE_MSG("Bad ratio offset %d for ration %d", offset, i);
michael@0 86 }
michael@0 87
michael@0 88 vdmx->offsets.push_back(offset);
michael@0 89 }
michael@0 90
michael@0 91 vdmx->groups.reserve(vdmx->num_recs);
michael@0 92 for (unsigned i = 0; i < vdmx->num_recs; ++i) {
michael@0 93 OpenTypeVDMXGroup group;
michael@0 94 if (!table.ReadU16(&group.recs) ||
michael@0 95 !table.ReadU8(&group.startsz) ||
michael@0 96 !table.ReadU8(&group.endsz)) {
michael@0 97 return OTS_FAILURE_MSG("Failed to read record header %d", i);
michael@0 98 }
michael@0 99 group.entries.reserve(group.recs);
michael@0 100 for (unsigned j = 0; j < group.recs; ++j) {
michael@0 101 OpenTypeVDMXVTable vt;
michael@0 102 if (!table.ReadU16(&vt.y_pel_height) ||
michael@0 103 !table.ReadS16(&vt.y_max) ||
michael@0 104 !table.ReadS16(&vt.y_min)) {
michael@0 105 return OTS_FAILURE_MSG("Failed to read reacord %d group %d", i, j);
michael@0 106 }
michael@0 107 if (vt.y_max < vt.y_min) {
michael@0 108 OTS_WARNING("bad y min/max");
michael@0 109 DROP_THIS_TABLE;
michael@0 110 return true;
michael@0 111 }
michael@0 112
michael@0 113 // This table must appear in sorted order (sorted by yPelHeight),
michael@0 114 // but need not be continuous.
michael@0 115 if ((j != 0) && (group.entries[j - 1].y_pel_height >= vt.y_pel_height)) {
michael@0 116 OTS_WARNING("the table is not sorted");
michael@0 117 DROP_THIS_TABLE;
michael@0 118 return true;
michael@0 119 }
michael@0 120
michael@0 121 group.entries.push_back(vt);
michael@0 122 }
michael@0 123 vdmx->groups.push_back(group);
michael@0 124 }
michael@0 125
michael@0 126 return true;
michael@0 127 }
michael@0 128
michael@0 129 bool ots_vdmx_should_serialise(OpenTypeFile *file) {
michael@0 130 if (!file->glyf) return false; // this table is not for CFF fonts.
michael@0 131 return file->vdmx != NULL;
michael@0 132 }
michael@0 133
michael@0 134 bool ots_vdmx_serialise(OTSStream *out, OpenTypeFile *file) {
michael@0 135 OpenTypeVDMX * const vdmx = file->vdmx;
michael@0 136
michael@0 137 if (!out->WriteU16(vdmx->version) ||
michael@0 138 !out->WriteU16(vdmx->num_recs) ||
michael@0 139 !out->WriteU16(vdmx->num_ratios)) {
michael@0 140 return OTS_FAILURE_MSG("Failed to write table header");
michael@0 141 }
michael@0 142
michael@0 143 for (unsigned i = 0; i < vdmx->rat_ranges.size(); ++i) {
michael@0 144 const OpenTypeVDMXRatioRecord& rec = vdmx->rat_ranges[i];
michael@0 145 if (!out->Write(&rec.charset, 1) ||
michael@0 146 !out->Write(&rec.x_ratio, 1) ||
michael@0 147 !out->Write(&rec.y_start_ratio, 1) ||
michael@0 148 !out->Write(&rec.y_end_ratio, 1)) {
michael@0 149 return OTS_FAILURE_MSG("Failed to write ratio %d", i);
michael@0 150 }
michael@0 151 }
michael@0 152
michael@0 153 for (unsigned i = 0; i < vdmx->offsets.size(); ++i) {
michael@0 154 if (!out->WriteU16(vdmx->offsets[i])) {
michael@0 155 return OTS_FAILURE_MSG("Failed to write ratio offset %d", i);
michael@0 156 }
michael@0 157 }
michael@0 158
michael@0 159 for (unsigned i = 0; i < vdmx->groups.size(); ++i) {
michael@0 160 const OpenTypeVDMXGroup& group = vdmx->groups[i];
michael@0 161 if (!out->WriteU16(group.recs) ||
michael@0 162 !out->Write(&group.startsz, 1) ||
michael@0 163 !out->Write(&group.endsz, 1)) {
michael@0 164 return OTS_FAILURE_MSG("Failed to write group %d", i);
michael@0 165 }
michael@0 166 for (unsigned j = 0; j < group.entries.size(); ++j) {
michael@0 167 const OpenTypeVDMXVTable& vt = group.entries[j];
michael@0 168 if (!out->WriteU16(vt.y_pel_height) ||
michael@0 169 !out->WriteS16(vt.y_max) ||
michael@0 170 !out->WriteS16(vt.y_min)) {
michael@0 171 return OTS_FAILURE_MSG("Failed to write group %d entry %d", i, j);
michael@0 172 }
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 return true;
michael@0 177 }
michael@0 178
michael@0 179 void ots_vdmx_free(OpenTypeFile *file) {
michael@0 180 delete file->vdmx;
michael@0 181 }
michael@0 182
michael@0 183 } // namespace ots

mercurial