summaryrefslogtreecommitdiff
path: root/gfx/ots/src/hdmx.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gfx/ots/src/hdmx.cc')
-rw-r--r--gfx/ots/src/hdmx.cc125
1 files changed, 49 insertions, 76 deletions
diff --git a/gfx/ots/src/hdmx.cc b/gfx/ots/src/hdmx.cc
index f57b71f59d..42ac9de8de 100644
--- a/gfx/ots/src/hdmx.cc
+++ b/gfx/ots/src/hdmx.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009-2017 The OTS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -9,139 +9,112 @@
// hdmx - Horizontal Device Metrics
// http://www.microsoft.com/typography/otspec/hdmx.htm
-#define TABLE_NAME "hdmx"
-
-#define DROP_THIS_TABLE(...) \
- do { \
- OTS_FAILURE_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__); \
- OTS_FAILURE_MSG("Table discarded"); \
- delete font->hdmx; \
- font->hdmx = 0; \
- } while (0)
-
namespace ots {
-bool ots_hdmx_parse(Font *font, const uint8_t *data, size_t length) {
+bool OpenTypeHDMX::Parse(const uint8_t *data, size_t length) {
Buffer table(data, length);
- font->hdmx = new OpenTypeHDMX;
- OpenTypeHDMX * const hdmx = font->hdmx;
- if (!font->head || !font->maxp) {
- return OTS_FAILURE_MSG("Missing maxp or head tables in font, needed by hdmx");
+ OpenTypeMAXP *maxp = static_cast<OpenTypeMAXP*>(
+ GetFont()->GetTypedTable(OTS_TAG_MAXP));
+ OpenTypeHEAD *head = static_cast<OpenTypeHEAD*>(
+ GetFont()->GetTypedTable(OTS_TAG_HEAD));
+ if (!head || !maxp) {
+ return Error("Missing maxp or head tables in font, needed by hdmx");
}
- if ((font->head->flags & 0x14) == 0) {
- // http://www.microsoft.com/typography/otspec/recom.htm
- DROP_THIS_TABLE("the table should not be present when bit 2 and 4 of the "
- "head->flags are not set");
- return true;
+ if ((head->flags & 0x14) == 0) {
+ // http://www.microsoft.com/typography/otspec/recom.htm#hdmx
+ return Drop("the table should not be present when bit 2 and 4 of the "
+ "head->flags are not set");
}
int16_t num_recs;
- if (!table.ReadU16(&hdmx->version) ||
+ if (!table.ReadU16(&this->version) ||
!table.ReadS16(&num_recs) ||
- !table.ReadS32(&hdmx->size_device_record)) {
- return OTS_FAILURE_MSG("Failed to read hdmx header");
+ !table.ReadS32(&this->size_device_record)) {
+ return Error("Failed to read table header");
}
- if (hdmx->version != 0) {
- DROP_THIS_TABLE("bad version: %u", hdmx->version);
- return true;
+ if (this->version != 0) {
+ return Drop("Unsupported version: %u", this->version);
}
if (num_recs <= 0) {
- DROP_THIS_TABLE("bad num_recs: %d", num_recs);
- return true;
+ return Drop("Bad numRecords: %d", num_recs);
}
- const int32_t actual_size_device_record = font->maxp->num_glyphs + 2;
- if (hdmx->size_device_record < actual_size_device_record) {
- DROP_THIS_TABLE("bad hdmx->size_device_record: %d", hdmx->size_device_record);
- return true;
+ const int32_t actual_size_device_record = maxp->num_glyphs + 2;
+ if (this->size_device_record < actual_size_device_record) {
+ return Drop("Bad sizeDeviceRecord: %d", this->size_device_record);
}
- hdmx->pad_len = hdmx->size_device_record - actual_size_device_record;
- if (hdmx->pad_len > 3) {
- return OTS_FAILURE_MSG("Bad padding %d", hdmx->pad_len);
+ this->pad_len = this->size_device_record - actual_size_device_record;
+ if (this->pad_len > 3) {
+ return Error("Bad DeviceRecord padding %d", this->pad_len);
}
uint8_t last_pixel_size = 0;
- hdmx->records.reserve(num_recs);
+ this->records.reserve(num_recs);
for (int i = 0; i < num_recs; ++i) {
OpenTypeHDMXDeviceRecord rec;
if (!table.ReadU8(&rec.pixel_size) ||
!table.ReadU8(&rec.max_width)) {
- return OTS_FAILURE_MSG("Failed to read hdmx record %d", i);
+ return Error("Failed to read DeviceRecord %d", i);
}
if ((i != 0) &&
(rec.pixel_size <= last_pixel_size)) {
- DROP_THIS_TABLE("records are not sorted");
- return true;
+ return Drop("DeviceRecord's are not sorted");
}
last_pixel_size = rec.pixel_size;
- rec.widths.reserve(font->maxp->num_glyphs);
- for (unsigned j = 0; j < font->maxp->num_glyphs; ++j) {
+ rec.widths.reserve(maxp->num_glyphs);
+ for (unsigned j = 0; j < maxp->num_glyphs; ++j) {
uint8_t width;
if (!table.ReadU8(&width)) {
- return OTS_FAILURE_MSG("Failed to read glyph width %d in record %d", j, i);
+ return Error("Failed to read glyph width %d in DeviceRecord %d", j, i);
}
rec.widths.push_back(width);
}
- if ((hdmx->pad_len > 0) &&
- !table.Skip(hdmx->pad_len)) {
- return OTS_FAILURE_MSG("Failed to skip padding %d", hdmx->pad_len);
+ if ((this->pad_len > 0) &&
+ !table.Skip(this->pad_len)) {
+ return Error("DeviceRecord %d should be padded by %d", i, this->pad_len);
}
- hdmx->records.push_back(rec);
+ this->records.push_back(rec);
}
return true;
}
-bool ots_hdmx_should_serialise(Font *font) {
- if (!font->hdmx) return false;
- if (!font->glyf) return false; // this table is not for CFF fonts.
- return true;
+bool OpenTypeHDMX::ShouldSerialize() {
+ return Table::ShouldSerialize() &&
+ // this table is not for CFF fonts.
+ GetFont()->GetTable(OTS_TAG_GLYF) != NULL;
}
-bool ots_hdmx_serialise(OTSStream *out, Font *font) {
- OpenTypeHDMX * const hdmx = font->hdmx;
-
- const int16_t num_recs = static_cast<int16_t>(hdmx->records.size());
- if (hdmx->records.size() >
+bool OpenTypeHDMX::Serialize(OTSStream *out) {
+ const int16_t num_recs = static_cast<int16_t>(this->records.size());
+ if (this->records.size() >
static_cast<size_t>(std::numeric_limits<int16_t>::max()) ||
- !out->WriteU16(hdmx->version) ||
+ !out->WriteU16(this->version) ||
!out->WriteS16(num_recs) ||
- !out->WriteS32(hdmx->size_device_record)) {
- return OTS_FAILURE_MSG("Failed to write hdmx header");
+ !out->WriteS32(this->size_device_record)) {
+ return Error("Failed to write table header");
}
for (int16_t i = 0; i < num_recs; ++i) {
- const OpenTypeHDMXDeviceRecord& rec = hdmx->records[i];
+ const OpenTypeHDMXDeviceRecord& rec = this->records[i];
if (!out->Write(&rec.pixel_size, 1) ||
!out->Write(&rec.max_width, 1) ||
!out->Write(&rec.widths[0], rec.widths.size())) {
- return OTS_FAILURE_MSG("Failed to write hdmx record %d", i);
+ return Error("Failed to write DeviceRecord %d", i);
}
- if ((hdmx->pad_len > 0) &&
- !out->Write((const uint8_t *)"\x00\x00\x00", hdmx->pad_len)) {
- return OTS_FAILURE_MSG("Failed to write hdmx padding of length %d", hdmx->pad_len);
+ if ((this->pad_len > 0) &&
+ !out->Write((const uint8_t *)"\x00\x00\x00", this->pad_len)) {
+ return Error("Failed to write padding of length %d", this->pad_len);
}
}
return true;
}
-void ots_hdmx_reuse(Font *font, Font *other) {
- font->hdmx = other->hdmx;
- font->hdmx_reused = true;
-}
-
-void ots_hdmx_free(Font *font) {
- delete font->hdmx;
-}
-
} // namespace ots
-
-#undef TABLE_NAME
-#undef DROP_THIS_TABLE