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