gdi32/tests: Prevent a crash on win95.
[wine/multimedia.git] / dlls / wineps.drv / glyphlist.c
blobf8d1c837694431950f9f69aeda321f4201b36f18
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.
9 * Copyright 2001 Ian Pilcher
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <string.h>
27 #include "psdrv.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
32 #define GLYPHLIST_ALLOCSIZE 1024
34 static GLYPHNAME **glyphList = NULL;
35 static INT glyphListSize = 0;
36 static BOOL glyphNamesIndexed = TRUE;
38 /*******************************************************************************
39 * PSDRV_GlyphListInit
41 * Allocates initial block of memory for the glyph list and copies pointers to
42 * the AGL glyph names into it; returns 0 on success, 1 on failure
45 INT PSDRV_GlyphListInit(void)
47 INT i;
50 * Compute the smallest multiple of GLYPHLIST_ALLOCSIZE that is
51 * greater than or equal to PSDRV_AGLGlyphNamesSize
54 glyphListSize = PSDRV_AGLGlyphNamesSize;
55 i = ((glyphListSize + GLYPHLIST_ALLOCSIZE - 1) / GLYPHLIST_ALLOCSIZE) *
56 GLYPHLIST_ALLOCSIZE;
58 TRACE("glyphList will initially hold %i glyph names\n", i);
60 glyphList = HeapAlloc(PSDRV_Heap, 0, i * sizeof(GLYPHNAME *));
61 if (glyphList == NULL) return 1;
63 for (i = 0; i < glyphListSize; ++i)
64 glyphList[i] = PSDRV_AGLGlyphNames + i;
66 return 0;
69 /*******************************************************************************
70 * GlyphListInsert
72 * Inserts a copy of the glyph name into the list at the index, growing the
73 * list if necessary; returns index on success (-1 on failure)
76 static inline INT GlyphListInsert(LPCSTR szName, INT index)
78 GLYPHNAME *g;
80 g = HeapAlloc(PSDRV_Heap, 0, sizeof(GLYPHNAME) + strlen(szName) + 1);
81 if (g == NULL) return -1;
83 g->index = -1;
84 g->sz = (LPSTR)(g + 1);
85 strcpy((LPSTR)g->sz, szName);
87 if (glyphListSize % GLYPHLIST_ALLOCSIZE == 0) /* grow the list? */
89 GLYPHNAME **newGlyphList;
91 newGlyphList = HeapReAlloc(PSDRV_Heap, 0, glyphList,
92 (glyphListSize + GLYPHLIST_ALLOCSIZE) * sizeof(GLYPHNAME *));
93 if (newGlyphList == NULL)
95 HeapFree(PSDRV_Heap, 0, g);
96 return -1;
99 glyphList = newGlyphList;
101 TRACE("glyphList will now hold %i glyph names\n",
102 glyphListSize + GLYPHLIST_ALLOCSIZE);
105 if (index < glyphListSize)
107 memmove(glyphList + (index + 1), glyphList + index,
108 (glyphListSize - index) * sizeof(GLYPHNAME *));
111 glyphList[index] = g;
112 ++glyphListSize;
113 glyphNamesIndexed = FALSE;
115 #if 0
116 TRACE("Added '%s' at glyphList[%i] (glyphListSize now %i)\n",
117 glyphList[index]->sz, index, glyphListSize);
118 #endif
119 return index;
122 /*******************************************************************************
123 * GlyphListSearch
125 * Searches the specified portion of the list for the glyph name and inserts it
126 * in the list if necessary; returns the index at which the name (now) resides
127 * (-1 if unable to insert it)
130 static INT GlyphListSearch(LPCSTR szName, INT loIndex, INT hiIndex)
132 INT midIndex, cmpResult;
134 while (1)
136 if (loIndex > hiIndex)
137 return GlyphListInsert(szName, loIndex);
139 midIndex = (loIndex + hiIndex) >> 1;
141 cmpResult = strcmp(szName, glyphList[midIndex]->sz);
143 if (cmpResult == 0)
145 #if 0
146 TRACE("Found '%s' at glyphList[%i]\n", glyphList[midIndex]->sz,
147 midIndex);
148 #endif
149 return midIndex;
152 if (cmpResult < 0)
153 hiIndex = midIndex - 1;
154 else
155 loIndex = midIndex + 1;
159 /*******************************************************************************
160 * PSDRV_GlyphName
162 * Searches the glyph name list for the provided name, adds it to the list if
163 * necessary, and returns a pointer to it (NULL if unable to add it)
166 const GLYPHNAME *PSDRV_GlyphName(LPCSTR szName)
168 INT index;
170 index = GlyphListSearch(szName, 0, glyphListSize - 1);
171 if (index < 0)
172 return NULL;
174 return glyphList[index];
177 /*******************************************************************************
178 * PSDRV_IndexGlyphList
180 * Initializes index member of all GLYPHNAME structures
183 VOID PSDRV_IndexGlyphList(void)
185 INT i;
187 if (glyphNamesIndexed)
188 return;
190 TRACE("%i glyph names:\n", glyphListSize);
192 for (i = 0; i < glyphListSize; ++i)
194 glyphList[i]->index = i;
195 #if 0
196 TRACE(" glyphList[%i] -> '%s'\n", i, glyphList[i]->sz);
197 #endif
200 glyphNamesIndexed = TRUE;