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
|
/*
* Copyright (C) 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GetEOTHeader.h"
#include "SharedBuffer.h"
namespace WebCore {
struct BigEndianUShort {
operator unsigned short() const { return (v & 0x00ff) << 8 | v >> 8; }
unsigned short v;
};
struct BigEndianULong {
operator unsigned() const { return (v & 0xff) << 24 | (v & 0xff00) << 8 | (v & 0xff0000) >> 8 | v >> 24; }
unsigned v;
};
#pragma pack(1)
struct EOTPrefix {
unsigned eotSize;
unsigned fontDataSize;
unsigned version;
unsigned flags;
UInt8 fontPANOSE[10];
UInt8 charset;
UInt8 italic;
unsigned weight;
unsigned short fsType;
unsigned short magicNumber;
unsigned unicodeRange[4];
unsigned codePageRange[2];
unsigned checkSumAdjustment;
unsigned reserved[4];
unsigned short padding1;
};
struct TableDirectoryEntry {
BigEndianULong tag;
BigEndianULong checkSum;
BigEndianULong offset;
BigEndianULong length;
};
struct sfntHeader {
Fixed version;
BigEndianUShort numTables;
BigEndianUShort searchRange;
BigEndianUShort entrySelector;
BigEndianUShort rangeShift;
TableDirectoryEntry tables[1];
};
struct OS2Table {
BigEndianUShort version;
BigEndianUShort avgCharWidth;
BigEndianUShort weightClass;
BigEndianUShort widthClass;
BigEndianUShort fsType;
BigEndianUShort subscriptXSize;
BigEndianUShort subscriptYSize;
BigEndianUShort subscriptXOffset;
BigEndianUShort subscriptYOffset;
BigEndianUShort superscriptXSize;
BigEndianUShort superscriptYSize;
BigEndianUShort superscriptXOffset;
BigEndianUShort superscriptYOffset;
BigEndianUShort strikeoutSize;
BigEndianUShort strikeoutPosition;
BigEndianUShort familyClass;
UInt8 panose[10];
BigEndianULong unicodeRange[4];
UInt8 vendID[4];
BigEndianUShort fsSelection;
BigEndianUShort firstCharIndex;
BigEndianUShort lastCharIndex;
BigEndianUShort typoAscender;
BigEndianUShort typoDescender;
BigEndianUShort typoLineGap;
BigEndianUShort winAscent;
BigEndianUShort winDescent;
BigEndianULong codePageRange[2];
BigEndianUShort xHeight;
BigEndianUShort capHeight;
BigEndianUShort defaultChar;
BigEndianUShort breakChar;
BigEndianUShort maxContext;
};
struct headTable {
Fixed version;
Fixed fontRevision;
BigEndianULong checkSumAdjustment;
BigEndianULong magicNumber;
BigEndianUShort flags;
BigEndianUShort unitsPerEm;
long long created;
long long modified;
BigEndianUShort xMin;
BigEndianUShort xMax;
BigEndianUShort yMin;
BigEndianUShort yMax;
BigEndianUShort macStyle;
BigEndianUShort lowestRectPPEM;
BigEndianUShort fontDirectionHint;
BigEndianUShort indexToLocFormat;
BigEndianUShort glyphDataFormat;
};
struct nameRecord {
BigEndianUShort platformID;
BigEndianUShort encodingID;
BigEndianUShort languageID;
BigEndianUShort nameID;
BigEndianUShort length;
BigEndianUShort offset;
};
struct nameTable {
BigEndianUShort format;
BigEndianUShort count;
BigEndianUShort stringOffset;
nameRecord nameRecords[1];
};
#pragma pack()
static void appendBigEndianStringToEOTHeader(Vector<UInt8, 512>& eotHeader, const BigEndianUShort* string, unsigned short length)
{
size_t size = eotHeader.size();
eotHeader.resize(size + length + 2 * sizeof(unsigned short));
UChar* dst = reinterpret_cast<UChar*>(eotHeader.data() + size);
unsigned i = 0;
dst[i++] = length;
unsigned numCharacters = length / 2;
for (unsigned j = 0; j < numCharacters; j++)
dst[i++] = string[j];
dst[i] = 0;
}
bool getEOTHeader(SharedBuffer* fontData, Vector<UInt8, 512>& eotHeader)
{
size_t dataLength = fontData->size();
const char* data = fontData->data();
eotHeader.resize(sizeof(EOTPrefix));
EOTPrefix* prefix = reinterpret_cast<EOTPrefix*>(eotHeader.data());
prefix->fontDataSize = dataLength;
prefix->version = 0x00020001;
prefix->flags = 0;
if (dataLength < offsetof(sfntHeader, tables))
return false;
const sfntHeader* sfnt = reinterpret_cast<const sfntHeader*>(data);
if (dataLength < offsetof(sfntHeader, tables) + sfnt->numTables * sizeof(TableDirectoryEntry))
return false;
bool haveOS2 = false;
bool haveHead = false;
bool haveName = false;
const BigEndianUShort* familyName = 0;
unsigned short familyNameLength = 0;
const BigEndianUShort* subfamilyName = 0;
unsigned short subfamilyNameLength = 0;
const BigEndianUShort* fullName = 0;
unsigned short fullNameLength = 0;
const BigEndianUShort* versionString = 0;
unsigned short versionStringLength = 0;
for (unsigned i = 0; i < sfnt->numTables; i++) {
unsigned tableOffset = sfnt->tables[i].offset;
unsigned tableLength = sfnt->tables[i].length;
if (dataLength < tableOffset || dataLength < tableLength || dataLength < tableOffset + tableLength)
return false;
unsigned tableTag = sfnt->tables[i].tag;
switch (tableTag) {
case 'OS/2':
{
if (dataLength < tableOffset + sizeof(OS2Table))
return false;
haveOS2 = true;
const OS2Table* OS2 = reinterpret_cast<const OS2Table*>(data + tableOffset);
for (unsigned j = 0; j < 10; j++)
prefix->fontPANOSE[j] = OS2->panose[j];
prefix->italic = OS2->fsSelection & 0x01;
prefix->weight = OS2->weightClass;
// FIXME: Should use OS2->fsType, but some TrueType fonts set it to an over-restrictive value.
// Since ATS does not enforce this on Mac OS X, we do not enforce it either.
prefix->fsType = 0;
for (unsigned j = 0; j < 4; j++)
prefix->unicodeRange[j] = OS2->unicodeRange[j];
for (unsigned j = 0; j < 2; j++)
prefix->codePageRange[j] = OS2->codePageRange[j];
break;
}
case 'head':
{
if (dataLength < tableOffset + sizeof(headTable))
return false;
haveHead = true;
const headTable* head = reinterpret_cast<const headTable*>(data + tableOffset);
prefix->checkSumAdjustment = head->checkSumAdjustment;
break;
}
case 'name':
{
if (dataLength < tableOffset + offsetof(nameTable, nameRecords))
return false;
haveName = true;
const nameTable* name = reinterpret_cast<const nameTable*>(data + tableOffset);
for (int j = 0; j < name->count; j++) {
if (dataLength < tableOffset + offsetof(nameTable, nameRecords) + (j + 1) * sizeof(nameRecord))
return false;
if (name->nameRecords[j].platformID == 3 && name->nameRecords[j].encodingID == 1 && name->nameRecords[j].languageID == 0x0409) {
if (dataLength < tableOffset + name->stringOffset + name->nameRecords[j].offset + name->nameRecords[j].length)
return false;
unsigned short nameLength = name->nameRecords[j].length;
const BigEndianUShort* nameString = reinterpret_cast<const BigEndianUShort*>(data + tableOffset + name->stringOffset + name->nameRecords[j].offset);
switch (name->nameRecords[j].nameID) {
case 1:
familyNameLength = nameLength;
familyName = nameString;
break;
case 2:
subfamilyNameLength = nameLength;
subfamilyName = nameString;
break;
case 4:
fullNameLength = nameLength;
fullName = nameString;
break;
case 5:
versionStringLength = nameLength;
versionString = nameString;
break;
default:
break;
}
}
}
break;
}
default:
break;
}
if (haveOS2 && haveHead && haveName)
break;
}
prefix->charset = DEFAULT_CHARSET;
prefix->magicNumber = 0x504c;
prefix->reserved[0] = 0;
prefix->reserved[1] = 0;
prefix->reserved[2] = 0;
prefix->reserved[3] = 0;
prefix->padding1 = 0;
appendBigEndianStringToEOTHeader(eotHeader, familyName, familyNameLength);
appendBigEndianStringToEOTHeader(eotHeader, subfamilyName, subfamilyNameLength);
appendBigEndianStringToEOTHeader(eotHeader, versionString, versionStringLength);
appendBigEndianStringToEOTHeader(eotHeader, fullName, fullNameLength);
unsigned short padding = 0;
eotHeader.append(reinterpret_cast<UInt8*>(&padding), sizeof(padding));
prefix->eotSize = eotHeader.size() + fontData->size();
return true;
}
}
|