gecko/gfx/thebes/public/gfxFontUtils.h

201 lines
6.5 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Foundation code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Stuart Parmenter <stuart@mozilla.com>
* John Daggett <jdaggett@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef GFX_FONT_UTILS_H
#define GFX_FONT_UTILS_H
#include "gfxTypes.h"
#include "prtypes.h"
#include "gfxFont.h"
#include "prcpucfg.h"
#include "nsDataHashtable.h"
/* Bug 341128 - w32api defines min/max which causes problems with <bitset> */
#ifdef __MINGW32__
#undef min
#undef max
#endif
#include <bitset>
// code from gfxWindowsFonts.h
class gfxSparseBitSet {
private:
enum { BLOCK_SIZE = 32 };
enum { BLOCK_SIZE_BITS = BLOCK_SIZE * 8 };
struct Block {
Block(unsigned char memsetValue = 0) { memset(mBits, memsetValue, BLOCK_SIZE); }
PRUint8 mBits[BLOCK_SIZE];
};
public:
PRBool test(PRUint32 aIndex) {
PRUint32 blockIndex = aIndex/BLOCK_SIZE_BITS;
if (blockIndex >= mBlocks.Length())
return PR_FALSE;
Block *block = mBlocks[blockIndex];
if (!block)
return PR_FALSE;
return ((block->mBits[(aIndex/8) & (BLOCK_SIZE - 1)]) & (1 << (aIndex & 0x7))) != 0;
}
void set(PRUint32 aIndex) {
PRUint32 blockIndex = aIndex/BLOCK_SIZE_BITS;
if (blockIndex >= mBlocks.Length()) {
nsAutoPtr<Block> *blocks = mBlocks.AppendElements(blockIndex + 1 - mBlocks.Length());
if (NS_UNLIKELY(!blocks)) // OOM
return;
}
Block *block = mBlocks[blockIndex];
if (!block) {
block = new Block;
if (NS_UNLIKELY(!block)) // OOM
return;
mBlocks[blockIndex] = block;
}
block->mBits[(aIndex/8) & (BLOCK_SIZE - 1)] |= 1 << (aIndex & 0x7);
}
void SetRange(PRUint32 aStart, PRUint32 aEnd) {
const PRUint32 startIndex = aStart/BLOCK_SIZE_BITS;
const PRUint32 endIndex = aEnd/BLOCK_SIZE_BITS;
if (endIndex >= mBlocks.Length()) {
PRUint32 numNewBlocks = endIndex + 1 - mBlocks.Length();
nsAutoPtr<Block> *blocks = mBlocks.AppendElements(numNewBlocks);
if (NS_UNLIKELY(!blocks)) // OOM
return;
}
for (PRUint32 i = startIndex; i <= endIndex; ++i) {
const PRUint32 blockFirstBit = i * BLOCK_SIZE_BITS;
const PRUint32 blockLastBit = blockFirstBit + BLOCK_SIZE_BITS - 1;
Block *block = mBlocks[i];
if (!block) {
PRBool fullBlock = PR_FALSE;
if (aStart <= blockFirstBit && aEnd >= blockLastBit)
fullBlock = PR_TRUE;
block = new Block(fullBlock ? 0xFF : 0);
if (NS_UNLIKELY(!block)) // OOM
return;
mBlocks[i] = block;
if (fullBlock)
continue;
}
const PRUint32 start = aStart > blockFirstBit ? aStart - blockFirstBit : 0;
const PRUint32 end = PR_MIN(aEnd - blockFirstBit, BLOCK_SIZE_BITS - 1);
for (PRUint32 bit = start; bit <= end; ++bit) {
block->mBits[bit/8] |= 1 << (bit & 0x7);
}
}
}
PRUint32 GetSize() {
PRUint32 size = 0;
for (PRUint32 i = 0; i < mBlocks.Length(); i++)
if (mBlocks[i])
size += sizeof(Block);
return size;
}
nsTArray< nsAutoPtr<Block> > mBlocks;
};
class THEBES_API gfxFontUtils {
public:
// for reading big-endian font data on either big or little-endian platforms
static inline PRUint16
ReadShortAt(const PRUint8 *aBuf, PRUint32 aIndex)
{
return (aBuf[aIndex] << 8) | aBuf[aIndex + 1];
}
static inline PRUint16
ReadShortAt16(const PRUint16 *aBuf, PRUint32 aIndex)
{
const PRUint8 *buf = (PRUint8*) aBuf;
PRUint32 index = aIndex << 1;
return (buf[index] << 8) | buf[index+1];
}
static inline PRUint32
ReadLongAt(const PRUint8 *aBuf, PRUint32 aIndex)
{
return ((aBuf[aIndex] << 24) | (aBuf[aIndex + 1] << 16) | (aBuf[aIndex + 2] << 8) | (aBuf[aIndex + 3]));
}
static nsresult
ReadCMAPTableFormat12(PRUint8 *aBuf, PRInt32 aLength, gfxSparseBitSet& aCharacterMap, std::bitset<128>& aUnicodeRanges);
static nsresult
ReadCMAPTableFormat4(PRUint8 *aBuf, PRInt32 aLength, gfxSparseBitSet& aCharacterMap, std::bitset<128>& aUnicodeRanges);
static nsresult
ReadCMAP(PRUint8 *aBuf, PRUint32 aBufLength, gfxSparseBitSet& aCharacterMap, std::bitset<128>& aUnicodeRanges, PRPackedBool& aUnicodeFont,
PRPackedBool& aSymbolFont);
static inline bool IsJoiner(PRUint32 ch) {
return (ch == 0x200C ||
ch == 0x200D ||
ch == 0x2060);
}
static inline bool IsInvalid(PRUint32 ch) {
return (ch == 0xFFFD);
}
static PRUint8 CharRangeBit(PRUint32 ch);
};
#endif /* GFX_FONT_UTILS_H */