home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fontutils-0.6 / gsrenderfont / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  5.4 KB  |  196 lines

  1. /* bbcount -- count bounding boxes in each character in a PBM file.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "bb-outline.h"
  22. #define CMDLINE_NO_DPI /* It's not in the input filename.  */
  23. #include "cmdline.h"
  24. #include "getopt.h"
  25. #include "report.h"
  26.  
  27. #include "input-pbm.h"
  28.  
  29.  
  30. /* For temporary debugging.  */
  31. boolean debug;
  32.  
  33. /* Show every scanline on the terminal as we read it? (-trace-scanlines) */
  34. boolean trace_scanlines = false;
  35.  
  36. /* The name of the file we're going to write.  (-output-file) */
  37. static string output_name;
  38.  
  39.  
  40. static string read_command_line (int, string[]);
  41.  
  42. /* The ``bitmap information'' file.  */
  43.  
  44. typedef struct
  45. {
  46.   /* The total number of characters in the image.  */
  47.   unsigned nchars;
  48.   
  49.   /* The height of each character, in pixels.  */
  50.   unsigned char_height;
  51. } bfi_info_type;
  52.  
  53. static bfi_info_type read_bfi_file (string);
  54.  
  55. /* It is sad that this program is needed at all.  But I couldn't see any
  56.    good approach to counting the bounding boxes for each character in
  57.    Ghostscript.  So this program reads an image, counts the bounding
  58.    boxes in each character, and writes another file with the bounding
  59.    box counts.
  60.    
  61.    We assume that all characters occupy the same number of scanlines; we
  62.    simply read that constant number for each character.  We can't put
  63.    other marks in the image to avoid this assumption, since then Imageto
  64.    will have to contend with t.  */
  65.  
  66. #define PROGRAM_NAME "bbcount"
  67.  
  68. int
  69. main (int argc, string argv[])
  70. {
  71.   FILE *bbs_file;
  72.   bfi_info_type bfi;
  73.   bitmap_type *b;
  74.   unsigned charcount = 0;
  75.   string font_name = read_command_line (argc, argv);
  76.   
  77.   /* We have to read two input files, but they might have given us a
  78.      suffix on the PBM file, so we have to remove it for the BFI.  This
  79.      doesn't support filenames like `a.b.pbm'.  */
  80.  
  81.   /* Open the image input file.  */
  82.   pbm_open_input_file (extend_filename (font_name, "pbm"));
  83.   
  84.   /* Read the information about that image file.  */
  85.   bfi = read_bfi_file (make_suffix (font_name, "bfi"));
  86.  
  87.   if (output_name == NULL)
  88.     {
  89.       bbs_file = stdout;
  90.       report_file = stderr;
  91.     }
  92.   else
  93.     {
  94.       output_name =  make_suffix (output_name, "bbs");
  95.       bbs_file = xfopen (output_name, "w");
  96.     }
  97.                     
  98.   while ((b = pbm_get_block (bfi.char_height)) != NULL)
  99.     {
  100.       bb_list_type boxes = find_outline_bbs (*b, false, 0, 0);
  101.       fprintf (bbs_file, "%d\n", BB_LIST_LENGTH (boxes));
  102.       
  103.       charcount++;
  104.       REPORT1 (".%s", charcount % 79 == 0 ? "\n" : "");
  105.       free_bitmap (b);
  106.     }
  107.   REPORT ("\n");
  108.  
  109.   if (charcount != bfi.nchars)
  110.     WARNING3 ("%s: Found %d characters, expected %d", PROGRAM_NAME,
  111.               charcount, bfi.nchars);
  112.  
  113.   pbm_close_input_file ();
  114.   
  115.   return 0;
  116. }
  117.  
  118. /* Read the BFI file FILENAME, returning what we find.  */
  119.  
  120. static bfi_info_type
  121. read_bfi_file (string filename)
  122. {
  123.   bfi_info_type i;
  124.   FILE *bfi_file = xfopen (filename, "r");
  125.   
  126.   if (fscanf (bfi_file, "%d", &i.nchars) != 1)
  127.     FATAL1 ("%s: Could not read character count", filename);
  128.  
  129.   if (fscanf (bfi_file, "%d", &i.char_height) != 1)
  130.     FATAL1 ("%s: Could not read character height", filename);
  131.  
  132.   xfclose (bfi_file, filename);
  133.   
  134.   return i;
  135. }
  136.  
  137. /* Reading the options.  */
  138.  
  139. /* This is defined in version.c.  */
  140. extern string version_string;
  141.  
  142. #define USAGE "Options:
  143.   <font_name> should be a base filename, e.g., `cmr10'."        \
  144.     GETOPT_USAGE                            \
  145. "help: print this message.
  146. output-file <filename>: write to <filename>.bbs if <filename> has no
  147.   suffix, and to <filename> if it has.  Default is standard output.
  148. trace-scanlines: show every scanline as we read it.
  149. verbose: output progress reports to stderr.
  150. version: print the version number of this program.
  151. "
  152.  
  153. static string
  154. read_command_line (int argc, string argv[])
  155. {
  156.   int g;  /* `getopt' return code.  */
  157.   int option_index;
  158.   boolean printed_version = false;
  159.   struct option long_options[]
  160.     = { { "output-file",    1, 0, 0 },
  161.         { "trace-scanlines",    0, (int *) &trace_scanlines, 1 },
  162.         { "verbose",        0, (int *) &verbose, 1 },
  163.         { "version",            0, (int *) &printed_version, 1 },
  164.         { 0, 0, 0, 0 } };
  165.  
  166.   while (true)
  167.     {
  168.       g = getopt_long_only (argc, argv, "", long_options, &option_index);
  169.       
  170.       if (g == EOF)
  171.         break;
  172.  
  173.       if (g == '?')
  174.         exit (1);  /* Unknown option.  */
  175.   
  176.       assert (g == 0); /* We have no short option names.  */
  177.       
  178.       if (ARGUMENT_IS ("help"))
  179.         {
  180.           fprintf (stderr, "Usage: %s [options] <font_name>.\n", argv[0]);
  181.           fprintf (stderr, USAGE);
  182.           exit (0);
  183.         }
  184.  
  185.       else if (ARGUMENT_IS ("output-file"))
  186.         output_name = optarg;
  187.       
  188.       else if (ARGUMENT_IS ("version"))
  189.         fprintf (stderr, "%s.\n", version_string);
  190.  
  191.       /* Else it was a flag; getopt has already done the assignment.  */
  192.     }
  193.  
  194.   FINISH_COMMAND_LINE ();
  195. }
  196.