--- a/gfx/thebes/gfxFontUtils.cpp
+++ b/gfx/thebes/gfxFontUtils.cpp
@@ -11,16 +11,17 @@
#include "gfxFontVariations.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/BinarySearch.h"
#include "mozilla/Sprintf.h"
+#include "mozilla/Unused.h"
#include "nsCOMPtr.h"
#include "nsIUUIDGenerator.h"
#include "mozilla/Encoding.h"
#include "harfbuzz/hb.h"
#include "plbase64.h"
@@ -85,17 +86,17 @@ gfxSparseBitSet::Dump(const char* aPrefi
uint8_t flip1 = ((bits & 0xaa) >> 1) | ((bits & 0x55) << 1);
uint8_t flip2 = ((flip1 & 0xcc) >> 2) | ((flip1 & 0x33) << 2);
uint8_t flipped = ((flip2 & 0xf0) >> 4) | ((flip2 & 0x0f) << 4);
index += snprintf(&outStr[index], BUFSIZE - index, "%2.2x", flipped);
}
if (i + 4 != 32) index += snprintf(&outStr[index], BUFSIZE - index, " ");
}
- index += snprintf(&outStr[index], BUFSIZE - index, "]");
+ Unused << snprintf(&outStr[index], BUFSIZE - index, "]");
LOG(aWhichLog, ("%s", outStr));
}
}
nsresult
gfxFontUtils::ReadCMAPTableFormat10(const uint8_t *aBuf, uint32_t aLength,
gfxSparseBitSet& aCharacterMap)
{
@@ -183,59 +184,59 @@ gfxFontUtils::ReadCMAPTableFormat12or13(
// Check that groups are in correct order and do not overlap,
// and record character coverage in aCharacterMap.
uint32_t prevEndCharCode = 0;
for (uint32_t i = 0; i < numGroups; i++, group++) {
uint32_t startCharCode = group->startCharCode;
const uint32_t endCharCode = group->endCharCode;
NS_ENSURE_TRUE((prevEndCharCode < startCharCode || i == 0) &&
startCharCode <= endCharCode &&
- endCharCode <= CMAP_MAX_CODEPOINT,
+ endCharCode <= CMAP_MAX_CODEPOINT,
NS_ERROR_GFX_CMAP_MALFORMED);
// don't include a character that maps to glyph ID 0 (.notdef)
if (group->startGlyphId == 0) {
startCharCode++;
}
if (startCharCode <= endCharCode) {
aCharacterMap.SetRange(startCharCode, endCharCode);
}
prevEndCharCode = endCharCode;
}
aCharacterMap.Compact();
return NS_OK;
}
-nsresult
+nsresult
gfxFontUtils::ReadCMAPTableFormat4(const uint8_t *aBuf, uint32_t aLength,
gfxSparseBitSet& aCharacterMap)
{
enum {
OffsetFormat = 0,
OffsetLength = 2,
OffsetLanguage = 4,
OffsetSegCountX2 = 6
};
- NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 4,
+ NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 4,
NS_ERROR_GFX_CMAP_MALFORMED);
uint16_t tablelen = ReadShortAt(aBuf, OffsetLength);
NS_ENSURE_TRUE(tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(tablelen > 16, NS_ERROR_GFX_CMAP_MALFORMED);
-
+
// This field should normally (except for Mac platform subtables) be zero according to
// the OT spec, but some buggy fonts have lang = 1 (which would be English for MacOS).
// E.g. Arial Narrow Bold, v. 1.1 (Tiger), Arial Unicode MS (see bug 530614).
// So accept either zero or one here; the error should be harmless.
- NS_ENSURE_TRUE((ReadShortAt(aBuf, OffsetLanguage) & 0xfffe) == 0,
+ NS_ENSURE_TRUE((ReadShortAt(aBuf, OffsetLanguage) & 0xfffe) == 0,
NS_ERROR_GFX_CMAP_MALFORMED);
uint16_t segCountX2 = ReadShortAt(aBuf, OffsetSegCountX2);
- NS_ENSURE_TRUE(tablelen >= 16 + (segCountX2 * 4),
+ NS_ENSURE_TRUE(tablelen >= 16 + (segCountX2 * 4),
NS_ERROR_GFX_CMAP_MALFORMED);
const uint16_t segCount = segCountX2 / 2;
const uint16_t *endCounts = reinterpret_cast<const uint16_t*>(aBuf + 14);
const uint16_t *startCounts = endCounts + 1 /* skip one uint16_t for reservedPad */ + segCount;
const uint16_t *idDeltas = startCounts + segCount;
const uint16_t *idRangeOffsets = idDeltas + segCount;
@@ -269,22 +270,22 @@ gfxFontUtils::ReadCMAPTableFormat4(const
endCount);
}
} else {
// const uint16_t idDelta = ReadShortAt16(idDeltas, i); // Unused: self-documenting.
for (uint32_t c = startCount; c <= endCount; ++c) {
if (c == 0xFFFF)
break;
- const uint16_t *gdata = (idRangeOffset/2
+ const uint16_t *gdata = (idRangeOffset/2
+ (c - startCount)
+ &idRangeOffsets[i]);
- NS_ENSURE_TRUE((uint8_t*)gdata > aBuf &&
- (uint8_t*)gdata < aBuf + aLength,
+ NS_ENSURE_TRUE((uint8_t*)gdata > aBuf &&
+ (uint8_t*)gdata < aBuf + aLength,
NS_ERROR_GFX_CMAP_MALFORMED);
// make sure we have a glyph
if (*gdata != 0) {
// The glyph index at this point is:
uint16_t glyph = ReadShortAt16(idDeltas, i) + *gdata;
if (glyph) {
aCharacterMap.set(c);
@@ -320,68 +321,68 @@ gfxFontUtils::ReadCMAPTableFormat14(cons
SizeOfNonDefUVSTable = 5,
NonDefUVSOffsetUnicodeValue = 0,
NonDefUVSOffsetGlyphID = 3
};
NS_ENSURE_TRUE(aLength >= OffsetVarSelectorRecords,
NS_ERROR_GFX_CMAP_MALFORMED);
- NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 14,
+ NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 14,
NS_ERROR_GFX_CMAP_MALFORMED);
uint32_t tablelen = ReadLongAt(aBuf, OffsetTableLength);
NS_ENSURE_TRUE(tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(tablelen >= OffsetVarSelectorRecords,
NS_ERROR_GFX_CMAP_MALFORMED);
const uint32_t numVarSelectorRecords = ReadLongAt(aBuf, OffsetNumVarSelectorRecords);
NS_ENSURE_TRUE((tablelen - OffsetVarSelectorRecords) /
SizeOfVarSelectorRecord >= numVarSelectorRecords,
NS_ERROR_GFX_CMAP_MALFORMED);
const uint8_t *records = aBuf + OffsetVarSelectorRecords;
- for (uint32_t i = 0; i < numVarSelectorRecords;
+ for (uint32_t i = 0; i < numVarSelectorRecords;
i++, records += SizeOfVarSelectorRecord) {
const uint32_t varSelector = ReadUint24At(records, VSRecOffsetVarSelector);
const uint32_t defUVSOffset = ReadLongAt(records, VSRecOffsetDefUVSOffset);
const uint32_t nonDefUVSOffset = ReadLongAt(records, VSRecOffsetNonDefUVSOffset);
NS_ENSURE_TRUE(varSelector <= CMAP_MAX_CODEPOINT &&
defUVSOffset <= tablelen - 4 &&
- nonDefUVSOffset <= tablelen - 4,
+ nonDefUVSOffset <= tablelen - 4,
NS_ERROR_GFX_CMAP_MALFORMED);
if (defUVSOffset) {
const uint32_t numUnicodeValueRanges = ReadLongAt(aBuf, defUVSOffset);
NS_ENSURE_TRUE((tablelen - defUVSOffset) /
SizeOfDefUVSTable >= numUnicodeValueRanges,
NS_ERROR_GFX_CMAP_MALFORMED);
const uint8_t *tables = aBuf + defUVSOffset + 4;
uint32_t prevEndUnicode = 0;
for (uint32_t j = 0; j < numUnicodeValueRanges; j++, tables += SizeOfDefUVSTable) {
const uint32_t startUnicode = ReadUint24At(tables, DefUVSOffsetStartUnicodeValue);
const uint32_t endUnicode = startUnicode + tables[DefUVSOffsetAdditionalCount];
NS_ENSURE_TRUE((prevEndUnicode < startUnicode || j == 0) &&
- endUnicode <= CMAP_MAX_CODEPOINT,
+ endUnicode <= CMAP_MAX_CODEPOINT,
NS_ERROR_GFX_CMAP_MALFORMED);
prevEndUnicode = endUnicode;
}
}
if (nonDefUVSOffset) {
const uint32_t numUVSMappings = ReadLongAt(aBuf, nonDefUVSOffset);
NS_ENSURE_TRUE((tablelen - nonDefUVSOffset) /
SizeOfNonDefUVSTable >= numUVSMappings,
NS_ERROR_GFX_CMAP_MALFORMED);
const uint8_t *tables = aBuf + nonDefUVSOffset + 4;
uint32_t prevUnicode = 0;
for (uint32_t j = 0; j < numUVSMappings; j++, tables += SizeOfNonDefUVSTable) {
const uint32_t unicodeValue = ReadUint24At(tables, NonDefUVSOffsetUnicodeValue);
NS_ENSURE_TRUE((prevUnicode < unicodeValue || j == 0) &&
- unicodeValue <= CMAP_MAX_CODEPOINT,
+ unicodeValue <= CMAP_MAX_CODEPOINT,
NS_ERROR_GFX_CMAP_MALFORMED);
prevUnicode = unicodeValue;
}
}
}
aTable = MakeUnique<uint8_t[]>(tablelen);
memcpy(aTable.get(), aBuf, tablelen);
@@ -822,32 +823,32 @@ gfxFontUtils::MapCharToGlyph(const uint8
return gid;
}
void gfxFontUtils::ParseFontList(const nsAString& aFamilyList,
nsTArray<nsString>& aFontList)
{
const char16_t kComma = char16_t(',');
-
+
// append each font name to the list
nsAutoString fontname;
const char16_t *p, *p_end;
aFamilyList.BeginReading(p);
aFamilyList.EndReading(p_end);
while (p < p_end) {
const char16_t *nameStart = p;
while (++p != p_end && *p != kComma)
/* nothing */ ;
- // pull out a single name and clean out leading/trailing whitespace
+ // pull out a single name and clean out leading/trailing whitespace
fontname = Substring(nameStart, p);
fontname.CompressWhitespace(true, true);
-
+
// append it to the list if it's not empty
if (!fontname.IsEmpty()) {
aFontList.AppendElement(fontname);
}
++p;
}
}
@@ -867,17 +868,17 @@ void gfxFontUtils::AppendPrefsFontList(c
void gfxFontUtils::GetPrefsFontList(const char *aPrefName,
nsTArray<nsString>& aFontList)
{
aFontList.Clear();
AppendPrefsFontList(aPrefName, aFontList);
}
// produce a unique font name that is (1) a valid Postscript name and (2) less
-// than 31 characters in length. Using AddFontMemResourceEx on Windows fails
+// than 31 characters in length. Using AddFontMemResourceEx on Windows fails
// for names longer than 30 characters in length.
#define MAX_B64_LEN 32
nsresult gfxFontUtils::MakeUniqueUserFontName(nsAString& aName)
{
nsCOMPtr<nsIUUIDGenerator> uuidgen =
do_GetService("@mozilla.org/uuid-generator;1");
@@ -956,32 +957,32 @@ gfxFontUtils::DetermineFontDataType(cons
// problem: EOT-Lite with 0x10000 length will look like TrueType!
if (aFontDataLength >= sizeof(SFNTHeader)) {
const SFNTHeader *sfntHeader = reinterpret_cast<const SFNTHeader*>(aFontData);
uint32_t sfntVersion = sfntHeader->sfntVersion;
if (IsValidSFNTVersion(sfntVersion)) {
return GFX_USERFONT_OPENTYPE;
}
}
-
+
// test for WOFF
if (aFontDataLength >= sizeof(AutoSwap_PRUint32)) {
- const AutoSwap_PRUint32 *version =
+ const AutoSwap_PRUint32 *version =
reinterpret_cast<const AutoSwap_PRUint32*>(aFontData);
if (uint32_t(*version) == TRUETYPE_TAG('w','O','F','F')) {
return GFX_USERFONT_WOFF;
}
if (Preferences::GetBool(GFX_PREF_WOFF2_ENABLED) &&
uint32_t(*version) == TRUETYPE_TAG('w','O','F','2')) {
return GFX_USERFONT_WOFF2;
}
}
-
+
// tests for other formats here
-
+
return GFX_USERFONT_UNKNOWN;
}
static int
DirEntryCmp(const void* aKey, const void* aItem)
{
int32_t tag = *static_cast<const int32_t*>(aKey);
const TableDirEntry* entry = static_cast<const TableDirEntry*>(aItem);
@@ -1011,25 +1012,25 @@ gfxFontUtils::GetTableFromFontData(const
dir->offset, dir->length,
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
}
return nullptr;
}
nsresult
-gfxFontUtils::RenameFont(const nsAString& aName, const uint8_t *aFontData,
+gfxFontUtils::RenameFont(const nsAString& aName, const uint8_t *aFontData,
uint32_t aFontDataLength, FallibleTArray<uint8_t> *aNewFont)
{
NS_ASSERTION(aNewFont, "null font data array");
-
+
uint64_t dataLength(aFontDataLength);
// new name table
- static const uint32_t neededNameIDs[] = {NAME_ID_FAMILY,
+ static const uint32_t neededNameIDs[] = {NAME_ID_FAMILY,
NAME_ID_STYLE,
NAME_ID_UNIQUE,
NAME_ID_FULL,
NAME_ID_POSTSCRIPT};
// calculate new name table size
uint16_t nameCount = ArrayLength(neededNameIDs);
@@ -1041,120 +1042,120 @@ gfxFontUtils::RenameFont(const nsAString
return NS_ERROR_FAILURE;
}
// round name table size up to 4-byte multiple
uint32_t nameTableSize = (sizeof(NameHeader) +
sizeof(NameRecord) * nameCount +
nameStrLength +
3) & ~3;
-
+
if (dataLength + nameTableSize > UINT32_MAX)
return NS_ERROR_FAILURE;
-
+
// bug 505386 - need to handle unpadded font length
uint32_t paddedFontDataSize = (aFontDataLength + 3) & ~3;
uint32_t adjFontDataSize = paddedFontDataSize + nameTableSize;
// create new buffer: old font data plus new name table
if (!aNewFont->AppendElements(adjFontDataSize, fallible))
return NS_ERROR_OUT_OF_MEMORY;
// copy the old font data
uint8_t *newFontData = reinterpret_cast<uint8_t*>(aNewFont->Elements());
-
+
// null the last four bytes in case the font length is not a multiple of 4
memset(newFontData + aFontDataLength, 0, paddedFontDataSize - aFontDataLength);
// copy font data
memcpy(newFontData, aFontData, aFontDataLength);
-
+
// null out the last 4 bytes for checksum calculations
memset(newFontData + adjFontDataSize - 4, 0, 4);
-
+
NameHeader *nameHeader = reinterpret_cast<NameHeader*>(newFontData +
paddedFontDataSize);
-
+
// -- name header
nameHeader->format = 0;
nameHeader->count = nameCount;
nameHeader->stringOffset = sizeof(NameHeader) + nameCount * sizeof(NameRecord);
-
+
// -- name records
uint32_t i;
NameRecord *nameRecord = reinterpret_cast<NameRecord*>(nameHeader + 1);
-
+
for (i = 0; i < nameCount; i++, nameRecord++) {
nameRecord->platformID = PLATFORM_ID_MICROSOFT;
nameRecord->encodingID = ENCODING_ID_MICROSOFT_UNICODEBMP;
nameRecord->languageID = LANG_ID_MICROSOFT_EN_US;
nameRecord->nameID = neededNameIDs[i];
nameRecord->offset = 0;
nameRecord->length = nameStrLength;
}
-
+
// -- string data, located after the name records, stored in big-endian form
char16_t *strData = reinterpret_cast<char16_t*>(nameRecord);
mozilla::NativeEndian::copyAndSwapToBigEndian(strData,
aName.BeginReading(),
aName.Length());
strData[aName.Length()] = 0; // add null termination
-
+
// adjust name table header to point to the new name table
SFNTHeader *sfntHeader = reinterpret_cast<SFNTHeader*>(newFontData);
// table directory entries begin immediately following SFNT header
TableDirEntry *dirEntry =
FindTableDirEntry(newFontData, TRUETYPE_TAG('n','a','m','e'));
// function only called if font validates, so this should always be true
MOZ_ASSERT(dirEntry, "attempt to rename font with no name table");
uint32_t numTables = sfntHeader->numTables;
-
+
// note: dirEntry now points to 'name' table record
-
+
// recalculate name table checksum
uint32_t checkSum = 0;
AutoSwap_PRUint32 *nameData = reinterpret_cast<AutoSwap_PRUint32*> (nameHeader);
AutoSwap_PRUint32 *nameDataEnd = nameData + (nameTableSize >> 2);
-
+
while (nameData < nameDataEnd)
checkSum = checkSum + *nameData++;
-
+
// adjust name table entry to point to new name table
dirEntry->offset = paddedFontDataSize;
dirEntry->length = nameTableSize;
dirEntry->checkSum = checkSum;
-
+
// fix up checksums
uint32_t checksum = 0;
-
+
// checksum for font = (checksum of header) + (checksum of tables)
uint32_t headerLen = sizeof(SFNTHeader) + sizeof(TableDirEntry) * numTables;
- const AutoSwap_PRUint32 *headerData =
+ const AutoSwap_PRUint32 *headerData =
reinterpret_cast<const AutoSwap_PRUint32*>(newFontData);
// header length is in bytes, checksum calculated in longwords
for (i = 0; i < (headerLen >> 2); i++, headerData++) {
checksum += *headerData;
}
-
+
uint32_t headOffset = 0;
dirEntry = reinterpret_cast<TableDirEntry*>(newFontData + sizeof(SFNTHeader));
for (i = 0; i < numTables; i++, dirEntry++) {
if (dirEntry->tag == TRUETYPE_TAG('h','e','a','d')) {
headOffset = dirEntry->offset;
}
checksum += dirEntry->checkSum;
}
-
+
NS_ASSERTION(headOffset != 0, "no head table for font");
-
+
HeadTable *headData = reinterpret_cast<HeadTable*>(newFontData + headOffset);
headData->checkSumAdjustment = HeadTable::HEAD_CHECKSUM_CALC_CONST - checksum;
return NS_OK;
}
// This is only called after the basic validity of the downloaded sfnt
@@ -1164,17 +1165,17 @@ gfxFontUtils::RenameFont(const nsAString
nsresult
gfxFontUtils::GetFullNameFromSFNT(const uint8_t* aFontData, uint32_t aLength,
nsAString& aFullName)
{
aFullName.AssignLiteral("(MISSING NAME)"); // should always get replaced
const TableDirEntry *dirEntry =
FindTableDirEntry(aFontData, TRUETYPE_TAG('n','a','m','e'));
-
+
// should never fail, as we're only called after font validation succeeded
NS_ENSURE_TRUE(dirEntry, NS_ERROR_NOT_AVAILABLE);
uint32_t len = dirEntry->length;
NS_ENSURE_TRUE(aLength > len && aLength - len >= dirEntry->offset,
NS_ERROR_UNEXPECTED);
hb_blob_t *nameBlob =
@@ -1237,17 +1238,17 @@ gfxFontUtils::GetFamilyNameFromTable(hb_
enum {
#if defined(XP_MACOSX)
CANONICAL_LANG_ID = gfxFontUtils::LANG_ID_MAC_ENGLISH,
PLATFORM_ID = gfxFontUtils::PLATFORM_ID_MAC
#else
CANONICAL_LANG_ID = gfxFontUtils::LANG_ID_MICROSOFT_EN_US,
PLATFORM_ID = gfxFontUtils::PLATFORM_ID_MICROSOFT
#endif
-};
+};
nsresult
gfxFontUtils::ReadNames(const char *aNameData, uint32_t aDataLen,
uint32_t aNameID, int32_t aPlatformID,
nsTArray<nsString>& aNames)
{
return ReadNames(aNameData, aDataLen, aNameID, LANG_ALL,
aPlatformID, aNames);
@@ -1262,54 +1263,54 @@ gfxFontUtils::ReadCanonicalName(hb_blob_
return ReadCanonicalName(nameTable, nameTableLen, aNameID, aName);
}
nsresult
gfxFontUtils::ReadCanonicalName(const char *aNameData, uint32_t aDataLen,
uint32_t aNameID, nsString& aName)
{
nsresult rv;
-
+
nsTArray<nsString> names;
-
+
// first, look for the English name (this will succeed 99% of the time)
- rv = ReadNames(aNameData, aDataLen, aNameID, CANONICAL_LANG_ID,
+ rv = ReadNames(aNameData, aDataLen, aNameID, CANONICAL_LANG_ID,
PLATFORM_ID, names);
NS_ENSURE_SUCCESS(rv, rv);
-
+
// otherwise, grab names for all languages
if (names.Length() == 0) {
rv = ReadNames(aNameData, aDataLen, aNameID, LANG_ALL,
PLATFORM_ID, names);
NS_ENSURE_SUCCESS(rv, rv);
}
-
+
#if defined(XP_MACOSX)
// may be dealing with font that only has Microsoft name entries
if (names.Length() == 0) {
rv = ReadNames(aNameData, aDataLen, aNameID, LANG_ID_MICROSOFT_EN_US,
PLATFORM_ID_MICROSOFT, names);
NS_ENSURE_SUCCESS(rv, rv);
-
+
// getting really desperate now, take anything!
if (names.Length() == 0) {
rv = ReadNames(aNameData, aDataLen, aNameID, LANG_ALL,
PLATFORM_ID_MICROSOFT, names);
NS_ENSURE_SUCCESS(rv, rv);
}
}
#endif
// return the first name (99.9% of the time names will
// contain a single English name)
if (names.Length()) {
aName.Assign(names[0]);
return NS_OK;
}
-
+
return NS_ERROR_FAILURE;
}
// Charsets to use for decoding Mac platform font names.
// This table is sorted by {encoding, language}, with the wildcard "ANY" being
// greater than any defined values for each field; we use a binary search on both
// fields, and fall back to matching only encoding if necessary
@@ -1442,17 +1443,17 @@ StartsWith(const nsACString& string, con
return false;
}
return memcmp(string.Data(), prefix, N - 1) == 0;
}
// convert a raw name from the name table to an nsString, if possible;
// return value indicates whether conversion succeeded
bool
-gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
+gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
uint32_t aPlatformCode, uint32_t aScriptCode,
uint32_t aLangCode, nsAString& aName)
{
if (aByteLen <= 0) {
NS_WARNING("empty font name");
aName.SetLength(0);
return true;
}