home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / groff / etc / addftinfo.c next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  5.0 KB  |  193 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.uucp)
  4.  
  5. This file is part of groff.
  6.  
  7. groff is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with groff; see the file LICENSE.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <stdlib.h>
  26. #include "lib.h"
  27. #include "errarg.h"
  28. #include "error.h"
  29. #include "stringclass.h"
  30. #include "cset.h"
  31. #include "guess.h"
  32.  
  33. static void usage();
  34. static void convert_font(const font_params &, FILE *, FILE *);
  35.  
  36. typedef font_params::*param_t;
  37.  
  38. static struct {
  39.   const char *name;
  40.   param_t par;
  41. } param_table[] = {
  42.   "x-height", &font_params::x_height,
  43.   "fig-height", &font_params::fig_height,
  44.   "asc-height", &font_params::asc_height,
  45.   "body-height", &font_params::body_height,
  46.   "cap-height", &font_params::cap_height,
  47.   "comma-depth", &font_params::comma_depth,
  48.   "desc-depth", &font_params::desc_depth,
  49.   "body-depth", &font_params::body_depth,
  50. };
  51.  
  52. // These are all in thousandths of an em.
  53. // These values are correct for PostScript Times Roman.
  54.  
  55. #define DEFAULT_X_HEIGHT 448
  56. #define DEFAULT_FIG_HEIGHT 676
  57. #define DEFAULT_ASC_HEIGHT 682
  58. #define DEFAULT_BODY_HEIGHT 676
  59. #define DEFAULT_CAP_HEIGHT 662
  60. #define DEFAULT_COMMA_DEPTH 143
  61. #define DEFAULT_DESC_DEPTH 217
  62. #define DEFAULT_BODY_DEPTH 177
  63.  
  64. int main(int argc, char **argv)
  65. {
  66.   program_name = argv[0];
  67.   if (argc < 4)
  68.     usage();
  69.   int resolution;
  70.   if (sscanf(argv[argc-3], "%d", &resolution) != 1)
  71.     usage();
  72.   if (resolution <= 0)
  73.     fatal("resolution must be > 0");
  74.   int unitwidth;
  75.   if (sscanf(argv[argc-2], "%d", &unitwidth) != 1)
  76.     usage();
  77.   if (unitwidth <= 0)
  78.     fatal("unitwidth must be > 0");
  79.   font_params param;
  80.   const char *font = argv[argc-1];
  81.   param.italic = (font[0] != '\0' && strchr(font, '\0')[-1] == 'I');
  82.   param.em = (resolution*unitwidth)/72;
  83.   param.x_height = DEFAULT_X_HEIGHT;
  84.   param.fig_height = DEFAULT_FIG_HEIGHT;
  85.   param.asc_height = DEFAULT_ASC_HEIGHT;
  86.   param.body_height = DEFAULT_BODY_HEIGHT;
  87.   param.cap_height = DEFAULT_CAP_HEIGHT;
  88.   param.comma_depth = DEFAULT_COMMA_DEPTH;
  89.   param.desc_depth = DEFAULT_DESC_DEPTH;
  90.   param.body_depth = DEFAULT_BODY_DEPTH;
  91.   for (int i = 1; i < argc && argv[i][0] == '-'; i++) {
  92.     if (argv[i][1] == '-' && argv[i][2] == '\0') {
  93.       i++;
  94.       break;
  95.     }
  96.     if (i + 1 >= argc)
  97.       usage();
  98.     for (int j = 0;; j++) {
  99.       if (j >= sizeof(param_table)/sizeof(param_table[0]))
  100.     fatal("parameter `%1' not recognized", argv[i] + 1);
  101.       if (strcmp(param_table[j].name, argv[i] + 1) == 0)
  102.     break;
  103.     }
  104.     if (sscanf(argv[i+1], "%d", &(param.*(param_table[j].par))) != 1)
  105.       fatal("invalid argument `%1'", argv[i+1]);
  106.     i++;
  107.   }    
  108.   if (argc - i != 3)
  109.     usage();
  110.   FILE *infp = fopen(font, "r");
  111.   if (infp == 0)
  112.     fatal("can't open `%1': %2", font, strerror(errno));
  113.   convert_font(param, infp, stdout);
  114.   exit(0);
  115. }
  116.  
  117. static void usage()
  118. {
  119.   fprintf(stderr, "usage: %s [-param value] ... resolution unitwidth font\n",
  120.       program_name);
  121.   exit(1);
  122. }
  123.  
  124. static int get_line(FILE *fp, string *p)
  125. {
  126.   int c;
  127.   p->clear();
  128.   while ((c = getc(fp)) != EOF) {
  129.     *p += char(c);
  130.     if (c == '\n')
  131.       break;
  132.   }
  133.   return p->length() > 0;
  134. }
  135.   
  136. static void convert_font(const font_params ¶m, FILE *infp, FILE *outfp)
  137. {
  138.   string s;
  139.   while (get_line(infp, &s)) {
  140.     put_string(s, outfp);
  141.     if (s.length() >= 8
  142.     && strncmp(&s[0], "charset", 7))
  143.       break;
  144.   }
  145.   while (get_line(infp, &s)) {
  146.     s += '\0';
  147.     string name;
  148.     const char *p = s.contents();
  149.     while (csspace(*p))
  150.       p++;
  151.     while (*p != '\0' && !csspace(*p))
  152.       name += *p++;
  153.     while (csspace(*p))
  154.       p++;
  155.     for (const char *q = s.contents(); q < p; q++)
  156.       putc(*q, outfp);
  157.     char *next;
  158.     char_metric metric;
  159.     metric.width = (int)strtol(p, &next, 10);
  160.     if (next != p) {
  161.       printf("%d", metric.width);
  162.       p = next;
  163.       metric.type = (int)strtol(p, &next, 10);
  164.       if (next != p) {
  165.     name += '\0';
  166.     guess(name.contents(), param, &metric);
  167.     if (metric.sk == 0) {
  168.       if (metric.left_ic == 0) {
  169.         if (metric.ic == 0) {
  170.           if (metric.depth == 0) {
  171.         if (metric.height != 0)
  172.           printf(",%d", metric.height);
  173.           }
  174.           else
  175.         printf(",%d,%d", metric.height, metric.depth);
  176.         }
  177.         else
  178.           printf(",%d,%d,%d", metric.height, metric.depth, metric.ic);
  179.       }
  180.       else
  181.         printf(",%d,%d,%d,%d", metric.height, metric.depth, metric.ic,
  182.            metric.left_ic);
  183.     }
  184.     else
  185.       printf(",%d,%d,%d,%d,%d", metric.height, metric.depth, metric.ic,
  186.          metric.left_ic, metric.sk);
  187.       }
  188.     }
  189.     fputs(p, outfp);
  190.   }
  191. }
  192.  
  193.