gfx/thebes/gfxFontUtils.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifdef MOZ_LOGGING
michael@0 7 #define FORCE_PR_LOG /* Allow logging in the release build */
michael@0 8 #include "prlog.h"
michael@0 9 #endif
michael@0 10
michael@0 11 #include "mozilla/ArrayUtils.h"
michael@0 12
michael@0 13 #include "gfxFontUtils.h"
michael@0 14
michael@0 15 #include "nsServiceManagerUtils.h"
michael@0 16
michael@0 17 #include "mozilla/dom/EncodingUtils.h"
michael@0 18 #include "mozilla/Preferences.h"
michael@0 19 #include "mozilla/Services.h"
michael@0 20
michael@0 21 #include "nsCOMPtr.h"
michael@0 22 #include "nsIUUIDGenerator.h"
michael@0 23 #include "nsIUnicodeDecoder.h"
michael@0 24
michael@0 25 #include "harfbuzz/hb.h"
michael@0 26
michael@0 27 #include "plbase64.h"
michael@0 28 #include "prlog.h"
michael@0 29
michael@0 30 #ifdef PR_LOGGING
michael@0 31
michael@0 32 #define LOG(log, args) PR_LOG(gfxPlatform::GetLog(log), \
michael@0 33 PR_LOG_DEBUG, args)
michael@0 34
michael@0 35 #endif // PR_LOGGING
michael@0 36
michael@0 37 #define UNICODE_BMP_LIMIT 0x10000
michael@0 38
michael@0 39 using namespace mozilla;
michael@0 40
michael@0 41 #pragma pack(1)
michael@0 42
michael@0 43 typedef struct {
michael@0 44 AutoSwap_PRUint16 format;
michael@0 45 AutoSwap_PRUint16 reserved;
michael@0 46 AutoSwap_PRUint32 length;
michael@0 47 AutoSwap_PRUint32 language;
michael@0 48 AutoSwap_PRUint32 numGroups;
michael@0 49 } Format12CmapHeader;
michael@0 50
michael@0 51 typedef struct {
michael@0 52 AutoSwap_PRUint32 startCharCode;
michael@0 53 AutoSwap_PRUint32 endCharCode;
michael@0 54 AutoSwap_PRUint32 startGlyphId;
michael@0 55 } Format12Group;
michael@0 56
michael@0 57 #pragma pack()
michael@0 58
michael@0 59 #if PR_LOGGING
michael@0 60 void
michael@0 61 gfxSparseBitSet::Dump(const char* aPrefix, eGfxLog aWhichLog) const
michael@0 62 {
michael@0 63 NS_ASSERTION(mBlocks.DebugGetHeader(), "mHdr is null, this is bad");
michael@0 64 uint32_t b, numBlocks = mBlocks.Length();
michael@0 65
michael@0 66 for (b = 0; b < numBlocks; b++) {
michael@0 67 Block *block = mBlocks[b];
michael@0 68 if (!block) continue;
michael@0 69 char outStr[256];
michael@0 70 int index = 0;
michael@0 71 index += sprintf(&outStr[index], "%s u+%6.6x [", aPrefix, (b << BLOCK_INDEX_SHIFT));
michael@0 72 for (int i = 0; i < 32; i += 4) {
michael@0 73 for (int j = i; j < i + 4; j++) {
michael@0 74 uint8_t bits = block->mBits[j];
michael@0 75 uint8_t flip1 = ((bits & 0xaa) >> 1) | ((bits & 0x55) << 1);
michael@0 76 uint8_t flip2 = ((flip1 & 0xcc) >> 2) | ((flip1 & 0x33) << 2);
michael@0 77 uint8_t flipped = ((flip2 & 0xf0) >> 4) | ((flip2 & 0x0f) << 4);
michael@0 78
michael@0 79 index += sprintf(&outStr[index], "%2.2x", flipped);
michael@0 80 }
michael@0 81 if (i + 4 != 32) index += sprintf(&outStr[index], " ");
michael@0 82 }
michael@0 83 index += sprintf(&outStr[index], "]");
michael@0 84 LOG(aWhichLog, ("%s", outStr));
michael@0 85 }
michael@0 86 }
michael@0 87 #endif
michael@0 88
michael@0 89
michael@0 90 nsresult
michael@0 91 gfxFontUtils::ReadCMAPTableFormat12(const uint8_t *aBuf, uint32_t aLength,
michael@0 92 gfxSparseBitSet& aCharacterMap)
michael@0 93 {
michael@0 94 // Ensure table is large enough that we can safely read the header
michael@0 95 NS_ENSURE_TRUE(aLength >= sizeof(Format12CmapHeader),
michael@0 96 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 97
michael@0 98 // Sanity-check header fields
michael@0 99 const Format12CmapHeader *cmap12 =
michael@0 100 reinterpret_cast<const Format12CmapHeader*>(aBuf);
michael@0 101 NS_ENSURE_TRUE(uint16_t(cmap12->format) == 12,
michael@0 102 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 103 NS_ENSURE_TRUE(uint16_t(cmap12->reserved) == 0,
michael@0 104 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 105
michael@0 106 uint32_t tablelen = cmap12->length;
michael@0 107 NS_ENSURE_TRUE(tablelen >= sizeof(Format12CmapHeader) &&
michael@0 108 tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 109
michael@0 110 NS_ENSURE_TRUE(cmap12->language == 0, NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 111
michael@0 112 // Check that the table is large enough for the group array
michael@0 113 const uint32_t numGroups = cmap12->numGroups;
michael@0 114 NS_ENSURE_TRUE((tablelen - sizeof(Format12CmapHeader)) /
michael@0 115 sizeof(Format12Group) >= numGroups,
michael@0 116 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 117
michael@0 118 // The array of groups immediately follows the subtable header.
michael@0 119 const Format12Group *group =
michael@0 120 reinterpret_cast<const Format12Group*>(aBuf + sizeof(Format12CmapHeader));
michael@0 121
michael@0 122 // Check that groups are in correct order and do not overlap,
michael@0 123 // and record character coverage in aCharacterMap.
michael@0 124 uint32_t prevEndCharCode = 0;
michael@0 125 for (uint32_t i = 0; i < numGroups; i++, group++) {
michael@0 126 uint32_t startCharCode = group->startCharCode;
michael@0 127 const uint32_t endCharCode = group->endCharCode;
michael@0 128 NS_ENSURE_TRUE((prevEndCharCode < startCharCode || i == 0) &&
michael@0 129 startCharCode <= endCharCode &&
michael@0 130 endCharCode <= CMAP_MAX_CODEPOINT,
michael@0 131 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 132 // don't include a character that maps to glyph ID 0 (.notdef)
michael@0 133 if (group->startGlyphId == 0) {
michael@0 134 startCharCode++;
michael@0 135 }
michael@0 136 if (startCharCode <= endCharCode) {
michael@0 137 aCharacterMap.SetRange(startCharCode, endCharCode);
michael@0 138 }
michael@0 139 prevEndCharCode = endCharCode;
michael@0 140 }
michael@0 141
michael@0 142 aCharacterMap.Compact();
michael@0 143
michael@0 144 return NS_OK;
michael@0 145 }
michael@0 146
michael@0 147 nsresult
michael@0 148 gfxFontUtils::ReadCMAPTableFormat4(const uint8_t *aBuf, uint32_t aLength,
michael@0 149 gfxSparseBitSet& aCharacterMap)
michael@0 150 {
michael@0 151 enum {
michael@0 152 OffsetFormat = 0,
michael@0 153 OffsetLength = 2,
michael@0 154 OffsetLanguage = 4,
michael@0 155 OffsetSegCountX2 = 6
michael@0 156 };
michael@0 157
michael@0 158 NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 4,
michael@0 159 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 160 uint16_t tablelen = ReadShortAt(aBuf, OffsetLength);
michael@0 161 NS_ENSURE_TRUE(tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 162 NS_ENSURE_TRUE(tablelen > 16, NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 163
michael@0 164 // This field should normally (except for Mac platform subtables) be zero according to
michael@0 165 // the OT spec, but some buggy fonts have lang = 1 (which would be English for MacOS).
michael@0 166 // E.g. Arial Narrow Bold, v. 1.1 (Tiger), Arial Unicode MS (see bug 530614).
michael@0 167 // So accept either zero or one here; the error should be harmless.
michael@0 168 NS_ENSURE_TRUE((ReadShortAt(aBuf, OffsetLanguage) & 0xfffe) == 0,
michael@0 169 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 170
michael@0 171 uint16_t segCountX2 = ReadShortAt(aBuf, OffsetSegCountX2);
michael@0 172 NS_ENSURE_TRUE(tablelen >= 16 + (segCountX2 * 4),
michael@0 173 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 174
michael@0 175 const uint16_t segCount = segCountX2 / 2;
michael@0 176
michael@0 177 const uint16_t *endCounts = reinterpret_cast<const uint16_t*>(aBuf + 14);
michael@0 178 const uint16_t *startCounts = endCounts + 1 /* skip one uint16_t for reservedPad */ + segCount;
michael@0 179 const uint16_t *idDeltas = startCounts + segCount;
michael@0 180 const uint16_t *idRangeOffsets = idDeltas + segCount;
michael@0 181 uint16_t prevEndCount = 0;
michael@0 182 for (uint16_t i = 0; i < segCount; i++) {
michael@0 183 const uint16_t endCount = ReadShortAt16(endCounts, i);
michael@0 184 const uint16_t startCount = ReadShortAt16(startCounts, i);
michael@0 185 const uint16_t idRangeOffset = ReadShortAt16(idRangeOffsets, i);
michael@0 186
michael@0 187 // sanity-check range
michael@0 188 // This permits ranges to overlap by 1 character, which is strictly
michael@0 189 // incorrect but occurs in Baskerville on OS X 10.7 (see bug 689087),
michael@0 190 // and appears to be harmless in practice
michael@0 191 NS_ENSURE_TRUE(startCount >= prevEndCount && startCount <= endCount,
michael@0 192 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 193 prevEndCount = endCount;
michael@0 194
michael@0 195 if (idRangeOffset == 0) {
michael@0 196 // figure out if there's a code in the range that would map to
michael@0 197 // glyph ID 0 (.notdef); if so, we need to skip setting that
michael@0 198 // character code in the map
michael@0 199 const uint16_t skipCode = 65536 - ReadShortAt16(idDeltas, i);
michael@0 200 if (startCount < skipCode) {
michael@0 201 aCharacterMap.SetRange(startCount,
michael@0 202 std::min<uint16_t>(skipCode - 1,
michael@0 203 endCount));
michael@0 204 }
michael@0 205 if (skipCode < endCount) {
michael@0 206 aCharacterMap.SetRange(std::max<uint16_t>(startCount,
michael@0 207 skipCode + 1),
michael@0 208 endCount);
michael@0 209 }
michael@0 210 } else {
michael@0 211 // const uint16_t idDelta = ReadShortAt16(idDeltas, i); // Unused: self-documenting.
michael@0 212 for (uint32_t c = startCount; c <= endCount; ++c) {
michael@0 213 if (c == 0xFFFF)
michael@0 214 break;
michael@0 215
michael@0 216 const uint16_t *gdata = (idRangeOffset/2
michael@0 217 + (c - startCount)
michael@0 218 + &idRangeOffsets[i]);
michael@0 219
michael@0 220 NS_ENSURE_TRUE((uint8_t*)gdata > aBuf &&
michael@0 221 (uint8_t*)gdata < aBuf + aLength,
michael@0 222 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 223
michael@0 224 // make sure we have a glyph
michael@0 225 if (*gdata != 0) {
michael@0 226 // The glyph index at this point is:
michael@0 227 uint16_t glyph = ReadShortAt16(idDeltas, i) + *gdata;
michael@0 228 if (glyph) {
michael@0 229 aCharacterMap.set(c);
michael@0 230 }
michael@0 231 }
michael@0 232 }
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 aCharacterMap.Compact();
michael@0 237
michael@0 238 return NS_OK;
michael@0 239 }
michael@0 240
michael@0 241 nsresult
michael@0 242 gfxFontUtils::ReadCMAPTableFormat14(const uint8_t *aBuf, uint32_t aLength,
michael@0 243 uint8_t*& aTable)
michael@0 244 {
michael@0 245 enum {
michael@0 246 OffsetFormat = 0,
michael@0 247 OffsetTableLength = 2,
michael@0 248 OffsetNumVarSelectorRecords = 6,
michael@0 249 OffsetVarSelectorRecords = 10,
michael@0 250
michael@0 251 SizeOfVarSelectorRecord = 11,
michael@0 252 VSRecOffsetVarSelector = 0,
michael@0 253 VSRecOffsetDefUVSOffset = 3,
michael@0 254 VSRecOffsetNonDefUVSOffset = 7,
michael@0 255
michael@0 256 SizeOfDefUVSTable = 4,
michael@0 257 DefUVSOffsetStartUnicodeValue = 0,
michael@0 258 DefUVSOffsetAdditionalCount = 3,
michael@0 259
michael@0 260 SizeOfNonDefUVSTable = 5,
michael@0 261 NonDefUVSOffsetUnicodeValue = 0,
michael@0 262 NonDefUVSOffsetGlyphID = 3
michael@0 263 };
michael@0 264 NS_ENSURE_TRUE(aLength >= OffsetVarSelectorRecords,
michael@0 265 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 266
michael@0 267 NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 14,
michael@0 268 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 269
michael@0 270 uint32_t tablelen = ReadLongAt(aBuf, OffsetTableLength);
michael@0 271 NS_ENSURE_TRUE(tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 272 NS_ENSURE_TRUE(tablelen >= OffsetVarSelectorRecords,
michael@0 273 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 274
michael@0 275 const uint32_t numVarSelectorRecords = ReadLongAt(aBuf, OffsetNumVarSelectorRecords);
michael@0 276 NS_ENSURE_TRUE((tablelen - OffsetVarSelectorRecords) /
michael@0 277 SizeOfVarSelectorRecord >= numVarSelectorRecords,
michael@0 278 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 279
michael@0 280 const uint8_t *records = aBuf + OffsetVarSelectorRecords;
michael@0 281 for (uint32_t i = 0; i < numVarSelectorRecords;
michael@0 282 i++, records += SizeOfVarSelectorRecord) {
michael@0 283 const uint32_t varSelector = ReadUint24At(records, VSRecOffsetVarSelector);
michael@0 284 const uint32_t defUVSOffset = ReadLongAt(records, VSRecOffsetDefUVSOffset);
michael@0 285 const uint32_t nonDefUVSOffset = ReadLongAt(records, VSRecOffsetNonDefUVSOffset);
michael@0 286 NS_ENSURE_TRUE(varSelector <= CMAP_MAX_CODEPOINT &&
michael@0 287 defUVSOffset <= tablelen - 4 &&
michael@0 288 nonDefUVSOffset <= tablelen - 4,
michael@0 289 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 290
michael@0 291 if (defUVSOffset) {
michael@0 292 const uint32_t numUnicodeValueRanges = ReadLongAt(aBuf, defUVSOffset);
michael@0 293 NS_ENSURE_TRUE((tablelen - defUVSOffset) /
michael@0 294 SizeOfDefUVSTable >= numUnicodeValueRanges,
michael@0 295 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 296 const uint8_t *tables = aBuf + defUVSOffset + 4;
michael@0 297 uint32_t prevEndUnicode = 0;
michael@0 298 for (uint32_t j = 0; j < numUnicodeValueRanges; j++, tables += SizeOfDefUVSTable) {
michael@0 299 const uint32_t startUnicode = ReadUint24At(tables, DefUVSOffsetStartUnicodeValue);
michael@0 300 const uint32_t endUnicode = startUnicode + tables[DefUVSOffsetAdditionalCount];
michael@0 301 NS_ENSURE_TRUE((prevEndUnicode < startUnicode || j == 0) &&
michael@0 302 endUnicode <= CMAP_MAX_CODEPOINT,
michael@0 303 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 304 prevEndUnicode = endUnicode;
michael@0 305 }
michael@0 306 }
michael@0 307
michael@0 308 if (nonDefUVSOffset) {
michael@0 309 const uint32_t numUVSMappings = ReadLongAt(aBuf, nonDefUVSOffset);
michael@0 310 NS_ENSURE_TRUE((tablelen - nonDefUVSOffset) /
michael@0 311 SizeOfNonDefUVSTable >= numUVSMappings,
michael@0 312 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 313 const uint8_t *tables = aBuf + nonDefUVSOffset + 4;
michael@0 314 uint32_t prevUnicode = 0;
michael@0 315 for (uint32_t j = 0; j < numUVSMappings; j++, tables += SizeOfNonDefUVSTable) {
michael@0 316 const uint32_t unicodeValue = ReadUint24At(tables, NonDefUVSOffsetUnicodeValue);
michael@0 317 NS_ENSURE_TRUE((prevUnicode < unicodeValue || j == 0) &&
michael@0 318 unicodeValue <= CMAP_MAX_CODEPOINT,
michael@0 319 NS_ERROR_GFX_CMAP_MALFORMED);
michael@0 320 prevUnicode = unicodeValue;
michael@0 321 }
michael@0 322 }
michael@0 323 }
michael@0 324
michael@0 325 aTable = new uint8_t[tablelen];
michael@0 326 memcpy(aTable, aBuf, tablelen);
michael@0 327
michael@0 328 return NS_OK;
michael@0 329 }
michael@0 330
michael@0 331 // Windows requires fonts to have a format-4 cmap with a Microsoft ID (3). On the Mac, fonts either have
michael@0 332 // a format-4 cmap with Microsoft platform/encoding id or they have one with a platformID == Unicode (0)
michael@0 333 // For fonts with two format-4 tables, the first one (Unicode platform) is preferred on the Mac.
michael@0 334
michael@0 335 #if defined(XP_MACOSX)
michael@0 336 #define acceptableFormat4(p,e,k) (((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDMicrosoft && !(k)) || \
michael@0 337 ((p) == PLATFORM_ID_UNICODE))
michael@0 338
michael@0 339 #define acceptableUCS4Encoding(p, e, k) \
michael@0 340 (((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDUCS4ForMicrosoftPlatform) && (k) != 12 || \
michael@0 341 ((p) == PLATFORM_ID_UNICODE && \
michael@0 342 ((e) == EncodingIDDefaultForUnicodePlatform || (e) >= EncodingIDUCS4ForUnicodePlatform)))
michael@0 343 #else
michael@0 344 #define acceptableFormat4(p,e,k) ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDMicrosoft)
michael@0 345
michael@0 346 #define acceptableUCS4Encoding(p, e, k) \
michael@0 347 ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDUCS4ForMicrosoftPlatform)
michael@0 348 #endif
michael@0 349
michael@0 350 #define acceptablePlatform(p) ((p) == PLATFORM_ID_UNICODE || (p) == PLATFORM_ID_MICROSOFT)
michael@0 351 #define isSymbol(p,e) ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDSymbol)
michael@0 352 #define isUVSEncoding(p, e) ((p) == PLATFORM_ID_UNICODE && (e) == EncodingIDUVSForUnicodePlatform)
michael@0 353
michael@0 354 uint32_t
michael@0 355 gfxFontUtils::FindPreferredSubtable(const uint8_t *aBuf, uint32_t aBufLength,
michael@0 356 uint32_t *aTableOffset,
michael@0 357 uint32_t *aUVSTableOffset,
michael@0 358 bool *aSymbolEncoding)
michael@0 359 {
michael@0 360 enum {
michael@0 361 OffsetVersion = 0,
michael@0 362 OffsetNumTables = 2,
michael@0 363 SizeOfHeader = 4,
michael@0 364
michael@0 365 TableOffsetPlatformID = 0,
michael@0 366 TableOffsetEncodingID = 2,
michael@0 367 TableOffsetOffset = 4,
michael@0 368 SizeOfTable = 8,
michael@0 369
michael@0 370 SubtableOffsetFormat = 0
michael@0 371 };
michael@0 372 enum {
michael@0 373 EncodingIDSymbol = 0,
michael@0 374 EncodingIDMicrosoft = 1,
michael@0 375 EncodingIDDefaultForUnicodePlatform = 0,
michael@0 376 EncodingIDUCS4ForUnicodePlatform = 3,
michael@0 377 EncodingIDUVSForUnicodePlatform = 5,
michael@0 378 EncodingIDUCS4ForMicrosoftPlatform = 10
michael@0 379 };
michael@0 380
michael@0 381 if (aUVSTableOffset) {
michael@0 382 *aUVSTableOffset = 0;
michael@0 383 }
michael@0 384
michael@0 385 if (!aBuf || aBufLength < SizeOfHeader) {
michael@0 386 // cmap table is missing, or too small to contain header fields!
michael@0 387 return 0;
michael@0 388 }
michael@0 389
michael@0 390 // uint16_t version = ReadShortAt(aBuf, OffsetVersion); // Unused: self-documenting.
michael@0 391 uint16_t numTables = ReadShortAt(aBuf, OffsetNumTables);
michael@0 392 if (aBufLength < uint32_t(SizeOfHeader + numTables * SizeOfTable)) {
michael@0 393 return 0;
michael@0 394 }
michael@0 395
michael@0 396 // save the format we want here
michael@0 397 uint32_t keepFormat = 0;
michael@0 398
michael@0 399 const uint8_t *table = aBuf + SizeOfHeader;
michael@0 400 for (uint16_t i = 0; i < numTables; ++i, table += SizeOfTable) {
michael@0 401 const uint16_t platformID = ReadShortAt(table, TableOffsetPlatformID);
michael@0 402 if (!acceptablePlatform(platformID))
michael@0 403 continue;
michael@0 404
michael@0 405 const uint16_t encodingID = ReadShortAt(table, TableOffsetEncodingID);
michael@0 406 const uint32_t offset = ReadLongAt(table, TableOffsetOffset);
michael@0 407 if (aBufLength - 2 < offset) {
michael@0 408 // this subtable is not valid - beyond end of buffer
michael@0 409 return 0;
michael@0 410 }
michael@0 411
michael@0 412 const uint8_t *subtable = aBuf + offset;
michael@0 413 const uint16_t format = ReadShortAt(subtable, SubtableOffsetFormat);
michael@0 414
michael@0 415 if (isSymbol(platformID, encodingID)) {
michael@0 416 keepFormat = format;
michael@0 417 *aTableOffset = offset;
michael@0 418 *aSymbolEncoding = true;
michael@0 419 break;
michael@0 420 } else if (format == 4 && acceptableFormat4(platformID, encodingID, keepFormat)) {
michael@0 421 keepFormat = format;
michael@0 422 *aTableOffset = offset;
michael@0 423 *aSymbolEncoding = false;
michael@0 424 } else if (format == 12 && acceptableUCS4Encoding(platformID, encodingID, keepFormat)) {
michael@0 425 keepFormat = format;
michael@0 426 *aTableOffset = offset;
michael@0 427 *aSymbolEncoding = false;
michael@0 428 if (platformID > PLATFORM_ID_UNICODE || !aUVSTableOffset || *aUVSTableOffset) {
michael@0 429 break; // we don't want to try anything else when this format is available.
michael@0 430 }
michael@0 431 } else if (format == 14 && isUVSEncoding(platformID, encodingID) && aUVSTableOffset) {
michael@0 432 *aUVSTableOffset = offset;
michael@0 433 if (keepFormat == 12) {
michael@0 434 break;
michael@0 435 }
michael@0 436 }
michael@0 437 }
michael@0 438
michael@0 439 return keepFormat;
michael@0 440 }
michael@0 441
michael@0 442 nsresult
michael@0 443 gfxFontUtils::ReadCMAP(const uint8_t *aBuf, uint32_t aBufLength,
michael@0 444 gfxSparseBitSet& aCharacterMap,
michael@0 445 uint32_t& aUVSOffset,
michael@0 446 bool& aUnicodeFont, bool& aSymbolFont)
michael@0 447 {
michael@0 448 uint32_t offset;
michael@0 449 bool symbol;
michael@0 450 uint32_t format = FindPreferredSubtable(aBuf, aBufLength,
michael@0 451 &offset, &aUVSOffset, &symbol);
michael@0 452
michael@0 453 if (format == 4) {
michael@0 454 if (symbol) {
michael@0 455 aUnicodeFont = false;
michael@0 456 aSymbolFont = true;
michael@0 457 } else {
michael@0 458 aUnicodeFont = true;
michael@0 459 aSymbolFont = false;
michael@0 460 }
michael@0 461 return ReadCMAPTableFormat4(aBuf + offset, aBufLength - offset,
michael@0 462 aCharacterMap);
michael@0 463 }
michael@0 464
michael@0 465 if (format == 12) {
michael@0 466 aUnicodeFont = true;
michael@0 467 aSymbolFont = false;
michael@0 468 return ReadCMAPTableFormat12(aBuf + offset, aBufLength - offset,
michael@0 469 aCharacterMap);
michael@0 470 }
michael@0 471
michael@0 472 return NS_ERROR_FAILURE;
michael@0 473 }
michael@0 474
michael@0 475 #pragma pack(1)
michael@0 476
michael@0 477 typedef struct {
michael@0 478 AutoSwap_PRUint16 format;
michael@0 479 AutoSwap_PRUint16 length;
michael@0 480 AutoSwap_PRUint16 language;
michael@0 481 AutoSwap_PRUint16 segCountX2;
michael@0 482 AutoSwap_PRUint16 searchRange;
michael@0 483 AutoSwap_PRUint16 entrySelector;
michael@0 484 AutoSwap_PRUint16 rangeShift;
michael@0 485
michael@0 486 AutoSwap_PRUint16 arrays[1];
michael@0 487 } Format4Cmap;
michael@0 488
michael@0 489 typedef struct {
michael@0 490 AutoSwap_PRUint16 format;
michael@0 491 AutoSwap_PRUint32 length;
michael@0 492 AutoSwap_PRUint32 numVarSelectorRecords;
michael@0 493
michael@0 494 typedef struct {
michael@0 495 AutoSwap_PRUint24 varSelector;
michael@0 496 AutoSwap_PRUint32 defaultUVSOffset;
michael@0 497 AutoSwap_PRUint32 nonDefaultUVSOffset;
michael@0 498 } VarSelectorRecord;
michael@0 499
michael@0 500 VarSelectorRecord varSelectorRecords[1];
michael@0 501 } Format14Cmap;
michael@0 502
michael@0 503 typedef struct {
michael@0 504 AutoSwap_PRUint32 numUVSMappings;
michael@0 505
michael@0 506 typedef struct {
michael@0 507 AutoSwap_PRUint24 unicodeValue;
michael@0 508 AutoSwap_PRUint16 glyphID;
michael@0 509 } UVSMapping;
michael@0 510
michael@0 511 UVSMapping uvsMappings[1];
michael@0 512 } NonDefUVSTable;
michael@0 513
michael@0 514 #pragma pack()
michael@0 515
michael@0 516 uint32_t
michael@0 517 gfxFontUtils::MapCharToGlyphFormat4(const uint8_t *aBuf, char16_t aCh)
michael@0 518 {
michael@0 519 const Format4Cmap *cmap4 = reinterpret_cast<const Format4Cmap*>(aBuf);
michael@0 520 uint16_t segCount;
michael@0 521 const AutoSwap_PRUint16 *endCodes;
michael@0 522 const AutoSwap_PRUint16 *startCodes;
michael@0 523 const AutoSwap_PRUint16 *idDelta;
michael@0 524 const AutoSwap_PRUint16 *idRangeOffset;
michael@0 525 uint16_t probe;
michael@0 526 uint16_t rangeShiftOver2;
michael@0 527 uint16_t index;
michael@0 528
michael@0 529 segCount = (uint16_t)(cmap4->segCountX2) / 2;
michael@0 530
michael@0 531 endCodes = &cmap4->arrays[0];
michael@0 532 startCodes = &cmap4->arrays[segCount + 1]; // +1 for reserved word between arrays
michael@0 533 idDelta = &startCodes[segCount];
michael@0 534 idRangeOffset = &idDelta[segCount];
michael@0 535
michael@0 536 probe = 1 << (uint16_t)(cmap4->entrySelector);
michael@0 537 rangeShiftOver2 = (uint16_t)(cmap4->rangeShift) / 2;
michael@0 538
michael@0 539 if ((uint16_t)(startCodes[rangeShiftOver2]) <= aCh) {
michael@0 540 index = rangeShiftOver2;
michael@0 541 } else {
michael@0 542 index = 0;
michael@0 543 }
michael@0 544
michael@0 545 while (probe > 1) {
michael@0 546 probe >>= 1;
michael@0 547 if ((uint16_t)(startCodes[index + probe]) <= aCh) {
michael@0 548 index += probe;
michael@0 549 }
michael@0 550 }
michael@0 551
michael@0 552 if (aCh >= (uint16_t)(startCodes[index]) && aCh <= (uint16_t)(endCodes[index])) {
michael@0 553 uint16_t result;
michael@0 554 if ((uint16_t)(idRangeOffset[index]) == 0) {
michael@0 555 result = aCh;
michael@0 556 } else {
michael@0 557 uint16_t offset = aCh - (uint16_t)(startCodes[index]);
michael@0 558 const AutoSwap_PRUint16 *glyphIndexTable =
michael@0 559 (const AutoSwap_PRUint16*)((const char*)&idRangeOffset[index] +
michael@0 560 (uint16_t)(idRangeOffset[index]));
michael@0 561 result = glyphIndexTable[offset];
michael@0 562 }
michael@0 563
michael@0 564 // note that this is unsigned 16-bit arithmetic, and may wrap around
michael@0 565 result += (uint16_t)(idDelta[index]);
michael@0 566 return result;
michael@0 567 }
michael@0 568
michael@0 569 return 0;
michael@0 570 }
michael@0 571
michael@0 572 uint32_t
michael@0 573 gfxFontUtils::MapCharToGlyphFormat12(const uint8_t *aBuf, uint32_t aCh)
michael@0 574 {
michael@0 575 const Format12CmapHeader *cmap12 =
michael@0 576 reinterpret_cast<const Format12CmapHeader*>(aBuf);
michael@0 577
michael@0 578 // We know that numGroups is within range for the subtable size
michael@0 579 // because it was checked by ReadCMAPTableFormat12.
michael@0 580 uint32_t numGroups = cmap12->numGroups;
michael@0 581
michael@0 582 // The array of groups immediately follows the subtable header.
michael@0 583 const Format12Group *groups =
michael@0 584 reinterpret_cast<const Format12Group*>(aBuf + sizeof(Format12CmapHeader));
michael@0 585
michael@0 586 // For most efficient binary search, we want to work on a range that
michael@0 587 // is a power of 2 so that we can always halve it by shifting.
michael@0 588 // So we find the largest power of 2 that is <= numGroups.
michael@0 589 // We will offset this range by rangeOffset so as to reach the end
michael@0 590 // of the table, provided that doesn't put us beyond the target
michael@0 591 // value from the outset.
michael@0 592 uint32_t powerOf2 = mozilla::FindHighestBit(numGroups);
michael@0 593 uint32_t rangeOffset = numGroups - powerOf2;
michael@0 594 uint32_t range = 0;
michael@0 595 uint32_t startCharCode;
michael@0 596
michael@0 597 if (groups[rangeOffset].startCharCode <= aCh) {
michael@0 598 range = rangeOffset;
michael@0 599 }
michael@0 600
michael@0 601 // Repeatedly halve the size of the range until we find the target group
michael@0 602 while (powerOf2 > 1) {
michael@0 603 powerOf2 >>= 1;
michael@0 604 if (groups[range + powerOf2].startCharCode <= aCh) {
michael@0 605 range += powerOf2;
michael@0 606 }
michael@0 607 }
michael@0 608
michael@0 609 // Check if the character is actually present in the range and return
michael@0 610 // the corresponding glyph ID
michael@0 611 startCharCode = groups[range].startCharCode;
michael@0 612 if (startCharCode <= aCh && groups[range].endCharCode >= aCh) {
michael@0 613 return groups[range].startGlyphId + aCh - startCharCode;
michael@0 614 }
michael@0 615
michael@0 616 // Else it's not present, so return the .notdef glyph
michael@0 617 return 0;
michael@0 618 }
michael@0 619
michael@0 620 uint16_t
michael@0 621 gfxFontUtils::MapUVSToGlyphFormat14(const uint8_t *aBuf, uint32_t aCh, uint32_t aVS)
michael@0 622 {
michael@0 623 const Format14Cmap *cmap14 = reinterpret_cast<const Format14Cmap*>(aBuf);
michael@0 624
michael@0 625 // binary search in varSelectorRecords
michael@0 626 uint32_t min = 0;
michael@0 627 uint32_t max = cmap14->numVarSelectorRecords;
michael@0 628 uint32_t nonDefUVSOffset = 0;
michael@0 629 while (min < max) {
michael@0 630 uint32_t index = (min + max) >> 1;
michael@0 631 uint32_t varSelector = cmap14->varSelectorRecords[index].varSelector;
michael@0 632 if (aVS == varSelector) {
michael@0 633 nonDefUVSOffset = cmap14->varSelectorRecords[index].nonDefaultUVSOffset;
michael@0 634 break;
michael@0 635 }
michael@0 636 if (aVS < varSelector) {
michael@0 637 max = index;
michael@0 638 } else {
michael@0 639 min = index + 1;
michael@0 640 }
michael@0 641 }
michael@0 642 if (!nonDefUVSOffset) {
michael@0 643 return 0;
michael@0 644 }
michael@0 645
michael@0 646 const NonDefUVSTable *table = reinterpret_cast<const NonDefUVSTable*>
michael@0 647 (aBuf + nonDefUVSOffset);
michael@0 648
michael@0 649 // binary search in uvsMappings
michael@0 650 min = 0;
michael@0 651 max = table->numUVSMappings;
michael@0 652 while (min < max) {
michael@0 653 uint32_t index = (min + max) >> 1;
michael@0 654 uint32_t unicodeValue = table->uvsMappings[index].unicodeValue;
michael@0 655 if (aCh == unicodeValue) {
michael@0 656 return table->uvsMappings[index].glyphID;
michael@0 657 }
michael@0 658 if (aCh < unicodeValue) {
michael@0 659 max = index;
michael@0 660 } else {
michael@0 661 min = index + 1;
michael@0 662 }
michael@0 663 }
michael@0 664
michael@0 665 return 0;
michael@0 666 }
michael@0 667
michael@0 668 uint32_t
michael@0 669 gfxFontUtils::MapCharToGlyph(const uint8_t *aCmapBuf, uint32_t aBufLength,
michael@0 670 uint32_t aUnicode, uint32_t aVarSelector)
michael@0 671 {
michael@0 672 uint32_t offset, uvsOffset;
michael@0 673 bool symbol;
michael@0 674 uint32_t format = FindPreferredSubtable(aCmapBuf, aBufLength, &offset,
michael@0 675 &uvsOffset, &symbol);
michael@0 676
michael@0 677 uint32_t gid;
michael@0 678 switch (format) {
michael@0 679 case 4:
michael@0 680 gid = aUnicode < UNICODE_BMP_LIMIT ?
michael@0 681 MapCharToGlyphFormat4(aCmapBuf + offset, char16_t(aUnicode)) : 0;
michael@0 682 break;
michael@0 683 case 12:
michael@0 684 gid = MapCharToGlyphFormat12(aCmapBuf + offset, aUnicode);
michael@0 685 break;
michael@0 686 default:
michael@0 687 NS_WARNING("unsupported cmap format, glyphs will be missing");
michael@0 688 gid = 0;
michael@0 689 }
michael@0 690
michael@0 691 if (aVarSelector && uvsOffset && gid) {
michael@0 692 uint32_t varGID =
michael@0 693 gfxFontUtils::MapUVSToGlyphFormat14(aCmapBuf + uvsOffset,
michael@0 694 aUnicode, aVarSelector);
michael@0 695 if (!varGID) {
michael@0 696 aUnicode = gfxFontUtils::GetUVSFallback(aUnicode, aVarSelector);
michael@0 697 if (aUnicode) {
michael@0 698 switch (format) {
michael@0 699 case 4:
michael@0 700 if (aUnicode < UNICODE_BMP_LIMIT) {
michael@0 701 varGID = MapCharToGlyphFormat4(aCmapBuf + offset,
michael@0 702 char16_t(aUnicode));
michael@0 703 }
michael@0 704 break;
michael@0 705 case 12:
michael@0 706 varGID = MapCharToGlyphFormat12(aCmapBuf + offset,
michael@0 707 aUnicode);
michael@0 708 break;
michael@0 709 }
michael@0 710 }
michael@0 711 }
michael@0 712 if (varGID) {
michael@0 713 gid = varGID;
michael@0 714 }
michael@0 715
michael@0 716 // else the variation sequence was not supported, use default mapping
michael@0 717 // of the character code alone
michael@0 718 }
michael@0 719
michael@0 720 return gid;
michael@0 721 }
michael@0 722
michael@0 723 void gfxFontUtils::GetPrefsFontList(const char *aPrefName, nsTArray<nsString>& aFontList)
michael@0 724 {
michael@0 725 const char16_t kComma = char16_t(',');
michael@0 726
michael@0 727 aFontList.Clear();
michael@0 728
michael@0 729 // get the list of single-face font families
michael@0 730 nsAdoptingString fontlistValue = Preferences::GetString(aPrefName);
michael@0 731 if (!fontlistValue) {
michael@0 732 return;
michael@0 733 }
michael@0 734
michael@0 735 // append each font name to the list
michael@0 736 nsAutoString fontname;
michael@0 737 const char16_t *p, *p_end;
michael@0 738 fontlistValue.BeginReading(p);
michael@0 739 fontlistValue.EndReading(p_end);
michael@0 740
michael@0 741 while (p < p_end) {
michael@0 742 const char16_t *nameStart = p;
michael@0 743 while (++p != p_end && *p != kComma)
michael@0 744 /* nothing */ ;
michael@0 745
michael@0 746 // pull out a single name and clean out leading/trailing whitespace
michael@0 747 fontname = Substring(nameStart, p);
michael@0 748 fontname.CompressWhitespace(true, true);
michael@0 749
michael@0 750 // append it to the list
michael@0 751 aFontList.AppendElement(fontname);
michael@0 752 ++p;
michael@0 753 }
michael@0 754
michael@0 755 }
michael@0 756
michael@0 757 // produce a unique font name that is (1) a valid Postscript name and (2) less
michael@0 758 // than 31 characters in length. Using AddFontMemResourceEx on Windows fails
michael@0 759 // for names longer than 30 characters in length.
michael@0 760
michael@0 761 #define MAX_B64_LEN 32
michael@0 762
michael@0 763 nsresult gfxFontUtils::MakeUniqueUserFontName(nsAString& aName)
michael@0 764 {
michael@0 765 nsCOMPtr<nsIUUIDGenerator> uuidgen =
michael@0 766 do_GetService("@mozilla.org/uuid-generator;1");
michael@0 767 NS_ENSURE_TRUE(uuidgen, NS_ERROR_OUT_OF_MEMORY);
michael@0 768
michael@0 769 nsID guid;
michael@0 770
michael@0 771 NS_ASSERTION(sizeof(guid) * 2 <= MAX_B64_LEN, "size of nsID has changed!");
michael@0 772
michael@0 773 nsresult rv = uuidgen->GenerateUUIDInPlace(&guid);
michael@0 774 NS_ENSURE_SUCCESS(rv, rv);
michael@0 775
michael@0 776 char guidB64[MAX_B64_LEN] = {0};
michael@0 777
michael@0 778 if (!PL_Base64Encode(reinterpret_cast<char*>(&guid), sizeof(guid), guidB64))
michael@0 779 return NS_ERROR_FAILURE;
michael@0 780
michael@0 781 // all b64 characters except for '/' are allowed in Postscript names, so convert / ==> -
michael@0 782 char *p;
michael@0 783 for (p = guidB64; *p; p++) {
michael@0 784 if (*p == '/')
michael@0 785 *p = '-';
michael@0 786 }
michael@0 787
michael@0 788 aName.Assign(NS_LITERAL_STRING("uf"));
michael@0 789 aName.AppendASCII(guidB64);
michael@0 790 return NS_OK;
michael@0 791 }
michael@0 792
michael@0 793
michael@0 794 // TrueType/OpenType table handling code
michael@0 795
michael@0 796 // need byte aligned structs
michael@0 797 #pragma pack(1)
michael@0 798
michael@0 799 // name table stores set of name record structures, followed by
michael@0 800 // large block containing all the strings. name record offset and length
michael@0 801 // indicates the offset and length within that block.
michael@0 802 // http://www.microsoft.com/typography/otspec/name.htm
michael@0 803 struct NameRecordData {
michael@0 804 uint32_t offset;
michael@0 805 uint32_t length;
michael@0 806 };
michael@0 807
michael@0 808 #pragma pack()
michael@0 809
michael@0 810 static bool
michael@0 811 IsValidSFNTVersion(uint32_t version)
michael@0 812 {
michael@0 813 // normally 0x00010000, CFF-style OT fonts == 'OTTO' and Apple TT fonts = 'true'
michael@0 814 // 'typ1' is also possible for old Type 1 fonts in a SFNT container but not supported
michael@0 815 return version == 0x10000 ||
michael@0 816 version == TRUETYPE_TAG('O','T','T','O') ||
michael@0 817 version == TRUETYPE_TAG('t','r','u','e');
michael@0 818 }
michael@0 819
michael@0 820 // copy and swap UTF-16 values, assume no surrogate pairs, can be in place
michael@0 821 static void
michael@0 822 CopySwapUTF16(const uint16_t *aInBuf, uint16_t *aOutBuf, uint32_t aLen)
michael@0 823 {
michael@0 824 const uint16_t *end = aInBuf + aLen;
michael@0 825 while (aInBuf < end) {
michael@0 826 uint16_t value = *aInBuf;
michael@0 827 *aOutBuf = (value >> 8) | (value & 0xff) << 8;
michael@0 828 aOutBuf++;
michael@0 829 aInBuf++;
michael@0 830 }
michael@0 831 }
michael@0 832
michael@0 833 gfxUserFontType
michael@0 834 gfxFontUtils::DetermineFontDataType(const uint8_t *aFontData, uint32_t aFontDataLength)
michael@0 835 {
michael@0 836 // test for OpenType font data
michael@0 837 // problem: EOT-Lite with 0x10000 length will look like TrueType!
michael@0 838 if (aFontDataLength >= sizeof(SFNTHeader)) {
michael@0 839 const SFNTHeader *sfntHeader = reinterpret_cast<const SFNTHeader*>(aFontData);
michael@0 840 uint32_t sfntVersion = sfntHeader->sfntVersion;
michael@0 841 if (IsValidSFNTVersion(sfntVersion)) {
michael@0 842 return GFX_USERFONT_OPENTYPE;
michael@0 843 }
michael@0 844 }
michael@0 845
michael@0 846 // test for WOFF
michael@0 847 if (aFontDataLength >= sizeof(AutoSwap_PRUint32)) {
michael@0 848 const AutoSwap_PRUint32 *version =
michael@0 849 reinterpret_cast<const AutoSwap_PRUint32*>(aFontData);
michael@0 850 if (uint32_t(*version) == TRUETYPE_TAG('w','O','F','F')) {
michael@0 851 return GFX_USERFONT_WOFF;
michael@0 852 }
michael@0 853 }
michael@0 854
michael@0 855 // tests for other formats here
michael@0 856
michael@0 857 return GFX_USERFONT_UNKNOWN;
michael@0 858 }
michael@0 859
michael@0 860 nsresult
michael@0 861 gfxFontUtils::RenameFont(const nsAString& aName, const uint8_t *aFontData,
michael@0 862 uint32_t aFontDataLength, FallibleTArray<uint8_t> *aNewFont)
michael@0 863 {
michael@0 864 NS_ASSERTION(aNewFont, "null font data array");
michael@0 865
michael@0 866 uint64_t dataLength(aFontDataLength);
michael@0 867
michael@0 868 // new name table
michael@0 869 static const uint32_t neededNameIDs[] = {NAME_ID_FAMILY,
michael@0 870 NAME_ID_STYLE,
michael@0 871 NAME_ID_UNIQUE,
michael@0 872 NAME_ID_FULL,
michael@0 873 NAME_ID_POSTSCRIPT};
michael@0 874
michael@0 875 // calculate new name table size
michael@0 876 uint16_t nameCount = ArrayLength(neededNameIDs);
michael@0 877
michael@0 878 // leave room for null-terminator
michael@0 879 uint16_t nameStrLength = (aName.Length() + 1) * sizeof(char16_t);
michael@0 880
michael@0 881 // round name table size up to 4-byte multiple
michael@0 882 uint32_t nameTableSize = (sizeof(NameHeader) +
michael@0 883 sizeof(NameRecord) * nameCount +
michael@0 884 nameStrLength +
michael@0 885 3) & ~3;
michael@0 886
michael@0 887 if (dataLength + nameTableSize > UINT32_MAX)
michael@0 888 return NS_ERROR_FAILURE;
michael@0 889
michael@0 890 // bug 505386 - need to handle unpadded font length
michael@0 891 uint32_t paddedFontDataSize = (aFontDataLength + 3) & ~3;
michael@0 892 uint32_t adjFontDataSize = paddedFontDataSize + nameTableSize;
michael@0 893
michael@0 894 // create new buffer: old font data plus new name table
michael@0 895 if (!aNewFont->AppendElements(adjFontDataSize))
michael@0 896 return NS_ERROR_OUT_OF_MEMORY;
michael@0 897
michael@0 898 // copy the old font data
michael@0 899 uint8_t *newFontData = reinterpret_cast<uint8_t*>(aNewFont->Elements());
michael@0 900
michael@0 901 // null the last four bytes in case the font length is not a multiple of 4
michael@0 902 memset(newFontData + aFontDataLength, 0, paddedFontDataSize - aFontDataLength);
michael@0 903
michael@0 904 // copy font data
michael@0 905 memcpy(newFontData, aFontData, aFontDataLength);
michael@0 906
michael@0 907 // null out the last 4 bytes for checksum calculations
michael@0 908 memset(newFontData + adjFontDataSize - 4, 0, 4);
michael@0 909
michael@0 910 NameHeader *nameHeader = reinterpret_cast<NameHeader*>(newFontData +
michael@0 911 paddedFontDataSize);
michael@0 912
michael@0 913 // -- name header
michael@0 914 nameHeader->format = 0;
michael@0 915 nameHeader->count = nameCount;
michael@0 916 nameHeader->stringOffset = sizeof(NameHeader) + nameCount * sizeof(NameRecord);
michael@0 917
michael@0 918 // -- name records
michael@0 919 uint32_t i;
michael@0 920 NameRecord *nameRecord = reinterpret_cast<NameRecord*>(nameHeader + 1);
michael@0 921
michael@0 922 for (i = 0; i < nameCount; i++, nameRecord++) {
michael@0 923 nameRecord->platformID = PLATFORM_ID_MICROSOFT;
michael@0 924 nameRecord->encodingID = ENCODING_ID_MICROSOFT_UNICODEBMP;
michael@0 925 nameRecord->languageID = LANG_ID_MICROSOFT_EN_US;
michael@0 926 nameRecord->nameID = neededNameIDs[i];
michael@0 927 nameRecord->offset = 0;
michael@0 928 nameRecord->length = nameStrLength;
michael@0 929 }
michael@0 930
michael@0 931 // -- string data, located after the name records, stored in big-endian form
michael@0 932 char16_t *strData = reinterpret_cast<char16_t*>(nameRecord);
michael@0 933
michael@0 934 mozilla::NativeEndian::copyAndSwapToBigEndian(strData,
michael@0 935 aName.BeginReading(),
michael@0 936 aName.Length());
michael@0 937 strData[aName.Length()] = 0; // add null termination
michael@0 938
michael@0 939 // adjust name table header to point to the new name table
michael@0 940 SFNTHeader *sfntHeader = reinterpret_cast<SFNTHeader*>(newFontData);
michael@0 941
michael@0 942 // table directory entries begin immediately following SFNT header
michael@0 943 TableDirEntry *dirEntry =
michael@0 944 reinterpret_cast<TableDirEntry*>(newFontData + sizeof(SFNTHeader));
michael@0 945
michael@0 946 uint32_t numTables = sfntHeader->numTables;
michael@0 947
michael@0 948 for (i = 0; i < numTables; i++, dirEntry++) {
michael@0 949 if (dirEntry->tag == TRUETYPE_TAG('n','a','m','e')) {
michael@0 950 break;
michael@0 951 }
michael@0 952 }
michael@0 953
michael@0 954 // function only called if font validates, so this should always be true
michael@0 955 NS_ASSERTION(i < numTables, "attempt to rename font with no name table");
michael@0 956
michael@0 957 // note: dirEntry now points to name record
michael@0 958
michael@0 959 // recalculate name table checksum
michael@0 960 uint32_t checkSum = 0;
michael@0 961 AutoSwap_PRUint32 *nameData = reinterpret_cast<AutoSwap_PRUint32*> (nameHeader);
michael@0 962 AutoSwap_PRUint32 *nameDataEnd = nameData + (nameTableSize >> 2);
michael@0 963
michael@0 964 while (nameData < nameDataEnd)
michael@0 965 checkSum = checkSum + *nameData++;
michael@0 966
michael@0 967 // adjust name table entry to point to new name table
michael@0 968 dirEntry->offset = paddedFontDataSize;
michael@0 969 dirEntry->length = nameTableSize;
michael@0 970 dirEntry->checkSum = checkSum;
michael@0 971
michael@0 972 // fix up checksums
michael@0 973 uint32_t checksum = 0;
michael@0 974
michael@0 975 // checksum for font = (checksum of header) + (checksum of tables)
michael@0 976 uint32_t headerLen = sizeof(SFNTHeader) + sizeof(TableDirEntry) * numTables;
michael@0 977 const AutoSwap_PRUint32 *headerData =
michael@0 978 reinterpret_cast<const AutoSwap_PRUint32*>(newFontData);
michael@0 979
michael@0 980 // header length is in bytes, checksum calculated in longwords
michael@0 981 for (i = 0; i < (headerLen >> 2); i++, headerData++) {
michael@0 982 checksum += *headerData;
michael@0 983 }
michael@0 984
michael@0 985 uint32_t headOffset = 0;
michael@0 986 dirEntry = reinterpret_cast<TableDirEntry*>(newFontData + sizeof(SFNTHeader));
michael@0 987
michael@0 988 for (i = 0; i < numTables; i++, dirEntry++) {
michael@0 989 if (dirEntry->tag == TRUETYPE_TAG('h','e','a','d')) {
michael@0 990 headOffset = dirEntry->offset;
michael@0 991 }
michael@0 992 checksum += dirEntry->checkSum;
michael@0 993 }
michael@0 994
michael@0 995 NS_ASSERTION(headOffset != 0, "no head table for font");
michael@0 996
michael@0 997 HeadTable *headData = reinterpret_cast<HeadTable*>(newFontData + headOffset);
michael@0 998
michael@0 999 headData->checkSumAdjustment = HeadTable::HEAD_CHECKSUM_CALC_CONST - checksum;
michael@0 1000
michael@0 1001 return NS_OK;
michael@0 1002 }
michael@0 1003
michael@0 1004 // This is only called after the basic validity of the downloaded sfnt
michael@0 1005 // data has been checked, so it should never fail to find the name table
michael@0 1006 // (though it might fail to read it, if memory isn't available);
michael@0 1007 // other checks here are just for extra paranoia.
michael@0 1008 nsresult
michael@0 1009 gfxFontUtils::GetFullNameFromSFNT(const uint8_t* aFontData, uint32_t aLength,
michael@0 1010 nsAString& aFullName)
michael@0 1011 {
michael@0 1012 aFullName.AssignLiteral("(MISSING NAME)"); // should always get replaced
michael@0 1013
michael@0 1014 NS_ENSURE_TRUE(aLength >= sizeof(SFNTHeader), NS_ERROR_UNEXPECTED);
michael@0 1015 const SFNTHeader *sfntHeader =
michael@0 1016 reinterpret_cast<const SFNTHeader*>(aFontData);
michael@0 1017 const TableDirEntry *dirEntry =
michael@0 1018 reinterpret_cast<const TableDirEntry*>(aFontData + sizeof(SFNTHeader));
michael@0 1019 uint32_t numTables = sfntHeader->numTables;
michael@0 1020 NS_ENSURE_TRUE(aLength >=
michael@0 1021 sizeof(SFNTHeader) + numTables * sizeof(TableDirEntry),
michael@0 1022 NS_ERROR_UNEXPECTED);
michael@0 1023 bool foundName = false;
michael@0 1024 for (uint32_t i = 0; i < numTables; i++, dirEntry++) {
michael@0 1025 if (dirEntry->tag == TRUETYPE_TAG('n','a','m','e')) {
michael@0 1026 foundName = true;
michael@0 1027 break;
michael@0 1028 }
michael@0 1029 }
michael@0 1030
michael@0 1031 // should never fail, as we're only called after font validation succeeded
michael@0 1032 NS_ENSURE_TRUE(foundName, NS_ERROR_NOT_AVAILABLE);
michael@0 1033
michael@0 1034 uint32_t len = dirEntry->length;
michael@0 1035 NS_ENSURE_TRUE(aLength > len && aLength - len >= dirEntry->offset,
michael@0 1036 NS_ERROR_UNEXPECTED);
michael@0 1037
michael@0 1038 hb_blob_t *nameBlob =
michael@0 1039 hb_blob_create((const char*)aFontData + dirEntry->offset, len,
michael@0 1040 HB_MEMORY_MODE_READONLY, nullptr, nullptr);
michael@0 1041 nsresult rv = GetFullNameFromTable(nameBlob, aFullName);
michael@0 1042 hb_blob_destroy(nameBlob);
michael@0 1043
michael@0 1044 return rv;
michael@0 1045 }
michael@0 1046
michael@0 1047 nsresult
michael@0 1048 gfxFontUtils::GetFullNameFromTable(hb_blob_t *aNameTable,
michael@0 1049 nsAString& aFullName)
michael@0 1050 {
michael@0 1051 nsAutoString name;
michael@0 1052 nsresult rv =
michael@0 1053 gfxFontUtils::ReadCanonicalName(aNameTable,
michael@0 1054 gfxFontUtils::NAME_ID_FULL,
michael@0 1055 name);
michael@0 1056 if (NS_SUCCEEDED(rv) && !name.IsEmpty()) {
michael@0 1057 aFullName = name;
michael@0 1058 return NS_OK;
michael@0 1059 }
michael@0 1060 rv = gfxFontUtils::ReadCanonicalName(aNameTable,
michael@0 1061 gfxFontUtils::NAME_ID_FAMILY,
michael@0 1062 name);
michael@0 1063 if (NS_SUCCEEDED(rv) && !name.IsEmpty()) {
michael@0 1064 nsAutoString styleName;
michael@0 1065 rv = gfxFontUtils::ReadCanonicalName(aNameTable,
michael@0 1066 gfxFontUtils::NAME_ID_STYLE,
michael@0 1067 styleName);
michael@0 1068 if (NS_SUCCEEDED(rv) && !styleName.IsEmpty()) {
michael@0 1069 name.AppendLiteral(" ");
michael@0 1070 name.Append(styleName);
michael@0 1071 aFullName = name;
michael@0 1072 }
michael@0 1073 return NS_OK;
michael@0 1074 }
michael@0 1075
michael@0 1076 return NS_ERROR_NOT_AVAILABLE;
michael@0 1077 }
michael@0 1078
michael@0 1079 nsresult
michael@0 1080 gfxFontUtils::GetFamilyNameFromTable(hb_blob_t *aNameTable,
michael@0 1081 nsAString& aFullName)
michael@0 1082 {
michael@0 1083 nsAutoString name;
michael@0 1084 nsresult rv =
michael@0 1085 gfxFontUtils::ReadCanonicalName(aNameTable,
michael@0 1086 gfxFontUtils::NAME_ID_FAMILY,
michael@0 1087 name);
michael@0 1088 if (NS_SUCCEEDED(rv) && !name.IsEmpty()) {
michael@0 1089 aFullName = name;
michael@0 1090 return NS_OK;
michael@0 1091 }
michael@0 1092 return NS_ERROR_NOT_AVAILABLE;
michael@0 1093 }
michael@0 1094
michael@0 1095 enum {
michael@0 1096 #if defined(XP_MACOSX)
michael@0 1097 CANONICAL_LANG_ID = gfxFontUtils::LANG_ID_MAC_ENGLISH,
michael@0 1098 PLATFORM_ID = gfxFontUtils::PLATFORM_ID_MAC
michael@0 1099 #else
michael@0 1100 CANONICAL_LANG_ID = gfxFontUtils::LANG_ID_MICROSOFT_EN_US,
michael@0 1101 PLATFORM_ID = gfxFontUtils::PLATFORM_ID_MICROSOFT
michael@0 1102 #endif
michael@0 1103 };
michael@0 1104
michael@0 1105 nsresult
michael@0 1106 gfxFontUtils::ReadNames(const char *aNameData, uint32_t aDataLen,
michael@0 1107 uint32_t aNameID, int32_t aPlatformID,
michael@0 1108 nsTArray<nsString>& aNames)
michael@0 1109 {
michael@0 1110 return ReadNames(aNameData, aDataLen, aNameID, LANG_ALL,
michael@0 1111 aPlatformID, aNames);
michael@0 1112 }
michael@0 1113
michael@0 1114 nsresult
michael@0 1115 gfxFontUtils::ReadCanonicalName(hb_blob_t *aNameTable, uint32_t aNameID,
michael@0 1116 nsString& aName)
michael@0 1117 {
michael@0 1118 uint32_t nameTableLen;
michael@0 1119 const char *nameTable = hb_blob_get_data(aNameTable, &nameTableLen);
michael@0 1120 return ReadCanonicalName(nameTable, nameTableLen, aNameID, aName);
michael@0 1121 }
michael@0 1122
michael@0 1123 nsresult
michael@0 1124 gfxFontUtils::ReadCanonicalName(const char *aNameData, uint32_t aDataLen,
michael@0 1125 uint32_t aNameID, nsString& aName)
michael@0 1126 {
michael@0 1127 nsresult rv;
michael@0 1128
michael@0 1129 nsTArray<nsString> names;
michael@0 1130
michael@0 1131 // first, look for the English name (this will succeed 99% of the time)
michael@0 1132 rv = ReadNames(aNameData, aDataLen, aNameID, CANONICAL_LANG_ID,
michael@0 1133 PLATFORM_ID, names);
michael@0 1134 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1135
michael@0 1136 // otherwise, grab names for all languages
michael@0 1137 if (names.Length() == 0) {
michael@0 1138 rv = ReadNames(aNameData, aDataLen, aNameID, LANG_ALL,
michael@0 1139 PLATFORM_ID, names);
michael@0 1140 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1141 }
michael@0 1142
michael@0 1143 #if defined(XP_MACOSX)
michael@0 1144 // may be dealing with font that only has Microsoft name entries
michael@0 1145 if (names.Length() == 0) {
michael@0 1146 rv = ReadNames(aNameData, aDataLen, aNameID, LANG_ID_MICROSOFT_EN_US,
michael@0 1147 PLATFORM_ID_MICROSOFT, names);
michael@0 1148 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1149
michael@0 1150 // getting really desperate now, take anything!
michael@0 1151 if (names.Length() == 0) {
michael@0 1152 rv = ReadNames(aNameData, aDataLen, aNameID, LANG_ALL,
michael@0 1153 PLATFORM_ID_MICROSOFT, names);
michael@0 1154 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1155 }
michael@0 1156 }
michael@0 1157 #endif
michael@0 1158
michael@0 1159 // return the first name (99.9% of the time names will
michael@0 1160 // contain a single English name)
michael@0 1161 if (names.Length()) {
michael@0 1162 aName.Assign(names[0]);
michael@0 1163 return NS_OK;
michael@0 1164 }
michael@0 1165
michael@0 1166 return NS_ERROR_FAILURE;
michael@0 1167 }
michael@0 1168
michael@0 1169 // Charsets to use for decoding Mac platform font names.
michael@0 1170 // This table is sorted by {encoding, language}, with the wildcard "ANY" being
michael@0 1171 // greater than any defined values for each field; we use a binary search on both
michael@0 1172 // fields, and fall back to matching only encoding if necessary
michael@0 1173
michael@0 1174 // Some "redundant" entries for specific combinations are included such as
michael@0 1175 // encoding=roman, lang=english, in order that common entries will be found
michael@0 1176 // on the first search.
michael@0 1177
michael@0 1178 #define ANY 0xffff
michael@0 1179 const gfxFontUtils::MacFontNameCharsetMapping gfxFontUtils::gMacFontNameCharsets[] =
michael@0 1180 {
michael@0 1181 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ENGLISH, "macintosh" },
michael@0 1182 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ICELANDIC, "x-mac-icelandic" },
michael@0 1183 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_TURKISH, "x-mac-turkish" },
michael@0 1184 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_POLISH, "x-mac-ce" },
michael@0 1185 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ROMANIAN, "x-mac-romanian" },
michael@0 1186 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_CZECH, "x-mac-ce" },
michael@0 1187 { ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_SLOVAK, "x-mac-ce" },
michael@0 1188 { ENCODING_ID_MAC_ROMAN, ANY, "macintosh" },
michael@0 1189 { ENCODING_ID_MAC_JAPANESE, LANG_ID_MAC_JAPANESE, "Shift_JIS" },
michael@0 1190 { ENCODING_ID_MAC_JAPANESE, ANY, "Shift_JIS" },
michael@0 1191 { ENCODING_ID_MAC_TRAD_CHINESE, LANG_ID_MAC_TRAD_CHINESE, "Big5" },
michael@0 1192 { ENCODING_ID_MAC_TRAD_CHINESE, ANY, "Big5" },
michael@0 1193 { ENCODING_ID_MAC_KOREAN, LANG_ID_MAC_KOREAN, "EUC-KR" },
michael@0 1194 { ENCODING_ID_MAC_KOREAN, ANY, "EUC-KR" },
michael@0 1195 { ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_ARABIC, "x-mac-arabic" },
michael@0 1196 { ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_URDU, "x-mac-farsi" },
michael@0 1197 { ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_FARSI, "x-mac-farsi" },
michael@0 1198 { ENCODING_ID_MAC_ARABIC, ANY, "x-mac-arabic" },
michael@0 1199 { ENCODING_ID_MAC_HEBREW, LANG_ID_MAC_HEBREW, "x-mac-hebrew" },
michael@0 1200 { ENCODING_ID_MAC_HEBREW, ANY, "x-mac-hebrew" },
michael@0 1201 { ENCODING_ID_MAC_GREEK, ANY, "x-mac-greek" },
michael@0 1202 { ENCODING_ID_MAC_CYRILLIC, ANY, "x-mac-cyrillic" },
michael@0 1203 { ENCODING_ID_MAC_DEVANAGARI, ANY, "x-mac-devanagari"},
michael@0 1204 { ENCODING_ID_MAC_GURMUKHI, ANY, "x-mac-gurmukhi" },
michael@0 1205 { ENCODING_ID_MAC_GUJARATI, ANY, "x-mac-gujarati" },
michael@0 1206 { ENCODING_ID_MAC_SIMP_CHINESE, LANG_ID_MAC_SIMP_CHINESE, "GB2312" },
michael@0 1207 { ENCODING_ID_MAC_SIMP_CHINESE, ANY, "GB2312" }
michael@0 1208 };
michael@0 1209
michael@0 1210 const char* gfxFontUtils::gISOFontNameCharsets[] =
michael@0 1211 {
michael@0 1212 /* 0 */ "us-ascii" ,
michael@0 1213 /* 1 */ nullptr , /* spec says "ISO 10646" but does not specify encoding form! */
michael@0 1214 /* 2 */ "ISO-8859-1"
michael@0 1215 };
michael@0 1216
michael@0 1217 const char* gfxFontUtils::gMSFontNameCharsets[] =
michael@0 1218 {
michael@0 1219 /* [0] ENCODING_ID_MICROSOFT_SYMBOL */ "" ,
michael@0 1220 /* [1] ENCODING_ID_MICROSOFT_UNICODEBMP */ "" ,
michael@0 1221 /* [2] ENCODING_ID_MICROSOFT_SHIFTJIS */ "Shift_JIS" ,
michael@0 1222 /* [3] ENCODING_ID_MICROSOFT_PRC */ nullptr ,
michael@0 1223 /* [4] ENCODING_ID_MICROSOFT_BIG5 */ "Big5" ,
michael@0 1224 /* [5] ENCODING_ID_MICROSOFT_WANSUNG */ nullptr ,
michael@0 1225 /* [6] ENCODING_ID_MICROSOFT_JOHAB */ "x-johab" ,
michael@0 1226 /* [7] reserved */ nullptr ,
michael@0 1227 /* [8] reserved */ nullptr ,
michael@0 1228 /* [9] reserved */ nullptr ,
michael@0 1229 /*[10] ENCODING_ID_MICROSOFT_UNICODEFULL */ ""
michael@0 1230 };
michael@0 1231
michael@0 1232 // Return the name of the charset we should use to decode a font name
michael@0 1233 // given the name table attributes.
michael@0 1234 // Special return values:
michael@0 1235 // "" charset is UTF16BE, no need for a converter
michael@0 1236 // nullptr unknown charset, do not attempt conversion
michael@0 1237 const char*
michael@0 1238 gfxFontUtils::GetCharsetForFontName(uint16_t aPlatform, uint16_t aScript, uint16_t aLanguage)
michael@0 1239 {
michael@0 1240 switch (aPlatform)
michael@0 1241 {
michael@0 1242 case PLATFORM_ID_UNICODE:
michael@0 1243 return "";
michael@0 1244
michael@0 1245 case PLATFORM_ID_MAC:
michael@0 1246 {
michael@0 1247 uint32_t lo = 0, hi = ArrayLength(gMacFontNameCharsets);
michael@0 1248 MacFontNameCharsetMapping searchValue = { aScript, aLanguage, nullptr };
michael@0 1249 for (uint32_t i = 0; i < 2; ++i) {
michael@0 1250 // binary search; if not found, set language to ANY and try again
michael@0 1251 while (lo < hi) {
michael@0 1252 uint32_t mid = (lo + hi) / 2;
michael@0 1253 const MacFontNameCharsetMapping& entry = gMacFontNameCharsets[mid];
michael@0 1254 if (entry < searchValue) {
michael@0 1255 lo = mid + 1;
michael@0 1256 continue;
michael@0 1257 }
michael@0 1258 if (searchValue < entry) {
michael@0 1259 hi = mid;
michael@0 1260 continue;
michael@0 1261 }
michael@0 1262 // found a match
michael@0 1263 return entry.mCharsetName;
michael@0 1264 }
michael@0 1265
michael@0 1266 // no match, so reset high bound for search and re-try
michael@0 1267 hi = ArrayLength(gMacFontNameCharsets);
michael@0 1268 searchValue.mLanguage = ANY;
michael@0 1269 }
michael@0 1270 }
michael@0 1271 break;
michael@0 1272
michael@0 1273 case PLATFORM_ID_ISO:
michael@0 1274 if (aScript < ArrayLength(gISOFontNameCharsets)) {
michael@0 1275 return gISOFontNameCharsets[aScript];
michael@0 1276 }
michael@0 1277 break;
michael@0 1278
michael@0 1279 case PLATFORM_ID_MICROSOFT:
michael@0 1280 if (aScript < ArrayLength(gMSFontNameCharsets)) {
michael@0 1281 return gMSFontNameCharsets[aScript];
michael@0 1282 }
michael@0 1283 break;
michael@0 1284 }
michael@0 1285
michael@0 1286 return nullptr;
michael@0 1287 }
michael@0 1288
michael@0 1289 // convert a raw name from the name table to an nsString, if possible;
michael@0 1290 // return value indicates whether conversion succeeded
michael@0 1291 bool
michael@0 1292 gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
michael@0 1293 uint32_t aPlatformCode, uint32_t aScriptCode,
michael@0 1294 uint32_t aLangCode, nsAString& aName)
michael@0 1295 {
michael@0 1296 if (aByteLen <= 0) {
michael@0 1297 NS_WARNING("empty font name");
michael@0 1298 aName.SetLength(0);
michael@0 1299 return true;
michael@0 1300 }
michael@0 1301
michael@0 1302 const char *csName = GetCharsetForFontName(aPlatformCode, aScriptCode, aLangCode);
michael@0 1303
michael@0 1304 if (!csName) {
michael@0 1305 // nullptr -> unknown charset
michael@0 1306 #ifdef DEBUG
michael@0 1307 char warnBuf[128];
michael@0 1308 if (aByteLen > 64)
michael@0 1309 aByteLen = 64;
michael@0 1310 sprintf(warnBuf, "skipping font name, unknown charset %d:%d:%d for <%.*s>",
michael@0 1311 aPlatformCode, aScriptCode, aLangCode, aByteLen, aNameData);
michael@0 1312 NS_WARNING(warnBuf);
michael@0 1313 #endif
michael@0 1314 return false;
michael@0 1315 }
michael@0 1316
michael@0 1317 if (csName[0] == 0) {
michael@0 1318 // empty charset name: data is utf16be, no need to instantiate a converter
michael@0 1319 uint32_t strLen = aByteLen / 2;
michael@0 1320 #ifdef IS_LITTLE_ENDIAN
michael@0 1321 aName.SetLength(strLen);
michael@0 1322 CopySwapUTF16(reinterpret_cast<const uint16_t*>(aNameData),
michael@0 1323 reinterpret_cast<uint16_t*>(aName.BeginWriting()), strLen);
michael@0 1324 #else
michael@0 1325 aName.Assign(reinterpret_cast<const char16_t*>(aNameData), strLen);
michael@0 1326 #endif
michael@0 1327 return true;
michael@0 1328 }
michael@0 1329
michael@0 1330 nsCOMPtr<nsIUnicodeDecoder> decoder =
michael@0 1331 mozilla::dom::EncodingUtils::DecoderForEncoding(csName);
michael@0 1332 if (!decoder) {
michael@0 1333 NS_WARNING("failed to get the decoder for a font name string");
michael@0 1334 return false;
michael@0 1335 }
michael@0 1336
michael@0 1337 int32_t destLength;
michael@0 1338 nsresult rv = decoder->GetMaxLength(aNameData, aByteLen, &destLength);
michael@0 1339 if (NS_FAILED(rv)) {
michael@0 1340 NS_WARNING("decoder->GetMaxLength failed, invalid font name?");
michael@0 1341 return false;
michael@0 1342 }
michael@0 1343
michael@0 1344 // make space for the converted string
michael@0 1345 aName.SetLength(destLength);
michael@0 1346 rv = decoder->Convert(aNameData, &aByteLen,
michael@0 1347 aName.BeginWriting(), &destLength);
michael@0 1348 if (NS_FAILED(rv)) {
michael@0 1349 NS_WARNING("decoder->Convert failed, invalid font name?");
michael@0 1350 return false;
michael@0 1351 }
michael@0 1352 aName.Truncate(destLength); // set the actual length
michael@0 1353
michael@0 1354 return true;
michael@0 1355 }
michael@0 1356
michael@0 1357 nsresult
michael@0 1358 gfxFontUtils::ReadNames(const char *aNameData, uint32_t aDataLen,
michael@0 1359 uint32_t aNameID,
michael@0 1360 int32_t aLangID, int32_t aPlatformID,
michael@0 1361 nsTArray<nsString>& aNames)
michael@0 1362 {
michael@0 1363 NS_ASSERTION(aDataLen != 0, "null name table");
michael@0 1364
michael@0 1365 if (!aDataLen) {
michael@0 1366 return NS_ERROR_FAILURE;
michael@0 1367 }
michael@0 1368
michael@0 1369 // -- name table data
michael@0 1370 const NameHeader *nameHeader = reinterpret_cast<const NameHeader*>(aNameData);
michael@0 1371
michael@0 1372 uint32_t nameCount = nameHeader->count;
michael@0 1373
michael@0 1374 // -- sanity check the number of name records
michael@0 1375 if (uint64_t(nameCount) * sizeof(NameRecord) > aDataLen) {
michael@0 1376 NS_WARNING("invalid font (name table data)");
michael@0 1377 return NS_ERROR_FAILURE;
michael@0 1378 }
michael@0 1379
michael@0 1380 // -- iterate through name records
michael@0 1381 const NameRecord *nameRecord
michael@0 1382 = reinterpret_cast<const NameRecord*>(aNameData + sizeof(NameHeader));
michael@0 1383 uint64_t nameStringsBase = uint64_t(nameHeader->stringOffset);
michael@0 1384
michael@0 1385 uint32_t i;
michael@0 1386 for (i = 0; i < nameCount; i++, nameRecord++) {
michael@0 1387 uint32_t platformID;
michael@0 1388
michael@0 1389 // skip over unwanted nameID's
michael@0 1390 if (uint32_t(nameRecord->nameID) != aNameID)
michael@0 1391 continue;
michael@0 1392
michael@0 1393 // skip over unwanted platform data
michael@0 1394 platformID = nameRecord->platformID;
michael@0 1395 if (aPlatformID != PLATFORM_ALL
michael@0 1396 && uint32_t(nameRecord->platformID) != PLATFORM_ID)
michael@0 1397 continue;
michael@0 1398
michael@0 1399 // skip over unwanted languages
michael@0 1400 if (aLangID != LANG_ALL
michael@0 1401 && uint32_t(nameRecord->languageID) != uint32_t(aLangID))
michael@0 1402 continue;
michael@0 1403
michael@0 1404 // add name to names array
michael@0 1405
michael@0 1406 // -- calculate string location
michael@0 1407 uint32_t namelen = nameRecord->length;
michael@0 1408 uint32_t nameoff = nameRecord->offset; // offset from base of string storage
michael@0 1409
michael@0 1410 if (nameStringsBase + uint64_t(nameoff) + uint64_t(namelen)
michael@0 1411 > aDataLen) {
michael@0 1412 NS_WARNING("invalid font (name table strings)");
michael@0 1413 return NS_ERROR_FAILURE;
michael@0 1414 }
michael@0 1415
michael@0 1416 // -- decode if necessary and make nsString
michael@0 1417 nsAutoString name;
michael@0 1418
michael@0 1419 DecodeFontName(aNameData + nameStringsBase + nameoff, namelen,
michael@0 1420 platformID, uint32_t(nameRecord->encodingID),
michael@0 1421 uint32_t(nameRecord->languageID), name);
michael@0 1422
michael@0 1423 uint32_t k, numNames;
michael@0 1424 bool foundName = false;
michael@0 1425
michael@0 1426 numNames = aNames.Length();
michael@0 1427 for (k = 0; k < numNames; k++) {
michael@0 1428 if (name.Equals(aNames[k])) {
michael@0 1429 foundName = true;
michael@0 1430 break;
michael@0 1431 }
michael@0 1432 }
michael@0 1433
michael@0 1434 if (!foundName)
michael@0 1435 aNames.AppendElement(name);
michael@0 1436
michael@0 1437 }
michael@0 1438
michael@0 1439 return NS_OK;
michael@0 1440 }
michael@0 1441
michael@0 1442 #ifdef XP_WIN
michael@0 1443
michael@0 1444 /* static */
michael@0 1445 bool
michael@0 1446 gfxFontUtils::IsCffFont(const uint8_t* aFontData)
michael@0 1447 {
michael@0 1448 // this is only called after aFontData has passed basic validation,
michael@0 1449 // so we know there is enough data present to allow us to read the version!
michael@0 1450 const SFNTHeader *sfntHeader = reinterpret_cast<const SFNTHeader*>(aFontData);
michael@0 1451 return (sfntHeader->sfntVersion == TRUETYPE_TAG('O','T','T','O'));
michael@0 1452 }
michael@0 1453
michael@0 1454 #endif
michael@0 1455

mercurial