Initial commit
[xorg_rtime.git] / xorg-server-1.4 / hw / xprint / pcl / PclSFonts.c
blob2474d90d7ac5ee1acf88d10ec1c68727fca829d2
1 /*******************************************************************
2 **
3 ** *********************************************************
4 ** *
5 ** * File: PclSFonts.c
6 ** *
7 ** * Contents:
8 ** * Send Soft Font Download data to the specified
9 ** * file pointer.
10 ** *
11 ** * Created: 3/4/96
12 ** *
13 ** *********************************************************
15 ********************************************************************/
17 (c) Copyright 1996 Hewlett-Packard Company
18 (c) Copyright 1996 International Business Machines Corp.
19 (c) Copyright 1996 Sun Microsystems, Inc.
20 (c) Copyright 1996 Novell, Inc.
21 (c) Copyright 1996 Digital Equipment Corp.
22 (c) Copyright 1996 Fujitsu Limited
23 (c) Copyright 1996 Hitachi, Ltd.
25 Permission is hereby granted, free of charge, to any person obtaining a copy
26 of this software and associated documentation files (the "Software"), to deal
27 in the Software without restriction, including without limitation the rights
28 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 copies of the Software, and to permit persons to whom the Software is
30 furnished to do so, subject to the following conditions:
32 The above copyright notice and this permission notice shall be included in
33 all copies or substantial portions of the Software.
35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
39 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 Except as contained in this notice, the names of the copyright holders shall
43 not be used in advertising or otherwise to promote the sale, use or other
44 dealings in this Software without prior written authorization from said
45 copyright holders.
49 #ifdef HAVE_DIX_CONFIG_H
50 #include <dix-config.h>
51 #endif
53 #include <stdio.h>
54 #include "Pcl.h"
56 static char tmp1;
57 static short tmp2;
58 #define Put1byte(fp, x) tmp1=x; fwrite((char *)&tmp1, 1, 1, fp)
59 #define Put2bytes(fp, x) tmp2=x; fwrite((char *)&tmp2, 2, 1, fp)
61 #define ESC 0x1b
62 #define SYMBOL_SET 277
64 static unsigned int PclDownloadChar(FILE *,PclCharDataPtr,unsigned short,unsigned char);
65 static unsigned int PclDownloadHeader(FILE *, PclFontDescPtr, unsigned short);
67 #ifdef PCL_FONT_COMPRESS
68 static unsigned char *compress_bitmap_data(PclCharDataPtr, unsigned int *);
69 #endif /* PCL_FONT_COMPRESS */
71 /* -*- PclDownloadSoftFont8 -*-
72 * Send the Character Definition Command for 8-bit font
73 * **************************************************************************/
74 void
75 PclDownloadSoftFont8(
76 FILE *fp,
77 PclSoftFontInfoPtr pSoftFontInfo,
78 PclFontHead8Ptr pfh,
79 PclCharDataPtr pcd,
80 unsigned char *code
84 * Check whether the font header has already been downloaded.
85 * If not, download it.
88 if ( !pfh->fid ) {
89 pfh->fid = pSoftFontInfo->cur_max_fid++;
90 PclDownloadHeader(fp, &(pfh->fd), pfh->fid);
92 pfh->index[*code] = *code;
93 PclDownloadChar(fp, pcd, pfh->fid, pfh->index[*code]);
97 /* -*- PclDownloadSoftFont16 -*-
98 * Send the Character Definition Command for 16 bit font
99 * **************************************************************************/
100 void
101 PclDownloadSoftFont16(
102 FILE *fp,
103 PclSoftFontInfoPtr pSoftFontInfo,
104 PclFontHead16Ptr pfh,
105 PclCharDataPtr pcd,
106 unsigned char row,
107 unsigned char col
111 * Check whether the font header is already downloaded.
112 * If not, download it.
115 if ( !pfh->cur_cindex ) {
116 pfh->cur_fid = pSoftFontInfo->cur_max_fid++;
117 PclDownloadHeader(fp, &(pfh->fd), pfh->cur_fid);
119 pfh->index[row][col].fid = pfh->cur_fid;
120 pfh->index[row][col].cindex = pfh->cur_cindex++;
122 PclDownloadChar(fp, pcd, pfh->index[row][col].fid, pfh->index[row][col].cindex);
125 /* -*- PclCreateSoftFontInfo -*-
126 * Create and Initialize the structure for storing the information
127 * of the downloaded soft font.
128 * **************************************************************************/
129 PclSoftFontInfoPtr
130 PclCreateSoftFontInfo(void)
132 PclSoftFontInfoPtr pSoftFontInfo;
134 pSoftFontInfo = (PclSoftFontInfoPtr)xalloc(sizeof(PclSoftFontInfoRec));
135 if ( pSoftFontInfo == (PclSoftFontInfoPtr) NULL)
136 return (PclSoftFontInfoPtr) NULL;
137 pSoftFontInfo->phead8 = (PclFontHead8Ptr)NULL;
138 pSoftFontInfo->phead16 = (PclFontHead16Ptr)NULL;
139 pSoftFontInfo->pinfont = (PclInternalFontPtr)NULL;
140 pSoftFontInfo->cur_max_fid = 1;
141 return pSoftFontInfo;
144 /* -*- PclDestroySoftFontInfo -*-
145 * Destroy the soft font information structure
146 * **************************************************************************/
147 void
148 PclDestroySoftFontInfo( PclSoftFontInfoPtr pSoftFontInfo )
150 PclFontHead8Ptr pfh8, pfh8_next;
151 PclFontHead16Ptr pfh16, pfh16_next;
152 PclInternalFontPtr pin, pin_next;
153 unsigned char nindex_row;
154 int i;
156 if ( pSoftFontInfo == (PclSoftFontInfoPtr) NULL )
157 return;
159 pfh8 = pSoftFontInfo->phead8;
160 while (pfh8 != (PclFontHead8Ptr) NULL) {
161 xfree(pfh8->fontname);
162 xfree(pfh8->index);
163 pfh8_next = pfh8->next;
164 xfree(pfh8);
165 pfh8 = pfh8_next;
168 pfh16 = pSoftFontInfo->phead16;
169 while (pfh16 != (PclFontHead16Ptr) NULL) {
170 xfree(pfh16->fontname);
171 nindex_row = pfh16->lastRow - pfh16->firstRow + 1;
172 for (i=0; i<nindex_row; i++)
173 xfree(pfh16->index[i]);
174 xfree(pfh16->index);
175 pfh16_next = pfh16->next;
176 xfree(pfh16);
177 pfh16 = pfh16_next;
180 pin = pSoftFontInfo->pinfont;
181 while (pin != (PclInternalFontPtr) NULL) {
182 xfree(pin->fontname);
183 pin_next = pin->next;
184 xfree(pin);
185 pin = pin_next;
188 xfree(pSoftFontInfo);
191 /* -*- PclDownloadHeader -*-
192 * Send the Font Header Commnad.
193 * Format 0 : Font Header for Pcl Bitmapped Fonts
194 * Format 20 : Font Header for Resolution Specified Bitmapped Fonts
195 * **************************************************************************/
196 static unsigned int
197 PclDownloadHeader(
198 FILE *fp,
199 PclFontDescPtr fd,
200 unsigned short fid
203 int nbytes;
205 #ifdef XP_PCL_LJ3
206 nbytes = 64;
207 #else
208 nbytes = 68;
209 #endif /* XP_PCL_LJ3 */
211 * Font ID Command : Esc *c#D
212 * (Default = 0, Range = 0 - 32767)
214 fprintf(fp, "%c*c%dD", ESC, fid);
217 * Font Header Commnad : Esc )s#W[font header data]
218 * (Default = 0, Range = 0 - 32767)
220 fprintf(fp, "%c)s%dW", ESC, nbytes);
222 Put2bytes(fp, nbytes); /* Font Description Size */
223 #ifdef XP_PCL_LJ3
224 Put1byte(fp, 0); /* Header Format */
225 #else
226 Put1byte(fp, 20); /* Header Format */
227 #endif /* XP_PCL_LJ3 */
228 Put1byte(fp, 2); /* Font Type */
229 Put2bytes(fp, 0); /* Style MSB */
230 Put2bytes(fp, fd->ascent); /* BaseLine Position */
231 Put2bytes(fp, fd->cellwidth); /* Cell Width */
232 Put2bytes(fp, fd->cellheight); /* Cell Height */
233 Put1byte(fp, 0); /* Orienation */
234 Put1byte(fp, fd->spacing); /* Spacing */
235 Put2bytes(fp, SYMBOL_SET); /* Symbol Set */
236 Put2bytes(fp, fd->pitch*4); /* font pitch */
237 Put2bytes(fp, fd->cellheight * 4); /* Height */
238 Put2bytes(fp, 0); /* x-Height */
239 Put1byte(fp, 0); /* width type (normal) */
240 Put1byte(fp, 0); /* Style LSB */
241 Put1byte(fp, 0); /* Stroke Weight */
242 Put1byte(fp, 5); /* Typeface LSB */
243 Put1byte(fp, 0); /* Typeface MSB */
244 Put1byte(fp, 0); /* Serif Style */
245 Put1byte(fp, 0); /* Quality */
246 Put1byte(fp, 0); /* Placement */
247 Put1byte(fp, 0); /* Underline Position */
248 Put1byte(fp, 0); /* Underline Thickness */
249 Put2bytes(fp, fd->cellheight*1.2); /* Text Height */
250 Put2bytes(fp, fd->cellwidth * 4); /* Text Width */
251 Put2bytes(fp, 0); /* First Code */
252 Put2bytes(fp, 255); /* Last Code */
253 Put1byte(fp, 0); /* Pitch Extend */
254 Put1byte(fp, 0); /* Height Extend */
255 Put2bytes(fp, 0); /* Cap Height */
256 Put2bytes(fp, 0); /* Font Number 1 */
257 Put2bytes(fp, 0); /* Font Number 2 */
258 Put2bytes(fp, 0); /* Font Name */
259 Put2bytes(fp, 0); /* Font Name */
260 Put2bytes(fp, 0); /* Font Name */
261 Put2bytes(fp, 0); /* Font Name */
262 Put2bytes(fp, 0); /* Font Name */
263 Put2bytes(fp, 0); /* Font Name */
264 Put2bytes(fp, 0); /* Font Name */
265 Put2bytes(fp, 0); /* Font Name */
267 #ifdef XP_PCL_LJ3
268 return 64;
269 #else
270 Put2bytes(fp, 300); /* X Resolution */
271 Put2bytes(fp, 300); /* Y Resolution */
272 return 68;
273 #endif /* XP_PCL_LJ3 */
277 /* -*- PclDownloadCharacter -*-
278 * Send the Character Definition Command.
279 * **************************************************************************/
280 static unsigned int
281 PclDownloadChar(
282 FILE *fp,
283 PclCharDataPtr cd,
284 unsigned short fid,
285 unsigned char code
288 unsigned int nbytes, n;
289 unsigned char *raster;
292 * Font ID Command : Esc *c#D
293 * (Default = 0, Range = 0 - 32767)
294 * Character Code Command : Esc *c#E
295 * (Default = 0, Range = 0 - 65535)
297 fprintf(fp, "%c*c%dd%dE", ESC, fid, code);
300 * Character Definition Command : Esc (s#W[character descriptor and data]
301 * (Default = N/A, Range = 0 - 32767)
304 nbytes = n = cd->height * ((cd->width + 7) / 8);
305 #ifdef PCL_FONT_COMPRESS
306 raster = compress_bitmap_data(cd, &nbytes);
307 #else
308 raster = (unsigned char *)NULL;
309 #endif /* PCL_FONT_COMPRESS */
310 fprintf(fp, "%c(s%dW", ESC, nbytes + 16);
312 Put1byte(fp, 4); /* Format */
313 Put1byte(fp, 0); /* Continuation */
314 Put1byte(fp, 14); /* Descriptor Size */
315 if (raster) { /* Class */
316 Put1byte(fp, 2);
317 } else {
318 Put1byte(fp, 1); /* Class */
320 Put2bytes(fp, 0); /* Orientation */
321 Put2bytes(fp, cd->h_offset); /* left offset */
322 Put2bytes(fp, cd->v_offset); /* top offset */
323 Put2bytes(fp, cd->width); /* character width */
324 Put2bytes(fp, cd->height); /* character height */
325 Put2bytes(fp, cd->font_pitch*4); /* delta X */
328 * Raster Character Data
330 if (raster) {
331 fwrite(raster, nbytes, 1, fp);
332 xfree(raster);
333 } else
334 fwrite(cd->raster_top, nbytes, 1, fp);
336 return n + 16;
340 #ifdef PCL_FONT_COMPRESS
341 /* -*- compress_bitmap_data -*-
342 * Compress Bitmap data
343 * **************************************************************************/
344 static unsigned char *
345 compress_bitmap_data(
346 PclCharDataPtr cd,
347 unsigned int *nbytes
350 unsigned int byte_width;
351 unsigned char *raster, *rptr_s, *rptr_e, *rptr_end;
352 unsigned char *tmp_s, *tmp_ptr;
353 unsigned char *p;
354 unsigned char cur, pixel;
355 unsigned int num;
357 int i, j, k, w;
359 byte_width = (cd->width + 7) / 8;
360 *nbytes = cd->height * byte_width;
362 /* Create buffer for storing compress bitmap glyph */
363 raster = (unsigned char *)xalloc(*nbytes);
364 rptr_s = raster;
365 rptr_e = raster;
366 rptr_end = raster + *nbytes;
368 tmp_s = (unsigned char *)xalloc(cd->width * 8 + 2);
370 p = cd->raster_top;
371 for (i=0; i<cd->height; i++) {
372 tmp_ptr = tmp_s;
373 *tmp_ptr++ = 0;
374 if ( (*p>>7)&0x1 == 1 ) {
375 *tmp_ptr++ = 0;
376 cur = 1;
377 } else {
378 cur = 0;
380 num = 0;
381 for (j=0, w=0; j<byte_width; j++, p++) {
382 for (k=0; k<8 && w<cd->width; k++, w++) {
383 pixel = (*p>>(7-k))&0x1;
384 if ( pixel == cur ) {
385 num++;
386 } else {
387 cur = pixel;
388 while (num > 255) {
389 *tmp_ptr++ = 255;
390 *tmp_ptr++ = 0;
391 num -= 255;
393 *tmp_ptr++ = num;
394 num = 1;
398 if ( pixel == cur ) {
399 while (num > 255) {
400 *tmp_ptr++ = 255;
401 *tmp_ptr++ = 0;
402 num -= 255;
404 *tmp_ptr++ = num&0xff;
405 } else
406 *tmp_ptr++ = num;
408 if ( ((rptr_e - rptr_s) == (tmp_ptr - tmp_s)) &&
409 !memcmp(rptr_s+1, tmp_s+1, (tmp_ptr - tmp_s) - 1) )
410 *rptr_s += 1;
411 else {
412 if ( rptr_e + (tmp_ptr - tmp_s) > rptr_end ) {
413 xfree(raster);
414 xfree(tmp_s);
415 return (unsigned char *)NULL;
417 memcpy (rptr_e, tmp_s, tmp_ptr - tmp_s);
418 rptr_s = rptr_e;
419 rptr_e = rptr_s + (tmp_ptr - tmp_s);
422 xfree(tmp_s);
423 *nbytes = rptr_e - raster;
425 return raster;
427 #endif /* PCL_FONT_COMPRESS */