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

t1ascii.c

Go to the documentation of this file.
00001 /*  $Id: t1ascii.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 /* t1ascii
00030  *
00031  * This program takes an Adobe Type-1 font program in binary (PFB) format and
00032  * converts it to ASCII (PFA) 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:43  ilh
00048  * MSDOS porting by Kai-Uwe Herbing (herbing@netmbx.netmbx.de)
00049  * incoporated.
00050  * 
00051  * Revision 1.1  92/05/22  11:47:24  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 #if 0
00091 #include "clp.h"
00092 #endif
00093 #include "ps/ps.h"
00094 #include "t1lib.h"
00095 
00096 /* int32 must be at least 32-bit */
00097 #if INT_MAX >= 0x7FFFFFFFUL
00098 typedef int int32;
00099 #else
00100 typedef long int32;
00101 #endif
00102 
00103 #if 0
00104 static FILE *ofp;
00105 #endif
00106 static int line_length = 64;
00107 
00108 
00109 /* PFA font_reader functions */
00110 
00111 static int hexcol = 0;
00112 
00113 static void
00114 pfa_output_ascii(PSStream* fio, char *data)
00115 {
00116   if (hexcol) {
00117     psPutC('\n', fio);
00118     hexcol = 0;
00119   }
00120   psPutS(data, fio);
00121 }
00122 
00123 static void
00124 pfa_output_binary(PSStream* fio, unsigned char *data, int len)
00125 {
00126   static char *hexchar = "0123456789abcdef";
00127   for (; len > 0; len--, data++) {
00128     /* trim hexadecimal lines to line_length columns */
00129     if (hexcol >= line_length) {
00130       psPutC('\n', fio);
00131       hexcol = 0;
00132     }
00133     psPutC(hexchar[(*data >> 4) & 0xf], fio);
00134     psPutC(hexchar[*data & 0xf], fio);
00135     hexcol += 2;
00136   }
00137 }
00138 
00139 static void
00140 pfa_output_end()
00141 {
00142 }
00143 
00144 static struct font_writer
00145   fw = 
00146   {
00147     pfa_output_ascii,
00148     pfa_output_binary,
00149     pfa_output_end
00150   };
00151 
00162 int
00163 psPFBToPFA(PSStream *in, PSStream *out, int lineLength)
00164 {
00165   int  
00166     c;
00167   
00168   hexcol = 0;
00169   line_length = lineLength;
00170   if(!line_length)
00171     line_length = 64;
00172   
00173   pserrno = PSERR_NONE;
00174     
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 /*****
00192  * Command line
00193  **/
00194 
00195 #define OUTPUT_OPT      301
00196 #define VERSION_OPT     302
00197 #define HELP_OPT        303
00198 #define LINE_LEN_OPT    304
00199 
00200 static Clp_Option options[] = {
00201   { "help", 0, HELP_OPT, 0, 0 },
00202   { "line-length", 'l', LINE_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 void
00209 fatal_error(const char *message, ...)
00210 {
00211   va_list val;
00212   va_start(val, message);
00213   fprintf(stderr, "%s: ", program_name);
00214   vfprintf(stderr, message, val);
00215   putc('\n', stderr);
00216   exit(1);
00217 }
00218 
00219 void
00220 error(const char *message, ...)
00221 {
00222   va_list val;
00223   va_start(val, message);
00224   fprintf(stderr, "%s: ", program_name);
00225   vfprintf(stderr, message, val);
00226   putc('\n', stderr);
00227 }
00228 
00229 void
00230 short_usage(void)
00231 {
00232   fprintf(stderr, "Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
00233 Try `%s --help' for more information.\n",
00234           program_name, program_name);
00235 }
00236 
00237 void
00238 usage(void)
00239 {
00240   printf("\
00241 `T1ascii' translates a PostScript Type 1 font from compact binary (PFB) to\n\
00242 ASCII (PFA) format. The result is written to the standard output unless an\n\
00243 OUTPUT file is given.\n\
00244 \n\
00245 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
00246 \n\
00247 Options:\n\
00248   -l, --line-length=NUM         Set max encrypted line length (default 64).\n\
00249   -o, --output=FILE             Write output to FILE.\n\
00250   -h, --help                    Print this message and exit.\n\
00251       --version                 Print version number and warranty and exit.\n\
00252 \n\
00253 Report bugs to <eddietwo@lcs.mit.edu>.\n", program_name);
00254 }
00255 
00256 
00257 int
00258 main(int argc, char **argv)
00259 {
00260   struct font_reader fr;
00261   int c;
00262   FILE *ifp = 0;
00263   const char *ifp_filename = "<stdin>";
00264   
00265   Clp_Parser *clp =
00266     Clp_NewParser(argc, argv, sizeof(options) / sizeof(options[0]), options);
00267   program_name = (char *)Clp_ProgramName(clp);
00268   
00269   /* interpret command line arguments using CLP */
00270   while (1) {
00271     int opt = Clp_Next(clp);
00272     switch (opt) {
00273       
00274      case LINE_LEN_OPT:
00275       line_length = clp->val.i;
00276       if (line_length < 4) {
00277         line_length = 4;
00278         error("warning: line length raised to %d", line_length);
00279       } else if (line_length > 1024) {
00280         line_length = 1024;
00281         error("warning: line length lowered to %d", line_length);
00282       }
00283       break;
00284       
00285      output_file:
00286      case OUTPUT_OPT:
00287       if (ofp)
00288         fatal_error("output file already specified");
00289       if (strcmp(clp->arg, "-") == 0)
00290         ofp = stdout;
00291       else {
00292         ofp = fopen(clp->arg, "w");
00293         if (!ofp) fatal_error("%s: %s", clp->arg, strerror(errno));
00294       }
00295       break;
00296       
00297      case HELP_OPT:
00298       usage();
00299       exit(0);
00300       break;
00301       
00302      case VERSION_OPT:
00303       printf("t1ascii (LCDF t1utils) %s\n", VERSION);
00304       printf("Copyright (C) 1992-2000 I. Lee Hetherington, Eddie Kohler et al.\n\
00305 This is free software; see the source for copying conditions.\n\
00306 There is NO warranty, not even for merchantability or fitness for a\n\
00307 particular purpose.\n");
00308       exit(0);
00309       break;
00310       
00311      case Clp_NotOption:
00312       if (ifp && ofp)
00313         fatal_error("too many arguments");
00314       else if (ifp)
00315         goto output_file;
00316       if (strcmp(clp->arg, "-") == 0)
00317         ifp = stdin;
00318       else {
00319         ifp_filename = clp->arg;
00320         ifp = fopen(clp->arg, "rb");
00321         if (!ifp) fatal_error("%s: %s", clp->arg, strerror(errno));
00322       }
00323       break;
00324       
00325      case Clp_Done:
00326       goto done;
00327       
00328      case Clp_BadOption:
00329       short_usage();
00330       exit(1);
00331       break;
00332       
00333     }
00334   }
00335   
00336  done:
00337   if (!ifp) ifp = stdin;
00338   if (!ofp) ofp = stdout;
00339   
00340 #if defined(_MSDOS) || defined(_WIN32)
00341   /* As we are processing a PFB (binary) input */
00342   /* file, we must set its file mode to binary. */
00343   _setmode(_fileno(ifp), _O_BINARY);
00344 #endif
00345   
00346   /* prepare font reader */
00347   fr.output_ascii = pfa_output_ascii;
00348   fr.output_binary = pfa_output_binary;
00349   fr.output_end = pfa_output_end;
00350   
00351   /* peek at first byte to see if it is the PFB marker 0x80 */
00352   c = getc(ifp);
00353   ungetc(c, ifp);
00354   
00355   /* do the file */
00356   if (c == PFB_MARKER)
00357     process_pfb(ifp, ifp_filename, &fr);
00358   else if (c == '%')
00359     process_pfa(ifp, ifp_filename, &fr);
00360   else
00361     fatal_error("%s does not start with font marker (`%%' or 0x80)", ifp_filename);
00362   
00363   fclose(ifp);
00364   fclose(ofp);
00365   return 0;
00366 }
00367 #endif

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