Tue, 06 Jan 2015 21:39:09 +0100
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 | #ifndef GFX_FONT_UTILS_H |
michael@0 | 7 | #define GFX_FONT_UTILS_H |
michael@0 | 8 | |
michael@0 | 9 | #include "gfxPlatform.h" |
michael@0 | 10 | #include "nsComponentManagerUtils.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "mozilla/Likely.h" |
michael@0 | 14 | #include "mozilla/Endian.h" |
michael@0 | 15 | #include "mozilla/MemoryReporting.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "zlib.h" |
michael@0 | 18 | #include <algorithm> |
michael@0 | 19 | |
michael@0 | 20 | /* Bug 341128 - w32api defines min/max which causes problems with <bitset> */ |
michael@0 | 21 | #ifdef __MINGW32__ |
michael@0 | 22 | #undef min |
michael@0 | 23 | #undef max |
michael@0 | 24 | #endif |
michael@0 | 25 | |
michael@0 | 26 | typedef struct hb_blob_t hb_blob_t; |
michael@0 | 27 | |
michael@0 | 28 | class gfxSparseBitSet { |
michael@0 | 29 | private: |
michael@0 | 30 | enum { BLOCK_SIZE = 32 }; // ==> 256 codepoints per block |
michael@0 | 31 | enum { BLOCK_SIZE_BITS = BLOCK_SIZE * 8 }; |
michael@0 | 32 | enum { BLOCK_INDEX_SHIFT = 8 }; |
michael@0 | 33 | |
michael@0 | 34 | struct Block { |
michael@0 | 35 | Block(const Block& aBlock) { memcpy(mBits, aBlock.mBits, sizeof(mBits)); } |
michael@0 | 36 | Block(unsigned char memsetValue = 0) { memset(mBits, memsetValue, BLOCK_SIZE); } |
michael@0 | 37 | uint8_t mBits[BLOCK_SIZE]; |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | public: |
michael@0 | 41 | gfxSparseBitSet() { } |
michael@0 | 42 | gfxSparseBitSet(const gfxSparseBitSet& aBitset) { |
michael@0 | 43 | uint32_t len = aBitset.mBlocks.Length(); |
michael@0 | 44 | mBlocks.AppendElements(len); |
michael@0 | 45 | for (uint32_t i = 0; i < len; ++i) { |
michael@0 | 46 | Block *block = aBitset.mBlocks[i]; |
michael@0 | 47 | if (block) |
michael@0 | 48 | mBlocks[i] = new Block(*block); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | bool Equals(const gfxSparseBitSet *aOther) const { |
michael@0 | 53 | if (mBlocks.Length() != aOther->mBlocks.Length()) { |
michael@0 | 54 | return false; |
michael@0 | 55 | } |
michael@0 | 56 | size_t n = mBlocks.Length(); |
michael@0 | 57 | for (size_t i = 0; i < n; ++i) { |
michael@0 | 58 | const Block *b1 = mBlocks[i]; |
michael@0 | 59 | const Block *b2 = aOther->mBlocks[i]; |
michael@0 | 60 | if (!b1 != !b2) { |
michael@0 | 61 | return false; |
michael@0 | 62 | } |
michael@0 | 63 | if (!b1) { |
michael@0 | 64 | continue; |
michael@0 | 65 | } |
michael@0 | 66 | if (memcmp(&b1->mBits, &b2->mBits, BLOCK_SIZE) != 0) { |
michael@0 | 67 | return false; |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | return true; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | bool test(uint32_t aIndex) const { |
michael@0 | 74 | NS_ASSERTION(mBlocks.DebugGetHeader(), "mHdr is null, this is bad"); |
michael@0 | 75 | uint32_t blockIndex = aIndex/BLOCK_SIZE_BITS; |
michael@0 | 76 | if (blockIndex >= mBlocks.Length()) |
michael@0 | 77 | return false; |
michael@0 | 78 | Block *block = mBlocks[blockIndex]; |
michael@0 | 79 | if (!block) |
michael@0 | 80 | return false; |
michael@0 | 81 | return ((block->mBits[(aIndex>>3) & (BLOCK_SIZE - 1)]) & (1 << (aIndex & 0x7))) != 0; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | #if PR_LOGGING |
michael@0 | 85 | // dump out contents of bitmap |
michael@0 | 86 | void Dump(const char* aPrefix, eGfxLog aWhichLog) const; |
michael@0 | 87 | #endif |
michael@0 | 88 | |
michael@0 | 89 | bool TestRange(uint32_t aStart, uint32_t aEnd) { |
michael@0 | 90 | uint32_t startBlock, endBlock, blockLen; |
michael@0 | 91 | |
michael@0 | 92 | // start point is beyond the end of the block array? return false immediately |
michael@0 | 93 | startBlock = aStart >> BLOCK_INDEX_SHIFT; |
michael@0 | 94 | blockLen = mBlocks.Length(); |
michael@0 | 95 | if (startBlock >= blockLen) return false; |
michael@0 | 96 | |
michael@0 | 97 | // check for blocks in range, if none, return false |
michael@0 | 98 | uint32_t blockIndex; |
michael@0 | 99 | bool hasBlocksInRange = false; |
michael@0 | 100 | |
michael@0 | 101 | endBlock = aEnd >> BLOCK_INDEX_SHIFT; |
michael@0 | 102 | blockIndex = startBlock; |
michael@0 | 103 | for (blockIndex = startBlock; blockIndex <= endBlock; blockIndex++) { |
michael@0 | 104 | if (blockIndex < blockLen && mBlocks[blockIndex]) |
michael@0 | 105 | hasBlocksInRange = true; |
michael@0 | 106 | } |
michael@0 | 107 | if (!hasBlocksInRange) return false; |
michael@0 | 108 | |
michael@0 | 109 | Block *block; |
michael@0 | 110 | uint32_t i, start, end; |
michael@0 | 111 | |
michael@0 | 112 | // first block, check bits |
michael@0 | 113 | if ((block = mBlocks[startBlock])) { |
michael@0 | 114 | start = aStart; |
michael@0 | 115 | end = std::min(aEnd, ((startBlock+1) << BLOCK_INDEX_SHIFT) - 1); |
michael@0 | 116 | for (i = start; i <= end; i++) { |
michael@0 | 117 | if ((block->mBits[(i>>3) & (BLOCK_SIZE - 1)]) & (1 << (i & 0x7))) |
michael@0 | 118 | return true; |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | if (endBlock == startBlock) return false; |
michael@0 | 122 | |
michael@0 | 123 | // [2..n-1] blocks check bytes |
michael@0 | 124 | for (blockIndex = startBlock + 1; blockIndex < endBlock; blockIndex++) { |
michael@0 | 125 | uint32_t index; |
michael@0 | 126 | |
michael@0 | 127 | if (blockIndex >= blockLen || !(block = mBlocks[blockIndex])) continue; |
michael@0 | 128 | for (index = 0; index < BLOCK_SIZE; index++) { |
michael@0 | 129 | if (block->mBits[index]) |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // last block, check bits |
michael@0 | 135 | if (endBlock < blockLen && (block = mBlocks[endBlock])) { |
michael@0 | 136 | start = endBlock << BLOCK_INDEX_SHIFT; |
michael@0 | 137 | end = aEnd; |
michael@0 | 138 | for (i = start; i <= end; i++) { |
michael@0 | 139 | if ((block->mBits[(i>>3) & (BLOCK_SIZE - 1)]) & (1 << (i & 0x7))) |
michael@0 | 140 | return true; |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | return false; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void set(uint32_t aIndex) { |
michael@0 | 148 | uint32_t blockIndex = aIndex/BLOCK_SIZE_BITS; |
michael@0 | 149 | if (blockIndex >= mBlocks.Length()) { |
michael@0 | 150 | nsAutoPtr<Block> *blocks = mBlocks.AppendElements(blockIndex + 1 - mBlocks.Length()); |
michael@0 | 151 | if (MOZ_UNLIKELY(!blocks)) // OOM |
michael@0 | 152 | return; |
michael@0 | 153 | } |
michael@0 | 154 | Block *block = mBlocks[blockIndex]; |
michael@0 | 155 | if (!block) { |
michael@0 | 156 | block = new Block; |
michael@0 | 157 | mBlocks[blockIndex] = block; |
michael@0 | 158 | } |
michael@0 | 159 | block->mBits[(aIndex>>3) & (BLOCK_SIZE - 1)] |= 1 << (aIndex & 0x7); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | void set(uint32_t aIndex, bool aValue) { |
michael@0 | 163 | if (aValue) |
michael@0 | 164 | set(aIndex); |
michael@0 | 165 | else |
michael@0 | 166 | clear(aIndex); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | void SetRange(uint32_t aStart, uint32_t aEnd) { |
michael@0 | 170 | const uint32_t startIndex = aStart/BLOCK_SIZE_BITS; |
michael@0 | 171 | const uint32_t endIndex = aEnd/BLOCK_SIZE_BITS; |
michael@0 | 172 | |
michael@0 | 173 | if (endIndex >= mBlocks.Length()) { |
michael@0 | 174 | uint32_t numNewBlocks = endIndex + 1 - mBlocks.Length(); |
michael@0 | 175 | nsAutoPtr<Block> *blocks = mBlocks.AppendElements(numNewBlocks); |
michael@0 | 176 | if (MOZ_UNLIKELY(!blocks)) // OOM |
michael@0 | 177 | return; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | for (uint32_t i = startIndex; i <= endIndex; ++i) { |
michael@0 | 181 | const uint32_t blockFirstBit = i * BLOCK_SIZE_BITS; |
michael@0 | 182 | const uint32_t blockLastBit = blockFirstBit + BLOCK_SIZE_BITS - 1; |
michael@0 | 183 | |
michael@0 | 184 | Block *block = mBlocks[i]; |
michael@0 | 185 | if (!block) { |
michael@0 | 186 | bool fullBlock = false; |
michael@0 | 187 | if (aStart <= blockFirstBit && aEnd >= blockLastBit) |
michael@0 | 188 | fullBlock = true; |
michael@0 | 189 | |
michael@0 | 190 | block = new Block(fullBlock ? 0xFF : 0); |
michael@0 | 191 | mBlocks[i] = block; |
michael@0 | 192 | |
michael@0 | 193 | if (fullBlock) |
michael@0 | 194 | continue; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | const uint32_t start = aStart > blockFirstBit ? aStart - blockFirstBit : 0; |
michael@0 | 198 | const uint32_t end = std::min<uint32_t>(aEnd - blockFirstBit, BLOCK_SIZE_BITS - 1); |
michael@0 | 199 | |
michael@0 | 200 | for (uint32_t bit = start; bit <= end; ++bit) { |
michael@0 | 201 | block->mBits[bit>>3] |= 1 << (bit & 0x7); |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | void clear(uint32_t aIndex) { |
michael@0 | 207 | uint32_t blockIndex = aIndex/BLOCK_SIZE_BITS; |
michael@0 | 208 | if (blockIndex >= mBlocks.Length()) { |
michael@0 | 209 | nsAutoPtr<Block> *blocks = mBlocks.AppendElements(blockIndex + 1 - mBlocks.Length()); |
michael@0 | 210 | if (MOZ_UNLIKELY(!blocks)) // OOM |
michael@0 | 211 | return; |
michael@0 | 212 | } |
michael@0 | 213 | Block *block = mBlocks[blockIndex]; |
michael@0 | 214 | if (!block) { |
michael@0 | 215 | return; |
michael@0 | 216 | } |
michael@0 | 217 | block->mBits[(aIndex>>3) & (BLOCK_SIZE - 1)] &= ~(1 << (aIndex & 0x7)); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | void ClearRange(uint32_t aStart, uint32_t aEnd) { |
michael@0 | 221 | const uint32_t startIndex = aStart/BLOCK_SIZE_BITS; |
michael@0 | 222 | const uint32_t endIndex = aEnd/BLOCK_SIZE_BITS; |
michael@0 | 223 | |
michael@0 | 224 | if (endIndex >= mBlocks.Length()) { |
michael@0 | 225 | uint32_t numNewBlocks = endIndex + 1 - mBlocks.Length(); |
michael@0 | 226 | nsAutoPtr<Block> *blocks = mBlocks.AppendElements(numNewBlocks); |
michael@0 | 227 | if (MOZ_UNLIKELY(!blocks)) // OOM |
michael@0 | 228 | return; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | for (uint32_t i = startIndex; i <= endIndex; ++i) { |
michael@0 | 232 | const uint32_t blockFirstBit = i * BLOCK_SIZE_BITS; |
michael@0 | 233 | |
michael@0 | 234 | Block *block = mBlocks[i]; |
michael@0 | 235 | if (!block) { |
michael@0 | 236 | // any nonexistent block is implicitly all clear, |
michael@0 | 237 | // so there's no need to even create it |
michael@0 | 238 | continue; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | const uint32_t start = aStart > blockFirstBit ? aStart - blockFirstBit : 0; |
michael@0 | 242 | const uint32_t end = std::min<uint32_t>(aEnd - blockFirstBit, BLOCK_SIZE_BITS - 1); |
michael@0 | 243 | |
michael@0 | 244 | for (uint32_t bit = start; bit <= end; ++bit) { |
michael@0 | 245 | block->mBits[bit>>3] &= ~(1 << (bit & 0x7)); |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { |
michael@0 | 251 | size_t total = mBlocks.SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 252 | for (uint32_t i = 0; i < mBlocks.Length(); i++) { |
michael@0 | 253 | if (mBlocks[i]) { |
michael@0 | 254 | total += aMallocSizeOf(mBlocks[i]); |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | return total; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { |
michael@0 | 261 | return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | // clear out all blocks in the array |
michael@0 | 265 | void reset() { |
michael@0 | 266 | uint32_t i; |
michael@0 | 267 | for (i = 0; i < mBlocks.Length(); i++) |
michael@0 | 268 | mBlocks[i] = nullptr; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | // set this bitset to the union of its current contents and another |
michael@0 | 272 | void Union(const gfxSparseBitSet& aBitset) { |
michael@0 | 273 | // ensure mBlocks is large enough |
michael@0 | 274 | uint32_t blockCount = aBitset.mBlocks.Length(); |
michael@0 | 275 | if (blockCount > mBlocks.Length()) { |
michael@0 | 276 | uint32_t needed = blockCount - mBlocks.Length(); |
michael@0 | 277 | nsAutoPtr<Block> *blocks = mBlocks.AppendElements(needed); |
michael@0 | 278 | if (MOZ_UNLIKELY(!blocks)) { // OOM |
michael@0 | 279 | return; |
michael@0 | 280 | } |
michael@0 | 281 | } |
michael@0 | 282 | // for each block that may be present in aBitset... |
michael@0 | 283 | for (uint32_t i = 0; i < blockCount; ++i) { |
michael@0 | 284 | // if it is missing (implicitly empty), just skip |
michael@0 | 285 | if (!aBitset.mBlocks[i]) { |
michael@0 | 286 | continue; |
michael@0 | 287 | } |
michael@0 | 288 | // if the block is missing in this set, just copy the other |
michael@0 | 289 | if (!mBlocks[i]) { |
michael@0 | 290 | mBlocks[i] = new Block(*aBitset.mBlocks[i]); |
michael@0 | 291 | continue; |
michael@0 | 292 | } |
michael@0 | 293 | // else set existing block to the union of both |
michael@0 | 294 | uint32_t *dst = reinterpret_cast<uint32_t*>(mBlocks[i]->mBits); |
michael@0 | 295 | const uint32_t *src = |
michael@0 | 296 | reinterpret_cast<const uint32_t*>(aBitset.mBlocks[i]->mBits); |
michael@0 | 297 | for (uint32_t j = 0; j < BLOCK_SIZE / 4; ++j) { |
michael@0 | 298 | dst[j] |= src[j]; |
michael@0 | 299 | } |
michael@0 | 300 | } |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | void Compact() { |
michael@0 | 304 | mBlocks.Compact(); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | uint32_t GetChecksum() const { |
michael@0 | 308 | uint32_t check = adler32(0, Z_NULL, 0); |
michael@0 | 309 | for (uint32_t i = 0; i < mBlocks.Length(); i++) { |
michael@0 | 310 | if (mBlocks[i]) { |
michael@0 | 311 | const Block *block = mBlocks[i]; |
michael@0 | 312 | check = adler32(check, (uint8_t*) (&i), 4); |
michael@0 | 313 | check = adler32(check, (uint8_t*) block, sizeof(Block)); |
michael@0 | 314 | } |
michael@0 | 315 | } |
michael@0 | 316 | return check; |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | private: |
michael@0 | 320 | nsTArray< nsAutoPtr<Block> > mBlocks; |
michael@0 | 321 | }; |
michael@0 | 322 | |
michael@0 | 323 | #define TRUETYPE_TAG(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d)) |
michael@0 | 324 | |
michael@0 | 325 | namespace mozilla { |
michael@0 | 326 | |
michael@0 | 327 | // Byte-swapping types and name table structure definitions moved from |
michael@0 | 328 | // gfxFontUtils.cpp to .h file so that gfxFont.cpp can also refer to them |
michael@0 | 329 | #pragma pack(1) |
michael@0 | 330 | |
michael@0 | 331 | struct AutoSwap_PRUint16 { |
michael@0 | 332 | #ifdef __SUNPRO_CC |
michael@0 | 333 | AutoSwap_PRUint16& operator = (const uint16_t aValue) |
michael@0 | 334 | { |
michael@0 | 335 | this->value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 336 | return *this; |
michael@0 | 337 | } |
michael@0 | 338 | #else |
michael@0 | 339 | AutoSwap_PRUint16(uint16_t aValue) |
michael@0 | 340 | { |
michael@0 | 341 | value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 342 | } |
michael@0 | 343 | #endif |
michael@0 | 344 | operator uint16_t() const |
michael@0 | 345 | { |
michael@0 | 346 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | operator uint32_t() const |
michael@0 | 350 | { |
michael@0 | 351 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | operator uint64_t() const |
michael@0 | 355 | { |
michael@0 | 356 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | private: |
michael@0 | 360 | uint16_t value; |
michael@0 | 361 | }; |
michael@0 | 362 | |
michael@0 | 363 | struct AutoSwap_PRInt16 { |
michael@0 | 364 | #ifdef __SUNPRO_CC |
michael@0 | 365 | AutoSwap_PRInt16& operator = (const int16_t aValue) |
michael@0 | 366 | { |
michael@0 | 367 | this->value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 368 | return *this; |
michael@0 | 369 | } |
michael@0 | 370 | #else |
michael@0 | 371 | AutoSwap_PRInt16(int16_t aValue) |
michael@0 | 372 | { |
michael@0 | 373 | value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 374 | } |
michael@0 | 375 | #endif |
michael@0 | 376 | operator int16_t() const |
michael@0 | 377 | { |
michael@0 | 378 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | operator uint32_t() const |
michael@0 | 382 | { |
michael@0 | 383 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | private: |
michael@0 | 387 | int16_t value; |
michael@0 | 388 | }; |
michael@0 | 389 | |
michael@0 | 390 | struct AutoSwap_PRUint32 { |
michael@0 | 391 | #ifdef __SUNPRO_CC |
michael@0 | 392 | AutoSwap_PRUint32& operator = (const uint32_t aValue) |
michael@0 | 393 | { |
michael@0 | 394 | this->value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 395 | return *this; |
michael@0 | 396 | } |
michael@0 | 397 | #else |
michael@0 | 398 | AutoSwap_PRUint32(uint32_t aValue) |
michael@0 | 399 | { |
michael@0 | 400 | value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 401 | } |
michael@0 | 402 | #endif |
michael@0 | 403 | operator uint32_t() const |
michael@0 | 404 | { |
michael@0 | 405 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | private: |
michael@0 | 409 | uint32_t value; |
michael@0 | 410 | }; |
michael@0 | 411 | |
michael@0 | 412 | struct AutoSwap_PRInt32 { |
michael@0 | 413 | #ifdef __SUNPRO_CC |
michael@0 | 414 | AutoSwap_PRInt32& operator = (const int32_t aValue) |
michael@0 | 415 | { |
michael@0 | 416 | this->value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 417 | return *this; |
michael@0 | 418 | } |
michael@0 | 419 | #else |
michael@0 | 420 | AutoSwap_PRInt32(int32_t aValue) |
michael@0 | 421 | { |
michael@0 | 422 | value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 423 | } |
michael@0 | 424 | #endif |
michael@0 | 425 | operator int32_t() const |
michael@0 | 426 | { |
michael@0 | 427 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | private: |
michael@0 | 431 | int32_t value; |
michael@0 | 432 | }; |
michael@0 | 433 | |
michael@0 | 434 | struct AutoSwap_PRUint64 { |
michael@0 | 435 | #ifdef __SUNPRO_CC |
michael@0 | 436 | AutoSwap_PRUint64& operator = (const uint64_t aValue) |
michael@0 | 437 | { |
michael@0 | 438 | this->value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 439 | return *this; |
michael@0 | 440 | } |
michael@0 | 441 | #else |
michael@0 | 442 | AutoSwap_PRUint64(uint64_t aValue) |
michael@0 | 443 | { |
michael@0 | 444 | value = mozilla::NativeEndian::swapToBigEndian(aValue); |
michael@0 | 445 | } |
michael@0 | 446 | #endif |
michael@0 | 447 | operator uint64_t() const |
michael@0 | 448 | { |
michael@0 | 449 | return mozilla::NativeEndian::swapFromBigEndian(value); |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | private: |
michael@0 | 453 | uint64_t value; |
michael@0 | 454 | }; |
michael@0 | 455 | |
michael@0 | 456 | struct AutoSwap_PRUint24 { |
michael@0 | 457 | operator uint32_t() const { return value[0] << 16 | value[1] << 8 | value[2]; } |
michael@0 | 458 | private: |
michael@0 | 459 | AutoSwap_PRUint24() { } |
michael@0 | 460 | uint8_t value[3]; |
michael@0 | 461 | }; |
michael@0 | 462 | |
michael@0 | 463 | struct SFNTHeader { |
michael@0 | 464 | AutoSwap_PRUint32 sfntVersion; // Fixed, 0x00010000 for version 1.0. |
michael@0 | 465 | AutoSwap_PRUint16 numTables; // Number of tables. |
michael@0 | 466 | AutoSwap_PRUint16 searchRange; // (Maximum power of 2 <= numTables) x 16. |
michael@0 | 467 | AutoSwap_PRUint16 entrySelector; // Log2(maximum power of 2 <= numTables). |
michael@0 | 468 | AutoSwap_PRUint16 rangeShift; // NumTables x 16-searchRange. |
michael@0 | 469 | }; |
michael@0 | 470 | |
michael@0 | 471 | struct TableDirEntry { |
michael@0 | 472 | AutoSwap_PRUint32 tag; // 4 -byte identifier. |
michael@0 | 473 | AutoSwap_PRUint32 checkSum; // CheckSum for this table. |
michael@0 | 474 | AutoSwap_PRUint32 offset; // Offset from beginning of TrueType font file. |
michael@0 | 475 | AutoSwap_PRUint32 length; // Length of this table. |
michael@0 | 476 | }; |
michael@0 | 477 | |
michael@0 | 478 | struct HeadTable { |
michael@0 | 479 | enum { |
michael@0 | 480 | HEAD_VERSION = 0x00010000, |
michael@0 | 481 | HEAD_MAGIC_NUMBER = 0x5F0F3CF5, |
michael@0 | 482 | HEAD_CHECKSUM_CALC_CONST = 0xB1B0AFBA |
michael@0 | 483 | }; |
michael@0 | 484 | |
michael@0 | 485 | AutoSwap_PRUint32 tableVersionNumber; // Fixed, 0x00010000 for version 1.0. |
michael@0 | 486 | AutoSwap_PRUint32 fontRevision; // Set by font manufacturer. |
michael@0 | 487 | AutoSwap_PRUint32 checkSumAdjustment; // To compute: set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum. |
michael@0 | 488 | AutoSwap_PRUint32 magicNumber; // Set to 0x5F0F3CF5. |
michael@0 | 489 | AutoSwap_PRUint16 flags; |
michael@0 | 490 | AutoSwap_PRUint16 unitsPerEm; // Valid range is from 16 to 16384. This value should be a power of 2 for fonts that have TrueType outlines. |
michael@0 | 491 | AutoSwap_PRUint64 created; // Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer |
michael@0 | 492 | AutoSwap_PRUint64 modified; // Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer |
michael@0 | 493 | AutoSwap_PRInt16 xMin; // For all glyph bounding boxes. |
michael@0 | 494 | AutoSwap_PRInt16 yMin; // For all glyph bounding boxes. |
michael@0 | 495 | AutoSwap_PRInt16 xMax; // For all glyph bounding boxes. |
michael@0 | 496 | AutoSwap_PRInt16 yMax; // For all glyph bounding boxes. |
michael@0 | 497 | AutoSwap_PRUint16 macStyle; // Bit 0: Bold (if set to 1); |
michael@0 | 498 | AutoSwap_PRUint16 lowestRecPPEM; // Smallest readable size in pixels. |
michael@0 | 499 | AutoSwap_PRInt16 fontDirectionHint; |
michael@0 | 500 | AutoSwap_PRInt16 indexToLocFormat; |
michael@0 | 501 | AutoSwap_PRInt16 glyphDataFormat; |
michael@0 | 502 | }; |
michael@0 | 503 | |
michael@0 | 504 | struct OS2Table { |
michael@0 | 505 | AutoSwap_PRUint16 version; // 0004 = OpenType 1.5 |
michael@0 | 506 | AutoSwap_PRInt16 xAvgCharWidth; |
michael@0 | 507 | AutoSwap_PRUint16 usWeightClass; |
michael@0 | 508 | AutoSwap_PRUint16 usWidthClass; |
michael@0 | 509 | AutoSwap_PRUint16 fsType; |
michael@0 | 510 | AutoSwap_PRInt16 ySubscriptXSize; |
michael@0 | 511 | AutoSwap_PRInt16 ySubscriptYSize; |
michael@0 | 512 | AutoSwap_PRInt16 ySubscriptXOffset; |
michael@0 | 513 | AutoSwap_PRInt16 ySubscriptYOffset; |
michael@0 | 514 | AutoSwap_PRInt16 ySuperscriptXSize; |
michael@0 | 515 | AutoSwap_PRInt16 ySuperscriptYSize; |
michael@0 | 516 | AutoSwap_PRInt16 ySuperscriptXOffset; |
michael@0 | 517 | AutoSwap_PRInt16 ySuperscriptYOffset; |
michael@0 | 518 | AutoSwap_PRInt16 yStrikeoutSize; |
michael@0 | 519 | AutoSwap_PRInt16 yStrikeoutPosition; |
michael@0 | 520 | AutoSwap_PRInt16 sFamilyClass; |
michael@0 | 521 | uint8_t panose[10]; |
michael@0 | 522 | AutoSwap_PRUint32 unicodeRange1; |
michael@0 | 523 | AutoSwap_PRUint32 unicodeRange2; |
michael@0 | 524 | AutoSwap_PRUint32 unicodeRange3; |
michael@0 | 525 | AutoSwap_PRUint32 unicodeRange4; |
michael@0 | 526 | uint8_t achVendID[4]; |
michael@0 | 527 | AutoSwap_PRUint16 fsSelection; |
michael@0 | 528 | AutoSwap_PRUint16 usFirstCharIndex; |
michael@0 | 529 | AutoSwap_PRUint16 usLastCharIndex; |
michael@0 | 530 | AutoSwap_PRInt16 sTypoAscender; |
michael@0 | 531 | AutoSwap_PRInt16 sTypoDescender; |
michael@0 | 532 | AutoSwap_PRInt16 sTypoLineGap; |
michael@0 | 533 | AutoSwap_PRUint16 usWinAscent; |
michael@0 | 534 | AutoSwap_PRUint16 usWinDescent; |
michael@0 | 535 | AutoSwap_PRUint32 codePageRange1; |
michael@0 | 536 | AutoSwap_PRUint32 codePageRange2; |
michael@0 | 537 | AutoSwap_PRInt16 sxHeight; |
michael@0 | 538 | AutoSwap_PRInt16 sCapHeight; |
michael@0 | 539 | AutoSwap_PRUint16 usDefaultChar; |
michael@0 | 540 | AutoSwap_PRUint16 usBreakChar; |
michael@0 | 541 | AutoSwap_PRUint16 usMaxContext; |
michael@0 | 542 | }; |
michael@0 | 543 | |
michael@0 | 544 | struct PostTable { |
michael@0 | 545 | AutoSwap_PRUint32 version; |
michael@0 | 546 | AutoSwap_PRInt32 italicAngle; |
michael@0 | 547 | AutoSwap_PRInt16 underlinePosition; |
michael@0 | 548 | AutoSwap_PRUint16 underlineThickness; |
michael@0 | 549 | AutoSwap_PRUint32 isFixedPitch; |
michael@0 | 550 | AutoSwap_PRUint32 minMemType42; |
michael@0 | 551 | AutoSwap_PRUint32 maxMemType42; |
michael@0 | 552 | AutoSwap_PRUint32 minMemType1; |
michael@0 | 553 | AutoSwap_PRUint32 maxMemType1; |
michael@0 | 554 | }; |
michael@0 | 555 | |
michael@0 | 556 | struct HheaTable { |
michael@0 | 557 | AutoSwap_PRUint32 version; |
michael@0 | 558 | AutoSwap_PRInt16 ascender; |
michael@0 | 559 | AutoSwap_PRInt16 descender; |
michael@0 | 560 | AutoSwap_PRInt16 lineGap; |
michael@0 | 561 | AutoSwap_PRUint16 advanceWidthMax; |
michael@0 | 562 | AutoSwap_PRInt16 minLeftSideBearing; |
michael@0 | 563 | AutoSwap_PRInt16 minRightSideBearing; |
michael@0 | 564 | AutoSwap_PRInt16 xMaxExtent; |
michael@0 | 565 | AutoSwap_PRInt16 caretSlopeRise; |
michael@0 | 566 | AutoSwap_PRInt16 caretSlopeRun; |
michael@0 | 567 | AutoSwap_PRInt16 caretOffset; |
michael@0 | 568 | AutoSwap_PRInt16 reserved1; |
michael@0 | 569 | AutoSwap_PRInt16 reserved2; |
michael@0 | 570 | AutoSwap_PRInt16 reserved3; |
michael@0 | 571 | AutoSwap_PRInt16 reserved4; |
michael@0 | 572 | AutoSwap_PRInt16 metricDataFormat; |
michael@0 | 573 | AutoSwap_PRUint16 numOfLongHorMetrics; |
michael@0 | 574 | }; |
michael@0 | 575 | |
michael@0 | 576 | struct MaxpTableHeader { |
michael@0 | 577 | AutoSwap_PRUint32 version; // CFF: 0x00005000; TrueType: 0x00010000 |
michael@0 | 578 | AutoSwap_PRUint16 numGlyphs; |
michael@0 | 579 | // truetype version has additional fields that we don't currently use |
michael@0 | 580 | }; |
michael@0 | 581 | |
michael@0 | 582 | // old 'kern' table, supported on Windows |
michael@0 | 583 | // see http://www.microsoft.com/typography/otspec/kern.htm |
michael@0 | 584 | struct KernTableVersion0 { |
michael@0 | 585 | AutoSwap_PRUint16 version; // 0x0000 |
michael@0 | 586 | AutoSwap_PRUint16 nTables; |
michael@0 | 587 | }; |
michael@0 | 588 | |
michael@0 | 589 | struct KernTableSubtableHeaderVersion0 { |
michael@0 | 590 | AutoSwap_PRUint16 version; |
michael@0 | 591 | AutoSwap_PRUint16 length; |
michael@0 | 592 | AutoSwap_PRUint16 coverage; |
michael@0 | 593 | }; |
michael@0 | 594 | |
michael@0 | 595 | // newer Mac-only 'kern' table, ignored by Windows |
michael@0 | 596 | // see http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6kern.html |
michael@0 | 597 | struct KernTableVersion1 { |
michael@0 | 598 | AutoSwap_PRUint32 version; // 0x00010000 |
michael@0 | 599 | AutoSwap_PRUint32 nTables; |
michael@0 | 600 | }; |
michael@0 | 601 | |
michael@0 | 602 | struct KernTableSubtableHeaderVersion1 { |
michael@0 | 603 | AutoSwap_PRUint32 length; |
michael@0 | 604 | AutoSwap_PRUint16 coverage; |
michael@0 | 605 | AutoSwap_PRUint16 tupleIndex; |
michael@0 | 606 | }; |
michael@0 | 607 | |
michael@0 | 608 | #pragma pack() |
michael@0 | 609 | |
michael@0 | 610 | // Return just the highest bit of the given value, i.e., the highest |
michael@0 | 611 | // power of 2 that is <= value, or zero if the input value is zero. |
michael@0 | 612 | inline uint32_t |
michael@0 | 613 | FindHighestBit(uint32_t value) |
michael@0 | 614 | { |
michael@0 | 615 | // propagate highest bit into all lower bits of the value |
michael@0 | 616 | value |= (value >> 1); |
michael@0 | 617 | value |= (value >> 2); |
michael@0 | 618 | value |= (value >> 4); |
michael@0 | 619 | value |= (value >> 8); |
michael@0 | 620 | value |= (value >> 16); |
michael@0 | 621 | // isolate the leftmost bit |
michael@0 | 622 | return (value & ~(value >> 1)); |
michael@0 | 623 | } |
michael@0 | 624 | |
michael@0 | 625 | } // namespace mozilla |
michael@0 | 626 | |
michael@0 | 627 | // used for overlaying name changes without touching original font data |
michael@0 | 628 | struct FontDataOverlay { |
michael@0 | 629 | // overlaySrc != 0 ==> use overlay |
michael@0 | 630 | uint32_t overlaySrc; // src offset from start of font data |
michael@0 | 631 | uint32_t overlaySrcLen; // src length |
michael@0 | 632 | uint32_t overlayDest; // dest offset from start of font data |
michael@0 | 633 | }; |
michael@0 | 634 | |
michael@0 | 635 | enum gfxUserFontType { |
michael@0 | 636 | GFX_USERFONT_UNKNOWN = 0, |
michael@0 | 637 | GFX_USERFONT_OPENTYPE = 1, |
michael@0 | 638 | GFX_USERFONT_SVG = 2, |
michael@0 | 639 | GFX_USERFONT_WOFF = 3 |
michael@0 | 640 | }; |
michael@0 | 641 | |
michael@0 | 642 | extern const uint8_t sCJKCompatSVSTable[]; |
michael@0 | 643 | |
michael@0 | 644 | class gfxFontUtils { |
michael@0 | 645 | |
michael@0 | 646 | public: |
michael@0 | 647 | // these are public because gfxFont.cpp also looks into the name table |
michael@0 | 648 | enum { |
michael@0 | 649 | NAME_ID_FAMILY = 1, |
michael@0 | 650 | NAME_ID_STYLE = 2, |
michael@0 | 651 | NAME_ID_UNIQUE = 3, |
michael@0 | 652 | NAME_ID_FULL = 4, |
michael@0 | 653 | NAME_ID_VERSION = 5, |
michael@0 | 654 | NAME_ID_POSTSCRIPT = 6, |
michael@0 | 655 | NAME_ID_PREFERRED_FAMILY = 16, |
michael@0 | 656 | NAME_ID_PREFERRED_STYLE = 17, |
michael@0 | 657 | |
michael@0 | 658 | PLATFORM_ALL = -1, |
michael@0 | 659 | PLATFORM_ID_UNICODE = 0, // Mac OS uses this typically |
michael@0 | 660 | PLATFORM_ID_MAC = 1, |
michael@0 | 661 | PLATFORM_ID_ISO = 2, |
michael@0 | 662 | PLATFORM_ID_MICROSOFT = 3, |
michael@0 | 663 | |
michael@0 | 664 | ENCODING_ID_MAC_ROMAN = 0, // traditional Mac OS script manager encodings |
michael@0 | 665 | ENCODING_ID_MAC_JAPANESE = 1, // (there are others defined, but some were never |
michael@0 | 666 | ENCODING_ID_MAC_TRAD_CHINESE = 2, // implemented by Apple, and I have never seen them |
michael@0 | 667 | ENCODING_ID_MAC_KOREAN = 3, // used in font names) |
michael@0 | 668 | ENCODING_ID_MAC_ARABIC = 4, |
michael@0 | 669 | ENCODING_ID_MAC_HEBREW = 5, |
michael@0 | 670 | ENCODING_ID_MAC_GREEK = 6, |
michael@0 | 671 | ENCODING_ID_MAC_CYRILLIC = 7, |
michael@0 | 672 | ENCODING_ID_MAC_DEVANAGARI = 9, |
michael@0 | 673 | ENCODING_ID_MAC_GURMUKHI = 10, |
michael@0 | 674 | ENCODING_ID_MAC_GUJARATI = 11, |
michael@0 | 675 | ENCODING_ID_MAC_SIMP_CHINESE = 25, |
michael@0 | 676 | |
michael@0 | 677 | ENCODING_ID_MICROSOFT_SYMBOL = 0, // Microsoft platform encoding IDs |
michael@0 | 678 | ENCODING_ID_MICROSOFT_UNICODEBMP = 1, |
michael@0 | 679 | ENCODING_ID_MICROSOFT_SHIFTJIS = 2, |
michael@0 | 680 | ENCODING_ID_MICROSOFT_PRC = 3, |
michael@0 | 681 | ENCODING_ID_MICROSOFT_BIG5 = 4, |
michael@0 | 682 | ENCODING_ID_MICROSOFT_WANSUNG = 5, |
michael@0 | 683 | ENCODING_ID_MICROSOFT_JOHAB = 6, |
michael@0 | 684 | ENCODING_ID_MICROSOFT_UNICODEFULL = 10, |
michael@0 | 685 | |
michael@0 | 686 | LANG_ALL = -1, |
michael@0 | 687 | LANG_ID_MAC_ENGLISH = 0, // many others are defined, but most don't affect |
michael@0 | 688 | LANG_ID_MAC_HEBREW = 10, // the charset; should check all the central/eastern |
michael@0 | 689 | LANG_ID_MAC_JAPANESE = 11, // european codes, though |
michael@0 | 690 | LANG_ID_MAC_ARABIC = 12, |
michael@0 | 691 | LANG_ID_MAC_ICELANDIC = 15, |
michael@0 | 692 | LANG_ID_MAC_TURKISH = 17, |
michael@0 | 693 | LANG_ID_MAC_TRAD_CHINESE = 19, |
michael@0 | 694 | LANG_ID_MAC_URDU = 20, |
michael@0 | 695 | LANG_ID_MAC_KOREAN = 23, |
michael@0 | 696 | LANG_ID_MAC_POLISH = 25, |
michael@0 | 697 | LANG_ID_MAC_FARSI = 31, |
michael@0 | 698 | LANG_ID_MAC_SIMP_CHINESE = 33, |
michael@0 | 699 | LANG_ID_MAC_ROMANIAN = 37, |
michael@0 | 700 | LANG_ID_MAC_CZECH = 38, |
michael@0 | 701 | LANG_ID_MAC_SLOVAK = 39, |
michael@0 | 702 | |
michael@0 | 703 | LANG_ID_MICROSOFT_EN_US = 0x0409, // with Microsoft platformID, EN US lang code |
michael@0 | 704 | |
michael@0 | 705 | CMAP_MAX_CODEPOINT = 0x10ffff // maximum possible Unicode codepoint |
michael@0 | 706 | // contained in a cmap |
michael@0 | 707 | }; |
michael@0 | 708 | |
michael@0 | 709 | // name table has a header, followed by name records, followed by string data |
michael@0 | 710 | struct NameHeader { |
michael@0 | 711 | mozilla::AutoSwap_PRUint16 format; // Format selector (=0). |
michael@0 | 712 | mozilla::AutoSwap_PRUint16 count; // Number of name records. |
michael@0 | 713 | mozilla::AutoSwap_PRUint16 stringOffset; // Offset to start of string storage |
michael@0 | 714 | // (from start of table) |
michael@0 | 715 | }; |
michael@0 | 716 | |
michael@0 | 717 | struct NameRecord { |
michael@0 | 718 | mozilla::AutoSwap_PRUint16 platformID; // Platform ID |
michael@0 | 719 | mozilla::AutoSwap_PRUint16 encodingID; // Platform-specific encoding ID |
michael@0 | 720 | mozilla::AutoSwap_PRUint16 languageID; // Language ID |
michael@0 | 721 | mozilla::AutoSwap_PRUint16 nameID; // Name ID. |
michael@0 | 722 | mozilla::AutoSwap_PRUint16 length; // String length (in bytes). |
michael@0 | 723 | mozilla::AutoSwap_PRUint16 offset; // String offset from start of storage |
michael@0 | 724 | // (in bytes). |
michael@0 | 725 | }; |
michael@0 | 726 | |
michael@0 | 727 | // for reading big-endian font data on either big or little-endian platforms |
michael@0 | 728 | |
michael@0 | 729 | static inline uint16_t |
michael@0 | 730 | ReadShortAt(const uint8_t *aBuf, uint32_t aIndex) |
michael@0 | 731 | { |
michael@0 | 732 | return (aBuf[aIndex] << 8) | aBuf[aIndex + 1]; |
michael@0 | 733 | } |
michael@0 | 734 | |
michael@0 | 735 | static inline uint16_t |
michael@0 | 736 | ReadShortAt16(const uint16_t *aBuf, uint32_t aIndex) |
michael@0 | 737 | { |
michael@0 | 738 | const uint8_t *buf = reinterpret_cast<const uint8_t*>(aBuf); |
michael@0 | 739 | uint32_t index = aIndex << 1; |
michael@0 | 740 | return (buf[index] << 8) | buf[index+1]; |
michael@0 | 741 | } |
michael@0 | 742 | |
michael@0 | 743 | static inline uint32_t |
michael@0 | 744 | ReadUint24At(const uint8_t *aBuf, uint32_t aIndex) |
michael@0 | 745 | { |
michael@0 | 746 | return ((aBuf[aIndex] << 16) | (aBuf[aIndex + 1] << 8) | |
michael@0 | 747 | (aBuf[aIndex + 2])); |
michael@0 | 748 | } |
michael@0 | 749 | |
michael@0 | 750 | static inline uint32_t |
michael@0 | 751 | ReadLongAt(const uint8_t *aBuf, uint32_t aIndex) |
michael@0 | 752 | { |
michael@0 | 753 | return ((aBuf[aIndex] << 24) | (aBuf[aIndex + 1] << 16) | |
michael@0 | 754 | (aBuf[aIndex + 2] << 8) | (aBuf[aIndex + 3])); |
michael@0 | 755 | } |
michael@0 | 756 | |
michael@0 | 757 | static nsresult |
michael@0 | 758 | ReadCMAPTableFormat12(const uint8_t *aBuf, uint32_t aLength, |
michael@0 | 759 | gfxSparseBitSet& aCharacterMap); |
michael@0 | 760 | |
michael@0 | 761 | static nsresult |
michael@0 | 762 | ReadCMAPTableFormat4(const uint8_t *aBuf, uint32_t aLength, |
michael@0 | 763 | gfxSparseBitSet& aCharacterMap); |
michael@0 | 764 | |
michael@0 | 765 | static nsresult |
michael@0 | 766 | ReadCMAPTableFormat14(const uint8_t *aBuf, uint32_t aLength, |
michael@0 | 767 | uint8_t*& aTable); |
michael@0 | 768 | |
michael@0 | 769 | static uint32_t |
michael@0 | 770 | FindPreferredSubtable(const uint8_t *aBuf, uint32_t aBufLength, |
michael@0 | 771 | uint32_t *aTableOffset, uint32_t *aUVSTableOffset, |
michael@0 | 772 | bool *aSymbolEncoding); |
michael@0 | 773 | |
michael@0 | 774 | static nsresult |
michael@0 | 775 | ReadCMAP(const uint8_t *aBuf, uint32_t aBufLength, |
michael@0 | 776 | gfxSparseBitSet& aCharacterMap, |
michael@0 | 777 | uint32_t& aUVSOffset, |
michael@0 | 778 | bool& aUnicodeFont, bool& aSymbolFont); |
michael@0 | 779 | |
michael@0 | 780 | static uint32_t |
michael@0 | 781 | MapCharToGlyphFormat4(const uint8_t *aBuf, char16_t aCh); |
michael@0 | 782 | |
michael@0 | 783 | static uint32_t |
michael@0 | 784 | MapCharToGlyphFormat12(const uint8_t *aBuf, uint32_t aCh); |
michael@0 | 785 | |
michael@0 | 786 | static uint16_t |
michael@0 | 787 | MapUVSToGlyphFormat14(const uint8_t *aBuf, uint32_t aCh, uint32_t aVS); |
michael@0 | 788 | |
michael@0 | 789 | // sCJKCompatSVSTable is a 'cmap' format 14 subtable that maps |
michael@0 | 790 | // <char + var-selector> pairs to the corresponding Unicode |
michael@0 | 791 | // compatibility ideograph codepoints. |
michael@0 | 792 | static MOZ_ALWAYS_INLINE uint32_t |
michael@0 | 793 | GetUVSFallback(uint32_t aCh, uint32_t aVS) { |
michael@0 | 794 | aCh = MapUVSToGlyphFormat14(sCJKCompatSVSTable, aCh, aVS); |
michael@0 | 795 | return aCh >= 0xFB00 ? aCh + (0x2F800 - 0xFB00) : aCh; |
michael@0 | 796 | } |
michael@0 | 797 | |
michael@0 | 798 | static uint32_t |
michael@0 | 799 | MapCharToGlyph(const uint8_t *aCmapBuf, uint32_t aBufLength, |
michael@0 | 800 | uint32_t aUnicode, uint32_t aVarSelector = 0); |
michael@0 | 801 | |
michael@0 | 802 | #ifdef XP_WIN |
michael@0 | 803 | // determine whether a font (which has already been sanitized, so is known |
michael@0 | 804 | // to be a valid sfnt) is CFF format rather than TrueType |
michael@0 | 805 | static bool |
michael@0 | 806 | IsCffFont(const uint8_t* aFontData); |
michael@0 | 807 | #endif |
michael@0 | 808 | |
michael@0 | 809 | // determine the format of font data |
michael@0 | 810 | static gfxUserFontType |
michael@0 | 811 | DetermineFontDataType(const uint8_t *aFontData, uint32_t aFontDataLength); |
michael@0 | 812 | |
michael@0 | 813 | // Read the fullname from the sfnt data (used to save the original name |
michael@0 | 814 | // prior to renaming the font for installation). |
michael@0 | 815 | // This is called with sfnt data that has already been validated, |
michael@0 | 816 | // so it should always succeed in finding the name table. |
michael@0 | 817 | static nsresult |
michael@0 | 818 | GetFullNameFromSFNT(const uint8_t* aFontData, uint32_t aLength, |
michael@0 | 819 | nsAString& aFullName); |
michael@0 | 820 | |
michael@0 | 821 | // helper to get fullname from name table, constructing from family+style |
michael@0 | 822 | // if no explicit fullname is present |
michael@0 | 823 | static nsresult |
michael@0 | 824 | GetFullNameFromTable(hb_blob_t *aNameTable, |
michael@0 | 825 | nsAString& aFullName); |
michael@0 | 826 | |
michael@0 | 827 | // helper to get family name from name table |
michael@0 | 828 | static nsresult |
michael@0 | 829 | GetFamilyNameFromTable(hb_blob_t *aNameTable, |
michael@0 | 830 | nsAString& aFamilyName); |
michael@0 | 831 | |
michael@0 | 832 | // create a new name table and build a new font with that name table |
michael@0 | 833 | // appended on the end, returns true on success |
michael@0 | 834 | static nsresult |
michael@0 | 835 | RenameFont(const nsAString& aName, const uint8_t *aFontData, |
michael@0 | 836 | uint32_t aFontDataLength, FallibleTArray<uint8_t> *aNewFont); |
michael@0 | 837 | |
michael@0 | 838 | // read all names matching aNameID, returning in aNames array |
michael@0 | 839 | static nsresult |
michael@0 | 840 | ReadNames(const char *aNameData, uint32_t aDataLen, uint32_t aNameID, |
michael@0 | 841 | int32_t aPlatformID, nsTArray<nsString>& aNames); |
michael@0 | 842 | |
michael@0 | 843 | // reads English or first name matching aNameID, returning in aName |
michael@0 | 844 | // platform based on OS |
michael@0 | 845 | static nsresult |
michael@0 | 846 | ReadCanonicalName(hb_blob_t *aNameTable, uint32_t aNameID, |
michael@0 | 847 | nsString& aName); |
michael@0 | 848 | |
michael@0 | 849 | static nsresult |
michael@0 | 850 | ReadCanonicalName(const char *aNameData, uint32_t aDataLen, |
michael@0 | 851 | uint32_t aNameID, nsString& aName); |
michael@0 | 852 | |
michael@0 | 853 | // convert a name from the raw name table data into an nsString, |
michael@0 | 854 | // provided we know how; return true if successful, or false |
michael@0 | 855 | // if we can't handle the encoding |
michael@0 | 856 | static bool |
michael@0 | 857 | DecodeFontName(const char *aBuf, int32_t aLength, |
michael@0 | 858 | uint32_t aPlatformCode, uint32_t aScriptCode, |
michael@0 | 859 | uint32_t aLangCode, nsAString& dest); |
michael@0 | 860 | |
michael@0 | 861 | static inline bool IsJoinCauser(uint32_t ch) { |
michael@0 | 862 | return (ch == 0x200D); |
michael@0 | 863 | } |
michael@0 | 864 | |
michael@0 | 865 | static inline bool IsJoinControl(uint32_t ch) { |
michael@0 | 866 | return (ch == 0x200C || ch == 0x200D); |
michael@0 | 867 | } |
michael@0 | 868 | |
michael@0 | 869 | enum { |
michael@0 | 870 | kUnicodeVS1 = 0xFE00, |
michael@0 | 871 | kUnicodeVS16 = 0xFE0F, |
michael@0 | 872 | kUnicodeVS17 = 0xE0100, |
michael@0 | 873 | kUnicodeVS256 = 0xE01EF |
michael@0 | 874 | }; |
michael@0 | 875 | |
michael@0 | 876 | static inline bool IsVarSelector(uint32_t ch) { |
michael@0 | 877 | return (ch >= kUnicodeVS1 && ch <= kUnicodeVS16) || |
michael@0 | 878 | (ch >= kUnicodeVS17 && ch <= kUnicodeVS256); |
michael@0 | 879 | } |
michael@0 | 880 | |
michael@0 | 881 | static inline bool IsInvalid(uint32_t ch) { |
michael@0 | 882 | return (ch == 0xFFFD); |
michael@0 | 883 | } |
michael@0 | 884 | |
michael@0 | 885 | // Font code may want to know if there is the potential for bidi behavior |
michael@0 | 886 | // to be triggered by any of the characters in a text run; this can be |
michael@0 | 887 | // used to test that possibility. |
michael@0 | 888 | enum { |
michael@0 | 889 | kUnicodeBidiScriptsStart = 0x0590, |
michael@0 | 890 | kUnicodeBidiScriptsEnd = 0x08FF, |
michael@0 | 891 | kUnicodeBidiPresentationStart = 0xFB1D, |
michael@0 | 892 | kUnicodeBidiPresentationEnd = 0xFEFC, |
michael@0 | 893 | kUnicodeFirstHighSurrogateBlock = 0xD800, |
michael@0 | 894 | kUnicodeRLM = 0x200F, |
michael@0 | 895 | kUnicodeRLE = 0x202B, |
michael@0 | 896 | kUnicodeRLO = 0x202E |
michael@0 | 897 | }; |
michael@0 | 898 | |
michael@0 | 899 | static inline bool PotentialRTLChar(char16_t aCh) { |
michael@0 | 900 | if (aCh >= kUnicodeBidiScriptsStart && aCh <= kUnicodeBidiScriptsEnd) |
michael@0 | 901 | // bidi scripts Hebrew, Arabic, Syriac, Thaana, N'Ko are all encoded together |
michael@0 | 902 | return true; |
michael@0 | 903 | |
michael@0 | 904 | if (aCh == kUnicodeRLM || aCh == kUnicodeRLE || aCh == kUnicodeRLO) |
michael@0 | 905 | // directional controls that trigger bidi layout |
michael@0 | 906 | return true; |
michael@0 | 907 | |
michael@0 | 908 | if (aCh >= kUnicodeBidiPresentationStart && |
michael@0 | 909 | aCh <= kUnicodeBidiPresentationEnd) |
michael@0 | 910 | // presentation forms of Arabic and Hebrew letters |
michael@0 | 911 | return true; |
michael@0 | 912 | |
michael@0 | 913 | if ((aCh & 0xFF00) == kUnicodeFirstHighSurrogateBlock) |
michael@0 | 914 | // surrogate that could be part of a bidi supplementary char |
michael@0 | 915 | // (Cypriot, Aramaic, Phoenecian, etc) |
michael@0 | 916 | return true; |
michael@0 | 917 | |
michael@0 | 918 | // otherwise we know this char cannot trigger bidi reordering |
michael@0 | 919 | return false; |
michael@0 | 920 | } |
michael@0 | 921 | |
michael@0 | 922 | // for a given font list pref name, set up a list of font names |
michael@0 | 923 | static void GetPrefsFontList(const char *aPrefName, |
michael@0 | 924 | nsTArray<nsString>& aFontList); |
michael@0 | 925 | |
michael@0 | 926 | // generate a unique font name |
michael@0 | 927 | static nsresult MakeUniqueUserFontName(nsAString& aName); |
michael@0 | 928 | |
michael@0 | 929 | protected: |
michael@0 | 930 | static nsresult |
michael@0 | 931 | ReadNames(const char *aNameData, uint32_t aDataLen, uint32_t aNameID, |
michael@0 | 932 | int32_t aLangID, int32_t aPlatformID, nsTArray<nsString>& aNames); |
michael@0 | 933 | |
michael@0 | 934 | // convert opentype name-table platform/encoding/language values to a charset name |
michael@0 | 935 | // we can use to convert the name data to unicode, or "" if data is UTF16BE |
michael@0 | 936 | static const char* |
michael@0 | 937 | GetCharsetForFontName(uint16_t aPlatform, uint16_t aScript, uint16_t aLanguage); |
michael@0 | 938 | |
michael@0 | 939 | struct MacFontNameCharsetMapping { |
michael@0 | 940 | uint16_t mEncoding; |
michael@0 | 941 | uint16_t mLanguage; |
michael@0 | 942 | const char *mCharsetName; |
michael@0 | 943 | |
michael@0 | 944 | bool operator<(const MacFontNameCharsetMapping& rhs) const { |
michael@0 | 945 | return (mEncoding < rhs.mEncoding) || |
michael@0 | 946 | ((mEncoding == rhs.mEncoding) && (mLanguage < rhs.mLanguage)); |
michael@0 | 947 | } |
michael@0 | 948 | }; |
michael@0 | 949 | static const MacFontNameCharsetMapping gMacFontNameCharsets[]; |
michael@0 | 950 | static const char* gISOFontNameCharsets[]; |
michael@0 | 951 | static const char* gMSFontNameCharsets[]; |
michael@0 | 952 | }; |
michael@0 | 953 | |
michael@0 | 954 | |
michael@0 | 955 | #endif /* GFX_FONT_UTILS_H */ |