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"
35a96d7823Smrg
36a96d7823Smrg#include <X11/fonts/fntfilst.h>
37a96d7823Smrg#include <X11/fonts/bitmap.h>
38a96d7823Smrg#include <X11/fonts/fontutil.h>
39a96d7823Smrg#if XFONT_BDFFORMAT
40a96d7823Smrg#include <X11/fonts/bdfint.h>
41a96d7823Smrg#endif
42a96d7823Smrg#if XFONT_PCFFORMAT
43a96d7823Smrg#include <X11/fonts/pcf.h>
44a96d7823Smrg#endif
45a96d7823Smrg#if XFONT_SNFFORMAT
46a96d7823Smrg#include "snfstr.h"
47a96d7823Smrg#endif
48a96d7823Smrg
49a96d7823Smrg#if XFONT_PCFFORMAT || XFONT_SNFFORMAT || XFONT_BDFFORMAT
50a96d7823Smrgtypedef struct _BitmapFileFunctions {
51a96d7823Smrg    int         (*ReadFont) (FontPtr /* pFont */, FontFilePtr /* file */,
52a96d7823Smrg			     int /* bit */, int /* byte */,
53a96d7823Smrg			     int /* glyph */, int /* scan */);
54a96d7823Smrg    int         (*ReadInfo) (  FontInfoPtr /* pFontInfo */,
55a96d7823Smrg			       FontFilePtr /* file */ );
56a96d7823Smrg}           BitmapFileFunctionsRec, *BitmapFileFunctionsPtr;
57a96d7823Smrg
58a96d7823Smrgstatic int BitmapGetRenderIndex(FontRendererPtr renderer);
59a96d7823Smrg
60a96d7823Smrg/*
61a96d7823Smrg * the readers[] and renderers[] arrays must be in the same order,
62a96d7823Smrg * and also in the same order as scale[] and find_scale[] in bitscale.c
63a96d7823Smrg *
64a96d7823Smrg */
65a96d7823Smrgstatic BitmapFileFunctionsRec readers[] = {
66a96d7823Smrg#if XFONT_PCFFORMAT
67a96d7823Smrg    { pcfReadFont, pcfReadFontInfo} ,
68a96d7823Smrg    { pcfReadFont, pcfReadFontInfo} ,
69a96d7823Smrg# ifdef X_GZIP_FONT_COMPRESSION
70a96d7823Smrg    { pcfReadFont, pcfReadFontInfo} ,
71a96d7823Smrg# endif
72a96d7823Smrg# ifdef X_BZIP2_FONT_COMPRESSION
73a96d7823Smrg    { pcfReadFont, pcfReadFontInfo} ,
74a96d7823Smrg# endif
75a96d7823Smrg#endif
76a96d7823Smrg#if XFONT_SNFFORMAT
77a96d7823Smrg    { snfReadFont, snfReadFontInfo},
78a96d7823Smrg    { snfReadFont, snfReadFontInfo},
79a96d7823Smrg# ifdef X_GZIP_FONT_COMPRESSION
80a96d7823Smrg    { snfReadFont, snfReadFontInfo} ,
81a96d7823Smrg# endif
82a96d7823Smrg# ifdef X_BZIP2_FONT_COMPRESSION
83a96d7823Smrg    { snfReadFont, snfReadFontInfo} ,
84a96d7823Smrg# endif
85a96d7823Smrg#endif
86a96d7823Smrg#if XFONT_BDFFORMAT
87a96d7823Smrg    { bdfReadFont, bdfReadFontInfo} ,
88a96d7823Smrg    { bdfReadFont, bdfReadFontInfo} ,
89a96d7823Smrg# ifdef X_GZIP_FONT_COMPRESSION
90a96d7823Smrg    { bdfReadFont, bdfReadFontInfo} ,
91a96d7823Smrg# endif
92a96d7823Smrg# ifdef X_BZIP2_FONT_COMPRESSION
93a96d7823Smrg    { bdfReadFont, bdfReadFontInfo} ,
94a96d7823Smrg# endif
95a96d7823Smrg#endif
96a96d7823Smrg};
97a96d7823Smrg
98a96d7823Smrg
99a96d7823Smrg#define CAPABILITIES (CAP_MATRIX | CAP_CHARSUBSETTING)
100a96d7823Smrg
101a96d7823Smrgstatic int
102a96d7823SmrgBitmapOpenBitmap (FontPathElementPtr fpe, FontPtr *ppFont, int flags,
103a96d7823Smrg		  FontEntryPtr entry, char *fileName,
104a96d7823Smrg		  fsBitmapFormat format, fsBitmapFormatMask fmask,
105a96d7823Smrg		  FontPtr non_cachable_font) /* We don't do licensing */
106a96d7823Smrg{
107a96d7823Smrg    FontFilePtr	file;
108a96d7823Smrg    FontPtr     pFont;
109a96d7823Smrg    int         i;
110a96d7823Smrg    int         ret;
111a96d7823Smrg    int         bit,
112a96d7823Smrg                byte,
113a96d7823Smrg                glyph,
114a96d7823Smrg                scan,
115a96d7823Smrg		image;
116a96d7823Smrg
117a96d7823Smrg    i = BitmapGetRenderIndex(entry->u.bitmap.renderer);
118a96d7823Smrg    file = FontFileOpen (fileName);
119a96d7823Smrg    if (!file)
120a96d7823Smrg	return BadFontName;
121a96d7823Smrg    if (!(pFont = CreateFontRec())) {
122a96d7823Smrg	fprintf(stderr, "Error: Couldn't allocate pFont (%ld)\n",
123a96d7823Smrg		(unsigned long)sizeof(FontRec));
124a96d7823Smrg	FontFileClose (file);
125a96d7823Smrg	return AllocError;
126a96d7823Smrg    }
127a96d7823Smrg    /* set up default values */
128a96d7823Smrg    FontDefaultFormat(&bit, &byte, &glyph, &scan);
129a96d7823Smrg    /* get any changes made from above */
130a96d7823Smrg    ret = CheckFSFormat(format, fmask, &bit, &byte, &scan, &glyph, &image);
131a96d7823Smrg
132a96d7823Smrg    /* Fill in font record. Data format filled in by reader. */
133a96d7823Smrg    pFont->refcnt = 0;
134a96d7823Smrg
135a96d7823Smrg    ret = (*readers[i].ReadFont) (pFont, file, bit, byte, glyph, scan);
136a96d7823Smrg
137a96d7823Smrg    FontFileClose (file);
138a96d7823Smrg    if (ret != Successful) {
139a96d7823Smrg	free(pFont);
140a96d7823Smrg    } else {
141a96d7823Smrg	*ppFont = pFont;
142a96d7823Smrg    }
143a96d7823Smrg    return ret;
144a96d7823Smrg}
145a96d7823Smrg
146a96d7823Smrgstatic int
147a96d7823SmrgBitmapGetInfoBitmap (FontPathElementPtr fpe, FontInfoPtr pFontInfo,
148a96d7823Smrg		     FontEntryPtr entry, char *fileName)
149a96d7823Smrg{
150a96d7823Smrg    FontFilePtr file;
151a96d7823Smrg    int		i;
152a96d7823Smrg    int		ret;
153a96d7823Smrg    FontRendererPtr renderer;
154a96d7823Smrg
155a96d7823Smrg    renderer = FontFileMatchRenderer (fileName);
156a96d7823Smrg    if (!renderer)
157a96d7823Smrg	return BadFontName;
158a96d7823Smrg    i = BitmapGetRenderIndex(renderer);
159a96d7823Smrg    file = FontFileOpen (fileName);
160a96d7823Smrg    if (!file)
161a96d7823Smrg	return BadFontName;
162a96d7823Smrg    ret = (*readers[i].ReadInfo) (pFontInfo, file);
163a96d7823Smrg    FontFileClose (file);
164a96d7823Smrg    return ret;
165a96d7823Smrg}
166a96d7823Smrg
167a96d7823Smrgstatic FontRendererRec	renderers[] = {
168a96d7823Smrg#if XFONT_PCFFORMAT
169a96d7823Smrg    { ".pcf", 4, BitmapOpenBitmap, BitmapOpenScalable,
170a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
171a96d7823Smrg	CAPABILITIES },
172a96d7823Smrg    { ".pcf.Z", 6, BitmapOpenBitmap, BitmapOpenScalable,
173a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
174a96d7823Smrg	CAPABILITIES },
175a96d7823Smrg# ifdef X_GZIP_FONT_COMPRESSION
176a96d7823Smrg    { ".pcf.gz", 7,
177a96d7823Smrg	BitmapOpenBitmap, BitmapOpenScalable,
178a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
179a96d7823Smrg	CAPABILITIES },
180a96d7823Smrg# endif
181a96d7823Smrg# ifdef X_BZIP2_FONT_COMPRESSION
182a96d7823Smrg    { ".pcf.bz2", 8,
183a96d7823Smrg	BitmapOpenBitmap, BitmapOpenScalable,
184a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
185a96d7823Smrg	CAPABILITIES },
186a96d7823Smrg# endif
187a96d7823Smrg#endif
188a96d7823Smrg#if XFONT_SNFFORMAT
189a96d7823Smrg    { ".snf", 4, BitmapOpenBitmap, BitmapOpenScalable,
190a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
191a96d7823Smrg	CAPABILITIES },
192a96d7823Smrg    { ".snf.Z", 6, BitmapOpenBitmap, BitmapOpenScalable,
193a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
194a96d7823Smrg	CAPABILITIES },
195a96d7823Smrg# ifdef X_GZIP_FONT_COMPRESSION
196a96d7823Smrg    { ".snf.gz", 7, BitmapOpenBitmap, BitmapOpenScalable,
197a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
198a96d7823Smrg	CAPABILITIES },
199a96d7823Smrg# endif
200a96d7823Smrg# ifdef X_BZIP2_FONT_COMPRESSION
201a96d7823Smrg    { ".snf.bz2", 8, BitmapOpenBitmap, BitmapOpenScalable,
202a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
203a96d7823Smrg	CAPABILITIES },
204a96d7823Smrg# endif
205a96d7823Smrg#endif
206a96d7823Smrg#if XFONT_BDFFORMAT
207a96d7823Smrg    { ".bdf", 4, BitmapOpenBitmap, BitmapOpenScalable,
208a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
209a96d7823Smrg	CAPABILITIES },
210a96d7823Smrg    { ".bdf.Z", 6, BitmapOpenBitmap, BitmapOpenScalable,
211a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
212a96d7823Smrg	CAPABILITIES },
213a96d7823Smrg# ifdef X_GZIP_FONT_COMPRESSION
214a96d7823Smrg    { ".bdf.gz", 7, BitmapOpenBitmap, BitmapOpenScalable,
215a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
216a96d7823Smrg	CAPABILITIES },
217a96d7823Smrg# endif
218a96d7823Smrg# ifdef X_BZIP2_FONT_COMPRESSION
219a96d7823Smrg    { ".bdf.bz2", 8, BitmapOpenBitmap, BitmapOpenScalable,
220a96d7823Smrg	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
221a96d7823Smrg	CAPABILITIES },
222a96d7823Smrg# endif
223a96d7823Smrg#endif
224a96d7823Smrg};
225a96d7823Smrg
226a96d7823Smrg#define numRenderers	(sizeof renderers / sizeof renderers[0])
227a96d7823Smrg
228a96d7823Smrgvoid
229a96d7823SmrgBitmapRegisterFontFileFunctions (void)
230a96d7823Smrg{
231a96d7823Smrg    int	    i;
232a96d7823Smrg
233a96d7823Smrg    for (i = 0; i < numRenderers; i++)
234a96d7823Smrg	FontFileRegisterRenderer (&renderers[i]);
235a96d7823Smrg}
236a96d7823Smrg
237a96d7823Smrg/*
238a96d7823Smrg * compute offset into renderers array - used to find the font reader,
239a96d7823Smrg * the font info reader, and the bitmap scaling routine.  All users
240a96d7823Smrg * of this routine must be kept in step with the renderer array.
241a96d7823Smrg */
242a96d7823Smrgstatic int
243a96d7823SmrgBitmapGetRenderIndex(FontRendererPtr renderer)
244a96d7823Smrg{
245a96d7823Smrg    return renderer - renderers;
246a96d7823Smrg}
247a96d7823Smrg
248a96d7823Smrg#else
249a96d7823Smrgvoid
250a96d7823SmrgBitmapRegisterFontFileFunctions (void)
251a96d7823Smrg{
252a96d7823Smrg    /* nothing to do */
253a96d7823Smrg}
254a96d7823Smrg#endif /* XFONT_PCFFORMAT || XFONT_SNFFORMAT || XFONT_BDFFORMAT */
255