util/dttypes: remove register keyword
[oweals/cde.git] / cde / programs / dtudcfonted / dtgpftobdf / gpftobdf.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: gpftobdf.c /main/5 1996/11/08 02:04:24 cde-fuj $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  */
30
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include "font.h"
40 #include "misc.h"
41 #include <X11/X.h>
42 #include <X11/Xproto.h>
43 #include <X11/Xmd.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include "fontstruct.h"
47 #include "snfstruct.h"
48 #include <time.h>
49 #include "bdftosnf.h"
50 #include <memory.h>
51 #if defined( SVR4 )
52 #include <sys/mman.h>
53 #endif
54
55 #include "udccom.h"
56 #include "udcutil.h"
57
58 #define GLYPHPADOPTIONS                 4
59 #define SIZEOF_COMPRESSED_METRIC        5
60
61 #include        "gtobdf.h"
62 #include        "pcf.h"
63
64 typedef struct  pcf_tmp {
65         FontInfoRec     info;
66         int     ntables;
67         PCFTablePtr     tables;
68         int     nprops;
69         FontPropPtr     props;
70         char    *isStringProp;
71         int     str_length;
72         char    *string;
73         int     nbitmaps;
74         CARD32  bmp_fmt;        /* bitOrder ,byteOrder, glyph, scan */
75         CARD32  *offsets;
76         int     sizebitmaps;
77         char    *bitmaps;
78         CARD32  enc_fmt;
79         CARD16  *encodingOffsets;
80         size_t  pcf_bufsz;
81         caddr_t         pcf_buffer;
82
83         /* scale width  */
84         CARD32  swd_fmt ;
85         int     swd_num ;
86         CARD32  *swidth ;
87
88         /* glyph name   */
89         CARD32  glyph_fmt ;
90         unsigned int    glyphs ;
91         int     *glyph_offset ;
92         char    *glyph_name ;
93
94         /* metrics      */
95         int     compress ;
96         CARD32  mtr_fmt ;
97         int     mtr_num ;
98         xCharInfo       *metrics;
99
100 } PcfTmp;
101
102 typedef struct prop_tmp {
103         char    *font_name;
104         FontPropRec     psize;
105         FontPropRec     resolution;
106 } PropTmp;
107
108
109
110 static  char    *readfontfile();
111 static  void    Anafprop();
112 static  void    pSIZE();
113 static  void    pPROPS();
114 static  void    pCHARS();
115 static  void    pfixCHARS();
116 static  int     getarg();
117
118 static  Bool    seekToType() ;
119 static  void    getMetric() ;
120 static  Bool    getAccel() ;
121 static  Bool    getProperties() ;
122 static  void    putPtn() ;
123 static  void    ByteSwap() ;
124 static  void    invertBits() ;
125
126 static  int     getINT16() ;
127 static  int     getINT32() ;
128 static  CARD32  getLSB32();
129 static  char    *make_toptn();
130 static  void    pSIZE_pcf();
131 static  PcfTmp  *openPcfFont();
132 static  void    setProp();
133 static  void    pPROPS_pcf();
134 static  void    pCHARS_pcf();
135 static  void    BitOrderInvert();
136 static  char    *bufp,buf[2048];
137
138 extern int      ChkPcfFontFile();
139
140
141 main( argc,argv )
142 int     argc;
143 char    *argv[];
144 {
145         int         i,k;
146
147
148         char        *fp;                /* font                         */
149         FontInfoRec *fip;               /* font information             */
150         CharInfoRec *cip, *wkcip;       /* character information        */
151         unsigned int glyphstotal;       /* glyph                        */
152         unsigned int charInfoNum ;      /* character information's number */
153         unsigned int charNum;           /* define character number         */
154         char        *glyphPtr;          /* glyph                        */
155         FontPropRec *fpropPtr,*fplistPtr;       /* font property list   */
156         FontPropRec psprop;             /* point size property          */
157         FontPropRec resprop;            /* resolution property          */
158         char        *fpropvnPtr;
159
160         unsigned int fplistNum;         /* font property number         */
161         char        *fontname;          /* fontname                     */
162         time_t      clock;
163         struct      tm *tm;
164         char        tmbuf[80] ;
165
166         char fname[1024];               /* snf filename                 */
167         int  bitorder;                  /* bitorder                     */
168         int byteorder;                  /* byteorder                    */
169         int scanunit;                   /* scanunit                     */
170         int glyphPad ;                  /* glyph                        */
171         int outLevel;                   /* output information level     */
172         int fix;
173
174
175         int     isPcf;
176
177         PcfTmp  *pcf;
178         PropTmp         proptmp;
179         char    *tmpfname;
180         extern  char    *GetRealFileName();
181
182
183         if (getarg(argc,argv,fname,&bitorder,&byteorder,&scanunit,&glyphPad,
184             &outLevel,&fix) != 0) {
185                 exit( -1 );
186         }
187
188         /*
189          *  read to fontfile 
190          */
191         if ( ( tmpfname = GetRealFileName( fname ) ) == NULL ) {
192                 USAGE("dtgpftobdf: cannot refer to substance file.\n" );
193                 exit( -1 );
194         }
195         if ( strcmp( tmpfname, fname ) != 0 ){
196                 strcpy( fname, tmpfname );
197         }
198
199         if ( ChkPcfFontFile( fname ) == 0 ){
200                 /* pcf font */
201                 isPcf = 1;
202                 if ( !( pcf = openPcfFont( fname ) ) ) {
203                         USAGE("dtgpftobdf: cannot open font\n" );
204                         exit( -1 );
205                 }
206         } else {
207                 /* snf font */
208                 isPcf = 0;
209                 if ( ( fp = readfontfile( fname ) ) == (char *)-1 ) {
210                         exit( -1 );
211                 }
212         }
213
214
215         if ( !isPcf ) {
216                 /* SNF format */
217                 COMM_SNF_GETHEADER( fip, fp ) ;
218                 /* font file check */
219                 if ((fip->version1 != fip->version2) || 
220                     (fip->version1 != FONT_FILE_VERSION)) {
221                         USAGE("dtgpftobdf: illegal font version\n");
222                         exit( -1 );
223                 }
224
225                 cip = (CharInfoRec *)((char *)fip + sizeof(FontInfoRec));
226
227                 charInfoNum = (fip->lastCol - fip->firstCol + 1)
228                     *(fip->lastRow - fip->firstRow + 1);
229                 glyphPtr = ((char *)cip) + ( charInfoNum * sizeof(CharInfoRec) );
230
231                 charNum = charInfoNum ;
232                 wkcip = cip ;
233
234                 glyphstotal = fip->maxbounds.byteOffset;
235
236                 glyphPad = (((fip->maxbounds.metrics.rightSideBearing
237                     - fip->maxbounds.metrics.leftSideBearing)+31)/32)*4;
238
239                 if ( fix == TRUE ) {
240                     for ( i = 0; i < charInfoNum; i++ ) {
241                         if ( wkcip->exists == FALSE ) {
242                             charNum-- ;
243                         }
244                         wkcip++;
245                     }
246                 } else {
247                     glyphstotal = 0 ;
248                     for ( i = 0; i < charInfoNum; i++ ) {
249                         if (wkcip->exists == FALSE) {
250                             charNum-- ;
251                             wkcip++;
252                             continue;
253                         }
254                         glyphstotal += (wkcip->metrics.ascent
255                             + wkcip->metrics.descent) * glyphPad;
256                         wkcip++;
257                     }
258                 }
259
260                 fpropPtr = (FontPropRec *)(glyphPtr + glyphstotal);
261                 fpropvnPtr = ((char *)fpropPtr) + (fip->nProps) * sizeof(FontPropRec);
262
263         }
264
265
266         if ( isPcf ) {
267                 setProp( pcf, &proptmp );
268         } else {
269                 fontname = (char *)0;
270                 Anafprop( 
271                     fip, fpropPtr, fpropvnPtr, 
272                     &fontname, &psprop, &resprop, 
273                     &fplistPtr, &fplistNum 
274                     );
275         }
276
277         /*
278          *¡¡output to BDF information
279          */
280         bufp = buf;
281         memcpy(bufp,"STARTFONT 2.1\n",14);
282         bufp += 14;
283         memcpy(bufp,"COMMENT\n",8);
284         bufp += 8;
285         memcpy(bufp,"COMMENT  This BDF was created by dtgpftobdf.\n",45 );
286         bufp += 45 ;
287         memcpy(bufp,"COMMENT\n",8);
288         bufp += 8;
289         time(&clock);
290         tm = localtime(&clock);
291
292         snprintf( tmbuf, sizeof(tmbuf), "%s", asctime(tm) ) ;
293
294         k = sprintf(bufp,"COMMENT    Created date : %s\n",tmbuf );
295         bufp += k-1;
296         memcpy(bufp,"COMMENT\n",8);
297         bufp += 8;
298
299         if ( isPcf ) {
300                 k = sprintf(bufp,"FONT %s\n",proptmp.font_name);
301                 bufp += k;
302                 *bufp = '\0';
303                 fprintf( stdout,"%s",buf );
304                 pSIZE_pcf( &proptmp );
305                 fprintf( stdout,"FONTBOUNDINGBOX %d %d %d %d\n",
306                     pcf->info.maxbounds.metrics.rightSideBearing 
307                     - pcf->info.maxbounds.metrics.leftSideBearing,
308                     pcf->info.maxbounds.metrics.ascent 
309                     + pcf->info.maxbounds.metrics.descent,
310                     pcf->info.maxbounds.metrics.leftSideBearing,
311                     - ( pcf->info.maxbounds.metrics.descent )
312                         );
313                 pPROPS_pcf( pcf );
314                 if ( outLevel == FALSE ) {
315                         pCHARS_pcf( pcf, fix );
316                 }
317         } else {
318                 k = sprintf( bufp,"FONT %s\n",fontname );
319                 bufp += k;
320                 *bufp = '\0';
321                 fprintf( stdout,"%s",buf );
322                 pSIZE( &psprop, &resprop );
323                 fprintf( stdout,"FONTBOUNDINGBOX %d %d %d %d\n",
324                     cip->metrics.rightSideBearing 
325                     - cip->metrics.leftSideBearing,
326                     cip->metrics.ascent + cip->metrics.descent,
327                     cip->metrics.leftSideBearing,
328                     -(cip->metrics.descent)
329                         );
330                 pPROPS( fip,fplistPtr,fplistNum );
331                 if ( outLevel == FALSE ) {
332                     if ( fix == TRUE ) {
333                         pfixCHARS( fip,cip,glyphPtr,charNum, glyphPad );
334                     } else {
335                         pCHARS(fip,cip,glyphPtr,charNum,glyphPad);
336                     }
337                 }
338
339         }
340
341         fprintf( stdout,"ENDFONT\n" );
342         exit( 0 );
343 }
344
345 /*
346  * read to font file
347  *
348  */
349 static  char    *readfontfile( fname )
350 char    *fname;
351 {
352         int     fd;
353         char    *fp = NULL;
354         struct stat stat;
355
356         fd = open( fname,O_RDONLY );
357         if ( fd < 0 ) {
358                 USAGE("dtgpftobdf: can't open file.\n" );
359                 return  ( char * )-1;
360         }
361 #if defined( SVR4 )
362         if ( fstat( fd,&stat ) == 0 ) {
363                 fp = (char *)mmap( 0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0 );
364                 if ( fp == (char *)-1 ) {
365                         if ( ( fp = ( char * )malloc( stat.st_size ) ) == NULL ) {
366                                 USAGE("dtgpftobdf: malloc error.\n" );
367                                 close( fd );
368                                 return  ( char * )-1;
369                         }
370 /*                      if ( read( fd, fp, stat.st_size ) < 0 ) {*/
371                         if ( read( fd, fp, stat.st_size ) != stat.st_size ) {
372                                 USAGE("dtgpftobdf: can't read file.\n" );
373                                 close( fd );
374                                 return  ( char * )-1;
375                         }
376                 }
377         } else {
378                 USAGE("dtgpftobdf: can't read file\n" );
379                 close( fd );
380                 return  ( char * )-1;
381         }
382         close( fd );
383 #else   /* pfa */
384         if ( fstat(fd,&stat) == 0 ) {
385                 if ( ( fp = (char *)malloc(stat.st_size) ) == NULL ) {
386                         USAGE("dtgpftobdf: malloc error.\n" );
387                         close( fd );
388                         return  ( char * )-1;
389                 }
390                 if ( read( fd, fp, stat.st_size ) < 0 ) {
391                         USAGE("dtgpftobdf: can't read file.\n" );
392                         close( fd );
393                         free(fp);
394                         return  ( char * )-1;
395                 }
396         } else {
397                 USAGE("dtgpftobdf: can't read file\n" );
398                 close( fd );
399                 return  ( char * )-1;
400         }
401         close( fd );
402 #endif /* pfa */
403         return  fp;
404 }
405
406
407 #define getINT8( p ) ( *p++ ) ;
408
409 static CARD32
410 getLSB32( p )
411 unsigned char *p;
412 {
413         CARD32  c;
414
415         c = *p++;
416         c |= (CARD32)(*p++) << 8;
417         c |= (CARD32)(*p++) << 16;
418         c |= (CARD32)(*p) << 24;
419
420         return  c;
421 }
422
423
424 static int
425 getINT32( p, format )
426 unsigned char   *p;
427 CARD32  format;
428 {
429         CARD32  c;
430
431         if ( PCF_BYTE_ORDER(format) == MSBFirst ) {
432                 c = (CARD32)(*p++) << 24;
433                 c |= (CARD32)(*p++) << 16;
434                 c |= (CARD32)(*p++) << 8;
435                 c |= (CARD32)(*p);
436         } else {
437                 c = (CARD32)(*p++);
438                 c |= (CARD32)(*p++) << 8;
439                 c |= (CARD32)(*p++) << 16;
440                 c |= (CARD32)(*p) << 24;
441         }
442
443         return ( int )c;
444 }
445
446 static int
447 getINT16( p, format )
448 unsigned char   *p;
449 CARD32  format;
450 {
451         CARD32  c;
452
453         if ( PCF_BYTE_ORDER(format) == MSBFirst ) {
454                 c = (CARD32)(*p++) << 8;
455                 c |= (CARD32)(*p);
456         } else {
457                 c = (CARD32)(*p++);
458                 c |= (CARD32)(*p) << 8;
459         }
460
461         return ( int )c;
462 }
463
464 static Bool
465 seekToType( tables, ntables, type, formatp, sizep, offsetp)
466 PCFTablePtr     tables;
467 int     ntables;
468 CARD32  type;
469 CARD32  *formatp;
470 CARD32  *sizep;
471 CARD32  *offsetp;
472 {
473         int     i;
474
475         for ( i = 0; i < ntables; i++ ) {
476                 if ( getLSB32( (unsigned char *)&tables[i].type) == type) {
477                         *formatp = getLSB32( (unsigned char *)&tables[i].format);
478                         *sizep = getLSB32( (unsigned char *)&tables[i].size);
479                         *offsetp = getLSB32( (unsigned char *)&tables[i].offset);
480                         return  TRUE;
481                 }
482         }
483         return  FALSE;
484 }
485
486
487 static void
488 getMetric( buf, format, metric )
489 caddr_t         buf;
490 CARD32  format;
491 xCharInfo       *metric;
492 {
493         metric->leftSideBearing = getINT16( (unsigned char *)buf, format);
494         buf += 2;
495         metric->rightSideBearing = getINT16( (unsigned char *)buf, format);
496         buf += 2;
497         metric->characterWidth = getINT16( (unsigned char *)buf, format);
498         buf += 2;
499         metric->ascent = getINT16( (unsigned char *)buf, format);
500         buf += 2;
501         metric->descent = getINT16( (unsigned char *)buf, format);
502         buf += 2;
503         metric->attributes = getINT16( (unsigned char *)buf, format);
504         buf += 2;
505 }
506
507 static Bool
508 getAccel( pFontInfo, maxink, buf_top, tables, ntables, type )
509 FontInfoPtr     pFontInfo;
510 xCharInfo       *maxink;
511 caddr_t         buf_top;
512 PCFTablePtr     tables;
513 int     ntables;
514 CARD32  type;
515 {
516         CARD32  format;
517         CARD32  size;
518         CARD32  offset;
519         caddr_t         buffer;
520
521         if ( !seekToType( tables, ntables, type, &format, &size, &offset)) {
522                 return  FALSE;
523         }
524
525         buffer = buf_top + offset;
526         format = getLSB32( (unsigned char *)buffer);
527         buffer += 4;
528         if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT )
529             && !PCF_FORMAT_MATCH( format, PCF_ACCEL_W_INKBOUNDS )
530             ) {
531                 return  FALSE;
532         }
533         pFontInfo->noOverlap = *buffer++;
534         pFontInfo->constantMetrics = *buffer++;
535         pFontInfo->terminalFont = *buffer++;
536         pFontInfo->constantWidth = *buffer++;
537         pFontInfo->inkInside = *buffer++;
538         pFontInfo->inkMetrics = *buffer++;
539         pFontInfo->drawDirection = *buffer++;
540         /* natural alignment */
541
542         buffer++;
543
544         pFontInfo->fontAscent = getINT32( (unsigned char *)buffer, format);
545         buffer += 4;
546
547         pFontInfo->fontDescent = getINT32((unsigned char *)buffer, format);
548         buffer +=4;
549
550         buffer += 4;
551
552         getMetric(buffer, format, &pFontInfo->minbounds.metrics);
553         buffer += 12;
554
555         getMetric(buffer, format, &pFontInfo->maxbounds.metrics);
556         buffer += 12;
557
558         if ( PCF_FORMAT_MATCH( format, PCF_ACCEL_W_INKBOUNDS ) ) {
559                 buffer += 12;
560                 getMetric( buffer, format, maxink);
561         } else {
562                 *maxink = pFontInfo->maxbounds.metrics;
563         }
564
565         return  TRUE;
566 }
567
568 static Bool
569 getProperties( pcf, buf_top, tables, ntables)
570 PcfTmp  *pcf;
571 caddr_t         buf_top;
572 PCFTablePtr     tables;
573 int     ntables;
574 {
575         CARD32  format;
576         CARD32  size;
577         CARD32  offset;
578         caddr_t         buffer;
579         int     i;
580
581         if ( !seekToType( tables, ntables, (CARD32)PCF_PROPERTIES, &format, &size, &offset ) ) {
582                 return  FALSE;
583         }
584
585         buffer = buf_top + offset;
586         format = getLSB32( (unsigned char *)buffer );
587         buffer += 4;
588         if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) {
589                 return  FALSE;
590         }
591         pcf->nprops = getINT32((unsigned char *)buffer, format);
592         buffer += 4;
593
594         if ( !( pcf->props = ( FontPropPtr )malloc( pcf->nprops * sizeof( FontPropRec ) ) ) ) {
595                 return  FALSE;
596         }
597
598         if ( !( pcf->isStringProp = ( char * )malloc(pcf->nprops * sizeof( char ) ) ) ) {
599                 return  FALSE;
600         }
601
602         for ( i = 0; i < pcf->nprops; i++ ) {
603                 pcf->props[i].name = getINT32((unsigned char *)buffer, format);
604                 buffer += 4;
605                 pcf->isStringProp[i] = *buffer++;
606                 pcf->props[i].value = getINT32((unsigned char *)buffer, format);
607                 buffer += 4;
608         }
609         if ( pcf->nprops & 3 ) {
610                 i = 4 - (pcf->nprops & 3);
611                 buffer += i;
612         }
613
614         pcf->str_length = getINT32((unsigned char *)buffer, format);
615         buffer += 4;
616
617         pcf->string = (char *)buffer;
618         return  TRUE;
619 }
620
621
622 static  PcfTmp  *
623 openPcfFont( fontname )
624 char    *fontname;
625 {
626         PcfTmp  *pcf_tmp;
627
628         CARD32  format;
629         CARD32  size;
630         CARD32  offset;
631         CARD32  *bitmapSizes;
632         xCharInfo       maxink;
633         caddr_t         buffp;
634         struct stat     st;
635
636         pcf_tmp = ( PcfTmp * )calloc( 1, sizeof( PcfTmp ) );
637         if ( !pcf_tmp ) {
638                 USAGE("dtgpftobdf : calloc() error.\n" ) ;
639                 goto Bail;
640         }
641
642         if ( stat( fontname, &st ) ) {
643                 goto Bail;
644         }
645
646         if ( ( pcf_tmp->pcf_buffer = readfontfile( fontname)) == (char *)-1 ) {
647                 goto Bail;
648         }
649
650         pcf_tmp->pcf_bufsz = st.st_size;
651
652         if ( (format = getLSB32( (unsigned char *)pcf_tmp->pcf_buffer )) != PCF_FILE_VERSION ) {
653                 USAGE1("dtgpftobdf : pcf file version(0x%x) error.\n", format ) ;
654                 goto Bail;
655         }
656
657         pcf_tmp->ntables = getLSB32( (unsigned char *)(pcf_tmp->pcf_buffer + 4) );
658         pcf_tmp->tables = (PCFTablePtr)(pcf_tmp->pcf_buffer + 8 );
659
660         if ( !getAccel( 
661             &pcf_tmp->info, &maxink, pcf_tmp->pcf_buffer,
662             pcf_tmp->tables, pcf_tmp->ntables, (CARD32)PCF_BDF_ACCELERATORS
663             )
664             ) {
665                 if ( !getAccel( 
666                     &pcf_tmp->info, &maxink, pcf_tmp->pcf_buffer,
667                     pcf_tmp->tables, pcf_tmp->ntables, (CARD32)PCF_ACCELERATORS
668                     )
669                     ) {
670                         USAGE("dtgpftobdf : Cannot get accelerators.\n" ) ;
671                         goto Bail;
672                 }
673         }
674
675         if ( !getProperties( 
676             pcf_tmp, pcf_tmp->pcf_buffer, 
677             pcf_tmp->tables, pcf_tmp->ntables
678             )
679             ) {
680                 USAGE("dtgpftobdf : getProperties error.\n" ) ;
681                 goto Bail;
682         }
683         if ( !seekToType( 
684             pcf_tmp->tables, pcf_tmp->ntables, 
685             (CARD32)PCF_BITMAPS, &format, &size, &offset
686             )
687             ) {
688                 USAGE("dtgpftobdf : PCF_BITMAPS error.\n" ) ;
689                 goto Bail;
690         }
691
692         buffp = pcf_tmp->pcf_buffer + offset;
693
694         format = getLSB32( (unsigned char *)buffp);
695         buffp += 4;
696
697         if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) {
698                 USAGE("dtgpftobdf : error.!PCF_FORMAT_MATCH(PCF_BITMAPS)\n" ) ;
699                 goto Bail;
700         }
701
702         pcf_tmp->nbitmaps = getINT32( (unsigned char *)buffp, format);
703         buffp += 4;
704         pcf_tmp->offsets = (CARD32 *)buffp;
705         buffp += sizeof( *pcf_tmp->offsets) * pcf_tmp->nbitmaps;
706
707
708         bitmapSizes = (CARD32 *)buffp;
709         pcf_tmp->sizebitmaps 
710             = getINT32( (unsigned char *)&bitmapSizes[PCF_GLYPH_PAD_INDEX(format)], format);
711         pcf_tmp->bmp_fmt = format;
712         buffp += sizeof( *bitmapSizes) * GLYPHPADOPTIONS;
713         pcf_tmp->bitmaps = buffp;
714         buffp += pcf_tmp->sizebitmaps;
715
716         if ( !seekToType( 
717             pcf_tmp->tables, pcf_tmp->ntables, 
718             (CARD32)PCF_BDF_ENCODINGS, &format, &size, &offset
719             )
720             ) {
721                 USAGE("dtgpftobdf : error.(PCF_BDF_ENCODINGS)\n" ) ;
722                 goto Bail;
723         }
724
725         buffp = pcf_tmp->pcf_buffer + offset;
726         format = getLSB32( (unsigned char *)buffp);
727         buffp += 4;
728         if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) {
729                 USAGE("dtgpftobdf : error.!PCF_FORMAT_MATCH(PCF_BDF_ENCODINGS)\n" ) ;
730                 goto Bail;
731         }
732
733         pcf_tmp->info.firstCol = getINT16( (unsigned char *)buffp, format);
734         buffp += 2;
735         pcf_tmp->info.lastCol = getINT16( (unsigned char *)buffp, format);
736         buffp += 2;
737         pcf_tmp->info.firstRow = getINT16( (unsigned char *)buffp, format);
738         buffp += 2;
739         pcf_tmp->info.lastRow = getINT16( (unsigned char *)buffp, format);
740         buffp += 2;
741         pcf_tmp->info.chDefault = getINT16( (unsigned char *)buffp, format);
742         buffp += 2;
743
744         pcf_tmp->info.allExist  = FALSE;
745         pcf_tmp->enc_fmt = format;
746         pcf_tmp->encodingOffsets = (CARD16 *)buffp;
747
748         /*
749         * get scale width infomations 
750         */
751         if ( !seekToType( 
752             pcf_tmp->tables, pcf_tmp->ntables, 
753             (CARD32)PCF_SWIDTHS, &format, &size, &offset
754             )
755             ) {
756                 goto Bail;
757         }
758
759         buffp = pcf_tmp->pcf_buffer + offset;
760         format = getLSB32( (unsigned char*)buffp);
761         buffp += 4;
762         if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) {
763                 goto Bail;
764         }
765         pcf_tmp->swd_fmt = (CARD32)format ;
766         pcf_tmp->swd_num = getINT32( (unsigned char*)buffp, format ) ;
767         buffp += 4;
768
769         pcf_tmp->swidth = (CARD32 *)buffp ;
770
771         /*
772         * get glyph names
773         */
774         if ( !seekToType( 
775             pcf_tmp->tables, pcf_tmp->ntables, 
776             (CARD32)PCF_GLYPH_NAMES, &format, &size, &offset
777             )
778             ) {
779                 goto Bail;
780         }
781
782         buffp = pcf_tmp->pcf_buffer + offset;
783         format = getLSB32( (unsigned char*)buffp);
784         buffp += 4;
785         if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) {
786                 goto Bail;
787         }
788         pcf_tmp->glyphs = getINT32( (unsigned char*)buffp, format ) ;
789         buffp += 4;
790         pcf_tmp->glyph_offset = (int *)buffp ;
791         buffp += 4 * (pcf_tmp->glyphs + 1) ;
792         pcf_tmp->glyph_name = (char *)buffp ;
793         pcf_tmp->glyph_fmt = (CARD32)format ;
794
795         /*
796         * get metrics
797         */
798         if ( !seekToType( 
799             pcf_tmp->tables, pcf_tmp->ntables, 
800             (CARD32)PCF_METRICS, &format, &size, &offset
801             )
802             ) {
803                 goto Bail;
804         }
805
806         buffp = pcf_tmp->pcf_buffer + offset;
807         format = getLSB32( (unsigned char*)buffp);
808         buffp += 4;
809         if ( PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) {
810             pcf_tmp->compress = 0 ;
811             pcf_tmp->mtr_num = getINT32( (unsigned char*)buffp, format ) ;
812             buffp += 4;
813         }else if ( PCF_FORMAT_MATCH( format, PCF_COMPRESSED_METRICS ) ) {
814             pcf_tmp->compress = 1 ;
815             pcf_tmp->mtr_num = (int)getINT16( (unsigned char*)buffp, format ) ;
816             buffp += 2;
817         }else{
818                 goto Bail;
819         }
820         pcf_tmp->metrics = (xCharInfo *)buffp ;
821         pcf_tmp->mtr_fmt = (CARD32)format ;
822
823         return  pcf_tmp;
824
825 Bail:
826         if ( pcf_tmp ) {
827                 free( pcf_tmp );
828         }
829         return  NULL;
830 }
831
832
833 static  void
834 Anafprop(fip,fpropPtr,fpropvnPtr,fontname,ps,res,fplistPtr,fplistNum)
835 FontInfoRec             *fip;
836 FontPropRec     *fpropPtr;
837 char    *fpropvnPtr;
838 char    **fontname;
839 FontPropRec     **fplistPtr;
840 FontPropRec     *ps;
841 FontPropRec     *res;
842 unsigned int    *fplistNum;
843 {
844         FontPropRec     *wkp ,*wklp;
845         int     i, fpnL;
846         char    *fpnp;
847
848         *fplistNum = fip->nProps - 1;
849         *fplistPtr = (FontPropRec *)malloc(*fplistNum * sizeof(FontPropRec));
850         wkp = fpropPtr;
851         wklp = *fplistPtr;
852         for (i = 0; i < fip->nProps; i++) {
853                 fpnp = fpropvnPtr + wkp->name;
854                 fpnL = strlen(fpnp);
855                 if ((fpnL == 4) && (strncmp(fpnp,"FONT",4) == 0)) {
856                         *fontname = fpropvnPtr + wkp->value;
857                         wkp++;
858                         continue;
859                 }
860                 if (fpnL == 10) {
861                         if (strncmp(fpnp,"POINT_SIZE",10) == 0) {
862                                 ps->value = wkp->value;
863                         }
864                         if (strncmp(fpnp,"RESOLUTION",10) == 0) {
865                                 res->value = wkp->value;
866                         }
867                 }
868                 wklp->name = (CARD32) (intptr_t) fpropvnPtr + wkp->name;
869                 if (wkp->indirect == TRUE) {
870                         wklp->value = (INT32) (intptr_t) fpropvnPtr + wkp->value;
871                 } else {
872                         wklp->value = wkp->value;
873                 }
874                 wklp->indirect = wkp->indirect;
875                 wkp++;
876                 wklp++;
877         }
878 }
879
880
881
882 static void
883 setProp( pcf, proptmp )
884 PcfTmp  *pcf;
885 PropTmp         *proptmp;
886 {
887         int     i, fpnL;
888         char    *fpnp;
889
890         for ( i = 0; i < pcf->nprops; i++ ) {
891                 fpnp = pcf->string + pcf->props[i].name;
892                 fpnL = strlen(fpnp);
893                 if ( ( fpnL == 4 ) && ( strncmp( fpnp,"FONT", 4 ) == 0 ) ) {
894                         proptmp->font_name = pcf->string + pcf->props[i].value;
895                         continue;
896                 }
897                 if ( fpnL == 10 ) {
898                         if ( strncmp( fpnp,"POINT_SIZE", 10 ) == 0 ) {
899                                 proptmp->psize.value = pcf->props[i].value;
900                         } else if ( strncmp( fpnp, "RESOLUTION", 10 ) == 0 ) {
901                                 proptmp->resolution.value = pcf->props[i].value;
902                         }
903                 }
904         }
905 }
906
907
908
909 /*
910  *  output to SIZE (SIZE point resolutionX resolutionY) 
911  * 
912  */
913 static  void
914 pSIZE( ps, res )
915 FontPropRec *ps, *res;
916 {
917         int     k;
918         float   f;
919         char    buf1[16], buf2[16];
920
921         f = ps->value / 10.0;
922         if ( ( ps->value % 10 ) != 0 ) {
923                 sprintf( buf1, "%.1f", f );
924         } else {
925                 sprintf( buf1, "%.0f", f );
926         }
927         k = ( ( res->value * 72.27 ) / 100.0 ) + 1;
928         sprintf( buf2, "%d %d", k, k );
929         fprintf( stdout, "SIZE %s %s\n", buf1, buf2 );
930         return;
931 }
932
933
934 /* output to SIZE of pcf font */
935
936 static void     
937 pSIZE_pcf( proptmp )
938 PropTmp         *proptmp;
939 {
940         int     k;
941         float   f;
942         char    buf1[16],buf2[16];
943
944         f = proptmp->psize.value / 10.0;
945         if ( ( proptmp->psize.value % 10 ) != 0 ) {
946                 snprintf( buf1, sizeof(buf1), "%.1f",f );
947         } else {
948                 snprintf( buf1, sizeof(buf1), "%.0f",f);
949         }
950         k = ( ( proptmp->resolution.value * 72.27 ) / 100.0 ) + 1;
951         snprintf( buf2, sizeof(buf2), "%d %d", k, k );
952         fprintf( stdout,"SIZE %s %s\n", buf1, buf2 );
953         return;
954 }
955
956
957
958 static  void
959 pPROPS( fip,fplistPtr,fplistNum )
960 FontInfoRec     *fip;
961 FontPropRec     *fplistPtr;
962 unsigned int    fplistNum;
963 {
964         FontPropRec     *wkp;
965         int     i, k;
966
967         bufp = buf;
968         k = snprintf( bufp, sizeof(buf), "STARTPROPERTIES %d\n", fplistNum+3 );
969         bufp += k;
970         k = snprintf( bufp, sizeof(buf) - (bufp - buf), "FONT_ASCENT %d\n", fip->fontAscent );
971         bufp += k;
972         k = snprintf( bufp, sizeof(buf) - (bufp - buf), "FONT_DESCENT %d\n", fip->fontDescent );
973         bufp += k;
974         k = snprintf( bufp, sizeof(buf) - (bufp - buf), "DEFAULT_CHAR %d\n", fip->chDefault );
975         bufp += k;
976         wkp = fplistPtr;
977         for ( i = 0; i < fplistNum; i++ ) {
978                 if ( wkp->indirect == TRUE ) {
979                         k = snprintf( bufp, sizeof(buf) - (bufp - buf), "%s \"%s\"\n", (char *) (intptr_t) wkp->name, (char *) (intptr_t) wkp->value );
980                         bufp += k;
981                 } else {
982                         k = snprintf( bufp, sizeof(buf) - (bufp - buf), "%s %d\n", (char *) (intptr_t) wkp->name, wkp->value );
983                         bufp += k;
984                 }
985                 wkp++;
986         }
987         k = snprintf( bufp, sizeof(buf) - (bufp - buf), "ENDPROPERTIES\n" );
988         bufp += k;
989         *bufp = '\0';
990         fprintf( stdout, "%s", buf );
991         return;
992
993 }
994
995
996 /* output to font property information of pcf fontpcf */
997 static  void    
998 pPROPS_pcf( pcf )
999 PcfTmp  *pcf;
1000 {
1001         FontPropPtr     wkp;
1002         int     i, k;
1003
1004         bufp = buf;
1005         k = sprintf( bufp, "STARTPROPERTIES %d\n", pcf->nprops+3 );
1006         bufp += k;
1007         k = sprintf( bufp, "FONT_ASCENT %d\n", 
1008             (pcf->info.fontAscent >= pcf->info.maxbounds.metrics.ascent)
1009             ? pcf->info.fontAscent : pcf->info.maxbounds.metrics.ascent
1010             );
1011         bufp += k;
1012         k = sprintf( bufp, "FONT_DESCENT %d\n",
1013             (pcf->info.fontDescent >= pcf->info.maxbounds.metrics.descent)
1014             ? pcf->info.fontDescent : pcf->info.maxbounds.metrics.descent
1015             );
1016         bufp += k;
1017         k = sprintf( bufp, "DEFAULT_CHAR %d\n", pcf->info.chDefault );
1018         bufp += k;
1019         wkp = pcf->props;
1020         for ( i = 0; i < pcf->nprops; i++ ) {
1021                 if ( pcf->isStringProp[i] ) {
1022                         k = sprintf(bufp,"%s \"%s\"\n", 
1023                             pcf->string + wkp->name, pcf->string + wkp->value
1024                             );
1025                         bufp += k;
1026                 } else {
1027                         k = sprintf(bufp,"%s %d\n", 
1028                             pcf->string + wkp->name, wkp->value
1029                             );
1030                         bufp += k;
1031                 }
1032                 wkp++;
1033         }
1034         k = sprintf( bufp, "ENDPROPERTIES\n" );
1035         bufp += k;
1036         *bufp = '\0';
1037         fprintf( stdout,"%s",buf );
1038         return;
1039
1040 }
1041
1042
1043
1044 /*
1045  * output to character information and patern
1046  *
1047  */
1048 static  void    
1049 pCHARS(fip,cip,glyphPtr,charNum,glyphPad)
1050 FontInfoRec     *fip;
1051 CharInfoRec     *cip;
1052 char    *glyphPtr;
1053 unsigned int    charNum;
1054 unsigned int    glyphPad;
1055
1056 {
1057         CharInfoRec     *wkp;
1058         int     i, j;
1059         unsigned int    frow, lrow, row, fcol, lcol, col;
1060         unsigned int    bbw, bbh;
1061         unsigned int    bml;
1062         char    *glyph;
1063
1064         fprintf( stdout, "CHARS %d\n", charNum );
1065
1066         frow = fip->firstRow;
1067         lrow = fip->lastRow;
1068         fcol = fip->firstCol;
1069         lcol = fip->lastCol;
1070         wkp = cip;
1071         glyph = glyphPtr;
1072         row = frow;
1073
1074         for ( i = 0; i < 256; i++ ) {
1075                 cvtp[i] = &cvt[i];
1076         }
1077
1078         for ( row = frow; row <= lrow; row++ ) {
1079                 for ( col = fcol; col <= lcol; col++ ) {
1080                         if ( wkp->exists == FALSE ) {
1081                                 wkp++;
1082                                 continue;
1083                         }
1084                         fprintf( stdout, "STARTCHAR %.2x%.2x\n", row,col );
1085                         fprintf( stdout, "ENCODING %d\n", (row << 8) + col );
1086                         fprintf( stdout, "SWIDTH 256 0\nDWIDTH %d %d\n",
1087                             wkp->metrics.characterWidth,0 );
1088                         bbw = wkp->metrics.rightSideBearing
1089                             - wkp->metrics.leftSideBearing;
1090                         bbh = wkp->metrics.ascent + wkp->metrics.descent;
1091                         fprintf( stdout, "BBX %d %d %d %d\nBITMAP\n", bbw, bbh,
1092                             wkp->metrics.leftSideBearing,
1093                             - ( wkp->metrics.descent ));
1094                         bml = (bbw + 7) / 8;
1095                         for ( i = 0; i < bbh; i++ ) {
1096                                 bufp = buf;
1097                                 for ( j = 0; j < bml; j++ ) {
1098                                         sprintf( bufp, "%s", (char *) cvtp[(unsigned char)glyph[j]] );
1099                                         bufp += 2;
1100                                 }
1101                                 fprintf( stdout, "%.*s\n", bml*2, buf );
1102                                 glyph += glyphPad;
1103                         }
1104                         fprintf( stdout,"ENDCHAR\n" );
1105                         wkp++;
1106                 }
1107         }
1108         return;
1109 }
1110
1111
1112 /*
1113  * output to character information and patern
1114  *
1115  */
1116 static  void    
1117 pfixCHARS(fip,cip,glyphPtr,charNum,glyphPad)
1118 FontInfoRec     *fip;
1119 CharInfoRec     *cip;
1120 char    *glyphPtr;
1121 unsigned int    charNum;
1122 unsigned int    glyphPad;
1123 {
1124         CharInfoRec     *wkp;
1125         register int    i, j, k;
1126         unsigned int    frow,lrow,fcol,lcol;
1127         register int    row, col;
1128         unsigned int    bbw, bbh, bbox, bboy;
1129         unsigned int    xd, yd;
1130         unsigned int    bml;
1131         register char   *glyph;
1132         char    fixbuf[240], *fixbufp;
1133         int     fixdl;
1134
1135         fprintf( stdout,"CHARS %d\n",charNum );
1136
1137         for ( i = 0; i < 256; i++ ) {
1138                 cvtp[i] = &cvt[i];
1139         }
1140
1141         frow = fip->firstRow;
1142         lrow = fip->lastRow;
1143         fcol = fip->firstCol;
1144         lcol = fip->lastCol;
1145         xd =  cip->metrics.characterWidth;
1146         yd = 0;
1147         bbw = cip->metrics.rightSideBearing - cip->metrics.leftSideBearing;
1148         bbh = cip->metrics.ascent + cip->metrics.descent;
1149         bbox = cip->metrics.leftSideBearing;
1150         bboy = -(cip->metrics.descent);
1151
1152         fixbufp = fixbuf;
1153         fixdl = sprintf( fixbufp, "SWIDTH 256 0\nDWIDTH %d %d\n", xd, yd );
1154         fixbufp += fixdl;
1155         k = sprintf(fixbufp,"BBX %d %d %d %d\nBITMAP\n", 
1156             bbw, bbh, bbox, bboy 
1157             );
1158         fixdl += k;
1159
1160         bml = (bbw + 7) / 8;
1161         wkp = cip;
1162         glyph = glyphPtr;
1163         for ( row = frow; row <= lrow; row++ ) {
1164                 for ( col = fcol; col <= lcol; col++ ) {
1165                         if ( wkp->exists == FALSE ) {
1166                                 wkp++;
1167                                 continue;
1168                         }
1169                         bufp = buf;
1170                         memcpy(bufp,"STARTCHAR ",10);
1171                         bufp += 10;
1172                         memcpy(bufp,cvtp[row],2);
1173                         bufp += 2;
1174                         memcpy(bufp,cvtp[col],3);
1175                         bufp += 3;
1176                         memcpy(bufp,"ENCODING ",9);
1177                         bufp += 9;
1178                         k = sprintf(bufp,"%d\n",(row << 8) + col);
1179                         bufp += k;
1180                         memcpy(bufp,fixbuf,fixdl);
1181                         bufp += fixdl;
1182                         for (i = 0; i < bbh; i++) {
1183                                 for (j = 0; j < bml; j++) {
1184                                         memcpy(bufp, cvtp[(unsigned char)glyph[j]],3);
1185                                         bufp += 2;
1186                                 }
1187                                 bufp ++;
1188                                 glyph += glyphPad;
1189                         }
1190                         memcpy( bufp, "ENDCHAR\n", 8 );
1191                         bufp += 8;
1192                         *bufp = '\0';
1193                         fprintf( stdout, "%s", buf );
1194                         wkp++;
1195                 }
1196         }
1197         return;
1198 }
1199
1200
1201 static void
1202 putPtn( bits, width, height)
1203 unsigned char   *bits;          /* 1 byte boundary , no byte swap data */
1204 int     width, height;
1205 {
1206         int     bytewidth;
1207         int     i, j;
1208
1209         bytewidth = ( width + 7) / 8;
1210
1211         for ( i = height; i-- > 0;) {
1212                 for ( j = bytewidth; j-- > 0; bits++) {
1213                         fprintf(stdout, "%.2x", *bits);
1214                 }
1215                 fprintf(stdout, "\n");
1216         }
1217
1218 }
1219
1220 static void
1221 ByteSwap( p, scan)
1222 char    *p;
1223 int     scan;
1224 {
1225         char    w;
1226
1227         switch( scan) {
1228         case 1:
1229                 break;
1230         case 2:
1231                 w = *p;
1232                 *p = *(p + 1);
1233                 *(p + 1) = w;
1234                 break;
1235         case 4:
1236                 w = *p;
1237                 *p = *(p + 3);
1238                 *(p + 3) = w;
1239                 w = *(p + 1);
1240                 *(p + 1) = *(p + 2);
1241                 *(p + 2) = w;
1242                 break;
1243         }
1244 }
1245
1246 static unsigned char _reverse_byte[0x100] = {
1247                         0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
1248                         0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
1249                         0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
1250                         0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
1251                         0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
1252                         0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
1253                         0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
1254                         0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
1255                         0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
1256                         0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
1257                         0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
1258                         0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
1259                         0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
1260                         0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
1261                         0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
1262                         0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
1263                         0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
1264                         0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
1265                         0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
1266                         0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
1267                         0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
1268                         0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
1269                         0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
1270                         0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
1271                         0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
1272                         0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
1273                         0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
1274                         0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
1275                         0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
1276                         0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
1277                         0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
1278                         0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
1279 };
1280
1281 static  void
1282 BitOrderInvert(buf, nbytes)
1283 register unsigned char *buf;
1284 register int nbytes;
1285 {
1286         register unsigned char *rev = _reverse_byte;
1287
1288         for (; --nbytes >= 0; buf++)
1289                 *buf = rev[*buf];
1290 }
1291
1292 static void
1293 invertBits( src, format, width, height, dest)
1294 char    *src;
1295 CARD32  format;
1296 int     width, height;
1297 char    *dest;
1298 {
1299         int     bit, byte, glyph, scan;
1300         int     src_bytewidth, dest_bytewidth;
1301         char    work[8];
1302         int     i, j;
1303
1304         bit = PCF_BIT_ORDER( format);
1305         byte = PCF_BYTE_ORDER( format);
1306         glyph = PCF_GLYPH_PAD( format);
1307         scan = PCF_SCAN_UNIT( format);
1308
1309         src_bytewidth = (( width + ( 8 * glyph ) - 1)/( 8 * glyph)) * glyph;
1310         dest_bytewidth = ( width + 7) /8;
1311
1312         for ( i = 0; i < height; i++, src += src_bytewidth,
1313             dest += dest_bytewidth) {
1314                 for ( j = 0; j < src_bytewidth; j += scan) {
1315                         memcpy( work, src + j, scan);
1316                         if ( bit == LSBFirst)
1317                                 BitOrderInvert( (unsigned char *)work, scan);
1318                         if ( byte == LSBFirst)
1319                                 ByteSwap( work, scan);
1320                         if (( j + scan) >= dest_bytewidth) {
1321                                 memcpy( dest + j, work, dest_bytewidth - j);
1322                                 break;
1323                         }
1324                         memcpy( dest + j, work, scan);
1325                 }
1326         }
1327
1328 }
1329
1330 static  void
1331 pCHARS_pcf (pcf, fix)
1332 PcfTmp  *pcf;
1333 int     fix ;
1334 {
1335         char    *bmp_ptn = NULL;
1336         char    *bitmap = NULL;
1337         int     ptn_width = 0, ptn_height = 0;
1338         int     bmp_width = 0, bmp_height = 0;
1339         int     cpy_height = 0;
1340         int     row, col;
1341         int     row_width;
1342         int     encoding;
1343         int     encodingOffset;
1344         int     nencoding;
1345         int     bmp_adj = 0, ptn_adj = 0;
1346         int     adj_hi;
1347         int     width_bytes;
1348         INT16   bl, br, ba, bd, bdw ;
1349         unsigned char   cl, cr, ca, cd, cdw ;
1350         unsigned char   *comp ;
1351         int     bbw, bbh, bbx, bby, dwidth ;
1352
1353         fprintf(stdout,"CHARS %d\n",pcf->nbitmaps);
1354
1355         row_width = pcf->info.lastCol - pcf->info.firstCol + 1;
1356         nencoding = row_width * (pcf->info.lastRow - pcf->info.firstRow + 1);
1357
1358         if( fix == TRUE ) {
1359             bmp_width = pcf->info.maxbounds.metrics.leftSideBearing 
1360                     + pcf->info.maxbounds.metrics.rightSideBearing ;
1361             bmp_height = pcf->info.fontAscent + pcf->info.fontDescent ;
1362             ptn_width  = bmp_width ;
1363             ptn_height = pcf->info.maxbounds.metrics.ascent +
1364                         pcf->info.maxbounds.metrics.descent;
1365
1366             if (!(bmp_ptn = make_toptn( ptn_width, ptn_height)))
1367                     return;
1368
1369             if ( ( adj_hi = pcf->info.fontAscent - pcf->info.maxbounds.metrics.ascent ) > 0) {
1370                     width_bytes =  8 * PCF_GLYPH_PAD( pcf->bmp_fmt);
1371                     width_bytes = (( width_bytes + bmp_width - 1)/width_bytes) * PCF_GLYPH_PAD( pcf->bmp_fmt);
1372                     bmp_adj = width_bytes * adj_hi;
1373                     ptn_adj = 0;
1374                     if (( cpy_height = bmp_height - adj_hi) > ptn_height)
1375                             cpy_height = ptn_height ;
1376             } else if ( adj_hi < 0) {
1377                     adj_hi *= -1;
1378                     width_bytes = ( ptn_width + 7) / 8;
1379                     bmp_adj = 0;
1380                     ptn_adj = width_bytes * adj_hi;
1381                     if (( cpy_height = ptn_height - adj_hi) > bmp_height)
1382                             cpy_height = bmp_height ;
1383             } else {
1384                     bmp_adj = 0;
1385                     ptn_adj = 0;
1386                     cpy_height = ptn_height ;
1387             }
1388         }
1389
1390
1391         for ( encoding = 0; encoding < nencoding; encoding++) {
1392                 if(fix == TRUE) memset( bmp_ptn, '\0', (ptn_width + 7)/8 * ptn_height);
1393                 encodingOffset = getINT16( (unsigned char *)(pcf->encodingOffsets + encoding), pcf->enc_fmt);
1394                 if (encodingOffset == 0xFFFF) continue;
1395
1396                 row = pcf->info.firstRow + encoding / row_width;
1397                 col = pcf->info.firstCol + encoding % row_width;
1398
1399                 fprintf(stdout,"STARTCHAR %s\n",pcf->glyph_name + 
1400                    getINT32( (unsigned char*)&pcf->glyph_offset[encodingOffset],
1401                    pcf->glyph_fmt) );
1402                 fprintf(stdout,"ENCODING %d\n",(row << 8) + col);
1403
1404                 fprintf( stdout,"SWIDTH %d 0\n", getINT32( (unsigned char*)(pcf->swidth + encodingOffset), pcf->swd_fmt) );
1405
1406                 if( pcf->compress ){
1407                     comp = (unsigned char*)pcf->metrics + 
1408                                 encodingOffset * SIZEOF_COMPRESSED_METRIC ;
1409                     cl  = getINT8( comp ) ;
1410                     cr  = getINT8( comp ) ;
1411                     cdw = getINT8( comp ) ;
1412                     ca  = getINT8( comp ) ;
1413                     cd  = getINT8( comp ) ;
1414
1415                     dwidth = (int)(cdw - 0x80) ;
1416                     bbw = (int)((cr - 0x80) - (cl - 0x80)) ;
1417                     bbh = (int)((ca - 0x80) + (cd - 0x80)) ;
1418                     bbx = (int)(cl - 0x80) ;
1419                     bby = (int)( -(cd - 0x80) ) ;
1420                 }else{
1421                     bl = getINT16( (unsigned char *)&pcf->metrics[encodingOffset].leftSideBearing, pcf->mtr_fmt ) ;
1422                     br = getINT16( (unsigned char *)&pcf->metrics[encodingOffset].rightSideBearing, pcf->mtr_fmt ) ;
1423                     bdw = getINT16( (unsigned char *)&pcf->metrics[encodingOffset].characterWidth, pcf->mtr_fmt ) ;
1424                     ba = getINT16( (unsigned char *)&pcf->metrics[encodingOffset].ascent, pcf->mtr_fmt ) ;
1425                     bd = getINT16( (unsigned char *)&pcf->metrics[encodingOffset].descent, pcf->mtr_fmt ) ;
1426                     dwidth = bdw ;
1427                     bbw = br-bl ;
1428                     bbh = ba+bd ;
1429                     bbx = bl ;
1430                     bby = -bd ;
1431                 }
1432                 fprintf( stdout,"DWIDTH %d 0\n", dwidth ) ;
1433                 fprintf( stdout,"BBX %d %d %d %d\nBITMAP\n", bbw, bbh, bbx, bby ) ;
1434
1435                 if( fix == FALSE ) {
1436                     bmp_width = pcf->info.maxbounds.metrics.leftSideBearing 
1437                             + pcf->info.maxbounds.metrics.rightSideBearing ;
1438                     bmp_height = pcf->info.fontAscent + pcf->info.fontDescent ;
1439                     ptn_width  = bbw ;
1440                     ptn_height = bbh ;
1441
1442                     if (!(bmp_ptn = make_toptn( ptn_width, ptn_height)))
1443                             return;
1444
1445                     if ( ( adj_hi = pcf->info.fontAscent - pcf->info.maxbounds.metrics.ascent ) > 0) {
1446                         width_bytes =  8 * PCF_GLYPH_PAD( pcf->bmp_fmt);
1447                         width_bytes = (( width_bytes + bmp_width - 1)/width_bytes) * PCF_GLYPH_PAD( pcf->bmp_fmt);
1448                         bmp_adj = width_bytes * adj_hi;
1449                         ptn_adj = 0;
1450                         if (( cpy_height = bmp_height - adj_hi) > ptn_height)
1451                                 cpy_height = ptn_height ;
1452                     } else if ( adj_hi < 0) {
1453                         adj_hi *= -1;
1454                         width_bytes = ( ptn_width + 7) / 8;
1455                         bmp_adj = 0;
1456                         ptn_adj = width_bytes * adj_hi;
1457                         if (( cpy_height = ptn_height - adj_hi) > bmp_height)
1458                                 cpy_height = bmp_height ;
1459                     } else {
1460                         bmp_adj = 0;
1461                         ptn_adj = 0;
1462                         cpy_height = ptn_height ;
1463                     }
1464                     memset( bmp_ptn, '\0', (ptn_width + 7)/8 * ptn_height);
1465                 }
1466
1467                 bitmap = pcf->bitmaps + getINT32( (unsigned char *)(pcf->offsets + encodingOffset),
1468                     pcf->bmp_fmt);
1469                 /* Only do this if non-NULL... */
1470                 if (bmp_ptn)
1471                 {
1472                     invertBits( bitmap + bmp_adj, pcf->bmp_fmt,
1473                                 ptn_width, cpy_height, bmp_ptn + ptn_adj);
1474                     putPtn( (unsigned char *)bmp_ptn, ptn_width, ptn_height );
1475                 }
1476                 else
1477                 {
1478                     fprintf(stderr, "%s: bmp_ptn is NULL", __FUNCTION__);
1479                 }
1480
1481                 fprintf(stdout,"ENDCHAR\n");
1482                 if( fix == FALSE )      free(bmp_ptn);
1483         }
1484         if( fix == TRUE )       free(bmp_ptn);
1485         return;
1486 }
1487
1488 static  char *
1489 make_toptn( width, height)
1490 int     width, height;
1491 {
1492         int     byte_width;
1493
1494         byte_width = (width + 7)/8;
1495
1496         return (char *)malloc( byte_width * height);
1497 }
1498
1499
1500
1501
1502 static  int     
1503 getarg(argc,argv,fname,bitorder,byteorder,scanunit,glyphPad,outLevel,fix)
1504 int  argc;
1505 char *argv[];
1506 char *fname;
1507 int  *bitorder;
1508 int  *byteorder;
1509 int  *scanunit;
1510 int  *glyphPad;
1511 int  *outLevel;
1512 int  *fix;
1513 {
1514         int i;
1515         int already;
1516         char *arg;
1517         char *usage="dtgpftobdf [-H] [-V] [-help]  font_file_name";
1518
1519         *glyphPad = DEFAULTGLPAD;
1520         *bitorder = DEFAULTBITORDER;
1521         *scanunit = DEFAULTSCANUNIT;
1522         *byteorder = DEFAULTBYTEORDER;
1523         *outLevel = FALSE;
1524         *fix = FALSE ;
1525         already = 0 ;
1526
1527         for (i = 1; i < argc; i++ ) {
1528                 arg = argv[i];
1529                 if (*arg == '-') {
1530                         *arg++;
1531                         switch (*arg) {
1532                         case 'p' :
1533                                 *glyphPad = atoi(arg+1) ;
1534                                 if (*glyphPad != 1 &&
1535                                     *glyphPad != 2 &&
1536                                     *glyphPad != 4 &&
1537                                     *glyphPad != 8 ) {
1538                                         USAGE("dtgpftobdf : illegal padding number (1/2/4/8)\n");
1539                                         return  -1;
1540                                 }
1541                                 break;
1542                         case 'H' :
1543                                 *outLevel = TRUE ;
1544                                 break;
1545                         case 'h' :
1546                                 USAGE1("usage: %s\n",usage);
1547                                 return (-1);
1548                         case 'V' :
1549                                 *fix = TRUE ;
1550                                 break;
1551                         default :
1552                                 USAGE1("dtgpftobdf : illegal option -- %s\n",arg);
1553                                 USAGE1("usage: %s\n",usage);
1554                                 return  -1;
1555                         }
1556
1557                 } else {
1558                         if (already == FALSE) {
1559                                 strcpy(fname,arg);
1560                                 already = TRUE;
1561                         } else {
1562                                 USAGE1("dtgpftobdf : extra parameter --- %s\n",arg);
1563                                 USAGE1("usage: %s\n", usage );
1564                                 return  -1;
1565                         }
1566                 }
1567         }
1568         if (already == FALSE) {
1569                 USAGE("dtgpftobdf : require file name\n");
1570                 USAGE1("usage: %s\n",usage);
1571                 return  -1;
1572         }
1573         return  0;
1574 }
1575