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
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 /*******************************************************************************
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)
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) *
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;
69 /*******************************************************************************
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)
80 g = HeapAlloc(PSDRV_Heap, 0, sizeof(GLYPHNAME) + strlen(szName) + 1);
81 if (g == NULL) return -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);
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;
113 glyphNamesIndexed = FALSE;
116 TRACE("Added '%s' at glyphList[%i] (glyphListSize now %i)\n",
117 glyphList[index]->sz, index, glyphListSize);
122 /*******************************************************************************
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;
136 if (loIndex > hiIndex)
137 return GlyphListInsert(szName, loIndex);
139 midIndex = (loIndex + hiIndex) >> 1;
141 cmpResult = strcmp(szName, glyphList[midIndex]->sz);
146 TRACE("Found '%s' at glyphList[%i]\n", glyphList[midIndex]->sz,
153 hiIndex = midIndex - 1;
155 loIndex = midIndex + 1;
159 /*******************************************************************************
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)
170 index = GlyphListSearch(szName, 0, glyphListSize - 1);
174 return glyphList[index];
177 /*******************************************************************************
178 * PSDRV_IndexGlyphList
180 * Initializes index member of all GLYPHNAME structures
183 VOID PSDRV_IndexGlyphList(void)
187 if (glyphNamesIndexed)
190 TRACE("%i glyph names:\n", glyphListSize);
192 for (i = 0; i < glyphListSize; ++i)
194 glyphList[i]->index = i;
196 TRACE(" glyphList[%i] -> '%s'\n", i, glyphList[i]->sz);
200 glyphNamesIndexed = TRUE;