bitmapfunc.c revision 23a0898a
1/* $Xorg: bitmapfunc.c,v 1.5 2001/02/09 02:04:02 xorgcvs Exp $ */
2
3/*
4
5Copyright 1991, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27*/
28
29/* $XFree86: xc/lib/font/bitmap/bitmapfunc.c,v 3.17 2002/09/19 13:21:58 tsi Exp $ */
30
31/*
32 * Author:  Keith Packard, MIT X Consortium
33 */
34
35#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38
39/*
40 * Translate monolithic #defines to modular definitions
41 */
42
43#ifdef PCFFORMAT
44#define XFONT_PCFFORMAT 1
45#endif
46
47#ifdef SNFFORMAT
48#define XFONT_SNFFORMAT 1
49#endif
50
51#ifdef BDFFORMAT
52#define XFONT_BDFFORMAT 1
53#endif
54
55#include <X11/fonts/fntfilst.h>
56#include <X11/fonts/bitmap.h>
57#include <X11/fonts/fontutil.h>
58#include <X11/fonts/bdfint.h>
59#include <X11/fonts/pcf.h>
60#include "snfstr.h"
61
62typedef struct _BitmapFileFunctions {
63    int         (*ReadFont) (FontPtr /* pFont */, FontFilePtr /* file */,
64			     int /* bit */, int /* byte */,
65			     int /* glyph */, int /* scan */);
66    int         (*ReadInfo) (  FontInfoPtr /* pFontInfo */,
67			       FontFilePtr /* file */ );
68}           BitmapFileFunctionsRec, *BitmapFileFunctionsPtr;
69
70
71/*
72 * the readers[] and renderers[] arrays must be in the same order,
73 * and also in the same order as scale[] and find_scale[] in bitscale.c
74 *
75 */
76static BitmapFileFunctionsRec readers[] = {
77#if XFONT_PCFFORMAT
78    { pcfReadFont, pcfReadFontInfo} ,
79    { pcfReadFont, pcfReadFontInfo} ,
80#ifdef X_GZIP_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#endif
91#if XFONT_BDFFORMAT
92    { bdfReadFont, bdfReadFontInfo} ,
93    { bdfReadFont, bdfReadFontInfo} ,
94#ifdef X_GZIP_FONT_COMPRESSION
95    { bdfReadFont, bdfReadFontInfo} ,
96#endif
97#endif
98#if XFONT_PCFFORMAT
99    { pmfReadFont, pcfReadFontInfo} ,
100#endif
101};
102
103
104#define CAPABILITIES (CAP_MATRIX | CAP_CHARSUBSETTING)
105
106static int
107BitmapOpenBitmap (FontPathElementPtr fpe, FontPtr *ppFont, int flags,
108		  FontEntryPtr entry, char *fileName,
109		  fsBitmapFormat format, fsBitmapFormatMask fmask,
110		  FontPtr non_cachable_font) /* We don't do licensing */
111{
112    FontFilePtr	file;
113    FontPtr     pFont;
114    int         i;
115    int         ret;
116    int         bit,
117                byte,
118                glyph,
119                scan,
120		image;
121
122    i = BitmapGetRenderIndex(entry->u.bitmap.renderer);
123    file = FontFileOpen (fileName);
124    if (!file)
125	return BadFontName;
126    if (!(pFont = CreateFontRec())) {
127	fprintf(stderr, "Error: Couldn't allocate pFont (%ld)\n",
128		(unsigned long)sizeof(FontRec));
129	FontFileClose (file);
130	return AllocError;
131    }
132    /* set up default values */
133    FontDefaultFormat(&bit, &byte, &glyph, &scan);
134    /* get any changes made from above */
135    ret = CheckFSFormat(format, fmask, &bit, &byte, &scan, &glyph, &image);
136
137    /* Fill in font record. Data format filled in by reader. */
138    pFont->refcnt = 0;
139
140    ret = (*readers[i].ReadFont) (pFont, file, bit, byte, glyph, scan);
141
142    FontFileClose (file);
143    if (ret != Successful) {
144	xfree(pFont);
145    } else {
146	*ppFont = pFont;
147    }
148    return ret;
149}
150
151static int
152BitmapGetInfoBitmap (FontPathElementPtr fpe, FontInfoPtr pFontInfo,
153		     FontEntryPtr entry, char *fileName)
154{
155    FontFilePtr file;
156    int		i;
157    int		ret;
158    FontRendererPtr renderer;
159
160    renderer = FontFileMatchRenderer (fileName);
161    if (!renderer)
162	return BadFontName;
163    i = BitmapGetRenderIndex(renderer);
164    file = FontFileOpen (fileName);
165    if (!file)
166	return BadFontName;
167    ret = (*readers[i].ReadInfo) (pFontInfo, file);
168    FontFileClose (file);
169    return ret;
170}
171
172static FontRendererRec	renderers[] = {
173#if XFONT_PCFFORMAT
174    { ".pcf", 4, BitmapOpenBitmap, BitmapOpenScalable,
175	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
176	CAPABILITIES },
177    { ".pcf.Z", 6, BitmapOpenBitmap, BitmapOpenScalable,
178	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
179	CAPABILITIES },
180#ifdef X_GZIP_FONT_COMPRESSION
181    { ".pcf.gz", 7,
182    BitmapOpenBitmap, BitmapOpenScalable,
183	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
184	CAPABILITIES },
185#endif
186#endif
187#if XFONT_SNFFORMAT
188    { ".snf", 4, BitmapOpenBitmap, BitmapOpenScalable,
189	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
190	CAPABILITIES },
191    { ".snf.Z", 6, BitmapOpenBitmap, BitmapOpenScalable,
192	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
193	CAPABILITIES },
194#ifdef X_GZIP_FONT_COMPRESSION
195    { ".snf.gz", 7, BitmapOpenBitmap, BitmapOpenScalable,
196	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
197	CAPABILITIES },
198#endif
199#endif
200#if XFONT_BDFFORMAT
201    { ".bdf", 4, BitmapOpenBitmap, BitmapOpenScalable,
202	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
203	CAPABILITIES },
204    { ".bdf.Z", 6, BitmapOpenBitmap, BitmapOpenScalable,
205	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
206	CAPABILITIES },
207#ifdef X_GZIP_FONT_COMPRESSION
208    { ".bdf.gz", 7, BitmapOpenBitmap, BitmapOpenScalable,
209	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
210	CAPABILITIES },
211#endif
212#endif
213#if XFONT_PCFFORMAT
214    { ".pmf", 4, BitmapOpenBitmap, BitmapOpenScalable,
215	BitmapGetInfoBitmap, BitmapGetInfoScalable, 0,
216	CAPABILITIES }
217#endif
218};
219
220#define numRenderers	(sizeof renderers / sizeof renderers[0])
221
222void
223BitmapRegisterFontFileFunctions (void)
224{
225    int	    i;
226
227    for (i = 0; i < numRenderers; i++)
228	FontFileRegisterRenderer (&renderers[i]);
229}
230
231/*
232 * compute offset into renderers array - used to find the font reader,
233 * the font info reader, and the bitmap scaling routine.  All users
234 * of this routine must be kept in step with the renderer array.
235 */
236int
237BitmapGetRenderIndex(FontRendererPtr renderer)
238{
239    return renderer - renderers;
240}
241