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