DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (a7f28a80e295)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
// 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.

#include <string>

#include "os2.h"
#include "head.h"

// OS/2 - OS/2 and Windows Metrics
// http://www.microsoft.com/typography/otspec/os2.htm

namespace ots {

bool OpenTypeOS2::Parse(const uint8_t *data, size_t length) {
  Buffer table(data, length);

  if (!table.ReadU16(&this->table.version) ||
      !table.ReadS16(&this->table.avg_char_width) ||
      !table.ReadU16(&this->table.weight_class) ||
      !table.ReadU16(&this->table.width_class) ||
      !table.ReadU16(&this->table.type) ||
      !table.ReadS16(&this->table.subscript_x_size) ||
      !table.ReadS16(&this->table.subscript_y_size) ||
      !table.ReadS16(&this->table.subscript_x_offset) ||
      !table.ReadS16(&this->table.subscript_y_offset) ||
      !table.ReadS16(&this->table.superscript_x_size) ||
      !table.ReadS16(&this->table.superscript_y_size) ||
      !table.ReadS16(&this->table.superscript_x_offset) ||
      !table.ReadS16(&this->table.superscript_y_offset) ||
      !table.ReadS16(&this->table.strikeout_size) ||
      !table.ReadS16(&this->table.strikeout_position) ||
      !table.ReadS16(&this->table.family_class)) {
    return Error("Error reading basic table elements");
  }

  if (this->table.version > 5) {
    return Error("Unsupported table version: %u", this->table.version);
  }

  if (this->table.weight_class < 1) {
    Warning("Bad usWeightClass: %u, changing it to %d",
             this->table.weight_class, 1);
    this->table.weight_class = 1;
  } else if (this->table.weight_class > 1000) {
    Warning("Bad usWeightClass: %u, changing it to %d",
             this->table.weight_class, 1000);
    this->table.weight_class = 1000;
  }

  if (this->table.width_class < 1) {
    Warning("Bad usWidthClass: %u, changing it to %d",
            this->table.width_class, 1);
    this->table.width_class = 1;
  } else if (this->table.width_class > 9) {
    Warning("Bad usWidthClass: %u, changing it to %d",
            this->table.width_class, 9);
    this->table.width_class = 9;
  }

  // lowest 3 bits of fsType are exclusive.
  if (this->table.type & 0x2) {
    // mask bits 2 & 3.
    this->table.type &= 0xfff3u;
  } else if (this->table.type & 0x4) {
    // mask bits 1 & 3.
    this->table.type &= 0xfff4u;
  } else if (this->table.type & 0x8) {
    // mask bits 1 & 2.
    this->table.type &= 0xfff9u;
  }

  // mask reserved bits. use only 0..3, 8, 9 bits.
  this->table.type &= 0x30f;

#define SET_TO_ZERO(a, b)                                                      \
  if (this->table.b < 0) {                                                     \
    Warning("Bad " a ": %d, setting it to zero", this->table.b);               \
    this->table.b = 0;                                                         \
  }

  SET_TO_ZERO("ySubscriptXSize", subscript_x_size);
  SET_TO_ZERO("ySubscriptYSize", subscript_y_size);
  SET_TO_ZERO("ySuperscriptXSize", superscript_x_size);
  SET_TO_ZERO("ySuperscriptYSize", superscript_y_size);
  SET_TO_ZERO("yStrikeoutSize", strikeout_size);
#undef SET_TO_ZERO

  static const char* panose_strings[10] = {
    "bFamilyType",
    "bSerifStyle",
    "bWeight",
    "bProportion",
    "bContrast",
    "bStrokeVariation",
    "bArmStyle",
    "bLetterform",
    "bMidline",
    "bXHeight",
  };
  for (unsigned i = 0; i < 10; ++i) {
    if (!table.ReadU8(&this->table.panose[i])) {
      return Error("Failed to read PANOSE %s", panose_strings[i]);
    }
  }

  if (!table.ReadU32(&this->table.unicode_range_1) ||
      !table.ReadU32(&this->table.unicode_range_2) ||
      !table.ReadU32(&this->table.unicode_range_3) ||
      !table.ReadU32(&this->table.unicode_range_4) ||
      !table.ReadU32(&this->table.vendor_id) ||
      !table.ReadU16(&this->table.selection) ||
      !table.ReadU16(&this->table.first_char_index) ||
      !table.ReadU16(&this->table.last_char_index) ||
      !table.ReadS16(&this->table.typo_ascender) ||
      !table.ReadS16(&this->table.typo_descender) ||
      !table.ReadS16(&this->table.typo_linegap) ||
      !table.ReadU16(&this->table.win_ascent) ||
      !table.ReadU16(&this->table.win_descent)) {
    return Error("Error reading more basic table fields");
  }

  // If bit 6 is set, then bits 0 and 5 must be clear.
  if (this->table.selection & 0x40) {
    this->table.selection &= 0xffdeu;
  }

  // the settings of bits 0 and 1 must be reflected in the macStyle bits
  // in the 'head' table.
  OpenTypeHEAD *head = static_cast<OpenTypeHEAD*>(
      GetFont()->GetTypedTable(OTS_TAG_HEAD));

  if ((this->table.selection & 0x1) &&
      head && !(head->mac_style & 0x2)) {
    Warning("Adjusting head.macStyle (italic) to match fsSelection");
    head->mac_style |= 0x2;
  }
  if ((this->table.selection & 0x2) &&
      head && !(head->mac_style & 0x4)) {
    Warning("Adjusting head.macStyle (underscore) to match fsSelection");
    head->mac_style |= 0x4;
  }

  // While bit 6 on implies that bits 0 and 1 of macStyle are clear,
  // the reverse is not true.
  if ((this->table.selection & 0x40) &&
      head && (head->mac_style & 0x3)) {
    Warning("Adjusting head.macStyle (regular) to match fsSelection");
    head->mac_style &= 0xfffcu;
  }

  if ((this->table.version < 4) &&
      (this->table.selection & 0x300)) {
    // bit 8 and 9 must be unset in OS/2 table versions less than 4.
    Warning("fsSelection bits 8 and 9 must be unset for table version %d",
            this->table.version);
  }

  // mask reserved bits. use only 0..9 bits.
  this->table.selection &= 0x3ff;

  if (this->table.first_char_index > this->table.last_char_index) {
    Warning("usFirstCharIndex %d > usLastCharIndex %d",
            this->table.first_char_index, this->table.last_char_index);
    this->table.first_char_index = this->table.last_char_index;
  }
  if (this->table.typo_linegap < 0) {
    Warning("Bad sTypoLineGap, setting it to 0: %d", this->table.typo_linegap);
    this->table.typo_linegap = 0;
  }

  if (this->table.version < 1) {
    // http://www.microsoft.com/typography/otspec/os2ver0.htm
    return true;
  }

  if (length < offsetof(OS2Data, code_page_range_2)) {
    Warning("Bad version number, setting it to 0: %u", this->table.version);
    // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version
    // numbers. Fix them.
    this->table.version = 0;
    return true;
  }

  if (!table.ReadU32(&this->table.code_page_range_1) ||
      !table.ReadU32(&this->table.code_page_range_2)) {
    return Error("Failed to read ulCodePageRange1 or ulCodePageRange2");
  }

  if (this->table.version < 2) {
    // http://www.microsoft.com/typography/otspec/os2ver1.htm
    return true;
  }

  if (length < offsetof(OS2Data, max_context)) {
    Warning("Bad version number, setting it to 1: %u", this->table.version);
    // some Japanese fonts (e.g., mona.ttf) have weird version number.
    // fix them.
    this->table.version = 1;
    return true;
  }

  if (!table.ReadS16(&this->table.x_height) ||
      !table.ReadS16(&this->table.cap_height) ||
      !table.ReadU16(&this->table.default_char) ||
      !table.ReadU16(&this->table.break_char) ||
      !table.ReadU16(&this->table.max_context)) {
    return Error("Failed to read version 2-specific fields");
  }

  if (this->table.x_height < 0) {
    Warning("Bad sxHeight settig it to 0: %d", this->table.x_height);
    this->table.x_height = 0;
  }
  if (this->table.cap_height < 0) {
    Warning("Bad sCapHeight setting it to 0: %d", this->table.cap_height);
    this->table.cap_height = 0;
  }

  if (this->table.version < 5) {
    // http://www.microsoft.com/typography/otspec/os2ver4.htm
    return true;
  }

  if (!table.ReadU16(&this->table.lower_optical_pointsize) ||
      !table.ReadU16(&this->table.upper_optical_pointsize)) {
    return Error("Failed to read version 5-specific fields");
  }

  if (this->table.lower_optical_pointsize > 0xFFFE) {
    Warning("usLowerOpticalPointSize is bigger than 0xFFFE: %d",
            this->table.lower_optical_pointsize);
    this->table.lower_optical_pointsize = 0xFFFE;
  }

  if (this->table.upper_optical_pointsize < 2) {
    Warning("usUpperOpticalPointSize is lower than 2: %d",
            this->table.upper_optical_pointsize);
    this->table.upper_optical_pointsize = 2;
  }

  return true;
}

bool OpenTypeOS2::Serialize(OTSStream *out) {
  if (!out->WriteU16(this->table.version) ||
      !out->WriteS16(this->table.avg_char_width) ||
      !out->WriteU16(this->table.weight_class) ||
      !out->WriteU16(this->table.width_class) ||
      !out->WriteU16(this->table.type) ||
      !out->WriteS16(this->table.subscript_x_size) ||
      !out->WriteS16(this->table.subscript_y_size) ||
      !out->WriteS16(this->table.subscript_x_offset) ||
      !out->WriteS16(this->table.subscript_y_offset) ||
      !out->WriteS16(this->table.superscript_x_size) ||
      !out->WriteS16(this->table.superscript_y_size) ||
      !out->WriteS16(this->table.superscript_x_offset) ||
      !out->WriteS16(this->table.superscript_y_offset) ||
      !out->WriteS16(this->table.strikeout_size) ||
      !out->WriteS16(this->table.strikeout_position) ||
      !out->WriteS16(this->table.family_class)) {
    return Error("Failed to write basic table data");
  }

  for (unsigned i = 0; i < 10; ++i) {
    if (!out->Write(&this->table.panose[i], 1)) {
      return Error("Failed to write PANOSE data");
    }
  }

  if (!out->WriteU32(this->table.unicode_range_1) ||
      !out->WriteU32(this->table.unicode_range_2) ||
      !out->WriteU32(this->table.unicode_range_3) ||
      !out->WriteU32(this->table.unicode_range_4) ||
      !out->WriteU32(this->table.vendor_id) ||
      !out->WriteU16(this->table.selection) ||
      !out->WriteU16(this->table.first_char_index) ||
      !out->WriteU16(this->table.last_char_index) ||
      !out->WriteS16(this->table.typo_ascender) ||
      !out->WriteS16(this->table.typo_descender) ||
      !out->WriteS16(this->table.typo_linegap) ||
      !out->WriteU16(this->table.win_ascent) ||
      !out->WriteU16(this->table.win_descent)) {
    return Error("Failed to write version 1-specific fields");
  }

  if (this->table.version < 1) {
    return true;
  }

  if (!out->WriteU32(this->table.code_page_range_1) ||
      !out->WriteU32(this->table.code_page_range_2)) {
    return Error("Failed to write codepage ranges");
  }

  if (this->table.version < 2) {
    return true;
  }

  if (!out->WriteS16(this->table.x_height) ||
      !out->WriteS16(this->table.cap_height) ||
      !out->WriteU16(this->table.default_char) ||
      !out->WriteU16(this->table.break_char) ||
      !out->WriteU16(this->table.max_context)) {
    return Error("Failed to write version 2-specific fields");
  }

  if (this->table.version < 5) {
    return true;
  }

  if (!out->WriteU16(this->table.lower_optical_pointsize) ||
      !out->WriteU16(this->table.upper_optical_pointsize)) {
    return Error("Failed to write version 5-specific fields");
  }

  return true;
}

}  // namespace ots