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