LCOV - code coverage report
Current view: directory - gfx/ots/src - os2.cc (source / functions) Found Hit Coverage
Test: app.info Lines: 161 0 0.0 %
Date: 2012-06-02 Functions: 4 0 0.0 %

       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.
       4                 : 
       5                 : #include "os2.h"
       6                 : 
       7                 : #include "head.h"
       8                 : 
       9                 : // OS/2 - OS/2 and Windows Metrics
      10                 : // http://www.microsoft.com/opentype/otspec/os2.htm
      11                 : 
      12                 : namespace ots {
      13                 : 
      14               0 : bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
      15               0 :   Buffer table(data, length);
      16                 : 
      17               0 :   OpenTypeOS2 *os2 = new OpenTypeOS2;
      18               0 :   file->os2 = os2;
      19                 : 
      20               0 :   if (!table.ReadU16(&os2->version) ||
      21               0 :       !table.ReadS16(&os2->avg_char_width) ||
      22               0 :       !table.ReadU16(&os2->weight_class) ||
      23               0 :       !table.ReadU16(&os2->width_class) ||
      24               0 :       !table.ReadU16(&os2->type) ||
      25               0 :       !table.ReadS16(&os2->subscript_x_size) ||
      26               0 :       !table.ReadS16(&os2->subscript_y_size) ||
      27               0 :       !table.ReadS16(&os2->subscript_x_offset) ||
      28               0 :       !table.ReadS16(&os2->subscript_y_offset) ||
      29               0 :       !table.ReadS16(&os2->superscript_x_size) ||
      30               0 :       !table.ReadS16(&os2->superscript_y_size) ||
      31               0 :       !table.ReadS16(&os2->superscript_x_offset) ||
      32               0 :       !table.ReadS16(&os2->superscript_y_offset) ||
      33               0 :       !table.ReadS16(&os2->strikeout_size) ||
      34               0 :       !table.ReadS16(&os2->strikeout_position) ||
      35               0 :       !table.ReadS16(&os2->family_class)) {
      36               0 :     return OTS_FAILURE();
      37                 :   }
      38                 : 
      39               0 :   if (os2->version > 4) {
      40               0 :     return OTS_FAILURE();
      41                 :   }
      42                 : 
      43                 :   // Some linux fonts (e.g., Kedage-t.ttf and LucidaSansDemiOblique.ttf) have
      44                 :   // weird weight/width classes. Overwrite them with FW_NORMAL/1/9.
      45               0 :   if (os2->weight_class < 100 ||
      46                 :       os2->weight_class > 900 ||
      47                 :       os2->weight_class % 100) {
      48                 :     OTS_WARNING("bad weight: %u", os2->weight_class);
      49               0 :     os2->weight_class = 400;  // FW_NORMAL
      50                 :   }
      51               0 :   if (os2->width_class < 1) {
      52                 :     OTS_WARNING("bad width: %u", os2->width_class);
      53               0 :     os2->width_class = 1;
      54               0 :   } else if (os2->width_class > 9) {
      55                 :     OTS_WARNING("bad width: %u", os2->width_class);
      56               0 :     os2->width_class = 9;
      57                 :   }
      58                 : 
      59                 :   // lowest 3 bits of fsType are exclusive.
      60               0 :   if (os2->type & 0x2) {
      61                 :     // mask bits 2 & 3.
      62               0 :     os2->type &= 0xfff3u;
      63               0 :   } else if (os2->type & 0x4) {
      64                 :     // mask bits 1 & 3.
      65               0 :     os2->type &= 0xfff4u;
      66               0 :   } else if (os2->type & 0x8) {
      67                 :     // mask bits 1 & 2.
      68               0 :     os2->type &= 0xfff9u;
      69                 :   }
      70                 : 
      71                 :   // mask reserved bits. use only 0..3, 8, 9 bits.
      72               0 :   os2->type &= 0x30f;
      73                 : 
      74               0 :   if (os2->subscript_x_size < 0) {
      75                 :     OTS_WARNING("bad subscript_x_size: %d", os2->subscript_x_size);
      76               0 :     os2->subscript_x_size = 0;
      77                 :   }
      78               0 :   if (os2->subscript_y_size < 0) {
      79                 :     OTS_WARNING("bad subscript_y_size: %d", os2->subscript_y_size);
      80               0 :     os2->subscript_y_size = 0;
      81                 :   }
      82               0 :   if (os2->superscript_x_size < 0) {
      83                 :     OTS_WARNING("bad superscript_x_size: %d", os2->superscript_x_size);
      84               0 :     os2->superscript_x_size = 0;
      85                 :   }
      86               0 :   if (os2->superscript_y_size < 0) {
      87                 :     OTS_WARNING("bad superscript_y_size: %d", os2->superscript_y_size);
      88               0 :     os2->superscript_y_size = 0;
      89                 :   }
      90               0 :   if (os2->strikeout_size < 0) {
      91                 :     OTS_WARNING("bad strikeout_size: %d", os2->strikeout_size);
      92               0 :     os2->strikeout_size = 0;
      93                 :   }
      94                 : 
      95               0 :   for (unsigned i = 0; i < 10; ++i) {
      96               0 :     if (!table.ReadU8(&os2->panose[i])) {
      97               0 :       return OTS_FAILURE();
      98                 :     }
      99                 :   }
     100                 : 
     101               0 :   if (!table.ReadU32(&os2->unicode_range_1) ||
     102               0 :       !table.ReadU32(&os2->unicode_range_2) ||
     103               0 :       !table.ReadU32(&os2->unicode_range_3) ||
     104               0 :       !table.ReadU32(&os2->unicode_range_4) ||
     105               0 :       !table.ReadU32(&os2->vendor_id) ||
     106               0 :       !table.ReadU16(&os2->selection) ||
     107               0 :       !table.ReadU16(&os2->first_char_index) ||
     108               0 :       !table.ReadU16(&os2->last_char_index) ||
     109               0 :       !table.ReadS16(&os2->typo_ascender) ||
     110               0 :       !table.ReadS16(&os2->typo_descender) ||
     111               0 :       !table.ReadS16(&os2->typo_linegap) ||
     112               0 :       !table.ReadU16(&os2->win_ascent) ||
     113               0 :       !table.ReadU16(&os2->win_descent)) {
     114               0 :     return OTS_FAILURE();
     115                 :   }
     116                 : 
     117                 :   // If bit 6 is set, then bits 0 and 5 must be clear.
     118               0 :   if (os2->selection & 0x40) {
     119               0 :     os2->selection &= 0xffdeu;
     120                 :   }
     121                 : 
     122                 :   // the settings of bits 0 and 1 must be reflected in the macStyle bits
     123                 :   // in the 'head' table.
     124               0 :   if (!file->head) {
     125               0 :     return OTS_FAILURE();
     126                 :   }
     127               0 :   if ((os2->selection & 0x1) &&
     128               0 :       !(file->head->mac_style & 0x2)) {
     129                 :     OTS_WARNING("adjusting Mac style (italic)");
     130               0 :     file->head->mac_style |= 0x2;
     131                 :   }
     132               0 :   if ((os2->selection & 0x2) &&
     133               0 :       !(file->head->mac_style & 0x4)) {
     134                 :     OTS_WARNING("adjusting Mac style (underscore)");
     135               0 :     file->head->mac_style |= 0x4;
     136                 :   }
     137                 : 
     138                 :   // While bit 6 on implies that bits 0 and 1 of macStyle are clear,
     139                 :   // the reverse is not true.
     140               0 :   if ((os2->selection & 0x40) &&
     141                 :       (file->head->mac_style & 0x3)) {
     142                 :     OTS_WARNING("adjusting Mac style (regular)");
     143               0 :     file->head->mac_style &= 0xfffcu;
     144                 :   }
     145                 : 
     146               0 :   if ((os2->version < 4) &&
     147                 :       (os2->selection & 0x300)) {
     148                 :     // bit 8 and 9 must be unset in OS/2 table versions less than 4.
     149               0 :     return OTS_FAILURE();
     150                 :   }
     151                 : 
     152                 :   // mask reserved bits. use only 0..9 bits.
     153               0 :   os2->selection &= 0x3ff;
     154                 : 
     155               0 :   if (os2->first_char_index > os2->last_char_index) {
     156               0 :     return OTS_FAILURE();
     157                 :   }
     158               0 :   if (os2->typo_linegap < 0) {
     159                 :     OTS_WARNING("bad linegap: %d", os2->typo_linegap);
     160               0 :     os2->typo_linegap = 0;
     161                 :   }
     162                 : 
     163               0 :   if (os2->version < 1) {
     164                 :     // http://www.microsoft.com/typography/otspec/os2ver0.htm
     165               0 :     return true;
     166                 :   }
     167                 : 
     168               0 :   if (length < offsetof(OpenTypeOS2, code_page_range_2)) {
     169                 :     OTS_WARNING("bad version number: %u", os2->version);
     170                 :     // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version
     171                 :     // numbers. Fix them.
     172               0 :     os2->version = 0;
     173               0 :     return true;
     174                 :   }
     175                 : 
     176               0 :   if (!table.ReadU32(&os2->code_page_range_1) ||
     177               0 :       !table.ReadU32(&os2->code_page_range_2)) {
     178               0 :     return OTS_FAILURE();
     179                 :   }
     180                 : 
     181               0 :   if (os2->version < 2) {
     182                 :     // http://www.microsoft.com/typography/otspec/os2ver1.htm
     183               0 :     return true;
     184                 :   }
     185                 : 
     186               0 :   if (length < offsetof(OpenTypeOS2, max_context)) {
     187                 :     OTS_WARNING("bad version number: %u", os2->version);
     188                 :     // some Japanese fonts (e.g., mona.ttf) have weird version number.
     189                 :     // fix them.
     190               0 :     os2->version = 1;
     191               0 :     return true;
     192                 :   }
     193                 : 
     194               0 :   if (!table.ReadS16(&os2->x_height) ||
     195               0 :       !table.ReadS16(&os2->cap_height) ||
     196               0 :       !table.ReadU16(&os2->default_char) ||
     197               0 :       !table.ReadU16(&os2->break_char) ||
     198               0 :       !table.ReadU16(&os2->max_context)) {
     199               0 :     return OTS_FAILURE();
     200                 :   }
     201                 : 
     202               0 :   if (os2->x_height < 0) {
     203                 :     OTS_WARNING("bad x_height: %d", os2->x_height);
     204               0 :     os2->x_height = 0;
     205                 :   }
     206               0 :   if (os2->cap_height < 0) {
     207                 :     OTS_WARNING("bad cap_height: %d", os2->cap_height);
     208               0 :     os2->cap_height = 0;
     209                 :   }
     210                 : 
     211               0 :   return true;
     212                 : }
     213                 : 
     214               0 : bool ots_os2_should_serialise(OpenTypeFile *file) {
     215               0 :   return file->os2 != NULL;
     216                 : }
     217                 : 
     218               0 : bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) {
     219               0 :   const OpenTypeOS2 *os2 = file->os2;
     220                 : 
     221               0 :   if (!out->WriteU16(os2->version) ||
     222               0 :       !out->WriteS16(os2->avg_char_width) ||
     223               0 :       !out->WriteU16(os2->weight_class) ||
     224               0 :       !out->WriteU16(os2->width_class) ||
     225               0 :       !out->WriteU16(os2->type) ||
     226               0 :       !out->WriteS16(os2->subscript_x_size) ||
     227               0 :       !out->WriteS16(os2->subscript_y_size) ||
     228               0 :       !out->WriteS16(os2->subscript_x_offset) ||
     229               0 :       !out->WriteS16(os2->subscript_y_offset) ||
     230               0 :       !out->WriteS16(os2->superscript_x_size) ||
     231               0 :       !out->WriteS16(os2->superscript_y_size) ||
     232               0 :       !out->WriteS16(os2->superscript_x_offset) ||
     233               0 :       !out->WriteS16(os2->superscript_y_offset) ||
     234               0 :       !out->WriteS16(os2->strikeout_size) ||
     235               0 :       !out->WriteS16(os2->strikeout_position) ||
     236               0 :       !out->WriteS16(os2->family_class)) {
     237               0 :     return OTS_FAILURE();
     238                 :   }
     239                 : 
     240               0 :   for (unsigned i = 0; i < 10; ++i) {
     241               0 :     if (!out->Write(&os2->panose[i], 1)) {
     242               0 :       return OTS_FAILURE();
     243                 :     }
     244                 :   }
     245                 : 
     246               0 :   if (!out->WriteU32(os2->unicode_range_1) ||
     247               0 :       !out->WriteU32(os2->unicode_range_2) ||
     248               0 :       !out->WriteU32(os2->unicode_range_3) ||
     249               0 :       !out->WriteU32(os2->unicode_range_4) ||
     250               0 :       !out->WriteU32(os2->vendor_id) ||
     251               0 :       !out->WriteU16(os2->selection) ||
     252               0 :       !out->WriteU16(os2->first_char_index) ||
     253               0 :       !out->WriteU16(os2->last_char_index) ||
     254               0 :       !out->WriteS16(os2->typo_ascender) ||
     255               0 :       !out->WriteS16(os2->typo_descender) ||
     256               0 :       !out->WriteS16(os2->typo_linegap) ||
     257               0 :       !out->WriteU16(os2->win_ascent) ||
     258               0 :       !out->WriteU16(os2->win_descent)) {
     259               0 :     return OTS_FAILURE();
     260                 :   }
     261                 : 
     262               0 :   if (os2->version < 1) {
     263               0 :     return true;
     264                 :   }
     265                 : 
     266               0 :   if (!out->WriteU32(os2->code_page_range_1) ||
     267               0 :       !out->WriteU32(os2->code_page_range_2)) {
     268               0 :     return OTS_FAILURE();
     269                 :   }
     270                 : 
     271               0 :   if (os2->version < 2) {
     272               0 :     return true;
     273                 :   }
     274                 : 
     275               0 :   if (!out->WriteS16(os2->x_height) ||
     276               0 :       !out->WriteS16(os2->cap_height) ||
     277               0 :       !out->WriteU16(os2->default_char) ||
     278               0 :       !out->WriteU16(os2->break_char) ||
     279               0 :       !out->WriteU16(os2->max_context)) {
     280               0 :     return OTS_FAILURE();
     281                 :   }
     282                 : 
     283               0 :   return true;
     284                 : }
     285                 : 
     286               0 : void ots_os2_free(OpenTypeFile *file) {
     287               0 :   delete file->os2;
     288               0 : }
     289                 : 
     290                 : }  // namespace ots

Generated by: LCOV version 1.7