1/*
2
3Copyright 1991, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27/*
28 * Author:  Keith Packard, MIT X Consortium
29 */
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include <X11/fonts/fntfilst.h>
35
36BitmapSourcesRec	FontFileBitmapSources;
37
38Bool
39FontFileRegisterBitmapSource (FontPathElementPtr fpe)
40{
41    FontPathElementPtr	*new;
42    int			i;
43    int			newsize;
44
45    for (i = 0; i < FontFileBitmapSources.count; i++)
46	if (FontFileBitmapSources.fpe[i] == fpe)
47	    return TRUE;
48    if (FontFileBitmapSources.count == FontFileBitmapSources.size)
49    {
50	newsize = FontFileBitmapSources.size + 4;
51	new = realloc (FontFileBitmapSources.fpe, newsize * sizeof *new);
52	if (!new)
53	    return FALSE;
54	FontFileBitmapSources.size = newsize;
55	FontFileBitmapSources.fpe = new;
56    }
57    FontFileBitmapSources.fpe[FontFileBitmapSources.count++] = fpe;
58    return TRUE;
59}
60
61void
62FontFileUnregisterBitmapSource (FontPathElementPtr fpe)
63{
64    int	    i;
65
66    for (i = 0; i < FontFileBitmapSources.count; i++)
67	if (FontFileBitmapSources.fpe[i] == fpe)
68	{
69	    FontFileBitmapSources.count--;
70	    if (FontFileBitmapSources.count == 0)
71	    {
72		FontFileBitmapSources.size = 0;
73		free (FontFileBitmapSources.fpe);
74		FontFileBitmapSources.fpe = 0;
75	    }
76	    else
77	    {
78	    	for (; i < FontFileBitmapSources.count; i++)
79		    FontFileBitmapSources.fpe[i] = FontFileBitmapSources.fpe[i+1];
80	    }
81	    break;
82	}
83}
84
85/*
86 * Our set_path_hook: unregister all bitmap sources.
87 * This is necessary because already open fonts will keep their FPEs
88 * allocated, but they may not be on the new font path.
89 * The bitmap sources in the new path will be registered by the init_func.
90 */
91void
92FontFileEmptyBitmapSource(void)
93{
94    if (FontFileBitmapSources.count == 0)
95	return;
96
97    FontFileBitmapSources.count = 0;
98    FontFileBitmapSources.size = 0;
99    free (FontFileBitmapSources.fpe);
100    FontFileBitmapSources.fpe = 0;
101}
102
103int
104FontFileMatchBitmapSource (FontPathElementPtr fpe,
105			   FontPtr *pFont,
106			   int flags,
107			   FontEntryPtr entry,
108			   FontNamePtr zeroPat,
109			   FontScalablePtr vals,
110			   fsBitmapFormat format,
111			   fsBitmapFormatMask fmask,
112			   Bool noSpecificSize)
113{
114    int			source;
115    FontEntryPtr	zero;
116    FontBitmapEntryPtr	bitmap;
117    int			ret;
118    FontDirectoryPtr	dir;
119    FontScaledPtr	scaled;
120
121    /*
122     * Look through all the registered bitmap sources for
123     * the same zero name as ours; entries along that one
124     * can be scaled as desired.
125     */
126    ret = BadFontName;
127    for (source = 0; source < FontFileBitmapSources.count; source++)
128    {
129    	if (FontFileBitmapSources.fpe[source] == fpe)
130	    continue;
131	dir = (FontDirectoryPtr) FontFileBitmapSources.fpe[source]->private;
132	zero = FontFileFindNameInDir (&dir->scalable, zeroPat);
133	if (!zero)
134	    continue;
135    	scaled = FontFileFindScaledInstance (zero, vals, noSpecificSize);
136    	if (scaled)
137    	{
138	    if (scaled->pFont)
139	    {
140		*pFont = scaled->pFont;
141		(*pFont)->fpe = FontFileBitmapSources.fpe[source];
142		ret = Successful;
143	    }
144	    else if (scaled->bitmap)
145	    {
146		entry = scaled->bitmap;
147		bitmap = &entry->u.bitmap;
148		if (bitmap->pFont)
149		{
150		    *pFont = bitmap->pFont;
151		    (*pFont)->fpe = FontFileBitmapSources.fpe[source];
152		    ret = Successful;
153		}
154		else
155		{
156		    ret = FontFileOpenBitmap (
157				FontFileBitmapSources.fpe[source],
158				pFont, flags, entry, format, fmask);
159		    if (ret == Successful && *pFont)
160			(*pFont)->fpe = FontFileBitmapSources.fpe[source];
161		}
162	    }
163	    else /* "cannot" happen */
164	    {
165		ret = BadFontName;
166	    }
167	    break;
168    	}
169    }
170    return ret;
171}
172