home *** CD-ROM | disk | FTP | other *** search
- //========================================================================
- //
- // FontEncoding.cc
- //
- // Copyright 1999 Derek B. Noonburg
- //
- //========================================================================
- //
- // Ported to EPOC by Sander van der Wal
- //
- // $Log: FontEncoding.cpp $
- // Revision 1.3 2000-09-20 20:48:58+02 svdwal
- // bugfix from xpdf 0.91
- //
- // Revision 1.2 2000-09-17 13:38:24+02 svdwal
- // Ported
- //
-
- #ifdef __GNUC__
- #pragma implementation
- #endif
-
- #ifdef __SYMBIAN32__
- #include <e32def.h> // remove warning about NULL redefinition
- #endif
-
- // --0 C Library
- #include <string.h>
-
- // --o GooLib
- #include "gmem.h"
- #include "FontEncoding.h"
-
-
- //------------------------------------------------------------------------
- // FontEncoding
- //------------------------------------------------------------------------
-
- inline int FontEncoding::hash(const char *name) const {
- register Guint h = (Guint)name[0] & 0xff;
- if (h && name[1])
- h = h * 61 + ((Guint)name[1] & 0xff);
- return (int)(h % (Guint)fontEncHashSize);
- }
-
-
- void FontEncoding::ConstructL()
- {
- register int i;
-
- freeEnc = gTrue;
- encoding = (const char **)User::AllocL(256 * sizeof(char *));
- size = 256;
- for (i = 0; i < 256; ++i)
- encoding[i] = 0;
- for (i = 0; i < fontEncHashSize; ++i)
- hashTab[i] = -1;
- }
-
-
- void FontEncoding::Construct(const char* const encoding[], int size) {
- register int i;
-
- freeEnc = gFalse;
- this->encoding = (const char**) encoding;
- this->size = size;
- for (i = 0; i < fontEncHashSize; ++i)
- hashTab[i] = -1;
- for (i = 0; i < size; ++i) {
- if (encoding[i])
- addChar1(i, encoding[i]);
- }
- }
-
-
- void FontEncoding::ConstructL(FontEncoding *fontEnc)
- {
- freeEnc = gTrue;
- encoding = (const char **)User::AllocL(fontEnc->size * sizeof(char *));
- // zeroed so cleanup in destructor works
- size = 0;
- for (register int i = 0; i < fontEnc->size; ++i) {
- encoding[i] = fontEnc->encoding[i] ? copyStringL(fontEnc->encoding[i]) : NULL;
- size++;
- }
- Mem::Copy(hashTab, fontEnc->hashTab, fontEncHashSize * sizeof(short));
- }
-
- FontEncoding *FontEncoding::copyL()
- {
- FontEncoding* self = new(ELeave) FontEncoding();
- CleanupStack::PushL(self);
- self->ConstructL(this);
- CleanupStack::Pop(); // self
- return self;
- }
-
- void FontEncoding::addChar(register int code, const char *name) {
-
- // replace character associated with code
- if (encoding[code]) {
- register int h = hash(encoding[code]);
- for (register int i = 0; i < fontEncHashSize; ++i) {
- if (hashTab[h] == code) {
- hashTab[h] = -2;
- break;
- }
- if (++h == fontEncHashSize)
- h = 0;
- }
- User::Free((void*)encoding[code]);
- encoding[code] = 0;
- }
-
- // associate name with code
- encoding[code] = name;
-
- // insert name in hash table
- addChar1(code, name);
- }
-
- void FontEncoding::addChar1(register int code, const char *name) {
-
- // insert name in hash table
- register int h = hash(name);
- for (register int i = 0; i < fontEncHashSize; ++i) {
- register int code2 = hashTab[h];
- if (code2 < 0) {
- hashTab[h] = code;
- break;
- }
- else if (encoding[code2] && !strcmp(encoding[code2], name)) {
- // keep the highest code for each char -- this is needed because
- // X won't display chars with codes < 32
- if (code > code2)
- hashTab[h] = code;
- break;
- }
- if (++h == fontEncHashSize)
- h = 0;
- }
- }
-
- FontEncoding::~FontEncoding() {
-
- if (freeEnc) {
- if (encoding) {
- for (register int i = 0; i < size; ++i)
- User::Free((void*)encoding[i]);
- }
- User::Free(encoding);
- }
- }
-
- int FontEncoding::getCharCode(const char *name) const {
-
- register int h = hash(name);
- for (register int i = 0; i < fontEncHashSize; ++i) {
- register int code = hashTab[h];
- if (code == -1 ||
- (code >= 0 && encoding[code] && !strcmp(encoding[code], name)))
- return code;
- if (++h >= fontEncHashSize)
- h = 0;
- }
- return -1;
- }
-