gfx/ots/src/metrics.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) 2011 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 "metrics.h"
michael@0 6
michael@0 7 #include "head.h"
michael@0 8 #include "maxp.h"
michael@0 9
michael@0 10 // OpenType horizontal and vertical common header format
michael@0 11 // http://www.microsoft.com/typography/otspec/hhea.htm
michael@0 12 // http://www.microsoft.com/typography/otspec/vhea.htm
michael@0 13
michael@0 14 #define TABLE_NAME "metrics" // XXX: use individual table names
michael@0 15
michael@0 16 namespace ots {
michael@0 17
michael@0 18 bool ParseMetricsHeader(OpenTypeFile *file, Buffer *table,
michael@0 19 OpenTypeMetricsHeader *header) {
michael@0 20 if (!table->ReadS16(&header->ascent) ||
michael@0 21 !table->ReadS16(&header->descent) ||
michael@0 22 !table->ReadS16(&header->linegap) ||
michael@0 23 !table->ReadU16(&header->adv_width_max) ||
michael@0 24 !table->ReadS16(&header->min_sb1) ||
michael@0 25 !table->ReadS16(&header->min_sb2) ||
michael@0 26 !table->ReadS16(&header->max_extent) ||
michael@0 27 !table->ReadS16(&header->caret_slope_rise) ||
michael@0 28 !table->ReadS16(&header->caret_slope_run) ||
michael@0 29 !table->ReadS16(&header->caret_offset)) {
michael@0 30 return OTS_FAILURE_MSG("Failed to read metrics header");
michael@0 31 }
michael@0 32
michael@0 33 if (header->ascent < 0) {
michael@0 34 OTS_WARNING("bad ascent: %d", header->ascent);
michael@0 35 header->ascent = 0;
michael@0 36 }
michael@0 37 if (header->linegap < 0) {
michael@0 38 OTS_WARNING("bad linegap: %d", header->linegap);
michael@0 39 header->linegap = 0;
michael@0 40 }
michael@0 41
michael@0 42 if (!file->head) {
michael@0 43 return OTS_FAILURE_MSG("Missing head font table");
michael@0 44 }
michael@0 45
michael@0 46 // if the font is non-slanted, caret_offset should be zero.
michael@0 47 if (!(file->head->mac_style & 2) &&
michael@0 48 (header->caret_offset != 0)) {
michael@0 49 OTS_WARNING("bad caret offset: %d", header->caret_offset);
michael@0 50 header->caret_offset = 0;
michael@0 51 }
michael@0 52
michael@0 53 // skip the reserved bytes
michael@0 54 if (!table->Skip(8)) {
michael@0 55 return OTS_FAILURE_MSG("Failed to skip reserverd bytes");
michael@0 56 }
michael@0 57
michael@0 58 int16_t data_format;
michael@0 59 if (!table->ReadS16(&data_format)) {
michael@0 60 return OTS_FAILURE_MSG("Failed to read data format");
michael@0 61 }
michael@0 62 if (data_format) {
michael@0 63 return OTS_FAILURE_MSG("Bad data format %d", data_format);
michael@0 64 }
michael@0 65
michael@0 66 if (!table->ReadU16(&header->num_metrics)) {
michael@0 67 return OTS_FAILURE_MSG("Failed to read number of metrics");
michael@0 68 }
michael@0 69
michael@0 70 if (!file->maxp) {
michael@0 71 return OTS_FAILURE_MSG("Missing maxp font table");
michael@0 72 }
michael@0 73
michael@0 74 if (header->num_metrics > file->maxp->num_glyphs) {
michael@0 75 return OTS_FAILURE_MSG("Bad number of metrics %d", header->num_metrics);
michael@0 76 }
michael@0 77
michael@0 78 return true;
michael@0 79 }
michael@0 80
michael@0 81 bool SerialiseMetricsHeader(const ots::OpenTypeFile *file,
michael@0 82 OTSStream *out,
michael@0 83 const OpenTypeMetricsHeader *header) {
michael@0 84 if (!out->WriteU32(header->version) ||
michael@0 85 !out->WriteS16(header->ascent) ||
michael@0 86 !out->WriteS16(header->descent) ||
michael@0 87 !out->WriteS16(header->linegap) ||
michael@0 88 !out->WriteU16(header->adv_width_max) ||
michael@0 89 !out->WriteS16(header->min_sb1) ||
michael@0 90 !out->WriteS16(header->min_sb2) ||
michael@0 91 !out->WriteS16(header->max_extent) ||
michael@0 92 !out->WriteS16(header->caret_slope_rise) ||
michael@0 93 !out->WriteS16(header->caret_slope_run) ||
michael@0 94 !out->WriteS16(header->caret_offset) ||
michael@0 95 !out->WriteR64(0) || // reserved
michael@0 96 !out->WriteS16(0) || // metric data format
michael@0 97 !out->WriteU16(header->num_metrics)) {
michael@0 98 return OTS_FAILURE_MSG("Failed to write metrics");
michael@0 99 }
michael@0 100
michael@0 101 return true;
michael@0 102 }
michael@0 103
michael@0 104 bool ParseMetricsTable(const ots::OpenTypeFile *file,
michael@0 105 Buffer *table,
michael@0 106 const uint16_t num_glyphs,
michael@0 107 const OpenTypeMetricsHeader *header,
michael@0 108 OpenTypeMetricsTable *metrics) {
michael@0 109 // |num_metrics| is a uint16_t, so it's bounded < 65536. This limits that
michael@0 110 // amount of memory that we'll allocate for this to a sane amount.
michael@0 111 const unsigned num_metrics = header->num_metrics;
michael@0 112
michael@0 113 if (num_metrics > num_glyphs) {
michael@0 114 return OTS_FAILURE_MSG("Bad number of metrics %d", num_metrics);
michael@0 115 }
michael@0 116 if (!num_metrics) {
michael@0 117 return OTS_FAILURE_MSG("No metrics!");
michael@0 118 }
michael@0 119 const unsigned num_sbs = num_glyphs - num_metrics;
michael@0 120
michael@0 121 metrics->entries.reserve(num_metrics);
michael@0 122 for (unsigned i = 0; i < num_metrics; ++i) {
michael@0 123 uint16_t adv = 0;
michael@0 124 int16_t sb = 0;
michael@0 125 if (!table->ReadU16(&adv) || !table->ReadS16(&sb)) {
michael@0 126 return OTS_FAILURE_MSG("Failed to read metric %d", i);
michael@0 127 }
michael@0 128
michael@0 129 // Since so many fonts don't have proper value on |adv| and |sb|,
michael@0 130 // we should not call ots_failure() here. For example, about 20% of fonts
michael@0 131 // in http://www.princexml.com/fonts/ (200+ fonts) fails these tests.
michael@0 132 if (adv > header->adv_width_max) {
michael@0 133 OTS_WARNING("bad adv: %u > %u", adv, header->adv_width_max);
michael@0 134 adv = header->adv_width_max;
michael@0 135 }
michael@0 136
michael@0 137 if (sb < header->min_sb1) {
michael@0 138 OTS_WARNING("bad sb: %d < %d", sb, header->min_sb1);
michael@0 139 sb = header->min_sb1;
michael@0 140 }
michael@0 141
michael@0 142 metrics->entries.push_back(std::make_pair(adv, sb));
michael@0 143 }
michael@0 144
michael@0 145 metrics->sbs.reserve(num_sbs);
michael@0 146 for (unsigned i = 0; i < num_sbs; ++i) {
michael@0 147 int16_t sb;
michael@0 148 if (!table->ReadS16(&sb)) {
michael@0 149 // Some Japanese fonts (e.g., mona.ttf) fail this test.
michael@0 150 return OTS_FAILURE_MSG("Failed to read side bearing %d", i + num_metrics);
michael@0 151 }
michael@0 152
michael@0 153 if (sb < header->min_sb1) {
michael@0 154 // The same as above. Three fonts in http://www.fontsquirrel.com/fontface
michael@0 155 // (e.g., Notice2Std.otf) have weird lsb values.
michael@0 156 OTS_WARNING("bad lsb: %d < %d", sb, header->min_sb1);
michael@0 157 sb = header->min_sb1;
michael@0 158 }
michael@0 159
michael@0 160 metrics->sbs.push_back(sb);
michael@0 161 }
michael@0 162
michael@0 163 return true;
michael@0 164 }
michael@0 165
michael@0 166 bool SerialiseMetricsTable(const ots::OpenTypeFile *file,
michael@0 167 OTSStream *out,
michael@0 168 const OpenTypeMetricsTable *metrics) {
michael@0 169 for (unsigned i = 0; i < metrics->entries.size(); ++i) {
michael@0 170 if (!out->WriteU16(metrics->entries[i].first) ||
michael@0 171 !out->WriteS16(metrics->entries[i].second)) {
michael@0 172 return OTS_FAILURE_MSG("Failed to write metric %d", i);
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 for (unsigned i = 0; i < metrics->sbs.size(); ++i) {
michael@0 177 if (!out->WriteS16(metrics->sbs[i])) {
michael@0 178 return OTS_FAILURE_MSG("Failed to write side bearing %ld", i + metrics->entries.size());
michael@0 179 }
michael@0 180 }
michael@0 181
michael@0 182 return true;
michael@0 183 }
michael@0 184
michael@0 185 } // namespace ots
michael@0 186

mercurial