a patch for ttc fonts with bad index
[luatex.git] / source / libs / poppler / poppler-0.36.0 / poppler / NameToCharCode.cc
blob5448dc4ce100022fa367a5881c059143ef980450
1 //========================================================================
2 //
3 // NameToCharCode.cc
4 //
5 // Copyright 2001-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <config.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <string.h>
16 #include "goo/gmem.h"
17 #include "NameToCharCode.h"
19 //------------------------------------------------------------------------
21 struct NameToCharCodeEntry {
22 char *name;
23 CharCode c;
26 //------------------------------------------------------------------------
28 NameToCharCode::NameToCharCode() {
29 int i;
31 size = 31;
32 len = 0;
33 tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
34 for (i = 0; i < size; ++i) {
35 tab[i].name = NULL;
39 NameToCharCode::~NameToCharCode() {
40 int i;
42 for (i = 0; i < size; ++i) {
43 if (tab[i].name) {
44 gfree(tab[i].name);
47 gfree(tab);
50 void NameToCharCode::add(const char *name, CharCode c) {
51 NameToCharCodeEntry *oldTab;
52 int h, i, oldSize;
54 // expand the table if necessary
55 if (len >= size / 2) {
56 oldSize = size;
57 oldTab = tab;
58 size = 2*size + 1;
59 tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
60 for (h = 0; h < size; ++h) {
61 tab[h].name = NULL;
63 for (i = 0; i < oldSize; ++i) {
64 if (oldTab[i].name) {
65 h = hash(oldTab[i].name);
66 while (tab[h].name) {
67 if (++h == size) {
68 h = 0;
71 tab[h] = oldTab[i];
74 gfree(oldTab);
77 // add the new name
78 h = hash(name);
79 while (tab[h].name && strcmp(tab[h].name, name)) {
80 if (++h == size) {
81 h = 0;
84 if (!tab[h].name) {
85 tab[h].name = copyString(name);
87 tab[h].c = c;
89 ++len;
92 CharCode NameToCharCode::lookup(const char *name) {
93 int h;
95 h = hash(name);
96 while (tab[h].name) {
97 if (!strcmp(tab[h].name, name)) {
98 return tab[h].c;
100 if (++h == size) {
101 h = 0;
104 return 0;
107 int NameToCharCode::hash(const char *name) {
108 const char *p;
109 unsigned int h;
111 h = 0;
112 for (p = name; *p; ++p) {
113 h = 17 * h + (int)(*p & 0xff);
115 return (int)(h % size);