Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members  

t1binary.c

Go to the documentation of this file.
00001 /*  $Id: t1binary.c,v 1.6 2000/12/06 18:42:30 dbryson Exp $ */
00002 
00003 /*
00004 **  Description:
00005 **  
00006 **    This file is part of libps
00007 **
00008 **
00009 **  Contact:
00010 **
00011 **      Mail:
00012 **
00013 **        Technology Associates, Inc.
00014 **        LIBPS Project
00015 **        1455 Deming Way #11
00016 **        Sparks, NV  89431
00017 **        USA
00018 **
00019 **      Email:
00020 **
00021 **        libps@techass.com
00022 **
00023 **      See our website at:
00024 **
00025 **        libps.sourceforge.net
00026 **
00027 */
00028 
00029 /* t1binary
00030  *
00031  * This program takes an Adobe Type-1 font program in ASCII (PFA) format and
00032  * converts it to binary (PFB) format.
00033  *
00034  * Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.
00035  *
00036  * Permission is hereby granted to use, modify, and distribute this program
00037  * for any purpose provided this copyright notice and the one below remain
00038  * intact.
00039  *
00040  * I. Lee Hetherington (ilh@lcs.mit.edu)
00041  *
00042  * 1.5 and later versions contain changes by, and are maintained by,
00043  * Eddie Kohler <eddietwo@lcs.mit.edu>.
00044  *
00045  * New change log in `NEWS'. Old change log:
00046  *
00047  * Revision 1.2  92/06/23  10:58:08  ilh
00048  * MSDOS porting by Kai-Uwe Herbing (herbing@netmbx.netmbx.de)
00049  * incoporated.
00050  * 
00051  * Revision 1.1  92/05/22  11:58:17  ilh
00052  * initial version
00053  *
00054  * Ported to Microsoft C/C++ Compiler and MS-DOS operating system by
00055  * Kai-Uwe Herbing (herbing@netmbx.netmbx.de) on June 12, 1992. Code
00056  * specific to the MS-DOS version is encapsulated with #ifdef _MSDOS
00057  * ... #endif, where _MSDOS is an identifier, which is automatically
00058  * defined, if you compile with the Microsoft C/C++ Compiler.
00059  *
00060  *
00061  * libps
00062  *
00063  * Made various changes for inclusion in libps including modifying these
00064  * routines to use the PSStream stuff and various name changes and making
00065  * this a library function rather than a program.
00066  *
00067  * November 21, 2000 Derry Bryson <derry@techass.com>
00068  */
00069 
00070 /* Note: this is ANSI C. */
00071 
00075 #ifdef HAVE_CONFIG_H
00076 # include "psconfig.h"
00077 #endif
00078 
00079 #if defined(_MSDOS) || defined(_WIN32)
00080 # include <fcntl.h>
00081 # include <io.h>
00082 #endif
00083 #include <stdio.h>
00084 #include <stdlib.h>
00085 #include <string.h>
00086 #include <ctype.h>
00087 #include <limits.h>
00088 #include <stdarg.h>
00089 #include <errno.h>
00090 /*#include "clp.h"*/
00091 #include "ps/ps.h"
00092 #include "t1lib.h"
00093 
00094 /* int32 must be at least 32-bit */
00095 #if INT_MAX >= 0x7FFFFFFFUL
00096 typedef int int32;
00097 typedef unsigned int uint32;
00098 #else
00099 typedef long int32;
00100 typedef unsigned long uint32;
00101 #endif
00102 
00103 typedef unsigned char byte;
00104 
00105 /* for PFB block buffering */
00106 static struct pfb_writer w;
00107 
00108 #if 0 /* removed for libps */
00109 void fatal_error(const char *message, ...);
00110 void error(const char *message, ...);
00111 #endif
00112 
00113 /* PFB font_reader functions */
00114 
00115 static void
00116 pfb_output_ascii(PSStream* fio, char *s)
00117 {
00118   if (w.blocktyp == PFB_BINARY) {
00119     pfb_writer_output_block(&w);
00120     w.blocktyp = PFB_ASCII;
00121   }
00122   for (; *s; s++)
00123     PFB_OUTPUT_BYTE(&w, (byte)*s);
00124 }
00125 
00126 static void
00127 pfb_output_binary(PSStream* fio, unsigned char *s, int len)
00128 {
00129   if (w.blocktyp == PFB_ASCII) {
00130     pfb_writer_output_block(&w);
00131     w.blocktyp = PFB_BINARY;
00132   }
00133   for (; len > 0; len--, s++)
00134     PFB_OUTPUT_BYTE(&w, *s);
00135 }
00136 
00137 static void
00138 pfb_output_end(PSStream* fio)
00139 {
00140   pfb_writer_end(&w);
00141 }
00142 
00143 static struct font_writer
00144   fw = 
00145   {
00146     pfb_output_ascii,
00147     pfb_output_binary,
00148     pfb_output_end
00149   };
00150 
00161 int
00162 psPFAToPFB(PSStream *in, PSStream *out, int maxBlockLen)
00163 {
00164   int
00165     c;
00166     
00167   struct pfb_writer
00168     w;
00169 
00170   pserrno = PSERR_NONE;      
00171   
00172   init_pfb_writer(&w, maxBlockLen, out);
00173   
00174   /* peek at first byte to see if it is the PFB marker 0x80 */
00175   c = psGetC(in);
00176   psUngetC(c, in);
00177   
00178   /* do the file */
00179   if (c == PFB_MARKER)
00180     process_pfb(in, &fw, out);
00181   else if (c == '%')
00182     process_pfa(in, &fw, out);
00183   else
00184     pserrno = PSERR_UNKNOWNFILETYPE;
00185     
00186   return !pserrno;
00187 }
00188 
00189 #if 0 /* removed for libps */
00190 /*****
00191  * Command line
00192  **/
00193 
00194 #define BLOCK_LEN_OPT   300
00195 #define OUTPUT_OPT      301
00196 #define VERSION_OPT     302
00197 #define HELP_OPT        303
00198 
00199 static Clp_Option options[] = {
00200   { "block-length", 'l', BLOCK_LEN_OPT, Clp_ArgInt, 0 },
00201   { "help", 0, HELP_OPT, 0, 0 },
00202   { "length", 0, BLOCK_LEN_OPT, Clp_ArgInt, 0 },
00203   { "output", 'o', OUTPUT_OPT, Clp_ArgString, 0 },
00204   { "version", 0, VERSION_OPT, 0, 0 },
00205 };
00206 static char *program_name;
00207 
00208 
00209 void
00210 fatal_error(const char *message, ...)
00211 {
00212   va_list val;
00213   va_start(val, message);
00214   fprintf(stderr, "%s: ", program_name);
00215   vfprintf(stderr, message, val);
00216   putc('\n', stderr);
00217   exit(1);
00218 }
00219 
00220 void
00221 error(const char *message, ...)
00222 {
00223   va_list val;
00224   va_start(val, message);
00225   fprintf(stderr, "%s: ", program_name);
00226   vfprintf(stderr, message, val);
00227   putc('\n', stderr);
00228 }
00229 
00230 
00231 void
00232 short_usage(void)
00233 {
00234   fprintf(stderr, "Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
00235 Try `%s --help' for more information.\n",
00236           program_name, program_name);
00237 }
00238 
00239 
00240 void
00241 usage(void)
00242 {
00243   printf("\
00244 `T1binary' translates a PostScript Type 1 font from ASCII (PFA) to compact\n\
00245 binary (PFB) format. The result is written to the standard output unless an\n\
00246 OUTPUT file is given.\n\
00247 \n\
00248 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
00249 \n\
00250 Options:\n\
00251   -l, --block-length=NUM        Set max output block length.\n\
00252   -o, --output=FILE             Write output to FILE.\n\
00253   -h, --help                    Print this message and exit.\n\
00254       --version                 Print version number and warranty and exit.\n\
00255 \n\
00256 Report bugs to <eddietwo@lcs.mit.edu>.\n", program_name);
00257 }
00258 
00259 
00260 int
00261 main(int argc, char **argv)
00262 {
00263   int c;
00264   FILE *ifp = 0, *ofp = 0;
00265   const char *ifp_filename = "<stdin>";
00266   struct font_reader fr;
00267   int max_blocklen = -1;
00268   
00269   Clp_Parser *clp =
00270     Clp_NewParser(argc, argv, sizeof(options) / sizeof(options[0]), options);
00271   program_name = (char *)Clp_ProgramName(clp);
00272   
00273   /* interpret command line arguments using CLP */
00274   while (1) {
00275     int opt = Clp_Next(clp);
00276     switch (opt) {
00277       
00278      case BLOCK_LEN_OPT:
00279       max_blocklen = clp->val.i;
00280       if (max_blocklen <= 0) {
00281         max_blocklen = 1;
00282         error("warning: block length raised to %d", max_blocklen);
00283       }
00284       break;
00285       
00286      output_file:
00287      case OUTPUT_OPT:
00288       if (ofp)
00289         fatal_error("output file already specified");
00290       if (strcmp(clp->arg, "-") == 0)
00291         ofp = stdout;
00292       else {
00293         ofp = fopen(clp->arg, "wb");
00294         if (!ofp) fatal_error("%s: %s", clp->arg, strerror(errno));
00295       }
00296       break;
00297       
00298      case HELP_OPT:
00299       usage();
00300       exit(0);
00301       break;
00302       
00303      case VERSION_OPT:
00304       printf("t1binary (LCDF t1utils) %s\n", VERSION);
00305       printf("Copyright (C) 1992-2000 I. Lee Hetherington, Eddie Kohler et al.\n\
00306 This is free software; see the source for copying conditions.\n\
00307 There is NO warranty, not even for merchantability or fitness for a\n\
00308 particular purpose.\n");
00309       exit(0);
00310       break;
00311       
00312      case Clp_NotOption:
00313       if (ifp && ofp)
00314         fatal_error("too many arguments");
00315       else if (ifp)
00316         goto output_file;
00317       if (strcmp(clp->arg, "-") == 0)
00318         ifp = stdin;
00319       else {
00320         ifp_filename = clp->arg;
00321         ifp = fopen(clp->arg, "r");
00322         if (!ifp) fatal_error("%s: %s", clp->arg, strerror(errno));
00323       }
00324       break;
00325       
00326      case Clp_Done:
00327       goto done;
00328       
00329      case Clp_BadOption:
00330       short_usage();
00331       exit(1);
00332       break;
00333       
00334     }
00335   }
00336   
00337  done:
00338   if (!ifp) ifp = stdin;
00339   if (!ofp) ofp = stdout;
00340   
00341 #if defined(_MSDOS) || defined(_WIN32)
00342   /* As we are processing a PFB (binary) output */
00343   /* file, we must set its file mode to binary. */
00344   _setmode(_fileno(ofp), _O_BINARY);
00345 #endif
00346   
00347   /* prepare font reader and pfb writer */
00348   fr.output_ascii = pfb_output_ascii;
00349   fr.output_binary = pfb_output_binary;
00350   fr.output_end = pfb_output_end;
00351   init_pfb_writer(&w, max_blocklen, ofp);
00352   
00353   /* peek at first byte to see if it is the PFB marker 0x80 */
00354   c = getc(ifp);
00355   ungetc(c, ifp);
00356   
00357   /* do the file */
00358   if (c == PFB_MARKER)
00359     process_pfb(ifp, ifp_filename, &fr);
00360   else if (c == '%')
00361     process_pfa(ifp, ifp_filename, &fr);
00362   else
00363     fatal_error("%s does not start with font marker (`%%' or 0x80)", ifp_filename);
00364   
00365   fclose(ifp);
00366   fclose(ofp);
00367   
00368   if (!w.binary_blocks_written)
00369     fatal_error("no binary blocks written! Are you sure this was a font?");
00370   
00371   return 0;
00372 }
00373 #endif

Generated at Mon Dec 11 22:46:28 2000 for Postscript Utility Library by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000