1 /*******************************************************************************
3 * Functions and data structures used to maintain a single list of glyph
4 * names. The list is sorted alphabetically and each name appears only
5 * once. After all font information has been read, the 'index' field of
6 * each GLYPHNAME structure is initialized, so future sorts/searches can
7 * be done without comparing strings.
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(psdrv);
17 #define GLYPHLIST_ALLOCSIZE 1024
19 static GLYPHNAME **glyphList = NULL;
20 static INT glyphListSize = 0;
21 static BOOL glyphNamesIndexed = TRUE;
23 /*******************************************************************************
26 * Allocates initial block of memory for the glyph list and copies pointers to
27 * the AGL glyph names into it; returns 0 on success, 1 on failure
30 INT PSDRV_GlyphListInit()
35 * Compute the smallest multiple of GLYPHLIST_ALLOCSIZE that is
36 * greater than or equal to PSDRV_AGLGlyphNamesSize
39 glyphListSize = PSDRV_AGLGlyphNamesSize;
40 i = ((glyphListSize + GLYPHLIST_ALLOCSIZE - 1) / GLYPHLIST_ALLOCSIZE) *
43 TRACE("glyphList will initially hold %i glyph names\n", i);
45 glyphList = (GLYPHNAME **) HeapAlloc(PSDRV_Heap, 0,
46 i * sizeof(GLYPHNAME *));
47 if (glyphList == NULL)
49 ERR("Failed to allocate %i bytes of memory\n", i * sizeof(GLYPHNAME *));
53 for (i = 0; i < glyphListSize; ++i)
54 glyphList[i] = PSDRV_AGLGlyphNames + i;
59 /*******************************************************************************
62 * Inserts a copy of the glyph name into the list at the index, growing the
63 * list if necessary; returns index on success (-1 on failure)
66 inline static INT GlyphListInsert(LPCSTR szName, INT index)
70 g = HeapAlloc(PSDRV_Heap, 0, sizeof(GLYPHNAME) + strlen(szName) + 1);
73 ERR("Failed to allocate %i bytes of memory\n",
74 sizeof(GLYPHNAME) + strlen(szName) + 1);
79 g->sz = (LPSTR)(g + 1);
80 strcpy((LPSTR)g->sz, szName);
82 if (glyphListSize % GLYPHLIST_ALLOCSIZE == 0) /* grow the list? */
84 GLYPHNAME **newGlyphList;
86 newGlyphList = (GLYPHNAME **) HeapReAlloc(PSDRV_Heap, 0, glyphList,
87 (glyphListSize + GLYPHLIST_ALLOCSIZE) * sizeof(GLYPHNAME *));
88 if (newGlyphList == NULL)
90 ERR("Failed to allocate %i bytes of memory\n", (glyphListSize +
91 GLYPHLIST_ALLOCSIZE) * sizeof (GLYPHNAME *));
92 HeapFree(PSDRV_Heap, 0, g);
96 glyphList = newGlyphList;
98 TRACE("glyphList will now hold %i glyph names\n",
99 glyphListSize + GLYPHLIST_ALLOCSIZE);
102 if (index < glyphListSize)
104 memmove(glyphList + (index + 1), glyphList + index,
105 (glyphListSize - index) * sizeof(GLYPHNAME *));
108 glyphList[index] = g;
110 glyphNamesIndexed = FALSE;
112 TRACE("Added '%s' at glyphList[%i] (glyphListSize now %i)\n",
113 glyphList[index]->sz, index, glyphListSize);
118 /*******************************************************************************
121 * Searches the specified portion of the list for the glyph name and inserts it
122 * in the list if necessary; returns the index at which the name (now) resides
123 * (-1 if unable to insert it)
126 static INT GlyphListSearch(LPCSTR szName, INT loIndex, INT hiIndex)
128 INT midIndex, cmpResult;
132 if (loIndex > hiIndex)
133 return GlyphListInsert(szName, loIndex);
135 midIndex = (loIndex + hiIndex) >> 1;
137 cmpResult = strcmp(szName, glyphList[midIndex]->sz);
141 TRACE("Found '%s' at glyphList[%i]\n", glyphList[midIndex]->sz,
147 hiIndex = midIndex - 1;
149 loIndex = midIndex + 1;
153 /*******************************************************************************
156 * Searches the glyph name list for the provided name, adds it to the list if
157 * necessary, and returns a pointer to it (NULL if unable to add it)
160 const GLYPHNAME *PSDRV_GlyphName(LPCSTR szName)
164 TRACE("'%s'\n", szName);
166 index = GlyphListSearch(szName, 0, glyphListSize - 1);
170 return glyphList[index];
173 /*******************************************************************************
174 * PSDRV_IndexGlyphList
176 * Initializes index member of all GLYPHNAME structures
179 VOID PSDRV_IndexGlyphList()
183 if (glyphNamesIndexed == TRUE)
186 TRACE("%i glyph names:\n", glyphListSize);
188 for (i = 0; i < glyphListSize; ++i)
190 glyphList[i]->index = i;
191 TRACE(" glyphList[%i] -> '%s'\n", i, glyphList[i]->sz);
194 glyphNamesIndexed = TRUE;