1
/* $Xorg: PclSFonts.c,v 1.3 2000/08/17 19:48:08 cpqbld Exp $ */
2
/*******************************************************************
4
** *********************************************************
9
** * Send Soft Font Download data to the specified
14
** *********************************************************
16
********************************************************************/
18
(c) Copyright 1996 Hewlett-Packard Company
19
(c) Copyright 1996 International Business Machines Corp.
20
(c) Copyright 1996 Sun Microsystems, Inc.
21
(c) Copyright 1996 Novell, Inc.
22
(c) Copyright 1996 Digital Equipment Corp.
23
(c) Copyright 1996 Fujitsu Limited
24
(c) Copyright 1996 Hitachi, Ltd.
26
Permission is hereby granted, free of charge, to any person obtaining a copy
27
of this software and associated documentation files (the "Software"), to deal
28
in the Software without restriction, including without limitation the rights
29
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30
copies of the Software, and to permit persons to whom the Software is
31
furnished to do so, subject to the following conditions:
33
The above copyright notice and this permission notice shall be included in
34
all copies or substantial portions of the Software.
36
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
40
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43
Except as contained in this notice, the names of the copyright holders shall
44
not be used in advertising or otherwise to promote the sale, use or other
45
dealings in this Software without prior written authorization from said
48
/* $XFree86: xc/programs/Xserver/Xprint/pcl/PclSFonts.c,v 1.6 2001/10/28 03:32:55 tsi Exp $ */
56
#define Put1byte(fp, x) tmp1=x; fwrite((char *)&tmp1, 1, 1, fp)
57
#define Put2bytes(fp, x) tmp2=x; fwrite((char *)&tmp2, 2, 1, fp)
60
#define SYMBOL_SET 277
61
#define MAX_CINDEX 255
63
static unsigned int PclDownloadChar(FILE *,PclCharDataPtr,unsigned short,unsigned char);
64
static unsigned int PclDownloadHeader(FILE *, PclFontDescPtr, unsigned short);
66
#ifdef PCL_FONT_COMPRESS
67
static unsigned char *compress_bitmap_data(PclCharDataPtr, unsigned int *);
68
#endif /* PCL_FONT_COMPRESS */
70
/* -*- PclDownloadSoftFont8 -*-
71
* Send the Character Definition Command for 8-bit font
72
* **************************************************************************/
76
PclSoftFontInfoPtr pSoftFontInfo,
83
* Check whether the font header has already been downloaded.
84
* If not, download it.
88
pfh->fid = pSoftFontInfo->cur_max_fid++;
89
PclDownloadHeader(fp, &(pfh->fd), pfh->fid);
91
pfh->index[*code] = *code;
92
PclDownloadChar(fp, pcd, pfh->fid, pfh->index[*code]);
96
/* -*- PclDownloadSoftFont16 -*-
97
* Send the Character Definition Command for 16 bit font
98
* **************************************************************************/
100
PclDownloadSoftFont16(
102
PclSoftFontInfoPtr pSoftFontInfo,
103
PclFontHead16Ptr pfh,
110
* Check whether the font header is already downloaded.
111
* If not, download it.
114
if ( !pfh->cur_cindex ) {
115
pfh->cur_fid = pSoftFontInfo->cur_max_fid++;
116
PclDownloadHeader(fp, &(pfh->fd), pfh->cur_fid);
118
pfh->index[row][col].fid = pfh->cur_fid;
119
pfh->index[row][col].cindex = pfh->cur_cindex++;
120
if ( pfh->cur_cindex > MAX_CINDEX )
123
PclDownloadChar(fp, pcd, pfh->index[row][col].fid, pfh->index[row][col].cindex);
126
/* -*- PclCreateSoftFontInfo -*-
127
* Create and Initialize the structure for storing the information
128
* of the downloaded soft font.
129
* **************************************************************************/
131
PclCreateSoftFontInfo(void)
133
PclSoftFontInfoPtr pSoftFontInfo;
135
pSoftFontInfo = (PclSoftFontInfoPtr)xalloc(sizeof(PclSoftFontInfoRec));
136
if ( pSoftFontInfo == (PclSoftFontInfoPtr) NULL)
137
return (PclSoftFontInfoPtr) NULL;
138
pSoftFontInfo->phead8 = (PclFontHead8Ptr)NULL;
139
pSoftFontInfo->phead16 = (PclFontHead16Ptr)NULL;
140
pSoftFontInfo->pinfont = (PclInternalFontPtr)NULL;
141
pSoftFontInfo->cur_max_fid = 1;
142
return pSoftFontInfo;
145
/* -*- PclDestroySoftFontInfo -*-
146
* Destroy the soft font information structure
147
* **************************************************************************/
149
PclDestroySoftFontInfo( PclSoftFontInfoPtr pSoftFontInfo )
151
PclFontHead8Ptr pfh8, pfh8_next;
152
PclFontHead16Ptr pfh16, pfh16_next;
153
PclInternalFontPtr pin, pin_next;
154
unsigned char nindex_row, nindex_col;
157
if ( pSoftFontInfo == (PclSoftFontInfoPtr) NULL )
160
pfh8 = pSoftFontInfo->phead8;
161
while (pfh8 != (PclFontHead8Ptr) NULL) {
162
xfree(pfh8->fontname);
164
pfh8_next = pfh8->next;
169
pfh16 = pSoftFontInfo->phead16;
170
while (pfh16 != (PclFontHead16Ptr) NULL) {
171
xfree(pfh16->fontname);
172
nindex_col = pfh16->lastCol - pfh16->firstCol + 1;
173
nindex_row = pfh16->lastRow - pfh16->firstRow + 1;
174
for (i=0; i<nindex_row; i++)
175
xfree(pfh16->index[i]);
177
pfh16_next = pfh16->next;
182
pin = pSoftFontInfo->pinfont;
183
while (pin != (PclInternalFontPtr) NULL) {
184
xfree(pin->fontname);
185
pin_next = pin->next;
190
xfree(pSoftFontInfo);
193
/* -*- PclDownloadHeader -*-
194
* Send the Font Header Commnad.
195
* Format 0 : Font Header for Pcl Bitmapped Fonts
196
* Format 20 : Font Header for Resolution Specified Bitmapped Fonts
197
* **************************************************************************/
211
#endif /* XP_PCL_LJ3 */
213
* Font ID Command : Esc *c#D
214
* (Default = 0, Range = 0 - 32767)
216
fprintf(fp, "%c*c%dD", ESC, fid);
219
* Font Header Commnad : Esc )s#W[font header data]
220
* (Default = 0, Range = 0 - 32767)
222
fprintf(fp, "%c)s%dW", ESC, nbytes);
224
Put2bytes(fp, nbytes); /* Font Description Size */
226
Put1byte(fp, 0); /* Header Format */
228
Put1byte(fp, 20); /* Header Format */
229
#endif /* XP_PCL_LJ3 */
230
Put1byte(fp, 2); /* Font Type */
231
Put2bytes(fp, 0); /* Style MSB */
232
Put2bytes(fp, fd->ascent); /* BaseLine Position */
233
Put2bytes(fp, fd->cellwidth); /* Cell Width */
234
Put2bytes(fp, fd->cellheight); /* Cell Height */
235
Put1byte(fp, 0); /* Orienation */
236
Put1byte(fp, fd->spacing); /* Spacing */
237
Put2bytes(fp, SYMBOL_SET); /* Symbol Set */
238
Put2bytes(fp, fd->pitch*4); /* font pitch */
239
Put2bytes(fp, fd->cellheight * 4); /* Height */
240
Put2bytes(fp, 0); /* x-Height */
241
Put1byte(fp, 0); /* width type (normal) */
242
Put1byte(fp, 0); /* Style LSB */
243
Put1byte(fp, 0); /* Stroke Weight */
244
Put1byte(fp, 5); /* Typeface LSB */
245
Put1byte(fp, 0); /* Typeface MSB */
246
Put1byte(fp, 0); /* Serif Style */
247
Put1byte(fp, 0); /* Quality */
248
Put1byte(fp, 0); /* Placement */
249
Put1byte(fp, 0); /* Underline Position */
250
Put1byte(fp, 0); /* Underline Thickness */
251
Put2bytes(fp, fd->cellheight*1.2); /* Text Height */
252
Put2bytes(fp, fd->cellwidth * 4); /* Text Width */
253
Put2bytes(fp, 0); /* First Code */
254
Put2bytes(fp, 255); /* Last Code */
255
Put1byte(fp, 0); /* Pitch Extend */
256
Put1byte(fp, 0); /* Height Extend */
257
Put2bytes(fp, 0); /* Cap Height */
258
Put2bytes(fp, 0); /* Font Number 1 */
259
Put2bytes(fp, 0); /* Font Number 2 */
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 */
266
Put2bytes(fp, 0); /* Font Name */
267
Put2bytes(fp, 0); /* Font Name */
272
Put2bytes(fp, 300); /* X Resolution */
273
Put2bytes(fp, 300); /* Y Resolution */
275
#endif /* XP_PCL_LJ3 */
279
/* -*- PclDownloadCharacter -*-
280
* Send the Character Definition Command.
281
* **************************************************************************/
290
unsigned int nbytes, n;
291
unsigned char *raster;
294
* Font ID Command : Esc *c#D
295
* (Default = 0, Range = 0 - 32767)
296
* Character Code Command : Esc *c#E
297
* (Default = 0, Range = 0 - 65535)
299
fprintf(fp, "%c*c%dd%dE", ESC, fid, code);
302
* Character Definition Command : Esc (s#W[character descriptor and data]
303
* (Default = N/A, Range = 0 - 32767)
306
nbytes = n = cd->height * ((cd->width + 7) / 8);
307
#ifdef PCL_FONT_COMPRESS
308
raster = compress_bitmap_data(cd, &nbytes);
310
raster = (unsigned char *)NULL;
311
#endif /* PCL_FONT_COMPRESS */
312
fprintf(fp, "%c(s%dW", ESC, nbytes + 16);
314
Put1byte(fp, 4); /* Format */
315
Put1byte(fp, 0); /* Continuation */
316
Put1byte(fp, 14); /* Descriptor Size */
317
if (raster) { /* Class */
320
Put1byte(fp, 1); /* Class */
322
Put2bytes(fp, 0); /* Orientation */
323
Put2bytes(fp, cd->h_offset); /* left offset */
324
Put2bytes(fp, cd->v_offset); /* top offset */
325
Put2bytes(fp, cd->width); /* character width */
326
Put2bytes(fp, cd->height); /* character height */
327
Put2bytes(fp, cd->font_pitch*4); /* delta X */
330
* Raster Character Data
333
fwrite(raster, nbytes, 1, fp);
336
fwrite(cd->raster_top, nbytes, 1, fp);
342
#ifdef PCL_FONT_COMPRESS
343
/* -*- compress_bitmap_data -*-
344
* Compress Bitmap data
345
* **************************************************************************/
346
static unsigned char *
347
compress_bitmap_data(
352
unsigned int byte_width;
353
unsigned char *raster, *rptr_s, *rptr_e, *rptr_end;
354
unsigned char *tmp_s, *tmp_ptr;
356
unsigned char cur, pixel;
361
byte_width = (cd->width + 7) / 8;
362
*nbytes = cd->height * byte_width;
364
/* Create buffer for storing compress bitmap glyph */
365
raster = (unsigned char *)xalloc(*nbytes);
368
rptr_end = raster + *nbytes;
370
tmp_s = (unsigned char *)xalloc(cd->width * 8 + 2);
373
for (i=0; i<cd->height; i++) {
376
if ( (*p>>7)&0x1 == 1 ) {
383
for (j=0, w=0; j<byte_width; j++, p++) {
384
for (k=0; k<8 && w<cd->width; k++, w++) {
385
pixel = (*p>>(7-k))&0x1;
386
if ( pixel == cur ) {
400
if ( pixel == cur ) {
406
*tmp_ptr++ = num&0xff;
410
if ( ((rptr_e - rptr_s) == (tmp_ptr - tmp_s)) &&
411
!memcmp(rptr_s+1, tmp_s+1, (tmp_ptr - tmp_s) - 1) )
414
if ( rptr_e + (tmp_ptr - tmp_s) > rptr_end ) {
417
return (unsigned char *)NULL;
419
memcpy (rptr_e, tmp_s, tmp_ptr - tmp_s);
421
rptr_e = rptr_s + (tmp_ptr - tmp_s);
425
*nbytes = rptr_e - raster;
429
#endif /* PCL_FONT_COMPRESS */