macfont.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. *
  11. * Program that converts Macintosh font files to a format that works on Unix
  12. * systems. Essentially all the information needed came from the Adobe paper
  13. * "Supporting Downloadable PostScript Fonts". To use the program type,
  14. *
  15. * macfont font.mac >font.unix
  16. *
  17. * where font.mac is the font file, exactly as it came over from a Macintosh,
  18. * and font.unix is equivalent host resident font file usable on Unix systems.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #define OFF 0
  24. #define ON 1
  25. #define NON_FATAL 0
  26. #define FATAL 1
  27. #define FALSE 0
  28. #define TRUE 1
  29. char **argv;
  30. int argc;
  31. char *prog_name;
  32. int x_stat;
  33. int debug = OFF;
  34. int ignore = OFF;
  35. FILE *fp_in = stdin;
  36. FILE *fp_out = stdout;
  37. /*****************************************************************************/
  38. main(agc, agv)
  39. int agc;
  40. char *agv[];
  41. {
  42. /*
  43. *
  44. * Macintosh to Unix font converter.
  45. *
  46. */
  47. argc = agc;
  48. argv = agv;
  49. prog_name = argv[0];
  50. options();
  51. arguments();
  52. exit(x_stat);
  53. } /* End of main */
  54. /*****************************************************************************/
  55. options()
  56. {
  57. int ch;
  58. char *names = "DI";
  59. extern char *optarg;
  60. extern int optind;
  61. /*
  62. *
  63. * Command line options.
  64. *
  65. */
  66. while ( (ch = getopt(argc, argv, names)) != EOF ) {
  67. switch ( ch ) {
  68. case 'D': /* debug flag */
  69. debug = ON;
  70. break;
  71. case 'I': /* ignore FATAL errors */
  72. ignore = ON;
  73. break;
  74. case '?': /* don't understand the option */
  75. error(FATAL, "");
  76. break;
  77. default: /* don't know what to do for ch */
  78. error(FATAL, "missing case for option %c\n", ch);
  79. break;
  80. } /* End switch */
  81. } /* End while */
  82. argc -= optind;
  83. argv += optind;
  84. } /* End of options */
  85. /*****************************************************************************/
  86. arguments()
  87. {
  88. /*
  89. *
  90. * Everything else is an input file. No arguments or '-' means stdin.
  91. *
  92. */
  93. if ( argc < 1 )
  94. conv();
  95. else
  96. while ( argc > 0 ) {
  97. if ( strcmp(*argv, "-") == 0 )
  98. fp_in = stdin;
  99. else if ( (fp_in = fopen(*argv, "r")) == NULL )
  100. error(FATAL, "can't open %s", *argv);
  101. conv();
  102. if ( fp_in != stdin )
  103. fclose(fp_in);
  104. argc--;
  105. argv++;
  106. } /* End while */
  107. } /* End of arguments */
  108. /*****************************************************************************/
  109. conv()
  110. {
  111. int blocksize;
  112. int blocktype;
  113. /*
  114. *
  115. * The first four bytes (in a block) are the block size, the fifth is the block
  116. * type, and the sixth always appears to be NULL. Type 0 blocks are comments and
  117. * are always skipped. Type 1 blocks are ASCII text, type 2 is binary data that
  118. * should be converted to hex, while type 5 blocks represent the end of the font
  119. * file. Commment block lengths appear to be from the first byte, while other
  120. * lengths seem to be measured from block type byte (ie. the fifth byte). Type
  121. * four blocks aren't used, while type 3 blocks mean an end of file indication
  122. * should be sent to the printer. Haven't done anything with type 3 blocks.
  123. *
  124. */
  125. while ( 1 ) {
  126. blocksize = getint(fp_in);
  127. blocktype = getc(fp_in);
  128. getc(fp_in);
  129. if ( debug == ON )
  130. fprintf(stderr, "blocktype = %d, blocksize = %d\n", blocktype, blocksize);
  131. switch ( blocktype ) {
  132. case 0: /* comment - skip blockcount bytes */
  133. fseek(fp_in, (long) blocksize - 6, 1);
  134. break;
  135. case 1:
  136. asciitext(blocksize - 2);
  137. break;
  138. case 2:
  139. hexdata(blocksize - 2);
  140. break;
  141. case 3:
  142. case 4:
  143. error(FATAL, "resource type %d not implemented", blocktype);
  144. break;
  145. case 5:
  146. return;
  147. default:
  148. error(FATAL, "unknown resource type %d", blocktype);
  149. } /* End switch */
  150. } /* End while */
  151. } /* End of conv */
  152. /*****************************************************************************/
  153. asciitext(count)
  154. int count; /* bytes left in the block */
  155. {
  156. int ch;
  157. int i = 0;
  158. /*
  159. *
  160. * Handles type 1 (ie. ASCII text) blocks. Changing carriage returns to newlines
  161. * is all I've done.
  162. *
  163. */
  164. for ( i = 0; i < count; i++ ) {
  165. if ( (ch = getc(fp_in)) == '\r' )
  166. ch = '\n';
  167. putc(ch, fp_out);
  168. } /* End for */
  169. } /* End of asciitext */
  170. /*****************************************************************************/
  171. hexdata(count)
  172. int count; /* bytes left in the block */
  173. {
  174. int i;
  175. int n;
  176. /*
  177. *
  178. * Reads the next count bytes and converts each byte to hex. Also starts a new
  179. * line every 80 hex characters.
  180. *
  181. */
  182. for ( i = 0, n = 0; i < count; i++ ) {
  183. fprintf(fp_out, "%.2X", getc(fp_in));
  184. if ( (++n % 40) == 0 )
  185. putc('\n', fp_out);
  186. } /* End for */
  187. } /* End of hexdata */
  188. /*****************************************************************************/
  189. getint()
  190. {
  191. int val;
  192. int i;
  193. /*
  194. *
  195. * Reads the next four bytes into an integer and returns the value to the caller.
  196. * First two bytes are probably always 0.
  197. *
  198. */
  199. for ( i = 0, val = (getc(fp_in) & 0377); i < 3; i++ )
  200. val = (val << 8) | (getc(fp_in) & 0377);
  201. return(val);
  202. } /* End of getint */
  203. /*****************************************************************************/
  204. error(kind, mesg, a1, a2, a3)
  205. int kind;
  206. char *mesg;
  207. unsigned a1, a2, a3;
  208. {
  209. /*
  210. *
  211. * Print *mesg then quit if kind is FATAL.
  212. *
  213. */
  214. if ( mesg != NULL && *mesg != '\0' ) {
  215. fprintf(stderr, "%s: ", prog_name);
  216. fprintf(stderr, mesg, a1, a2, a3);
  217. putc('\n', stderr);
  218. } /* End if */
  219. if ( kind == FATAL && ignore == OFF )
  220. exit(x_stat | 01);
  221. } /* End of error */
  222. /*****************************************************************************/