tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

os2.cc (11106B)


      1 // Copyright (c) 2009-2017 The OTS 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.
      4 
      5 #include <string>
      6 
      7 #include "os2.h"
      8 #include "head.h"
      9 
     10 // OS/2 - OS/2 and Windows Metrics
     11 // http://www.microsoft.com/typography/otspec/os2.htm
     12 
     13 namespace ots {
     14 
     15 bool OpenTypeOS2::Parse(const uint8_t *data, size_t length) {
     16  Buffer table(data, length);
     17 
     18  if (!table.ReadU16(&this->table.version) ||
     19      !table.ReadS16(&this->table.avg_char_width) ||
     20      !table.ReadU16(&this->table.weight_class) ||
     21      !table.ReadU16(&this->table.width_class) ||
     22      !table.ReadU16(&this->table.type) ||
     23      !table.ReadS16(&this->table.subscript_x_size) ||
     24      !table.ReadS16(&this->table.subscript_y_size) ||
     25      !table.ReadS16(&this->table.subscript_x_offset) ||
     26      !table.ReadS16(&this->table.subscript_y_offset) ||
     27      !table.ReadS16(&this->table.superscript_x_size) ||
     28      !table.ReadS16(&this->table.superscript_y_size) ||
     29      !table.ReadS16(&this->table.superscript_x_offset) ||
     30      !table.ReadS16(&this->table.superscript_y_offset) ||
     31      !table.ReadS16(&this->table.strikeout_size) ||
     32      !table.ReadS16(&this->table.strikeout_position) ||
     33      !table.ReadS16(&this->table.family_class)) {
     34    return Error("Error reading basic table elements");
     35  }
     36 
     37  if (this->table.version > 5) {
     38    return Error("Unsupported table version: %u", this->table.version);
     39  }
     40 
     41  if (this->table.weight_class < 1) {
     42    Warning("Bad usWeightClass: %u, changing it to %d",
     43             this->table.weight_class, 1);
     44    this->table.weight_class = 1;
     45  } else if (this->table.weight_class > 1000) {
     46    Warning("Bad usWeightClass: %u, changing it to %d",
     47             this->table.weight_class, 1000);
     48    this->table.weight_class = 1000;
     49  }
     50 
     51  if (this->table.width_class < 1) {
     52    Warning("Bad usWidthClass: %u, changing it to %d",
     53            this->table.width_class, 1);
     54    this->table.width_class = 1;
     55  } else if (this->table.width_class > 9) {
     56    Warning("Bad usWidthClass: %u, changing it to %d",
     57            this->table.width_class, 9);
     58    this->table.width_class = 9;
     59  }
     60 
     61  // lowest 3 bits of fsType are exclusive.
     62  if (this->table.type & 0x2) {
     63    // mask bits 2 & 3.
     64    this->table.type &= 0xfff3u;
     65  } else if (this->table.type & 0x4) {
     66    // mask bits 1 & 3.
     67    this->table.type &= 0xfff4u;
     68  } else if (this->table.type & 0x8) {
     69    // mask bits 1 & 2.
     70    this->table.type &= 0xfff9u;
     71  }
     72 
     73  // mask reserved bits. use only 0..3, 8, 9 bits.
     74  this->table.type &= 0x30f;
     75 
     76 #define SET_TO_ZERO(a, b)                                                      \
     77  if (this->table.b < 0) {                                                     \
     78    Warning("Bad " a ": %d, setting it to zero", this->table.b);               \
     79    this->table.b = 0;                                                         \
     80  }
     81 
     82  SET_TO_ZERO("ySubscriptXSize", subscript_x_size);
     83  SET_TO_ZERO("ySubscriptYSize", subscript_y_size);
     84  SET_TO_ZERO("ySuperscriptXSize", superscript_x_size);
     85  SET_TO_ZERO("ySuperscriptYSize", superscript_y_size);
     86  SET_TO_ZERO("yStrikeoutSize", strikeout_size);
     87 #undef SET_TO_ZERO
     88 
     89  static const char* panose_strings[10] = {
     90    "bFamilyType",
     91    "bSerifStyle",
     92    "bWeight",
     93    "bProportion",
     94    "bContrast",
     95    "bStrokeVariation",
     96    "bArmStyle",
     97    "bLetterform",
     98    "bMidline",
     99    "bXHeight",
    100  };
    101  for (unsigned i = 0; i < 10; ++i) {
    102    if (!table.ReadU8(&this->table.panose[i])) {
    103      return Error("Failed to read PANOSE %s", panose_strings[i]);
    104    }
    105  }
    106 
    107  if (!table.ReadU32(&this->table.unicode_range_1) ||
    108      !table.ReadU32(&this->table.unicode_range_2) ||
    109      !table.ReadU32(&this->table.unicode_range_3) ||
    110      !table.ReadU32(&this->table.unicode_range_4) ||
    111      !table.ReadU32(&this->table.vendor_id) ||
    112      !table.ReadU16(&this->table.selection) ||
    113      !table.ReadU16(&this->table.first_char_index) ||
    114      !table.ReadU16(&this->table.last_char_index) ||
    115      !table.ReadS16(&this->table.typo_ascender) ||
    116      !table.ReadS16(&this->table.typo_descender) ||
    117      !table.ReadS16(&this->table.typo_linegap) ||
    118      !table.ReadU16(&this->table.win_ascent) ||
    119      !table.ReadU16(&this->table.win_descent)) {
    120    return Error("Error reading more basic table fields");
    121  }
    122 
    123  // If bit 6 is set, then bits 0 and 5 must be clear.
    124  if (this->table.selection & 0x40) {
    125    this->table.selection &= 0xffdeu;
    126  }
    127 
    128  // the settings of bits 0 and 1 must be reflected in the macStyle bits
    129  // in the 'head' table.
    130  OpenTypeHEAD *head = static_cast<OpenTypeHEAD*>(
    131      GetFont()->GetTypedTable(OTS_TAG_HEAD));
    132 
    133  if ((this->table.selection & 0x1) &&
    134      head && !(head->mac_style & 0x2)) {
    135    Warning("Adjusting head.macStyle (italic) to match fsSelection");
    136    head->mac_style |= 0x2;
    137  }
    138  if ((this->table.selection & 0x2) &&
    139      head && !(head->mac_style & 0x4)) {
    140    Warning("Adjusting head.macStyle (underscore) to match fsSelection");
    141    head->mac_style |= 0x4;
    142  }
    143 
    144  // While bit 6 on implies that bits 0 and 1 of macStyle are clear,
    145  // the reverse is not true.
    146  if ((this->table.selection & 0x40) &&
    147      head && (head->mac_style & 0x3)) {
    148    Warning("Adjusting head.macStyle (regular) to match fsSelection");
    149    head->mac_style &= 0xfffcu;
    150  }
    151 
    152  if ((this->table.version < 4) &&
    153      (this->table.selection & 0x300)) {
    154    // bit 8 and 9 must be unset in OS/2 table versions less than 4.
    155    Warning("fsSelection bits 8 and 9 must be unset for table version %d",
    156            this->table.version);
    157  }
    158 
    159  // mask reserved bits. use only 0..9 bits.
    160  this->table.selection &= 0x3ff;
    161 
    162  if (this->table.first_char_index > this->table.last_char_index) {
    163    Warning("usFirstCharIndex %d > usLastCharIndex %d",
    164            this->table.first_char_index, this->table.last_char_index);
    165    this->table.first_char_index = this->table.last_char_index;
    166  }
    167  if (this->table.typo_linegap < 0) {
    168    Warning("Bad sTypoLineGap, setting it to 0: %d", this->table.typo_linegap);
    169    this->table.typo_linegap = 0;
    170  }
    171 
    172  if (this->table.version < 1) {
    173    // http://www.microsoft.com/typography/otspec/os2ver0.htm
    174    return true;
    175  }
    176 
    177  if (length < offsetof(OS2Data, code_page_range_2)) {
    178    Warning("Bad version number, setting it to 0: %u", this->table.version);
    179    // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version
    180    // numbers. Fix them.
    181    this->table.version = 0;
    182    return true;
    183  }
    184 
    185  if (!table.ReadU32(&this->table.code_page_range_1) ||
    186      !table.ReadU32(&this->table.code_page_range_2)) {
    187    return Error("Failed to read ulCodePageRange1 or ulCodePageRange2");
    188  }
    189 
    190  if (this->table.version < 2) {
    191    // http://www.microsoft.com/typography/otspec/os2ver1.htm
    192    return true;
    193  }
    194 
    195  if (length < offsetof(OS2Data, max_context)) {
    196    Warning("Bad version number, setting it to 1: %u", this->table.version);
    197    // some Japanese fonts (e.g., mona.ttf) have weird version number.
    198    // fix them.
    199    this->table.version = 1;
    200    return true;
    201  }
    202 
    203  if (!table.ReadS16(&this->table.x_height) ||
    204      !table.ReadS16(&this->table.cap_height) ||
    205      !table.ReadU16(&this->table.default_char) ||
    206      !table.ReadU16(&this->table.break_char) ||
    207      !table.ReadU16(&this->table.max_context)) {
    208    return Error("Failed to read version 2-specific fields");
    209  }
    210 
    211  if (this->table.x_height < 0) {
    212    Warning("Bad sxHeight setting it to 0: %d", this->table.x_height);
    213    this->table.x_height = 0;
    214  }
    215  if (this->table.cap_height < 0) {
    216    Warning("Bad sCapHeight setting it to 0: %d", this->table.cap_height);
    217    this->table.cap_height = 0;
    218  }
    219 
    220  if (this->table.version < 5) {
    221    // http://www.microsoft.com/typography/otspec/os2ver4.htm
    222    return true;
    223  }
    224 
    225  if (!table.ReadU16(&this->table.lower_optical_pointsize) ||
    226      !table.ReadU16(&this->table.upper_optical_pointsize)) {
    227    return Error("Failed to read version 5-specific fields");
    228  }
    229 
    230  if (this->table.lower_optical_pointsize > 0xFFFE) {
    231    Warning("usLowerOpticalPointSize is bigger than 0xFFFE: %d",
    232            this->table.lower_optical_pointsize);
    233    this->table.lower_optical_pointsize = 0xFFFE;
    234  }
    235 
    236  if (this->table.upper_optical_pointsize < 2) {
    237    Warning("usUpperOpticalPointSize is lower than 2: %d",
    238            this->table.upper_optical_pointsize);
    239    this->table.upper_optical_pointsize = 2;
    240  }
    241 
    242  return true;
    243 }
    244 
    245 bool OpenTypeOS2::Serialize(OTSStream *out) {
    246  if (!out->WriteU16(this->table.version) ||
    247      !out->WriteS16(this->table.avg_char_width) ||
    248      !out->WriteU16(this->table.weight_class) ||
    249      !out->WriteU16(this->table.width_class) ||
    250      !out->WriteU16(this->table.type) ||
    251      !out->WriteS16(this->table.subscript_x_size) ||
    252      !out->WriteS16(this->table.subscript_y_size) ||
    253      !out->WriteS16(this->table.subscript_x_offset) ||
    254      !out->WriteS16(this->table.subscript_y_offset) ||
    255      !out->WriteS16(this->table.superscript_x_size) ||
    256      !out->WriteS16(this->table.superscript_y_size) ||
    257      !out->WriteS16(this->table.superscript_x_offset) ||
    258      !out->WriteS16(this->table.superscript_y_offset) ||
    259      !out->WriteS16(this->table.strikeout_size) ||
    260      !out->WriteS16(this->table.strikeout_position) ||
    261      !out->WriteS16(this->table.family_class)) {
    262    return Error("Failed to write basic table data");
    263  }
    264 
    265  for (unsigned i = 0; i < 10; ++i) {
    266    if (!out->Write(&this->table.panose[i], 1)) {
    267      return Error("Failed to write PANOSE data");
    268    }
    269  }
    270 
    271  if (!out->WriteU32(this->table.unicode_range_1) ||
    272      !out->WriteU32(this->table.unicode_range_2) ||
    273      !out->WriteU32(this->table.unicode_range_3) ||
    274      !out->WriteU32(this->table.unicode_range_4) ||
    275      !out->WriteU32(this->table.vendor_id) ||
    276      !out->WriteU16(this->table.selection) ||
    277      !out->WriteU16(this->table.first_char_index) ||
    278      !out->WriteU16(this->table.last_char_index) ||
    279      !out->WriteS16(this->table.typo_ascender) ||
    280      !out->WriteS16(this->table.typo_descender) ||
    281      !out->WriteS16(this->table.typo_linegap) ||
    282      !out->WriteU16(this->table.win_ascent) ||
    283      !out->WriteU16(this->table.win_descent)) {
    284    return Error("Failed to write version 1-specific fields");
    285  }
    286 
    287  if (this->table.version < 1) {
    288    return true;
    289  }
    290 
    291  if (!out->WriteU32(this->table.code_page_range_1) ||
    292      !out->WriteU32(this->table.code_page_range_2)) {
    293    return Error("Failed to write codepage ranges");
    294  }
    295 
    296  if (this->table.version < 2) {
    297    return true;
    298  }
    299 
    300  if (!out->WriteS16(this->table.x_height) ||
    301      !out->WriteS16(this->table.cap_height) ||
    302      !out->WriteU16(this->table.default_char) ||
    303      !out->WriteU16(this->table.break_char) ||
    304      !out->WriteU16(this->table.max_context)) {
    305    return Error("Failed to write version 2-specific fields");
    306  }
    307 
    308  if (this->table.version < 5) {
    309    return true;
    310  }
    311 
    312  if (!out->WriteU16(this->table.lower_optical_pointsize) ||
    313      !out->WriteU16(this->table.upper_optical_pointsize)) {
    314    return Error("Failed to write version 5-specific fields");
    315  }
    316 
    317  return true;
    318 }
    319 
    320 }  // namespace ots