1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/ArrayUtils.h"
7 #include "mozilla/BinarySearch.h"
9 #include "gfxFontUtils.h"
10 #include "gfxFontEntry.h"
11 #include "gfxFontVariations.h"
13 #include "nsServiceManagerUtils.h"
15 #include "mozilla/Preferences.h"
16 #include "mozilla/Services.h"
17 #include "mozilla/BinarySearch.h"
18 #include "mozilla/Sprintf.h"
19 #include "mozilla/Unused.h"
22 #include "nsIUUIDGenerator.h"
23 #include "mozilla/Encoding.h"
25 #include "harfbuzz/hb.h"
28 #include "mozilla/Logging.h"
31 #include <CoreFoundation/CoreFoundation.h>
34 #define LOG(log, args) MOZ_LOG(gfxPlatform::GetLog(log), \
35 LogLevel::Debug, args)
37 #define UNICODE_BMP_LIMIT 0x10000
39 using namespace mozilla
;
44 AutoSwap_PRUint16 format
;
45 AutoSwap_PRUint16 reserved
;
46 AutoSwap_PRUint32 length
;
47 AutoSwap_PRUint32 language
;
48 AutoSwap_PRUint32 startCharCode
;
49 AutoSwap_PRUint32 numChars
;
53 AutoSwap_PRUint16 format
;
54 AutoSwap_PRUint16 reserved
;
55 AutoSwap_PRUint32 length
;
56 AutoSwap_PRUint32 language
;
57 AutoSwap_PRUint32 numGroups
;
61 AutoSwap_PRUint32 startCharCode
;
62 AutoSwap_PRUint32 endCharCode
;
63 AutoSwap_PRUint32 startGlyphId
;
69 gfxSparseBitSet::Dump(const char* aPrefix
, eGfxLog aWhichLog
) const
71 uint32_t numBlocks
= mBlockIndex
.Length();
73 for (uint32_t b
= 0; b
< numBlocks
; b
++) {
74 if (mBlockIndex
[b
] == NO_BLOCK
) {
77 const Block
* block
= &mBlocks
[mBlockIndex
[b
]];
78 const int BUFSIZE
= 256;
81 index
+= snprintf(&outStr
[index
], BUFSIZE
- index
, "%s u+%6.6x [",
82 aPrefix
, (b
* BLOCK_SIZE_BITS
));
83 for (int i
= 0; i
< 32; i
+= 4) {
84 for (int j
= i
; j
< i
+ 4; j
++) {
85 uint8_t bits
= block
->mBits
[j
];
86 uint8_t flip1
= ((bits
& 0xaa) >> 1) | ((bits
& 0x55) << 1);
87 uint8_t flip2
= ((flip1
& 0xcc) >> 2) | ((flip1
& 0x33) << 2);
88 uint8_t flipped
= ((flip2
& 0xf0) >> 4) | ((flip2
& 0x0f) << 4);
90 index
+= snprintf(&outStr
[index
], BUFSIZE
- index
, "%2.2x", flipped
);
92 if (i
+ 4 != 32) index
+= snprintf(&outStr
[index
], BUFSIZE
- index
, " ");
94 Unused
<< snprintf(&outStr
[index
], BUFSIZE
- index
, "]");
95 LOG(aWhichLog
, ("%s", outStr
));
100 gfxFontUtils::ReadCMAPTableFormat10(const uint8_t *aBuf
, uint32_t aLength
,
101 gfxSparseBitSet
& aCharacterMap
)
103 // Ensure table is large enough that we can safely read the header
104 NS_ENSURE_TRUE(aLength
>= sizeof(Format10CmapHeader
),
105 NS_ERROR_GFX_CMAP_MALFORMED
);
107 // Sanity-check header fields
108 const Format10CmapHeader
*cmap10
=
109 reinterpret_cast<const Format10CmapHeader
*>(aBuf
);
110 NS_ENSURE_TRUE(uint16_t(cmap10
->format
) == 10,
111 NS_ERROR_GFX_CMAP_MALFORMED
);
112 NS_ENSURE_TRUE(uint16_t(cmap10
->reserved
) == 0,
113 NS_ERROR_GFX_CMAP_MALFORMED
);
115 uint32_t tablelen
= cmap10
->length
;
116 NS_ENSURE_TRUE(tablelen
>= sizeof(Format10CmapHeader
) &&
117 tablelen
<= aLength
, NS_ERROR_GFX_CMAP_MALFORMED
);
119 NS_ENSURE_TRUE(cmap10
->language
== 0, NS_ERROR_GFX_CMAP_MALFORMED
);
121 uint32_t numChars
= cmap10
->numChars
;
122 NS_ENSURE_TRUE(tablelen
== sizeof(Format10CmapHeader
) +
123 numChars
* sizeof(uint16_t), NS_ERROR_GFX_CMAP_MALFORMED
);
125 uint32_t charCode
= cmap10
->startCharCode
;
126 NS_ENSURE_TRUE(charCode
<= CMAP_MAX_CODEPOINT
&&
127 charCode
+ numChars
<= CMAP_MAX_CODEPOINT
,
128 NS_ERROR_GFX_CMAP_MALFORMED
);
130 // glyphs[] array immediately follows the subtable header
131 const AutoSwap_PRUint16
*glyphs
=
132 reinterpret_cast<const AutoSwap_PRUint16
*>(cmap10
+ 1);
134 for (uint32_t i
= 0; i
< numChars
; ++i
) {
135 if (uint16_t(*glyphs
) != 0) {
136 aCharacterMap
.set(charCode
);
142 aCharacterMap
.Compact();
148 gfxFontUtils::ReadCMAPTableFormat12or13(const uint8_t *aBuf
, uint32_t aLength
,
149 gfxSparseBitSet
& aCharacterMap
)
151 // Format 13 has the same structure as format 12, the only difference is
152 // the interpretation of the glyphID field. So we can share the code here
153 // that reads the table and just records character coverage.
155 // Ensure table is large enough that we can safely read the header
156 NS_ENSURE_TRUE(aLength
>= sizeof(Format12CmapHeader
),
157 NS_ERROR_GFX_CMAP_MALFORMED
);
159 // Sanity-check header fields
160 const Format12CmapHeader
*cmap12
=
161 reinterpret_cast<const Format12CmapHeader
*>(aBuf
);
162 NS_ENSURE_TRUE(uint16_t(cmap12
->format
) == 12 ||
163 uint16_t(cmap12
->format
) == 13,
164 NS_ERROR_GFX_CMAP_MALFORMED
);
165 NS_ENSURE_TRUE(uint16_t(cmap12
->reserved
) == 0,
166 NS_ERROR_GFX_CMAP_MALFORMED
);
168 uint32_t tablelen
= cmap12
->length
;
169 NS_ENSURE_TRUE(tablelen
>= sizeof(Format12CmapHeader
) &&
170 tablelen
<= aLength
, NS_ERROR_GFX_CMAP_MALFORMED
);
172 NS_ENSURE_TRUE(cmap12
->language
== 0, NS_ERROR_GFX_CMAP_MALFORMED
);
174 // Check that the table is large enough for the group array
175 const uint32_t numGroups
= cmap12
->numGroups
;
176 NS_ENSURE_TRUE((tablelen
- sizeof(Format12CmapHeader
)) /
177 sizeof(Format12Group
) >= numGroups
,
178 NS_ERROR_GFX_CMAP_MALFORMED
);
180 // The array of groups immediately follows the subtable header.
181 const Format12Group
*group
=
182 reinterpret_cast<const Format12Group
*>(aBuf
+ sizeof(Format12CmapHeader
));
184 // Check that groups are in correct order and do not overlap,
185 // and record character coverage in aCharacterMap.
186 uint32_t prevEndCharCode
= 0;
187 for (uint32_t i
= 0; i
< numGroups
; i
++, group
++) {
188 uint32_t startCharCode
= group
->startCharCode
;
189 const uint32_t endCharCode
= group
->endCharCode
;
190 NS_ENSURE_TRUE((prevEndCharCode
< startCharCode
|| i
== 0) &&
191 startCharCode
<= endCharCode
&&
192 endCharCode
<= CMAP_MAX_CODEPOINT
,
193 NS_ERROR_GFX_CMAP_MALFORMED
);
194 // don't include a character that maps to glyph ID 0 (.notdef)
195 if (group
->startGlyphId
== 0) {
198 if (startCharCode
<= endCharCode
) {
199 aCharacterMap
.SetRange(startCharCode
, endCharCode
);
201 prevEndCharCode
= endCharCode
;
204 aCharacterMap
.Compact();
210 gfxFontUtils::ReadCMAPTableFormat4(const uint8_t *aBuf
, uint32_t aLength
,
211 gfxSparseBitSet
& aCharacterMap
)
220 NS_ENSURE_TRUE(ReadShortAt(aBuf
, OffsetFormat
) == 4,
221 NS_ERROR_GFX_CMAP_MALFORMED
);
222 uint16_t tablelen
= ReadShortAt(aBuf
, OffsetLength
);
223 NS_ENSURE_TRUE(tablelen
<= aLength
, NS_ERROR_GFX_CMAP_MALFORMED
);
224 NS_ENSURE_TRUE(tablelen
> 16, NS_ERROR_GFX_CMAP_MALFORMED
);
226 // This field should normally (except for Mac platform subtables) be zero according to
227 // the OT spec, but some buggy fonts have lang = 1 (which would be English for MacOS).
228 // E.g. Arial Narrow Bold, v. 1.1 (Tiger), Arial Unicode MS (see bug 530614).
229 // So accept either zero or one here; the error should be harmless.
230 NS_ENSURE_TRUE((ReadShortAt(aBuf
, OffsetLanguage
) & 0xfffe) == 0,
231 NS_ERROR_GFX_CMAP_MALFORMED
);
233 uint16_t segCountX2
= ReadShortAt(aBuf
, OffsetSegCountX2
);
234 NS_ENSURE_TRUE(tablelen
>= 16 + (segCountX2
* 4),
235 NS_ERROR_GFX_CMAP_MALFORMED
);
237 const uint16_t segCount
= segCountX2
/ 2;
239 const uint16_t *endCounts
= reinterpret_cast<const uint16_t*>(aBuf
+ 14);
240 const uint16_t *startCounts
= endCounts
+ 1 /* skip one uint16_t for reservedPad */ + segCount
;
241 const uint16_t *idDeltas
= startCounts
+ segCount
;
242 const uint16_t *idRangeOffsets
= idDeltas
+ segCount
;
243 uint16_t prevEndCount
= 0;
244 for (uint16_t i
= 0; i
< segCount
; i
++) {
245 const uint16_t endCount
= ReadShortAt16(endCounts
, i
);
246 const uint16_t startCount
= ReadShortAt16(startCounts
, i
);
247 const uint16_t idRangeOffset
= ReadShortAt16(idRangeOffsets
, i
);
249 // sanity-check range
250 // This permits ranges to overlap by 1 character, which is strictly
251 // incorrect but occurs in Baskerville on OS X 10.7 (see bug 689087),
252 // and appears to be harmless in practice
253 NS_ENSURE_TRUE(startCount
>= prevEndCount
&& startCount
<= endCount
,
254 NS_ERROR_GFX_CMAP_MALFORMED
);
255 prevEndCount
= endCount
;
257 if (idRangeOffset
== 0) {
258 // figure out if there's a code in the range that would map to
259 // glyph ID 0 (.notdef); if so, we need to skip setting that
260 // character code in the map
261 const uint16_t skipCode
= 65536 - ReadShortAt16(idDeltas
, i
);
262 if (startCount
< skipCode
) {
263 aCharacterMap
.SetRange(startCount
,
264 std::min
<uint16_t>(skipCode
- 1,
267 if (skipCode
< endCount
) {
268 aCharacterMap
.SetRange(std::max
<uint16_t>(startCount
,
273 // const uint16_t idDelta = ReadShortAt16(idDeltas, i); // Unused: self-documenting.
274 for (uint32_t c
= startCount
; c
<= endCount
; ++c
) {
278 const uint16_t *gdata
= (idRangeOffset
/2
280 + &idRangeOffsets
[i
]);
282 NS_ENSURE_TRUE((uint8_t*)gdata
> aBuf
&&
283 (uint8_t*)gdata
< aBuf
+ aLength
,
284 NS_ERROR_GFX_CMAP_MALFORMED
);
286 // make sure we have a glyph
288 // The glyph index at this point is:
289 uint16_t glyph
= ReadShortAt16(idDeltas
, i
) + *gdata
;
291 aCharacterMap
.set(c
);
298 aCharacterMap
.Compact();
304 gfxFontUtils::ReadCMAPTableFormat14(const uint8_t *aBuf
, uint32_t aLength
,
305 UniquePtr
<uint8_t[]>& aTable
)
309 OffsetTableLength
= 2,
310 OffsetNumVarSelectorRecords
= 6,
311 OffsetVarSelectorRecords
= 10,
313 SizeOfVarSelectorRecord
= 11,
314 VSRecOffsetVarSelector
= 0,
315 VSRecOffsetDefUVSOffset
= 3,
316 VSRecOffsetNonDefUVSOffset
= 7,
318 SizeOfDefUVSTable
= 4,
319 DefUVSOffsetStartUnicodeValue
= 0,
320 DefUVSOffsetAdditionalCount
= 3,
322 SizeOfNonDefUVSTable
= 5,
323 NonDefUVSOffsetUnicodeValue
= 0,
324 NonDefUVSOffsetGlyphID
= 3
326 NS_ENSURE_TRUE(aLength
>= OffsetVarSelectorRecords
,
327 NS_ERROR_GFX_CMAP_MALFORMED
);
329 NS_ENSURE_TRUE(ReadShortAt(aBuf
, OffsetFormat
) == 14,
330 NS_ERROR_GFX_CMAP_MALFORMED
);
332 uint32_t tablelen
= ReadLongAt(aBuf
, OffsetTableLength
);
333 NS_ENSURE_TRUE(tablelen
<= aLength
, NS_ERROR_GFX_CMAP_MALFORMED
);
334 NS_ENSURE_TRUE(tablelen
>= OffsetVarSelectorRecords
,
335 NS_ERROR_GFX_CMAP_MALFORMED
);
337 const uint32_t numVarSelectorRecords
= ReadLongAt(aBuf
, OffsetNumVarSelectorRecords
);
338 NS_ENSURE_TRUE((tablelen
- OffsetVarSelectorRecords
) /
339 SizeOfVarSelectorRecord
>= numVarSelectorRecords
,
340 NS_ERROR_GFX_CMAP_MALFORMED
);
342 const uint8_t *records
= aBuf
+ OffsetVarSelectorRecords
;
343 for (uint32_t i
= 0; i
< numVarSelectorRecords
;
344 i
++, records
+= SizeOfVarSelectorRecord
) {
345 const uint32_t varSelector
= ReadUint24At(records
, VSRecOffsetVarSelector
);
346 const uint32_t defUVSOffset
= ReadLongAt(records
, VSRecOffsetDefUVSOffset
);
347 const uint32_t nonDefUVSOffset
= ReadLongAt(records
, VSRecOffsetNonDefUVSOffset
);
348 NS_ENSURE_TRUE(varSelector
<= CMAP_MAX_CODEPOINT
&&
349 defUVSOffset
<= tablelen
- 4 &&
350 nonDefUVSOffset
<= tablelen
- 4,
351 NS_ERROR_GFX_CMAP_MALFORMED
);
354 const uint32_t numUnicodeValueRanges
= ReadLongAt(aBuf
, defUVSOffset
);
355 NS_ENSURE_TRUE((tablelen
- defUVSOffset
) /
356 SizeOfDefUVSTable
>= numUnicodeValueRanges
,
357 NS_ERROR_GFX_CMAP_MALFORMED
);
358 const uint8_t *tables
= aBuf
+ defUVSOffset
+ 4;
359 uint32_t prevEndUnicode
= 0;
360 for (uint32_t j
= 0; j
< numUnicodeValueRanges
; j
++, tables
+= SizeOfDefUVSTable
) {
361 const uint32_t startUnicode
= ReadUint24At(tables
, DefUVSOffsetStartUnicodeValue
);
362 const uint32_t endUnicode
= startUnicode
+ tables
[DefUVSOffsetAdditionalCount
];
363 NS_ENSURE_TRUE((prevEndUnicode
< startUnicode
|| j
== 0) &&
364 endUnicode
<= CMAP_MAX_CODEPOINT
,
365 NS_ERROR_GFX_CMAP_MALFORMED
);
366 prevEndUnicode
= endUnicode
;
370 if (nonDefUVSOffset
) {
371 const uint32_t numUVSMappings
= ReadLongAt(aBuf
, nonDefUVSOffset
);
372 NS_ENSURE_TRUE((tablelen
- nonDefUVSOffset
) /
373 SizeOfNonDefUVSTable
>= numUVSMappings
,
374 NS_ERROR_GFX_CMAP_MALFORMED
);
375 const uint8_t *tables
= aBuf
+ nonDefUVSOffset
+ 4;
376 uint32_t prevUnicode
= 0;
377 for (uint32_t j
= 0; j
< numUVSMappings
; j
++, tables
+= SizeOfNonDefUVSTable
) {
378 const uint32_t unicodeValue
= ReadUint24At(tables
, NonDefUVSOffsetUnicodeValue
);
379 NS_ENSURE_TRUE((prevUnicode
< unicodeValue
|| j
== 0) &&
380 unicodeValue
<= CMAP_MAX_CODEPOINT
,
381 NS_ERROR_GFX_CMAP_MALFORMED
);
382 prevUnicode
= unicodeValue
;
387 aTable
= MakeUnique
<uint8_t[]>(tablelen
);
388 memcpy(aTable
.get(), aBuf
, tablelen
);
393 // For fonts with two format-4 tables, the first one (Unicode platform) is preferred on the Mac;
394 // on other platforms we allow the Microsoft-platform subtable to replace it.
396 #if defined(XP_MACOSX)
397 #define acceptableFormat4(p,e,k) (((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDMicrosoft && !(k)) || \
398 ((p) == PLATFORM_ID_UNICODE))
400 #define acceptableUCS4Encoding(p, e, k) \
401 (((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDUCS4ForMicrosoftPlatform) && (k) != 12 || \
402 ((p) == PLATFORM_ID_UNICODE && \
403 ((e) != EncodingIDUVSForUnicodePlatform)))
405 #define acceptableFormat4(p,e,k) (((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDMicrosoft) || \
406 ((p) == PLATFORM_ID_UNICODE))
408 #define acceptableUCS4Encoding(p, e, k) \
409 ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDUCS4ForMicrosoftPlatform)
412 #define acceptablePlatform(p) ((p) == PLATFORM_ID_UNICODE || (p) == PLATFORM_ID_MICROSOFT)
413 #define isSymbol(p,e) ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDSymbol)
414 #define isUVSEncoding(p, e) ((p) == PLATFORM_ID_UNICODE && (e) == EncodingIDUVSForUnicodePlatform)
417 gfxFontUtils::FindPreferredSubtable(const uint8_t *aBuf
, uint32_t aBufLength
,
418 uint32_t *aTableOffset
,
419 uint32_t *aUVSTableOffset
)
426 TableOffsetPlatformID
= 0,
427 TableOffsetEncodingID
= 2,
428 TableOffsetOffset
= 4,
431 SubtableOffsetFormat
= 0
434 EncodingIDSymbol
= 0,
435 EncodingIDMicrosoft
= 1,
436 EncodingIDDefaultForUnicodePlatform
= 0,
437 EncodingIDUCS4ForUnicodePlatform
= 3,
438 EncodingIDUVSForUnicodePlatform
= 5,
439 EncodingIDUCS4ForMicrosoftPlatform
= 10
442 if (aUVSTableOffset
) {
443 *aUVSTableOffset
= 0;
446 if (!aBuf
|| aBufLength
< SizeOfHeader
) {
447 // cmap table is missing, or too small to contain header fields!
451 // uint16_t version = ReadShortAt(aBuf, OffsetVersion); // Unused: self-documenting.
452 uint16_t numTables
= ReadShortAt(aBuf
, OffsetNumTables
);
453 if (aBufLength
< uint32_t(SizeOfHeader
+ numTables
* SizeOfTable
)) {
457 // save the format we want here
458 uint32_t keepFormat
= 0;
460 const uint8_t *table
= aBuf
+ SizeOfHeader
;
461 for (uint16_t i
= 0; i
< numTables
; ++i
, table
+= SizeOfTable
) {
462 const uint16_t platformID
= ReadShortAt(table
, TableOffsetPlatformID
);
463 if (!acceptablePlatform(platformID
))
466 const uint16_t encodingID
= ReadShortAt(table
, TableOffsetEncodingID
);
467 const uint32_t offset
= ReadLongAt(table
, TableOffsetOffset
);
468 if (aBufLength
- 2 < offset
) {
469 // this subtable is not valid - beyond end of buffer
473 const uint8_t *subtable
= aBuf
+ offset
;
474 const uint16_t format
= ReadShortAt(subtable
, SubtableOffsetFormat
);
476 if (isSymbol(platformID
, encodingID
)) {
478 *aTableOffset
= offset
;
480 } else if (format
== 4 && acceptableFormat4(platformID
, encodingID
, keepFormat
)) {
482 *aTableOffset
= offset
;
483 } else if ((format
== 10 || format
== 12 || format
== 13) &&
484 acceptableUCS4Encoding(platformID
, encodingID
, keepFormat
)) {
486 *aTableOffset
= offset
;
487 if (platformID
> PLATFORM_ID_UNICODE
|| !aUVSTableOffset
|| *aUVSTableOffset
) {
488 break; // we don't want to try anything else when this format is available.
490 } else if (format
== 14 && isUVSEncoding(platformID
, encodingID
) && aUVSTableOffset
) {
491 *aUVSTableOffset
= offset
;
492 if (keepFormat
== 10 || keepFormat
== 12) {
502 gfxFontUtils::ReadCMAP(const uint8_t *aBuf
, uint32_t aBufLength
,
503 gfxSparseBitSet
& aCharacterMap
,
504 uint32_t& aUVSOffset
)
507 uint32_t format
= FindPreferredSubtable(aBuf
, aBufLength
,
508 &offset
, &aUVSOffset
);
512 return ReadCMAPTableFormat4(aBuf
+ offset
, aBufLength
- offset
,
516 return ReadCMAPTableFormat10(aBuf
+ offset
, aBufLength
- offset
,
521 return ReadCMAPTableFormat12or13(aBuf
+ offset
, aBufLength
- offset
,
528 return NS_ERROR_FAILURE
;
534 AutoSwap_PRUint16 format
;
535 AutoSwap_PRUint16 length
;
536 AutoSwap_PRUint16 language
;
537 AutoSwap_PRUint16 segCountX2
;
538 AutoSwap_PRUint16 searchRange
;
539 AutoSwap_PRUint16 entrySelector
;
540 AutoSwap_PRUint16 rangeShift
;
542 AutoSwap_PRUint16 arrays
[1];
546 AutoSwap_PRUint16 format
;
547 AutoSwap_PRUint32 length
;
548 AutoSwap_PRUint32 numVarSelectorRecords
;
551 AutoSwap_PRUint24 varSelector
;
552 AutoSwap_PRUint32 defaultUVSOffset
;
553 AutoSwap_PRUint32 nonDefaultUVSOffset
;
556 VarSelectorRecord varSelectorRecords
[1];
560 AutoSwap_PRUint32 numUVSMappings
;
563 AutoSwap_PRUint24 unicodeValue
;
564 AutoSwap_PRUint16 glyphID
;
567 UVSMapping uvsMappings
[1];
573 gfxFontUtils::MapCharToGlyphFormat4(const uint8_t* aBuf
, uint32_t aLength
,
576 const Format4Cmap
*cmap4
= reinterpret_cast<const Format4Cmap
*>(aBuf
);
578 uint16_t segCount
= (uint16_t)(cmap4
->segCountX2
) / 2;
580 const AutoSwap_PRUint16
* endCodes
= &cmap4
->arrays
[0];
581 const AutoSwap_PRUint16
* startCodes
= &cmap4
->arrays
[segCount
+ 1];
582 const AutoSwap_PRUint16
* idDelta
= &startCodes
[segCount
];
583 const AutoSwap_PRUint16
* idRangeOffset
= &idDelta
[segCount
];
585 // Sanity-check that the fixed-size arrays don't exceed the buffer.
586 const uint8_t* const limit
= aBuf
+ aLength
;
587 if ((const uint8_t*)(&idRangeOffset
[segCount
]) > limit
) {
588 return 0; // broken font, just bail out safely
591 // For most efficient binary search, we want to work on a range of segment
592 // indexes that is a power of 2 so that we can always halve it by shifting.
593 // So we find the largest power of 2 that is <= segCount.
594 // We will offset this range by segOffset so as to reach the end
595 // of the table, provided that doesn't put us beyond the target
596 // value from the outset.
597 uint32_t powerOf2
= mozilla::FindHighestBit(segCount
);
598 uint32_t segOffset
= segCount
- powerOf2
;
601 if (uint16_t(startCodes
[segOffset
]) <= aCh
) {
605 // Repeatedly halve the size of the range until we find the target group
606 while (powerOf2
> 1) {
608 if (uint16_t(startCodes
[idx
+ powerOf2
]) <= aCh
) {
613 if (aCh
>= uint16_t(startCodes
[idx
]) && aCh
<= uint16_t(endCodes
[idx
])) {
615 if (uint16_t(idRangeOffset
[idx
]) == 0) {
618 uint16_t offset
= aCh
- uint16_t(startCodes
[idx
]);
619 const AutoSwap_PRUint16
* glyphIndexTable
=
620 (const AutoSwap_PRUint16
*)((const char*)&idRangeOffset
[idx
] +
621 uint16_t(idRangeOffset
[idx
]));
622 if ((const uint8_t*)(glyphIndexTable
+ offset
+ 1) > limit
) {
623 return 0; // broken font, just bail out safely
625 result
= glyphIndexTable
[offset
];
628 // Note that this is unsigned 16-bit arithmetic, and may wrap around
629 // (which is required behavior per spec)
630 result
+= uint16_t(idDelta
[idx
]);
638 gfxFontUtils::MapCharToGlyphFormat10(const uint8_t *aBuf
, uint32_t aCh
)
640 const Format10CmapHeader
*cmap10
=
641 reinterpret_cast<const Format10CmapHeader
*>(aBuf
);
643 uint32_t startChar
= cmap10
->startCharCode
;
644 uint32_t numChars
= cmap10
->numChars
;
646 if (aCh
< startChar
|| aCh
>= startChar
+ numChars
) {
650 const AutoSwap_PRUint16
*glyphs
=
651 reinterpret_cast<const AutoSwap_PRUint16
*>(cmap10
+ 1);
653 uint16_t glyph
= glyphs
[aCh
- startChar
];
658 gfxFontUtils::MapCharToGlyphFormat12or13(const uint8_t *aBuf
, uint32_t aCh
)
660 // The only difference between formats 12 and 13 is the interpretation of
661 // the glyphId field. So the code here uses the same "Format12" structures,
662 // etc., to handle both subtable formats.
664 const Format12CmapHeader
*cmap12
=
665 reinterpret_cast<const Format12CmapHeader
*>(aBuf
);
667 // We know that numGroups is within range for the subtable size
668 // because it was checked by ReadCMAPTableFormat12or13.
669 uint32_t numGroups
= cmap12
->numGroups
;
671 // The array of groups immediately follows the subtable header.
672 const Format12Group
*groups
=
673 reinterpret_cast<const Format12Group
*>(aBuf
+ sizeof(Format12CmapHeader
));
675 // For most efficient binary search, we want to work on a range that
676 // is a power of 2 so that we can always halve it by shifting.
677 // So we find the largest power of 2 that is <= numGroups.
678 // We will offset this range by rangeOffset so as to reach the end
679 // of the table, provided that doesn't put us beyond the target
680 // value from the outset.
681 uint32_t powerOf2
= mozilla::FindHighestBit(numGroups
);
682 uint32_t rangeOffset
= numGroups
- powerOf2
;
684 uint32_t startCharCode
;
686 if (groups
[rangeOffset
].startCharCode
<= aCh
) {
690 // Repeatedly halve the size of the range until we find the target group
691 while (powerOf2
> 1) {
693 if (groups
[range
+ powerOf2
].startCharCode
<= aCh
) {
698 // Check if the character is actually present in the range and return
699 // the corresponding glyph ID. Here is where formats 12 and 13 interpret
700 // the startGlyphId (12) or glyphId (13) field differently
701 startCharCode
= groups
[range
].startCharCode
;
702 if (startCharCode
<= aCh
&& groups
[range
].endCharCode
>= aCh
) {
703 return uint16_t(cmap12
->format
) == 12
704 ? uint16_t(groups
[range
].startGlyphId
) + aCh
- startCharCode
705 : uint16_t(groups
[range
].startGlyphId
);
708 // Else it's not present, so return the .notdef glyph
714 struct Format14CmapWrapper
716 const Format14Cmap
& mCmap14
;
717 explicit Format14CmapWrapper(const Format14Cmap
& cmap14
) : mCmap14(cmap14
) {}
718 uint32_t operator[](size_t index
) const {
719 return mCmap14
.varSelectorRecords
[index
].varSelector
;
723 struct NonDefUVSTableWrapper
725 const NonDefUVSTable
& mTable
;
726 explicit NonDefUVSTableWrapper(const NonDefUVSTable
& table
) : mTable(table
) {}
727 uint32_t operator[](size_t index
) const {
728 return mTable
.uvsMappings
[index
].unicodeValue
;
735 gfxFontUtils::MapUVSToGlyphFormat14(const uint8_t *aBuf
, uint32_t aCh
, uint32_t aVS
)
737 using mozilla::BinarySearch
;
738 const Format14Cmap
*cmap14
= reinterpret_cast<const Format14Cmap
*>(aBuf
);
741 if (!BinarySearch(Format14CmapWrapper(*cmap14
),
742 0, cmap14
->numVarSelectorRecords
, aVS
, &index
)) {
746 const uint32_t nonDefUVSOffset
= cmap14
->varSelectorRecords
[index
].nonDefaultUVSOffset
;
747 if (!nonDefUVSOffset
) {
751 const NonDefUVSTable
*table
= reinterpret_cast<const NonDefUVSTable
*>
752 (aBuf
+ nonDefUVSOffset
);
754 if (BinarySearch(NonDefUVSTableWrapper(*table
), 0, table
->numUVSMappings
,
756 return table
->uvsMappings
[index
].glyphID
;
763 gfxFontUtils::MapCharToGlyph(const uint8_t *aCmapBuf
, uint32_t aBufLength
,
764 uint32_t aUnicode
, uint32_t aVarSelector
)
766 uint32_t offset
, uvsOffset
;
767 uint32_t format
= FindPreferredSubtable(aCmapBuf
, aBufLength
, &offset
,
773 gid
= aUnicode
< UNICODE_BMP_LIMIT
?
774 MapCharToGlyphFormat4(aCmapBuf
+ offset
, aBufLength
- offset
,
775 char16_t(aUnicode
)) : 0;
778 gid
= MapCharToGlyphFormat10(aCmapBuf
+ offset
, aUnicode
);
782 gid
= MapCharToGlyphFormat12or13(aCmapBuf
+ offset
, aUnicode
);
785 NS_WARNING("unsupported cmap format, glyphs will be missing");
789 if (aVarSelector
&& uvsOffset
&& gid
) {
791 gfxFontUtils::MapUVSToGlyphFormat14(aCmapBuf
+ uvsOffset
,
792 aUnicode
, aVarSelector
);
794 aUnicode
= gfxFontUtils::GetUVSFallback(aUnicode
, aVarSelector
);
798 if (aUnicode
< UNICODE_BMP_LIMIT
) {
799 varGID
= MapCharToGlyphFormat4(aCmapBuf
+ offset
,
805 varGID
= MapCharToGlyphFormat10(aCmapBuf
+ offset
,
810 varGID
= MapCharToGlyphFormat12or13(aCmapBuf
+ offset
,
820 // else the variation sequence was not supported, use default mapping
821 // of the character code alone
827 void gfxFontUtils::ParseFontList(const nsACString
& aFamilyList
,
828 nsTArray
<nsCString
>& aFontList
)
830 const char kComma
= ',';
832 // append each font name to the list
833 nsAutoCString fontname
;
834 const char *p
, *p_end
;
835 aFamilyList
.BeginReading(p
);
836 aFamilyList
.EndReading(p_end
);
839 const char *nameStart
= p
;
840 while (++p
!= p_end
&& *p
!= kComma
)
843 // pull out a single name and clean out leading/trailing whitespace
844 fontname
= Substring(nameStart
, p
);
845 fontname
.CompressWhitespace(true, true);
847 // append it to the list if it's not empty
848 if (!fontname
.IsEmpty()) {
849 aFontList
.AppendElement(fontname
);
855 void gfxFontUtils::AppendPrefsFontList(const char *aPrefName
,
856 nsTArray
<nsCString
>& aFontList
)
858 // get the list of single-face font families
859 nsAutoCString fontlistValue
;
860 nsresult rv
= Preferences::GetCString(aPrefName
, fontlistValue
);
865 ParseFontList(fontlistValue
, aFontList
);
868 void gfxFontUtils::GetPrefsFontList(const char *aPrefName
,
869 nsTArray
<nsCString
>& aFontList
)
872 AppendPrefsFontList(aPrefName
, aFontList
);
875 // produce a unique font name that is (1) a valid Postscript name and (2) less
876 // than 31 characters in length. Using AddFontMemResourceEx on Windows fails
877 // for names longer than 30 characters in length.
879 #define MAX_B64_LEN 32
881 nsresult
gfxFontUtils::MakeUniqueUserFontName(nsAString
& aName
)
883 nsCOMPtr
<nsIUUIDGenerator
> uuidgen
=
884 do_GetService("@mozilla.org/uuid-generator;1");
885 NS_ENSURE_TRUE(uuidgen
, NS_ERROR_OUT_OF_MEMORY
);
889 NS_ASSERTION(sizeof(guid
) * 2 <= MAX_B64_LEN
, "size of nsID has changed!");
891 nsresult rv
= uuidgen
->GenerateUUIDInPlace(&guid
);
892 NS_ENSURE_SUCCESS(rv
, rv
);
894 char guidB64
[MAX_B64_LEN
] = {0};
896 if (!PL_Base64Encode(reinterpret_cast<char*>(&guid
), sizeof(guid
), guidB64
))
897 return NS_ERROR_FAILURE
;
899 // all b64 characters except for '/' are allowed in Postscript names, so convert / ==> -
901 for (p
= guidB64
; *p
; p
++) {
906 aName
.AssignLiteral(u
"uf");
907 aName
.AppendASCII(guidB64
);
912 // TrueType/OpenType table handling code
914 // need byte aligned structs
917 // name table stores set of name record structures, followed by
918 // large block containing all the strings. name record offset and length
919 // indicates the offset and length within that block.
920 // http://www.microsoft.com/typography/otspec/name.htm
921 struct NameRecordData
{
929 IsValidSFNTVersion(uint32_t version
)
931 // normally 0x00010000, CFF-style OT fonts == 'OTTO' and Apple TT fonts = 'true'
932 // 'typ1' is also possible for old Type 1 fonts in a SFNT container but not supported
933 return version
== 0x10000 ||
934 version
== TRUETYPE_TAG('O','T','T','O') ||
935 version
== TRUETYPE_TAG('t','r','u','e');
939 gfxFontUtils::DetermineFontDataType(const uint8_t *aFontData
, uint32_t aFontDataLength
)
941 // test for OpenType font data
942 // problem: EOT-Lite with 0x10000 length will look like TrueType!
943 if (aFontDataLength
>= sizeof(SFNTHeader
)) {
944 const SFNTHeader
*sfntHeader
= reinterpret_cast<const SFNTHeader
*>(aFontData
);
945 uint32_t sfntVersion
= sfntHeader
->sfntVersion
;
946 if (IsValidSFNTVersion(sfntVersion
)) {
947 return GFX_USERFONT_OPENTYPE
;
952 if (aFontDataLength
>= sizeof(AutoSwap_PRUint32
)) {
953 const AutoSwap_PRUint32
*version
=
954 reinterpret_cast<const AutoSwap_PRUint32
*>(aFontData
);
955 if (uint32_t(*version
) == TRUETYPE_TAG('w','O','F','F')) {
956 return GFX_USERFONT_WOFF
;
958 if (Preferences::GetBool(GFX_PREF_WOFF2_ENABLED
) &&
959 uint32_t(*version
) == TRUETYPE_TAG('w','O','F','2')) {
960 return GFX_USERFONT_WOFF2
;
964 // tests for other formats here
966 return GFX_USERFONT_UNKNOWN
;
970 DirEntryCmp(const void* aKey
, const void* aItem
)
972 int32_t tag
= *static_cast<const int32_t*>(aKey
);
973 const TableDirEntry
* entry
= static_cast<const TableDirEntry
*>(aItem
);
974 return tag
- int32_t(entry
->tag
);
979 gfxFontUtils::FindTableDirEntry(const void* aFontData
, uint32_t aTableTag
)
981 const SFNTHeader
* header
=
982 reinterpret_cast<const SFNTHeader
*>(aFontData
);
983 const TableDirEntry
* dir
=
984 reinterpret_cast<const TableDirEntry
*>(header
+ 1);
985 return static_cast<TableDirEntry
*>
986 (bsearch(&aTableTag
, dir
, uint16_t(header
->numTables
),
987 sizeof(TableDirEntry
), DirEntryCmp
));
992 gfxFontUtils::GetTableFromFontData(const void* aFontData
, uint32_t aTableTag
)
994 const TableDirEntry
* dir
= FindTableDirEntry(aFontData
, aTableTag
);
996 return hb_blob_create(reinterpret_cast<const char*>(aFontData
) +
997 dir
->offset
, dir
->length
,
998 HB_MEMORY_MODE_READONLY
, nullptr, nullptr);
1005 gfxFontUtils::RenameFont(const nsAString
& aName
, const uint8_t *aFontData
,
1006 uint32_t aFontDataLength
, FallibleTArray
<uint8_t> *aNewFont
)
1008 NS_ASSERTION(aNewFont
, "null font data array");
1010 uint64_t dataLength(aFontDataLength
);
1013 static const uint32_t neededNameIDs
[] = {NAME_ID_FAMILY
,
1017 NAME_ID_POSTSCRIPT
};
1019 // calculate new name table size
1020 uint16_t nameCount
= ArrayLength(neededNameIDs
);
1022 // leave room for null-terminator
1023 uint32_t nameStrLength
= (aName
.Length() + 1) * sizeof(char16_t
);
1024 if (nameStrLength
> 65535) {
1025 // The name length _in bytes_ must fit in an unsigned short field;
1026 // therefore, a name longer than this cannot be used.
1027 return NS_ERROR_FAILURE
;
1030 // round name table size up to 4-byte multiple
1031 uint32_t nameTableSize
= (sizeof(NameHeader
) +
1032 sizeof(NameRecord
) * nameCount
+
1036 if (dataLength
+ nameTableSize
> UINT32_MAX
)
1037 return NS_ERROR_FAILURE
;
1039 // bug 505386 - need to handle unpadded font length
1040 uint32_t paddedFontDataSize
= (aFontDataLength
+ 3) & ~3;
1041 uint32_t adjFontDataSize
= paddedFontDataSize
+ nameTableSize
;
1043 // create new buffer: old font data plus new name table
1044 if (!aNewFont
->AppendElements(adjFontDataSize
, fallible
))
1045 return NS_ERROR_OUT_OF_MEMORY
;
1047 // copy the old font data
1048 uint8_t *newFontData
= reinterpret_cast<uint8_t*>(aNewFont
->Elements());
1050 // null the last four bytes in case the font length is not a multiple of 4
1051 memset(newFontData
+ aFontDataLength
, 0, paddedFontDataSize
- aFontDataLength
);
1054 memcpy(newFontData
, aFontData
, aFontDataLength
);
1056 // null out the last 4 bytes for checksum calculations
1057 memset(newFontData
+ adjFontDataSize
- 4, 0, 4);
1059 NameHeader
*nameHeader
= reinterpret_cast<NameHeader
*>(newFontData
+
1060 paddedFontDataSize
);
1063 nameHeader
->format
= 0;
1064 nameHeader
->count
= nameCount
;
1065 nameHeader
->stringOffset
= sizeof(NameHeader
) + nameCount
* sizeof(NameRecord
);
1069 NameRecord
*nameRecord
= reinterpret_cast<NameRecord
*>(nameHeader
+ 1);
1071 for (i
= 0; i
< nameCount
; i
++, nameRecord
++) {
1072 nameRecord
->platformID
= PLATFORM_ID_MICROSOFT
;
1073 nameRecord
->encodingID
= ENCODING_ID_MICROSOFT_UNICODEBMP
;
1074 nameRecord
->languageID
= LANG_ID_MICROSOFT_EN_US
;
1075 nameRecord
->nameID
= neededNameIDs
[i
];
1076 nameRecord
->offset
= 0;
1077 nameRecord
->length
= nameStrLength
;
1080 // -- string data, located after the name records, stored in big-endian form
1081 char16_t
*strData
= reinterpret_cast<char16_t
*>(nameRecord
);
1083 mozilla::NativeEndian::copyAndSwapToBigEndian(strData
,
1084 aName
.BeginReading(),
1086 strData
[aName
.Length()] = 0; // add null termination
1088 // adjust name table header to point to the new name table
1089 SFNTHeader
*sfntHeader
= reinterpret_cast<SFNTHeader
*>(newFontData
);
1091 // table directory entries begin immediately following SFNT header
1092 TableDirEntry
*dirEntry
=
1093 FindTableDirEntry(newFontData
, TRUETYPE_TAG('n','a','m','e'));
1094 // function only called if font validates, so this should always be true
1095 MOZ_ASSERT(dirEntry
, "attempt to rename font with no name table");
1097 uint32_t numTables
= sfntHeader
->numTables
;
1099 // note: dirEntry now points to 'name' table record
1101 // recalculate name table checksum
1102 uint32_t checkSum
= 0;
1103 AutoSwap_PRUint32
*nameData
= reinterpret_cast<AutoSwap_PRUint32
*> (nameHeader
);
1104 AutoSwap_PRUint32
*nameDataEnd
= nameData
+ (nameTableSize
>> 2);
1106 while (nameData
< nameDataEnd
)
1107 checkSum
= checkSum
+ *nameData
++;
1109 // adjust name table entry to point to new name table
1110 dirEntry
->offset
= paddedFontDataSize
;
1111 dirEntry
->length
= nameTableSize
;
1112 dirEntry
->checkSum
= checkSum
;
1115 uint32_t checksum
= 0;
1117 // checksum for font = (checksum of header) + (checksum of tables)
1118 uint32_t headerLen
= sizeof(SFNTHeader
) + sizeof(TableDirEntry
) * numTables
;
1119 const AutoSwap_PRUint32
*headerData
=
1120 reinterpret_cast<const AutoSwap_PRUint32
*>(newFontData
);
1122 // header length is in bytes, checksum calculated in longwords
1123 for (i
= 0; i
< (headerLen
>> 2); i
++, headerData
++) {
1124 checksum
+= *headerData
;
1127 uint32_t headOffset
= 0;
1128 dirEntry
= reinterpret_cast<TableDirEntry
*>(newFontData
+ sizeof(SFNTHeader
));
1130 for (i
= 0; i
< numTables
; i
++, dirEntry
++) {
1131 if (dirEntry
->tag
== TRUETYPE_TAG('h','e','a','d')) {
1132 headOffset
= dirEntry
->offset
;
1134 checksum
+= dirEntry
->checkSum
;
1137 NS_ASSERTION(headOffset
!= 0, "no head table for font");
1139 HeadTable
*headData
= reinterpret_cast<HeadTable
*>(newFontData
+ headOffset
);
1141 headData
->checkSumAdjustment
= HeadTable::HEAD_CHECKSUM_CALC_CONST
- checksum
;
1146 // This is only called after the basic validity of the downloaded sfnt
1147 // data has been checked, so it should never fail to find the name table
1148 // (though it might fail to read it, if memory isn't available);
1149 // other checks here are just for extra paranoia.
1151 gfxFontUtils::GetFullNameFromSFNT(const uint8_t* aFontData
, uint32_t aLength
,
1152 nsACString
& aFullName
)
1154 aFullName
= "(MISSING NAME)"; // should always get replaced
1156 const TableDirEntry
*dirEntry
=
1157 FindTableDirEntry(aFontData
, TRUETYPE_TAG('n','a','m','e'));
1159 // should never fail, as we're only called after font validation succeeded
1160 NS_ENSURE_TRUE(dirEntry
, NS_ERROR_NOT_AVAILABLE
);
1162 uint32_t len
= dirEntry
->length
;
1163 NS_ENSURE_TRUE(aLength
> len
&& aLength
- len
>= dirEntry
->offset
,
1164 NS_ERROR_UNEXPECTED
);
1166 hb_blob_t
*nameBlob
=
1167 hb_blob_create((const char*)aFontData
+ dirEntry
->offset
, len
,
1168 HB_MEMORY_MODE_READONLY
, nullptr, nullptr);
1169 nsresult rv
= GetFullNameFromTable(nameBlob
, aFullName
);
1170 hb_blob_destroy(nameBlob
);
1176 gfxFontUtils::GetFullNameFromTable(hb_blob_t
*aNameTable
,
1177 nsACString
& aFullName
)
1181 gfxFontUtils::ReadCanonicalName(aNameTable
,
1182 gfxFontUtils::NAME_ID_FULL
,
1184 if (NS_SUCCEEDED(rv
) && !name
.IsEmpty()) {
1188 rv
= gfxFontUtils::ReadCanonicalName(aNameTable
,
1189 gfxFontUtils::NAME_ID_FAMILY
,
1191 if (NS_SUCCEEDED(rv
) && !name
.IsEmpty()) {
1192 nsAutoCString styleName
;
1193 rv
= gfxFontUtils::ReadCanonicalName(aNameTable
,
1194 gfxFontUtils::NAME_ID_STYLE
,
1196 if (NS_SUCCEEDED(rv
) && !styleName
.IsEmpty()) {
1198 name
.Append(styleName
);
1204 return NS_ERROR_NOT_AVAILABLE
;
1208 gfxFontUtils::GetFamilyNameFromTable(hb_blob_t
*aNameTable
,
1209 nsACString
& aFamilyName
)
1213 gfxFontUtils::ReadCanonicalName(aNameTable
,
1214 gfxFontUtils::NAME_ID_FAMILY
,
1216 if (NS_SUCCEEDED(rv
) && !name
.IsEmpty()) {
1220 return NS_ERROR_NOT_AVAILABLE
;
1224 #if defined(XP_MACOSX)
1225 CANONICAL_LANG_ID
= gfxFontUtils::LANG_ID_MAC_ENGLISH
,
1226 PLATFORM_ID
= gfxFontUtils::PLATFORM_ID_MAC
1228 CANONICAL_LANG_ID
= gfxFontUtils::LANG_ID_MICROSOFT_EN_US
,
1229 PLATFORM_ID
= gfxFontUtils::PLATFORM_ID_MICROSOFT
1234 gfxFontUtils::ReadNames(const char *aNameData
, uint32_t aDataLen
,
1235 uint32_t aNameID
, int32_t aPlatformID
,
1236 nsTArray
<nsCString
>& aNames
)
1238 return ReadNames(aNameData
, aDataLen
, aNameID
, LANG_ALL
,
1239 aPlatformID
, aNames
);
1243 gfxFontUtils::ReadCanonicalName(hb_blob_t
*aNameTable
, uint32_t aNameID
,
1246 uint32_t nameTableLen
;
1247 const char *nameTable
= hb_blob_get_data(aNameTable
, &nameTableLen
);
1248 return ReadCanonicalName(nameTable
, nameTableLen
, aNameID
, aName
);
1252 gfxFontUtils::ReadCanonicalName(const char *aNameData
, uint32_t aDataLen
,
1253 uint32_t aNameID
, nsCString
& aName
)
1257 nsTArray
<nsCString
> names
;
1259 // first, look for the English name (this will succeed 99% of the time)
1260 rv
= ReadNames(aNameData
, aDataLen
, aNameID
, CANONICAL_LANG_ID
,
1261 PLATFORM_ID
, names
);
1262 NS_ENSURE_SUCCESS(rv
, rv
);
1264 // otherwise, grab names for all languages
1265 if (names
.Length() == 0) {
1266 rv
= ReadNames(aNameData
, aDataLen
, aNameID
, LANG_ALL
,
1267 PLATFORM_ID
, names
);
1268 NS_ENSURE_SUCCESS(rv
, rv
);
1271 #if defined(XP_MACOSX)
1272 // may be dealing with font that only has Microsoft name entries
1273 if (names
.Length() == 0) {
1274 rv
= ReadNames(aNameData
, aDataLen
, aNameID
, LANG_ID_MICROSOFT_EN_US
,
1275 PLATFORM_ID_MICROSOFT
, names
);
1276 NS_ENSURE_SUCCESS(rv
, rv
);
1278 // getting really desperate now, take anything!
1279 if (names
.Length() == 0) {
1280 rv
= ReadNames(aNameData
, aDataLen
, aNameID
, LANG_ALL
,
1281 PLATFORM_ID_MICROSOFT
, names
);
1282 NS_ENSURE_SUCCESS(rv
, rv
);
1287 // return the first name (99.9% of the time names will
1288 // contain a single English name)
1289 if (names
.Length()) {
1290 aName
.Assign(names
[0]);
1294 return NS_ERROR_FAILURE
;
1297 // Charsets to use for decoding Mac platform font names.
1298 // This table is sorted by {encoding, language}, with the wildcard "ANY" being
1299 // greater than any defined values for each field; we use a binary search on both
1300 // fields, and fall back to matching only encoding if necessary
1302 // Some "redundant" entries for specific combinations are included such as
1303 // encoding=roman, lang=english, in order that common entries will be found
1304 // on the first search.
1306 const uint16_t ANY
= 0xffff;
1307 const gfxFontUtils::MacFontNameCharsetMapping
gfxFontUtils::gMacFontNameCharsets
[] =
1309 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_ENGLISH
, MACINTOSH_ENCODING
},
1310 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_ICELANDIC
, X_USER_DEFINED_ENCODING
},
1311 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_TURKISH
, X_USER_DEFINED_ENCODING
},
1312 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_POLISH
, X_USER_DEFINED_ENCODING
},
1313 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_ROMANIAN
, X_USER_DEFINED_ENCODING
},
1314 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_CZECH
, X_USER_DEFINED_ENCODING
},
1315 { ENCODING_ID_MAC_ROMAN
, LANG_ID_MAC_SLOVAK
, X_USER_DEFINED_ENCODING
},
1316 { ENCODING_ID_MAC_ROMAN
, ANY
, MACINTOSH_ENCODING
},
1317 { ENCODING_ID_MAC_JAPANESE
, LANG_ID_MAC_JAPANESE
, SHIFT_JIS_ENCODING
},
1318 { ENCODING_ID_MAC_JAPANESE
, ANY
, SHIFT_JIS_ENCODING
},
1319 { ENCODING_ID_MAC_TRAD_CHINESE
, LANG_ID_MAC_TRAD_CHINESE
, BIG5_ENCODING
},
1320 { ENCODING_ID_MAC_TRAD_CHINESE
, ANY
, BIG5_ENCODING
},
1321 { ENCODING_ID_MAC_KOREAN
, LANG_ID_MAC_KOREAN
, EUC_KR_ENCODING
},
1322 { ENCODING_ID_MAC_KOREAN
, ANY
, EUC_KR_ENCODING
},
1323 { ENCODING_ID_MAC_ARABIC
, LANG_ID_MAC_ARABIC
, X_USER_DEFINED_ENCODING
},
1324 { ENCODING_ID_MAC_ARABIC
, LANG_ID_MAC_URDU
, X_USER_DEFINED_ENCODING
},
1325 { ENCODING_ID_MAC_ARABIC
, LANG_ID_MAC_FARSI
, X_USER_DEFINED_ENCODING
},
1326 { ENCODING_ID_MAC_ARABIC
, ANY
, X_USER_DEFINED_ENCODING
},
1327 { ENCODING_ID_MAC_HEBREW
, LANG_ID_MAC_HEBREW
, X_USER_DEFINED_ENCODING
},
1328 { ENCODING_ID_MAC_HEBREW
, ANY
, X_USER_DEFINED_ENCODING
},
1329 { ENCODING_ID_MAC_GREEK
, ANY
, X_USER_DEFINED_ENCODING
},
1330 { ENCODING_ID_MAC_CYRILLIC
, ANY
, X_MAC_CYRILLIC_ENCODING
},
1331 { ENCODING_ID_MAC_DEVANAGARI
, ANY
, X_USER_DEFINED_ENCODING
},
1332 { ENCODING_ID_MAC_GURMUKHI
, ANY
, X_USER_DEFINED_ENCODING
},
1333 { ENCODING_ID_MAC_GUJARATI
, ANY
, X_USER_DEFINED_ENCODING
},
1334 { ENCODING_ID_MAC_SIMP_CHINESE
, LANG_ID_MAC_SIMP_CHINESE
, GB18030_ENCODING
},
1335 { ENCODING_ID_MAC_SIMP_CHINESE
, ANY
, GB18030_ENCODING
}
1338 const Encoding
* gfxFontUtils::gISOFontNameCharsets
[] =
1340 /* 0 */ WINDOWS_1252_ENCODING
, /* US-ASCII */
1341 /* 1 */ nullptr , /* spec says "ISO 10646" but does not specify encoding form! */
1342 /* 2 */ WINDOWS_1252_ENCODING
/* ISO-8859-1 */
1345 const Encoding
* gfxFontUtils::gMSFontNameCharsets
[] =
1347 /* [0] ENCODING_ID_MICROSOFT_SYMBOL */ UTF_16BE_ENCODING
,
1348 /* [1] ENCODING_ID_MICROSOFT_UNICODEBMP */ UTF_16BE_ENCODING
,
1349 /* [2] ENCODING_ID_MICROSOFT_SHIFTJIS */ SHIFT_JIS_ENCODING
,
1350 /* [3] ENCODING_ID_MICROSOFT_PRC */ nullptr ,
1351 /* [4] ENCODING_ID_MICROSOFT_BIG5 */ BIG5_ENCODING
,
1352 /* [5] ENCODING_ID_MICROSOFT_WANSUNG */ nullptr ,
1353 /* [6] ENCODING_ID_MICROSOFT_JOHAB */ nullptr ,
1354 /* [7] reserved */ nullptr ,
1355 /* [8] reserved */ nullptr ,
1356 /* [9] reserved */ nullptr ,
1357 /*[10] ENCODING_ID_MICROSOFT_UNICODEFULL */ UTF_16BE_ENCODING
1360 struct MacCharsetMappingComparator
1362 typedef gfxFontUtils::MacFontNameCharsetMapping MacFontNameCharsetMapping
;
1363 const MacFontNameCharsetMapping
& mSearchValue
;
1364 explicit MacCharsetMappingComparator(const MacFontNameCharsetMapping
& aSearchValue
)
1365 : mSearchValue(aSearchValue
) {}
1366 int operator()(const MacFontNameCharsetMapping
& aEntry
) const {
1367 if (mSearchValue
< aEntry
) {
1370 if (aEntry
< mSearchValue
) {
1377 // Return the Encoding object we should use to decode a font name
1378 // given the name table attributes.
1379 // Special return values:
1380 // X_USER_DEFINED_ENCODING One of Mac legacy encodings that is not a part
1381 // of Encoding Standard
1382 // nullptr unknown charset, do not attempt conversion
1384 gfxFontUtils::GetCharsetForFontName(uint16_t aPlatform
, uint16_t aScript
, uint16_t aLanguage
)
1388 case PLATFORM_ID_UNICODE
:
1389 return UTF_16BE_ENCODING
;
1391 case PLATFORM_ID_MAC
:
1393 MacFontNameCharsetMapping searchValue
= { aScript
, aLanguage
, nullptr };
1394 for (uint32_t i
= 0; i
< 2; ++i
) {
1396 if (BinarySearchIf(gMacFontNameCharsets
, 0, ArrayLength(gMacFontNameCharsets
),
1397 MacCharsetMappingComparator(searchValue
), &idx
)) {
1398 return gMacFontNameCharsets
[idx
].mEncoding
;
1401 // no match, so try again finding one in any language
1402 searchValue
.mLanguage
= ANY
;
1407 case PLATFORM_ID_ISO
:
1408 if (aScript
< ArrayLength(gISOFontNameCharsets
)) {
1409 return gISOFontNameCharsets
[aScript
];
1413 case PLATFORM_ID_MICROSOFT
:
1414 if (aScript
< ArrayLength(gMSFontNameCharsets
)) {
1415 return gMSFontNameCharsets
[aScript
];
1425 StartsWith(const nsACString
& string
, const char (&prefix
)[N
])
1427 if (N
- 1 > string
.Length()) {
1430 return memcmp(string
.Data(), prefix
, N
- 1) == 0;
1433 // convert a raw name from the name table to an nsString, if possible;
1434 // return value indicates whether conversion succeeded
1436 gfxFontUtils::DecodeFontName(const char *aNameData
, int32_t aByteLen
,
1437 uint32_t aPlatformCode
, uint32_t aScriptCode
,
1438 uint32_t aLangCode
, nsACString
& aName
)
1440 if (aByteLen
<= 0) {
1441 NS_WARNING("empty font name");
1446 auto encoding
= GetCharsetForFontName(aPlatformCode
, aScriptCode
, aLangCode
);
1449 // nullptr -> unknown charset
1454 SprintfLiteral(warnBuf
, "skipping font name, unknown charset %d:%d:%d for <%.*s>",
1455 aPlatformCode
, aScriptCode
, aLangCode
, aByteLen
, aNameData
);
1456 NS_WARNING(warnBuf
);
1461 if (encoding
== X_USER_DEFINED_ENCODING
) {
1463 // Special case for macOS only: support legacy Mac encodings
1464 // that aren't part of the Encoding Standard.
1465 if (aPlatformCode
== PLATFORM_ID_MAC
) {
1467 CFStringCreateWithBytes(kCFAllocatorDefault
,
1468 (const UInt8
*)aNameData
, aByteLen
,
1469 aScriptCode
, false);
1471 CFIndex length
= CFStringGetLength(str
);
1472 nsAutoString name16
;
1473 name16
.SetLength(length
);
1474 CFStringGetCharacters(str
, CFRangeMake(0, length
),
1475 (UniChar
*)name16
.BeginWriting());
1477 CopyUTF16toUTF8(name16
, aName
);
1482 NS_WARNING("failed to get the decoder for a font name string");
1486 auto rv
= encoding
->DecodeWithoutBOMHandling(
1487 nsDependentCSubstring(aNameData
, aByteLen
), aName
);
1488 return NS_SUCCEEDED(rv
);
1492 gfxFontUtils::ReadNames(const char *aNameData
, uint32_t aDataLen
,
1494 int32_t aLangID
, int32_t aPlatformID
,
1495 nsTArray
<nsCString
>& aNames
)
1497 NS_ASSERTION(aDataLen
!= 0, "null name table");
1500 return NS_ERROR_FAILURE
;
1503 // -- name table data
1504 const NameHeader
*nameHeader
= reinterpret_cast<const NameHeader
*>(aNameData
);
1506 uint32_t nameCount
= nameHeader
->count
;
1508 // -- sanity check the number of name records
1509 if (uint64_t(nameCount
) * sizeof(NameRecord
) > aDataLen
) {
1510 NS_WARNING("invalid font (name table data)");
1511 return NS_ERROR_FAILURE
;
1514 // -- iterate through name records
1515 const NameRecord
*nameRecord
1516 = reinterpret_cast<const NameRecord
*>(aNameData
+ sizeof(NameHeader
));
1517 uint64_t nameStringsBase
= uint64_t(nameHeader
->stringOffset
);
1520 for (i
= 0; i
< nameCount
; i
++, nameRecord
++) {
1521 uint32_t platformID
;
1523 // skip over unwanted nameID's
1524 if (uint32_t(nameRecord
->nameID
) != aNameID
) {
1528 // skip over unwanted platform data
1529 platformID
= nameRecord
->platformID
;
1530 if (aPlatformID
!= PLATFORM_ALL
&&
1531 platformID
!= uint32_t(aPlatformID
)) {
1535 // skip over unwanted languages
1536 if (aLangID
!= LANG_ALL
&&
1537 uint32_t(nameRecord
->languageID
) != uint32_t(aLangID
)) {
1541 // add name to names array
1543 // -- calculate string location
1544 uint32_t namelen
= nameRecord
->length
;
1545 uint32_t nameoff
= nameRecord
->offset
; // offset from base of string storage
1547 if (nameStringsBase
+ uint64_t(nameoff
) + uint64_t(namelen
)
1549 NS_WARNING("invalid font (name table strings)");
1550 return NS_ERROR_FAILURE
;
1553 // -- decode if necessary and make nsString
1556 DecodeFontName(aNameData
+ nameStringsBase
+ nameoff
, namelen
,
1557 platformID
, uint32_t(nameRecord
->encodingID
),
1558 uint32_t(nameRecord
->languageID
), name
);
1560 uint32_t k
, numNames
;
1561 bool foundName
= false;
1563 numNames
= aNames
.Length();
1564 for (k
= 0; k
< numNames
; k
++) {
1565 if (name
.Equals(aNames
[k
])) {
1572 aNames
.AppendElement(name
);
1581 struct COLRBaseGlyphRecord
{
1582 AutoSwap_PRUint16 glyphId
;
1583 AutoSwap_PRUint16 firstLayerIndex
;
1584 AutoSwap_PRUint16 numLayers
;
1587 struct COLRLayerRecord
{
1588 AutoSwap_PRUint16 glyphId
;
1589 AutoSwap_PRUint16 paletteEntryIndex
;
1592 struct CPALColorRecord
{
1602 gfxFontUtils::ValidateColorGlyphs(hb_blob_t
* aCOLR
, hb_blob_t
* aCPAL
)
1604 unsigned int colrLength
;
1605 const COLRHeader
* colr
=
1606 reinterpret_cast<const COLRHeader
*>(hb_blob_get_data(aCOLR
, &colrLength
));
1607 unsigned int cpalLength
;
1608 const CPALHeaderVersion0
* cpal
=
1609 reinterpret_cast<const CPALHeaderVersion0
*>(hb_blob_get_data(aCPAL
, &cpalLength
));
1611 if (!colr
|| !cpal
|| !colrLength
|| !cpalLength
) {
1615 if (uint16_t(colr
->version
) != 0 || uint16_t(cpal
->version
) != 0) {
1616 // We only support version 0 headers.
1620 const uint32_t offsetBaseGlyphRecord
= colr
->offsetBaseGlyphRecord
;
1621 const uint16_t numBaseGlyphRecord
= colr
->numBaseGlyphRecord
;
1622 const uint32_t offsetLayerRecord
= colr
->offsetLayerRecord
;
1623 const uint16_t numLayerRecords
= colr
->numLayerRecords
;
1625 const uint32_t offsetFirstColorRecord
= cpal
->offsetFirstColorRecord
;
1626 const uint16_t numColorRecords
= cpal
->numColorRecords
;
1627 const uint32_t numPaletteEntries
= cpal
->numPaletteEntries
;
1629 if (offsetBaseGlyphRecord
>= colrLength
) {
1633 if (offsetLayerRecord
>= colrLength
) {
1637 if (offsetFirstColorRecord
>= cpalLength
) {
1641 if (!numPaletteEntries
) {
1645 if (sizeof(COLRBaseGlyphRecord
) * numBaseGlyphRecord
>
1646 colrLength
- offsetBaseGlyphRecord
) {
1647 // COLR base glyph record will be overflow
1651 if (sizeof(COLRLayerRecord
) * numLayerRecords
>
1652 colrLength
- offsetLayerRecord
) {
1653 // COLR layer record will be overflow
1657 if (sizeof(CPALColorRecord
) * numColorRecords
>
1658 cpalLength
- offsetFirstColorRecord
) {
1659 // CPAL color record will be overflow
1663 if (numPaletteEntries
* uint16_t(cpal
->numPalettes
) != numColorRecords
) {
1664 // palette of CPAL color record will be overflow.
1668 uint16_t lastGlyphId
= 0;
1669 const COLRBaseGlyphRecord
* baseGlyph
=
1670 reinterpret_cast<const COLRBaseGlyphRecord
*>(
1671 reinterpret_cast<const uint8_t*>(colr
) + offsetBaseGlyphRecord
);
1673 for (uint16_t i
= 0; i
< numBaseGlyphRecord
; i
++, baseGlyph
++) {
1674 const uint32_t firstLayerIndex
= baseGlyph
->firstLayerIndex
;
1675 const uint16_t numLayers
= baseGlyph
->numLayers
;
1676 const uint16_t glyphId
= baseGlyph
->glyphId
;
1678 if (lastGlyphId
&& lastGlyphId
>= glyphId
) {
1679 // glyphId must be sorted
1682 lastGlyphId
= glyphId
;
1688 if (firstLayerIndex
+ numLayers
> numLayerRecords
) {
1689 // layer length of target glyph is overflow
1694 const COLRLayerRecord
* layer
=
1695 reinterpret_cast<const COLRLayerRecord
*>(
1696 reinterpret_cast<const uint8_t*>(colr
) + offsetLayerRecord
);
1698 for (uint16_t i
= 0; i
< numLayerRecords
; i
++, layer
++) {
1699 if (uint16_t(layer
->paletteEntryIndex
) >= numPaletteEntries
&&
1700 uint16_t(layer
->paletteEntryIndex
) != 0xFFFF) {
1701 // CPAL palette entry record is overflow
1710 CompareBaseGlyph(const void* key
, const void* data
)
1712 uint32_t glyphId
= (uint32_t)(uintptr_t)key
;
1713 const COLRBaseGlyphRecord
* baseGlyph
=
1714 reinterpret_cast<const COLRBaseGlyphRecord
*>(data
);
1715 uint32_t baseGlyphId
= uint16_t(baseGlyph
->glyphId
);
1717 if (baseGlyphId
== glyphId
) {
1721 return baseGlyphId
> glyphId
? -1 : 1;
1725 COLRBaseGlyphRecord
*
1726 LookForBaseGlyphRecord(const COLRHeader
* aCOLR
, uint32_t aGlyphId
)
1728 const uint8_t* baseGlyphRecords
=
1729 reinterpret_cast<const uint8_t*>(aCOLR
) +
1730 uint32_t(aCOLR
->offsetBaseGlyphRecord
);
1731 // BaseGlyphRecord is sorted by glyphId
1732 return reinterpret_cast<COLRBaseGlyphRecord
*>(
1733 bsearch((void*)(uintptr_t)aGlyphId
,
1735 uint16_t(aCOLR
->numBaseGlyphRecord
),
1736 sizeof(COLRBaseGlyphRecord
),
1741 gfxFontUtils::GetColorGlyphLayers(hb_blob_t
* aCOLR
,
1744 const mozilla::gfx::Color
& aDefaultColor
,
1745 nsTArray
<uint16_t>& aGlyphs
,
1746 nsTArray
<mozilla::gfx::Color
>& aColors
)
1748 unsigned int blobLength
;
1749 const COLRHeader
* colr
=
1750 reinterpret_cast<const COLRHeader
*>(hb_blob_get_data(aCOLR
,
1752 MOZ_ASSERT(colr
, "Cannot get COLR raw data");
1753 MOZ_ASSERT(blobLength
, "Found COLR data, but length is 0");
1755 COLRBaseGlyphRecord
* baseGlyph
= LookForBaseGlyphRecord(colr
, aGlyphId
);
1760 const CPALHeaderVersion0
* cpal
=
1761 reinterpret_cast<const CPALHeaderVersion0
*>(
1762 hb_blob_get_data(aCPAL
, &blobLength
));
1763 MOZ_ASSERT(cpal
, "Cannot get CPAL raw data");
1764 MOZ_ASSERT(blobLength
, "Found CPAL data, but length is 0");
1766 const COLRLayerRecord
* layer
=
1767 reinterpret_cast<const COLRLayerRecord
*>(
1768 reinterpret_cast<const uint8_t*>(colr
) +
1769 uint32_t(colr
->offsetLayerRecord
) +
1770 sizeof(COLRLayerRecord
) * uint16_t(baseGlyph
->firstLayerIndex
));
1771 const uint16_t numLayers
= baseGlyph
->numLayers
;
1772 const uint32_t offsetFirstColorRecord
= cpal
->offsetFirstColorRecord
;
1774 for (uint16_t layerIndex
= 0; layerIndex
< numLayers
; layerIndex
++) {
1775 aGlyphs
.AppendElement(uint16_t(layer
->glyphId
));
1776 if (uint16_t(layer
->paletteEntryIndex
) == 0xFFFF) {
1777 aColors
.AppendElement(aDefaultColor
);
1779 const CPALColorRecord
* color
=
1780 reinterpret_cast<const CPALColorRecord
*>(
1781 reinterpret_cast<const uint8_t*>(cpal
) +
1782 offsetFirstColorRecord
+
1783 sizeof(CPALColorRecord
) * uint16_t(layer
->paletteEntryIndex
));
1784 aColors
.AppendElement(mozilla::gfx::Color(color
->red
/ 255.0,
1785 color
->green
/ 255.0,
1786 color
->blue
/ 255.0,
1787 color
->alpha
/ 255.0));
1795 gfxFontUtils::GetVariationInstances(gfxFontEntry
* aFontEntry
,
1796 nsTArray
<gfxFontVariationInstance
>& aInstances
)
1798 MOZ_ASSERT(aInstances
.IsEmpty());
1800 if (!aFontEntry
->HasVariations()) {
1804 // Some platforms don't offer a simple API to return the list of instances,
1805 // so we have to interpret the 'fvar' table ourselves.
1807 // https://www.microsoft.com/typography/otspec/fvar.htm#fvarHeader
1809 AutoSwap_PRUint16 majorVersion
;
1810 AutoSwap_PRUint16 minorVersion
;
1811 AutoSwap_PRUint16 axesArrayOffset
;
1812 AutoSwap_PRUint16 reserved
;
1813 AutoSwap_PRUint16 axisCount
;
1814 AutoSwap_PRUint16 axisSize
;
1815 AutoSwap_PRUint16 instanceCount
;
1816 AutoSwap_PRUint16 instanceSize
;
1819 // https://www.microsoft.com/typography/otspec/fvar.htm#variationAxisRecord
1821 AutoSwap_PRUint32 axisTag
;
1822 AutoSwap_PRInt32 minValue
;
1823 AutoSwap_PRInt32 defaultValue
;
1824 AutoSwap_PRInt32 maxValue
;
1825 AutoSwap_PRUint16 flags
;
1826 AutoSwap_PRUint16 axisNameID
;
1829 // https://www.microsoft.com/typography/otspec/fvar.htm#instanceRecord
1830 struct InstanceRecord
{
1831 AutoSwap_PRUint16 subfamilyNameID
;
1832 AutoSwap_PRUint16 flags
;
1833 AutoSwap_PRInt32 coordinates
[1]; // variable-size array [axisCount]
1834 // The variable-length 'coordinates' array may be followed by an
1835 // optional extra field 'postScriptNameID'. We can't directly
1836 // represent this in the struct, because its offset varies depending
1837 // on the number of axes present.
1838 // (Not currently used by our code here anyhow.)
1839 // AutoSwap_PRUint16 postScriptNameID;
1842 // Helper to ensure we free a font table when we return.
1845 explicit AutoHBBlob(hb_blob_t
* aBlob
) : mBlob(aBlob
)
1850 hb_blob_destroy(mBlob
);
1854 operator hb_blob_t
* () { return mBlob
; }
1857 hb_blob_t
* const mBlob
;
1860 // Load the two font tables we need as harfbuzz blobs; if either is absent,
1862 AutoHBBlob
fvarTable(aFontEntry
->GetFontTable(TRUETYPE_TAG('f','v','a','r')));
1863 AutoHBBlob
nameTable(aFontEntry
->GetFontTable(TRUETYPE_TAG('n','a','m','e')));
1864 if (!fvarTable
|| !nameTable
) {
1868 const char* data
= hb_blob_get_data(fvarTable
, &len
);
1869 if (len
< sizeof(FvarHeader
)) {
1872 // Read the fields of the table header; bail out if it looks broken.
1873 auto fvar
= reinterpret_cast<const FvarHeader
*>(data
);
1874 if (uint16_t(fvar
->majorVersion
) != 1 ||
1875 uint16_t(fvar
->minorVersion
) != 0 ||
1876 uint16_t(fvar
->reserved
) != 2) {
1879 uint16_t axisCount
= fvar
->axisCount
;
1880 uint16_t axisSize
= fvar
->axisSize
;
1881 uint16_t instanceCount
= fvar
->instanceCount
;
1882 uint16_t instanceSize
= fvar
->instanceSize
;
1883 if (axisCount
== 0 || // no axes?
1884 // https://www.microsoft.com/typography/otspec/fvar.htm#axisSize
1885 axisSize
!= 20 || // required value for current table version
1886 instanceCount
== 0 || // no instances?
1887 // https://www.microsoft.com/typography/otspec/fvar.htm#instanceSize
1888 (instanceSize
!= axisCount
* sizeof(int32_t) + 4 &&
1889 instanceSize
!= axisCount
* sizeof(int32_t) + 6)) {
1892 // Check that axis array will not exceed table size
1893 uint16_t axesOffset
= fvar
->axesArrayOffset
;
1894 if (axesOffset
+ uint32_t(axisCount
) * axisSize
> len
) {
1897 // Get pointer to the array of axis records
1898 auto axes
= reinterpret_cast<const AxisRecord
*>(data
+ axesOffset
);
1899 // Get address of instance array, and check it doesn't overflow table size.
1900 // https://www.microsoft.com/typography/otspec/fvar.htm#axisAndInstanceArrays
1901 auto instData
= data
+ axesOffset
+ axisCount
* axisSize
;
1902 if (instData
+ uint32_t(instanceCount
) * instanceSize
> data
+ len
) {
1905 aInstances
.SetCapacity(instanceCount
);
1906 for (unsigned i
= 0; i
< instanceCount
; ++i
, instData
+= instanceSize
) {
1907 // Typed pointer to the current instance record, to read its fields.
1908 auto inst
= reinterpret_cast<const InstanceRecord
*>(instData
);
1909 // Pointer to the coordinates array within the instance record.
1910 // This array has axisCount elements, and is included in instanceSize
1911 // (which depends on axisCount, and was validated above) so we know
1912 // access to coords[j] below will not be outside the table bounds.
1913 auto coords
= &inst
->coordinates
[0];
1914 gfxFontVariationInstance instance
;
1915 uint16_t nameID
= inst
->subfamilyNameID
;
1917 ReadCanonicalName(nameTable
, nameID
, instance
.mName
);
1918 if (NS_FAILED(rv
)) {
1919 // If no name was available for the instance, ignore it.
1922 instance
.mValues
.SetCapacity(axisCount
);
1923 for (unsigned j
= 0; j
< axisCount
; ++j
) {
1924 gfxFontVariationValue value
;
1925 value
.mAxis
= axes
[j
].axisTag
;
1926 value
.mValue
= int32_t(coords
[j
]) / 65536.0;
1927 instance
.mValues
.AppendElement(value
);
1929 aInstances
.AppendElement(instance
);
1937 gfxFontUtils::IsCffFont(const uint8_t* aFontData
)
1939 // this is only called after aFontData has passed basic validation,
1940 // so we know there is enough data present to allow us to read the version!
1941 const SFNTHeader
*sfntHeader
= reinterpret_cast<const SFNTHeader
*>(aFontData
);
1942 return (sfntHeader
->sfntVersion
== TRUETYPE_TAG('O','T','T','O'));
1947 #undef acceptablePlatform
1949 #undef isUVSEncoding