First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xprint / ps / PsCache.c
blob5c823a13e7afa4ae9002d6ee8d1a50bc0cfc6287
1 /*
3 Copyright 1996, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
27 * (c) Copyright 1996 Hewlett-Packard Company
28 * (c) Copyright 1996 International Business Machines Corp.
29 * (c) Copyright 1996, 2000 Sun Microsystems, Inc. All Rights Reserved.
30 * (c) Copyright 1996 Novell, Inc.
31 * (c) Copyright 1996 Digital Equipment Corp.
32 * (c) Copyright 1996 Fujitsu Limited
33 * (c) Copyright 1996 Hitachi, Ltd.
35 * Permission is hereby granted, free of charge, to any person obtaining
36 * a copy of this software and associated documentation files (the
37 * "Software"), to deal in the Software without restriction, including
38 * without limitation the rights to use, copy, modify, merge, publish,
39 * distribute, sublicense, and/or sell copies of the Software, and to
40 * permit persons to whom the Software is furnished to do so, subject
41 * to the following conditions:
43 * The above copyright notice and this permission notice shall be included
44 * in all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
50 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
51 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
52 * SOFTWARE.
54 * Except as contained in this notice, the names of the copyright holders
55 * shall not be used in advertising or otherwise to promote the sale, use
56 * or other dealings in this Software without prior written authorization
57 * from said copyright holders.
60 /*******************************************************************
62 ** *********************************************************
63 ** *
64 ** * File: PsCache.c
65 ** *
66 ** * Contents: Character-caching routines
67 ** *
68 ** * Created By: Jay Hobson (Sun MicroSystems)
69 ** *
70 ** * Copyright: Copyright 1996 The Open Group, Inc.
71 ** *
72 ** *********************************************************
74 ********************************************************************/
76 #ifdef HAVE_DIX_CONFIG_H
77 #include <dix-config.h>
78 #endif
80 #include "Ps.h"
81 #include "gcstruct.h"
82 #include "windowstr.h"
83 #include <X11/fonts/fntfil.h>
84 #include <X11/fonts/fntfilst.h>
86 #define GET 0
87 #define RESET 1
89 struct bm_cache_list {
90 struct bm_cache_list *next;
91 struct bm_cache_list *prev;
92 int height;
93 long id;
94 char *pBuffer;
97 struct bm_cache_head {
98 struct bm_cache_list *head;
99 int width;
100 struct bm_cache_head *next;
101 struct bm_cache_head *prev;
104 static struct bm_cache_head *bm_cache = NULL;
106 static long
107 PsBmUniqueId(int func)
109 static long unique_id = 0;
111 if(func == RESET)
113 unique_id = 0;
114 return 0;
116 else
117 return ++unique_id;
120 int
121 PsBmIsImageCached(
122 int gWidth,
123 int gHeight,
124 char *pBuffer)
126 int return_val = 0;
127 struct bm_cache_head *pList = bm_cache;
129 while(pList != NULL && !return_val)
131 if(pList->width == gWidth)
133 struct bm_cache_list *pItem = pList->head;
135 while(pItem != NULL)
137 if(pItem->height == gHeight)
139 int length = 4*(gWidth/32+(gWidth%32!=0))*gHeight;
141 if(!memcmp(pItem->pBuffer, pBuffer, sizeof(char)*length))
143 return_val = pItem->id;
144 break;
147 else if(pItem->height > gHeight)
148 break;
150 pItem = pItem->next;
153 else if(pList->width > gWidth)
154 break;
156 pList = pList->next;
158 return return_val;
162 PsBmPutImageInCache(
163 int gWidth,
164 int gHeight,
165 char *pBuffer)
167 int return_val = 0;
168 struct bm_cache_head *pList = bm_cache;
169 struct bm_cache_list *pNew;
170 int length = 4*(gWidth/32+(gWidth%32!=0))*gHeight;
172 if(gWidth == 1 && gHeight == 1 && pBuffer[0] == 0)
173 return return_val;
175 pNew = (struct bm_cache_list *)malloc(sizeof(struct bm_cache_list));
176 pNew->next = NULL;
177 pNew->prev = NULL;
178 pNew->height = gHeight;
179 pNew->id = PsBmUniqueId(GET);
180 pNew->pBuffer = (char *)malloc(sizeof(char)*length);
182 memcpy(pNew->pBuffer, pBuffer, length);
184 while(pList != NULL)
186 if(pList->width == gWidth)
188 struct bm_cache_list *pItem = pList->head;
190 while(pItem != NULL)
192 if(pItem->height >= gHeight)
194 pNew->next = pItem;
195 pNew->prev = pItem->prev;
196 if(pItem->prev != NULL)
197 pItem->prev->next = pNew;
198 else
199 pList->head = pNew;
200 pItem->prev = pNew;
202 return_val = pNew->id;
204 break;
206 else if(pItem->next == NULL)
208 pNew->prev = pItem;
209 pItem->next = pNew;
211 return_val = pNew->id;
213 break;
216 pItem = pItem->next;
219 break;
222 pList = pList->next;
225 if(pList == NULL)
227 struct bm_cache_head *pNewList;
229 pNewList = (struct bm_cache_head *)malloc(sizeof(struct bm_cache_head));
231 pNewList->next = NULL;
232 pNewList->prev = NULL;
233 pNewList->width = gWidth;
234 pNewList->head = pNew;
236 if(bm_cache == NULL)
238 bm_cache = pNewList;
239 return_val = pNew->id;
241 else
243 pList = bm_cache;
245 while(pList != NULL)
247 if(pList->width > gWidth)
249 pNewList->next = pList;
250 pNewList->prev = pList->prev;
252 if(pList->prev != NULL)
253 pList->prev->next = pNewList;
254 else
255 bm_cache = pNewList;
256 pList->prev = pNewList;
258 return_val = pNew->id;
260 break;
262 else if(pList->next == NULL)
264 pNewList->prev = pList;
265 pList->next = pNewList;
267 return_val = pNew->id;
269 break;
272 pList = pList->next;
277 return return_val;
281 static void
282 PsBmClearImageCacheItem(
283 struct bm_cache_list *pItem)
285 if(pItem != NULL)
287 if(pItem->pBuffer != NULL)
288 free(pItem->pBuffer);
289 pItem->pBuffer = NULL;
291 if(pItem->next)
292 PsBmClearImageCacheItem(pItem->next);
293 pItem->next = NULL;
295 free(pItem);
296 pItem = NULL;
300 static void
301 PsBmClearImageCacheList(
302 struct bm_cache_head *pList)
304 if(pList != NULL)
306 if(pList->head)
307 PsBmClearImageCacheItem(pList->head);
308 pList->head = NULL;
310 if(pList->next)
311 PsBmClearImageCacheList(pList->next);
312 pList->next = NULL;
314 free(pList);
315 pList = NULL;
319 void
320 PsBmClearImageCache(void)
322 PsBmClearImageCacheList(bm_cache);
324 bm_cache = NULL;
326 PsBmUniqueId(RESET);