gfx/ots/src/head.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/ots/src/head.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "head.h"
     1.9 +
    1.10 +#include <cstring>
    1.11 +
    1.12 +// head - Font Header
    1.13 +// http://www.microsoft.com/typography/otspec/head.htm
    1.14 +
    1.15 +#define TABLE_NAME "head"
    1.16 +
    1.17 +namespace ots {
    1.18 +
    1.19 +bool ots_head_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
    1.20 +  Buffer table(data, length);
    1.21 +  file->head = new OpenTypeHEAD;
    1.22 +
    1.23 +  uint32_t version = 0;
    1.24 +  if (!table.ReadU32(&version) ||
    1.25 +      !table.ReadU32(&file->head->revision)) {
    1.26 +    return OTS_FAILURE_MSG("Failed to read head header");
    1.27 +  }
    1.28 +
    1.29 +  if (version >> 16 != 1) {
    1.30 +    return OTS_FAILURE_MSG("Bad head table version of %d", version);
    1.31 +  }
    1.32 +
    1.33 +  // Skip the checksum adjustment
    1.34 +  if (!table.Skip(4)) {
    1.35 +    return OTS_FAILURE_MSG("Failed to read checksum");
    1.36 +  }
    1.37 +
    1.38 +  uint32_t magic;
    1.39 +  if (!table.ReadTag(&magic) ||
    1.40 +      std::memcmp(&magic, "\x5F\x0F\x3C\xF5", 4)) {
    1.41 +    return OTS_FAILURE_MSG("Failed to read font magic number");
    1.42 +  }
    1.43 +
    1.44 +  if (!table.ReadU16(&file->head->flags)) {
    1.45 +    return OTS_FAILURE_MSG("Failed to read head flags");
    1.46 +  }
    1.47 +
    1.48 +  // We allow bits 0..4, 11..13
    1.49 +  file->head->flags &= 0x381f;
    1.50 +
    1.51 +  if (!table.ReadU16(&file->head->ppem)) {
    1.52 +    return OTS_FAILURE_MSG("Failed to read pixels per em");
    1.53 +  }
    1.54 +
    1.55 +  // ppem must be in range
    1.56 +  if (file->head->ppem < 16 ||
    1.57 +      file->head->ppem > 16384) {
    1.58 +    return OTS_FAILURE_MSG("Bad ppm of %d", file->head->ppem);
    1.59 +  }
    1.60 +
    1.61 +  // ppem must be a power of two
    1.62 +#if 0
    1.63 +  // We don't call ots_failure() for now since lots of TrueType fonts are
    1.64 +  // not following this rule. Putting OTS_WARNING here is too noisy.
    1.65 +  if ((file->head->ppem - 1) & file->head->ppem) {
    1.66 +    return OTS_FAILURE_MSG("ppm not a power of two: %d", file->head->ppem);
    1.67 +  }
    1.68 +#endif
    1.69 +
    1.70 +  if (!table.ReadR64(&file->head->created) ||
    1.71 +      !table.ReadR64(&file->head->modified)) {
    1.72 +    return OTS_FAILURE_MSG("Can't read font dates");
    1.73 +  }
    1.74 +
    1.75 +  if (!table.ReadS16(&file->head->xmin) ||
    1.76 +      !table.ReadS16(&file->head->ymin) ||
    1.77 +      !table.ReadS16(&file->head->xmax) ||
    1.78 +      !table.ReadS16(&file->head->ymax)) {
    1.79 +    return OTS_FAILURE_MSG("Failed to read font bounding box");
    1.80 +  }
    1.81 +
    1.82 +  if (file->head->xmin > file->head->xmax) {
    1.83 +    return OTS_FAILURE_MSG("Bad x dimension in the font bounding box (%d, %d)", file->head->xmin, file->head->xmax);
    1.84 +  }
    1.85 +  if (file->head->ymin > file->head->ymax) {
    1.86 +    return OTS_FAILURE_MSG("Bad y dimension in the font bounding box (%d, %d)", file->head->ymin, file->head->ymax);
    1.87 +  }
    1.88 +
    1.89 +  if (!table.ReadU16(&file->head->mac_style)) {
    1.90 +    return OTS_FAILURE_MSG("Failed to read font style");
    1.91 +  }
    1.92 +
    1.93 +  // We allow bits 0..6
    1.94 +  file->head->mac_style &= 0x7f;
    1.95 +
    1.96 +  if (!table.ReadU16(&file->head->min_ppem)) {
    1.97 +    return OTS_FAILURE_MSG("Failed to read font minimum ppm");
    1.98 +  }
    1.99 +
   1.100 +  // We don't care about the font direction hint
   1.101 +  if (!table.Skip(2)) {
   1.102 +    return OTS_FAILURE_MSG("Failed to skip font direction hint");
   1.103 +  }
   1.104 +
   1.105 +  if (!table.ReadS16(&file->head->index_to_loc_format)) {
   1.106 +    return OTS_FAILURE_MSG("Failed to read index to loc format");
   1.107 +  }
   1.108 +  if (file->head->index_to_loc_format < 0 ||
   1.109 +      file->head->index_to_loc_format > 1) {
   1.110 +    return OTS_FAILURE_MSG("Bad index to loc format %d", file->head->index_to_loc_format);
   1.111 +  }
   1.112 +
   1.113 +  int16_t glyph_data_format;
   1.114 +  if (!table.ReadS16(&glyph_data_format) ||
   1.115 +      glyph_data_format) {
   1.116 +    return OTS_FAILURE_MSG("Failed to read glyph data format");
   1.117 +  }
   1.118 +
   1.119 +  return true;
   1.120 +}
   1.121 +
   1.122 +bool ots_head_should_serialise(OpenTypeFile *file) {
   1.123 +  return file->head != NULL;
   1.124 +}
   1.125 +
   1.126 +bool ots_head_serialise(OTSStream *out, OpenTypeFile *file) {
   1.127 +  if (!out->WriteU32(0x00010000) ||
   1.128 +      !out->WriteU32(file->head->revision) ||
   1.129 +      !out->WriteU32(0) ||  // check sum not filled in yet
   1.130 +      !out->WriteU32(0x5F0F3CF5) ||
   1.131 +      !out->WriteU16(file->head->flags) ||
   1.132 +      !out->WriteU16(file->head->ppem) ||
   1.133 +      !out->WriteR64(file->head->created) ||
   1.134 +      !out->WriteR64(file->head->modified) ||
   1.135 +      !out->WriteS16(file->head->xmin) ||
   1.136 +      !out->WriteS16(file->head->ymin) ||
   1.137 +      !out->WriteS16(file->head->xmax) ||
   1.138 +      !out->WriteS16(file->head->ymax) ||
   1.139 +      !out->WriteU16(file->head->mac_style) ||
   1.140 +      !out->WriteU16(file->head->min_ppem) ||
   1.141 +      !out->WriteS16(2) ||
   1.142 +      !out->WriteS16(file->head->index_to_loc_format) ||
   1.143 +      !out->WriteS16(0)) {
   1.144 +    return OTS_FAILURE_MSG("Failed to write head table");
   1.145 +  }
   1.146 +
   1.147 +  return true;
   1.148 +}
   1.149 +
   1.150 +void ots_head_free(OpenTypeFile *file) {
   1.151 +  delete file->head;
   1.152 +}
   1.153 +
   1.154 +}  // namespace

mercurial