2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: importbdf.c /main/5 1996/06/27 10:40:33 ageorge $ */
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
30 * Authors: Seiya Miyazaki FUJITSU LIMITED
31 * Hiroyuki Chiba FUJITSU LIMITED
40 #include <sys/types.h>
44 #include <sys/resource.h>
54 #include <X11/Xproto.h>
60 #define UNLINK_TMPFILE( file ){ \
61 if ( unlink( file ) != 0 ) { \
66 static impFileConvInit();
67 static impGetGpfInf();
68 static impReadBdfHeaderAndPut();
70 static impModifyPtn();
71 static impInsertPtn();
73 static int impWritePtnToBdf();
74 static int impPutDefaultChars();
75 static int impReadBdfToMemory();
77 extern int expCheckCode();
80 static struct ptobhead WriteGpf;
82 static char *targ_file = NULL; /* UDC_file_name */
88 if (WriteGpf.out_file) {
89 UNLINK_TMPFILE( WriteGpf.out_file );
98 ImpBDFCodeList( bdf_name, code_num, code_list )
110 if( (bdfp = fopen( bdf_name, "r" )) == NULL ){
114 /* get number of characters in BDF file */
116 if( fgets( buf, BUFSIZE, bdfp ) == NULL) {
121 if ( !strncmp( p, CHARS, (unsigned int)strlen( CHARS ) ) ) {
122 if ( ( sscanf( p, "CHARS %d", code_num ) ) != 1 ) {
129 /* alloc memory for codes */
130 if( (glyph_list = (int *)calloc( *code_num, (unsigned int)sizeof(int) )) == NULL ){
134 /* get codes of BDF file */
136 while( cnt < *code_num ) {
137 if (fgets(buf, BUFSIZE, bdfp ) == NULL) {
142 if (!strncmp(p, ENDFONT, (unsigned int)ENDFONTsz)) {
144 }else if (!strncmp(p, ENCODING, (unsigned int)ENCODINGsz)) {
145 if ((sscanf(p, "ENCODING %d", &glyph_list[cnt] )) != 1) {
151 if( cnt != *code_num ){
156 if( fclose( bdfp ) ){
160 *code_list = glyph_list ;
168 ImpBDFCodeListFree( code_list )
178 ImpBDFCheck( bdf_name, gpf_name )
179 char *bdf_name ; /* BDF file name */
180 char *gpf_name ; /* GPF file name */
182 /* parameter check */
183 if( bdf_name == NULL || gpf_name == NULL ){
193 ImpBDFtoGpf( bdf_name, gpf_name, bdf_codenum, bdf_codelist)
201 struct btophead ReadGpf;
202 struct btophead ReadUdc;
208 ReadGpf.in_file = ReadUdc.in_file = WriteGpf.out_file = NULL;
210 if (!( bdftopcf = get_cmd_path( getenv( "PATH" ), BDFTOPCF_CMD ))) {
213 if (!( oakgtobdf = get_cmd_path( getenv( "PATH" ), SNFTOBDF_CMD ))) {
214 oakgtobdf = SNFTOBDF;
216 if (!( bdftosnf = get_cmd_path( getenv( "PATH" ), BDFTOSNF_CMD ))) {
221 * set input/output file name
224 ReadUdc.in_file = bdf_name ;
225 ReadUdc.code_category = ALL_CODE ;
226 ReadUdc.start_code = MIN_CODE ;
227 ReadUdc.end_code = MAX_CODE ;
229 ReadGpf.in_file = gpf_name ;
230 ReadGpf.code_category = ALL_CODE ;
231 ReadGpf.start_code = MIN_CODE ;
232 ReadGpf.end_code = MAX_CODE ;
235 * get real file name of GPF file
237 if ((targ_file = GetRealFileName( ReadGpf.in_file )) == NULL){
242 * WriteGpf.snf_file = ReadGpf.in_file
244 WriteGpf.snf_file = targ_file;
246 signal( SIGHUP , (void(*)())sigint_out );
247 signal( SIGINT , (void(*)())sigint_out );
248 signal( SIGQUIT, (void(*)())sigint_out );
249 signal( SIGTERM, (void(*)())sigint_out );
256 * (ReadGpf.in_file) --> SNFTOBDF ==H
258 * oakaddp <-- (ReadUdc.in_file)
260 * (WriteGpf.out_file) <-- BDFTOSNF ==H
265 * make a temporary file by BDF format corresponded to target GPF file
267 if ( rtn = impFileConvInit( &ReadUdc, &ReadGpf, &WriteGpf ) ) {
268 if ( WriteGpf.out_file ) {
269 UNLINK_TMPFILE( WriteGpf.out_file );
275 * get informations from import file in BDF format
277 if ( rtn = ReadBdfHeader( &ReadUdc, buf ) ) {
278 if ( WriteGpf.out_file ) {
279 UNLINK_TMPFILE( WriteGpf.out_file );
285 * get informations from target file in BDF format
288 if ( rtn = impGetGpfInf( &ReadGpf, &WriteGpf, buf, bdf_codenum ) ) {
289 if ( WriteGpf.out_file ) {
290 UNLINK_TMPFILE( WriteGpf.out_file );
295 /* wait for dtgpftobdf */
296 fclose( ReadGpf.input );
300 * merge the UDC glyphs into GPF file (in BDF format)
302 if ( ( rtn = impMergePtn( &ReadUdc, &ReadGpf, buf,
303 bdf_codenum, bdf_codelist ) ) ) {
304 if ( WriteGpf.out_file ) {
305 UNLINK_TMPFILE( WriteGpf.out_file );
311 * write the UDC data into GPF output file (in BDF format)
313 if ( ( rtn = impWriteSnf( &ReadGpf, &WriteGpf ) ) ) {
314 if ( WriteGpf.out_file ) {
315 UNLINK_TMPFILE( WriteGpf.out_file );
321 * convert temporary file into GPF format
323 fclose( ReadUdc.input );
324 fclose( WriteGpf.output );
326 #if !defined( SVR4 ) && !defined( SYSV )
327 if ( !WIFEXITED(exit_stat) ) {
329 if ( ! ( WIFEXITED(exit_stat) && !WEXITSTATUS(exit_stat) ) ) {
331 UNLINK_TMPFILE( WriteGpf.out_file );
334 signal( SIGHUP , SIG_IGN );
335 signal( SIGINT , SIG_IGN );
336 signal( SIGQUIT, SIG_IGN );
337 signal( SIGTERM, SIG_IGN );
340 if ( ( stat( WriteGpf.out_file, &statbuf ) ) || ( statbuf.st_size == 0 )
342 UNLINK_TMPFILE( WriteGpf.out_file );
345 if ( stat( WriteGpf.snf_file, &statbuf ) ) {
346 UNLINK_TMPFILE( WriteGpf.out_file );
350 * convert the temporary file to target file
352 return( Make_NewFefFile( WriteGpf.snf_file, WriteGpf.out_file,
353 FONT_FILE_PARM, (uid_t)statbuf.st_uid, (gid_t)statbuf.st_gid, "ImpBDFtoGpf()" ));
362 impFileConvInit(r_udc, r_gpf, w_gpf )
363 struct btophead *r_udc;
364 struct btophead *r_gpf;
365 struct ptobhead *w_gpf;
367 int fd[2], snf_fd, permission;
372 char *optlist[5] = {0,0,0,0,0};
375 struct stat statbuf ;
379 if ( ( r_udc->input = fopen(r_udc->in_file, "r")) == NULL ) {
386 if ( ChkPcfFontFile( w_gpf->snf_file ) ) {
388 if ( ( snf_fd = open( w_gpf->snf_file, O_RDONLY ) ) >= 0 ) {
389 COMM_SNF_FILEVERSION( snf_fd, finf, buf, permission ) ;
390 if( permission < 0 ) {
405 if ( dup( fd[1] ) < 0 ) {
412 execl( oakgtobdf, oakgtobdf, r_gpf->in_file, 0 );
422 if ( ( r_gpf->input = (FILE *)fdopen( fd[0], "r" ) ) == NULL ) {
426 if ( !( w_gpf->out_file = GetTmpPath( targ_file ) ) ) {
430 if ( pipe( pfd ) != 0 ) {
435 if ( ( ofd = open( w_gpf->out_file, O_WRONLY | O_CREAT, 0664 ) ) < 0 ) {
439 if ( dup(pfd[0]) < 0 ) {
445 if( dup( ofd ) < 0 ) {
450 * case of PCF file format
452 if ( ChkPcfFontFile( w_gpf->snf_file ) == 0 ) {
453 execl( bdftopcf, bdftopcf, 0 );
457 * case of SNF file format
459 COMM_SNF_EXECLBDFTOSNF( permission, buf, w_gpf->snf_file ) ;
469 if ( ( w_gpf->output = (FILE *)fdopen( pfd[1], "w" ) ) == NULL ) {
480 impGetGpfInf( r_gpf, w_gpf, buf, bdf_codenum )
481 struct btophead *r_gpf;
482 struct ptobhead *w_gpf;
488 if ( ( rtn = impReadBdfHeaderAndPut( r_gpf, w_gpf, buf ) ) ) {
492 nchar = r_gpf->num_chars + bdf_codenum ;
494 if ( ( r_gpf->code = (int *)malloc( sizeof(int) * nchar ) ) == NULL ) {
498 if ( ( r_gpf->ptn = (char **)malloc( sizeof(char *) * nchar ) ) == NULL ) {
502 return ReadBdfToMemory( r_gpf, buf ) ;
508 impReadBdfHeaderAndPut(r_gpf, w_gpf, buf)
509 struct btophead *r_gpf;
510 struct ptobhead *w_gpf;
514 unsigned int getstat = 0;
517 if ( fgets( buf, BUFSIZE, r_gpf->input ) == NULL ) {
522 if ( !strncmp( p, SIZE, (unsigned int)strlen( SIZE ) ) ) {
523 if ( ( sscanf( p, "SIZE %f%d",
524 &(r_gpf->bdf_point), &(r_gpf->bdf_xdpi))) != 2 ) {
527 fprintf( w_gpf->output, "%s", buf );
530 } else if ( !strncmp( p, FONTBOUNDINGBOX, (unsigned int)strlen( FONTBOUNDINGBOX ) ) ) {
531 if ( ( sscanf( p, "FONTBOUNDINGBOX %d %d %d %d",
532 &(r_gpf->bdf_width), &(r_gpf->bdf_height),
533 &(r_gpf->bdf_x), &(r_gpf->bdf_y) ) ) != 4 ) {
536 fprintf( w_gpf->output, "%s", buf );
539 } else if ( !strncmp( p, CHARS, (unsigned int)strlen( CHARS ) ) ) {
540 if ( ( sscanf( p, "CHARS %d", &( r_gpf->num_chars ) ) ) != 1 ) {
546 fprintf( w_gpf->output, "%s", buf );
550 if ( getstat != 0x07 ) {
558 impMergePtn(r_udc, r_gpf, buf, bdf_codenum, bdf_codelist )
559 struct btophead *r_udc;
560 struct btophead *r_gpf;
565 int code, rtn, msize, i, j;
568 if ( ( r_udc->bdf_width != r_gpf->bdf_width )
569 || ( r_udc->bdf_height != r_gpf->bdf_height )
573 msize = ( r_udc->bdf_width + 7 ) / 8 * r_udc->bdf_height;
575 if ( ( ptn = (char *)malloc( msize ) ) == NULL ) {
584 for ( i = 0; i < r_udc->num_chars; i++ ) {
585 if ( ( rtn = GetBdfCode( r_udc, buf, &code ) ) < 0 ) {
587 } else if ( rtn == FILE_END ) {
591 if( expCheckCode( code, bdf_codenum, bdf_codelist ) ) {
595 for ( j = 0; j < r_gpf->num_chars; j++ ) {
596 if ( r_gpf->code[j] == code ) {
597 if ( ( rtn = impModifyPtn( r_udc, r_gpf, buf, j ) ) ) {
601 } else if ( r_gpf->code[j] > code ) {
602 if ( ( rtn = impInsertPtn( r_udc, r_gpf, buf, code, j ) ) ) {
608 if ( j == r_gpf->num_chars ) {
609 if ( ( rtn = impInsertPtn( r_udc, r_gpf, buf, code, j ) ) ) {
618 impModifyPtn( r_udc, r_gpf, buf, ix )
619 struct btophead *r_udc;
620 struct btophead *r_gpf;
624 int mwidth, msize, rtn;
626 mwidth = ( r_udc->bdf_width + 7 ) / 8;
627 msize = mwidth * r_udc->bdf_height;
629 if ( r_udc->zoomf ) {
630 if ( ( rtn = GetBdfPtn( r_udc, buf, r_udc->ptn[0], mwidth, msize ) ) ) {
633 if ( ( rtn = PtnZoom( r_gpf->ptn[ix], r_udc->ptn[0],
634 r_udc->bdf_width, r_udc->bdf_height,
635 r_gpf->bdf_width, r_gpf->bdf_height ) ) ) {
639 if ( ( rtn = GetBdfPtn( r_udc, buf, r_gpf->ptn[ix], mwidth, msize ) ) ) {
647 impInsertPtn( r_udc, r_gpf, buf, code, ix )
648 struct btophead *r_udc;
649 struct btophead *r_gpf;
654 int mwidth, msize, rtn, i;
656 for ( i = r_gpf->num_chars; i > ix; i-- ) {
657 r_gpf->code[i] = r_gpf->code[i-1];
658 r_gpf->ptn[i] = r_gpf->ptn[i-1];
660 r_gpf->code[ix] = code;
664 mwidth = (r_gpf->bdf_width + 7) / 8;
665 msize = mwidth * r_gpf->bdf_height;
667 if ( ( r_gpf->ptn[ix] = (char *)malloc( msize ) ) == NULL ) {
671 if ( r_udc->zoomf ) {
672 mwidth = (r_udc->bdf_width + 7) / 8;
673 msize = mwidth * r_udc->bdf_height;
674 if ( ( rtn = GetBdfPtn( r_udc, buf, r_udc->ptn[0], mwidth, msize ) ) ) {
677 if ( ( rtn = PtnZoom( r_gpf->ptn[ix], r_udc->ptn[0],
678 r_udc->bdf_width, r_udc->bdf_height,
679 r_gpf->bdf_width, r_gpf->bdf_height ) ) ) {
683 if ( ( rtn = GetBdfPtn( r_udc, buf, r_gpf->ptn[ix], mwidth, msize ) ) ) {
691 impWriteSnf( r_gpf, w_gpf )
692 struct btophead *r_gpf;
693 struct ptobhead *w_gpf;
696 w_gpf->num_chars = r_gpf->num_chars;
697 w_gpf->code = r_gpf->code;
698 w_gpf->ptn = r_gpf->ptn;
699 w_gpf->bdf_width = r_gpf->bdf_width;
700 w_gpf->bdf_height = r_gpf->bdf_height;
701 w_gpf->bdf_x = r_gpf->bdf_x;
702 w_gpf->bdf_y = r_gpf->bdf_y;
703 w_gpf->bdf_point = r_gpf->bdf_point;
704 w_gpf->bdf_xdpi = r_gpf->bdf_xdpi;
706 return WritePtnToBdf( w_gpf );