gfx/ots/src/hdmx.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.

     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
     3 // found in the LICENSE file.
     5 #include "hdmx.h"
     6 #include "head.h"
     7 #include "maxp.h"
     9 // hdmx - Horizontal Device Metrics
    10 // http://www.microsoft.com/typography/otspec/hdmx.htm
    12 #define TABLE_NAME "hdmx"
    14 #define DROP_THIS_TABLE \
    15   do { \
    16     delete file->hdmx; \
    17     file->hdmx = 0; \
    18     OTS_FAILURE_MSG("Table discarded"); \
    19   } while (0)
    21 namespace ots {
    23 bool ots_hdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
    24   Buffer table(data, length);
    25   file->hdmx = new OpenTypeHDMX;
    26   OpenTypeHDMX * const hdmx = file->hdmx;
    28   if (!file->head || !file->maxp) {
    29     return OTS_FAILURE_MSG("Missing maxp or head tables in font, needed by hdmx");
    30   }
    32   if ((file->head->flags & 0x14) == 0) {
    33     // http://www.microsoft.com/typography/otspec/recom.htm
    34     OTS_WARNING("the table should not be present when bit 2 and 4 of the "
    35                 "head->flags are not set");
    36     DROP_THIS_TABLE;
    37     return true;
    38   }
    40   int16_t num_recs;
    41   if (!table.ReadU16(&hdmx->version) ||
    42       !table.ReadS16(&num_recs) ||
    43       !table.ReadS32(&hdmx->size_device_record)) {
    44     return OTS_FAILURE_MSG("Failed to read hdmx header");
    45   }
    46   if (hdmx->version != 0) {
    47     OTS_WARNING("bad version: %u", hdmx->version);
    48     DROP_THIS_TABLE;
    49     return true;
    50   }
    51   if (num_recs <= 0) {
    52     OTS_WARNING("bad num_recs: %d", num_recs);
    53     DROP_THIS_TABLE;
    54     return true;
    55   }
    56   const int32_t actual_size_device_record = file->maxp->num_glyphs + 2;
    57   if (hdmx->size_device_record < actual_size_device_record) {
    58     OTS_WARNING("bad hdmx->size_device_record: %d", hdmx->size_device_record);
    59     DROP_THIS_TABLE;
    60     return true;
    61   }
    63   hdmx->pad_len = hdmx->size_device_record - actual_size_device_record;
    64   if (hdmx->pad_len > 3) {
    65     return OTS_FAILURE_MSG("Bad padding %d", hdmx->pad_len);
    66   }
    68   uint8_t last_pixel_size = 0;
    69   hdmx->records.reserve(num_recs);
    70   for (int i = 0; i < num_recs; ++i) {
    71     OpenTypeHDMXDeviceRecord rec;
    73     if (!table.ReadU8(&rec.pixel_size) ||
    74         !table.ReadU8(&rec.max_width)) {
    75       return OTS_FAILURE_MSG("Failed to read hdmx record %d", i);
    76     }
    77     if ((i != 0) &&
    78         (rec.pixel_size <= last_pixel_size)) {
    79       OTS_WARNING("records are not sorted");
    80       DROP_THIS_TABLE;
    81       return true;
    82     }
    83     last_pixel_size = rec.pixel_size;
    85     rec.widths.reserve(file->maxp->num_glyphs);
    86     for (unsigned j = 0; j < file->maxp->num_glyphs; ++j) {
    87       uint8_t width;
    88       if (!table.ReadU8(&width)) {
    89         return OTS_FAILURE_MSG("Failed to read glyph width %d in record %d", j, i);
    90       }
    91       rec.widths.push_back(width);
    92     }
    94     if ((hdmx->pad_len > 0) &&
    95         !table.Skip(hdmx->pad_len)) {
    96       return OTS_FAILURE_MSG("Failed to skip padding %d", hdmx->pad_len);
    97     }
    99     hdmx->records.push_back(rec);
   100   }
   102   return true;
   103 }
   105 bool ots_hdmx_should_serialise(OpenTypeFile *file) {
   106   if (!file->hdmx) return false;
   107   if (!file->glyf) return false;  // this table is not for CFF fonts.
   108   return true;
   109 }
   111 bool ots_hdmx_serialise(OTSStream *out, OpenTypeFile *file) {
   112   OpenTypeHDMX * const hdmx = file->hdmx;
   114   if (!out->WriteU16(hdmx->version) ||
   115       !out->WriteS16(hdmx->records.size()) ||
   116       !out->WriteS32(hdmx->size_device_record)) {
   117     return OTS_FAILURE_MSG("Failed to write hdmx header");
   118   }
   120   for (unsigned i = 0; i < hdmx->records.size(); ++i) {
   121     const OpenTypeHDMXDeviceRecord& rec = hdmx->records[i];
   122     if (!out->Write(&rec.pixel_size, 1) ||
   123         !out->Write(&rec.max_width, 1) ||
   124         !out->Write(&rec.widths[0], rec.widths.size())) {
   125       return OTS_FAILURE_MSG("Failed to write hdmx record %d", i);
   126     }
   127     if ((hdmx->pad_len > 0) &&
   128         !out->Write((const uint8_t *)"\x00\x00\x00", hdmx->pad_len)) {
   129       return OTS_FAILURE_MSG("Failed to write hdmx padding of length %d", hdmx->pad_len);
   130     }
   131   }
   133   return true;
   134 }
   136 void ots_hdmx_free(OpenTypeFile *file) {
   137   delete file->hdmx;
   138 }
   140 }  // namespace ots

mercurial