home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / util / compile_metrics.c next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  8.4 KB  |  270 lines  |  [TEXT/CWIE]

  1. /*---------------------------------------------------------------------------*
  2.  |              PDFlib - A library for generating PDF on the fly             |
  3.  +---------------------------------------------------------------------------+
  4.  | Copyright (c) 1997-2001 PDFlib GmbH and Thomas Merz. All rights reserved. |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is NOT in the public domain.  It can be used under two   |
  7.  |    substantially different licensing terms:                               |
  8.  |                                                                           |
  9.  |    The commercial license is available for a fee, and allows you to       |
  10.  |    - ship a commercial product based on PDFlib                            |
  11.  |    - implement commercial Web services with PDFlib                        |
  12.  |    - distribute (free or commercial) software when the source code is     |
  13.  |      not made available                                                   |
  14.  |    Details can be found in the file PDFlib-license.pdf.                   |
  15.  |                                                                           |
  16.  |    The "Aladdin Free Public License" doesn't require any license fee,     |
  17.  |    and allows you to                                                      |
  18.  |    - develop and distribute PDFlib-based software for which the complete  |
  19.  |      source code is made available                                        |
  20.  |    - redistribute PDFlib non-commercially under certain conditions        |
  21.  |    - redistribute PDFlib on digital media for a fee if the complete       |
  22.  |      contents of the media are freely redistributable                     |
  23.  |    Details can be found in the file aladdin-license.pdf.                  |
  24.  |                                                                           |
  25.  |    These conditions extend to ports to other programming languages.       |
  26.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  27.  |    however, will receive warranty and support statements in writing.      |
  28.  *---------------------------------------------------------------------------*/
  29.  
  30. /* compile_metrics.c
  31.  *
  32.  * Generate C header file with compiled font metrics from AFM, PFM, or TrueType
  33.  *
  34.  */
  35.  
  36. /* $Id: compile_metrics.c,v 1.13 2001/04/02 16:29:48 tm Exp $ */
  37.  
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <ctype.h>
  41.  
  42. #if defined(__CYGWIN32__)
  43. #include <getopt.h>
  44. #elif defined(WIN32)
  45. int getopt(int argc, char * const argv[], const char *optstring);
  46. extern char *optarg;
  47. extern int optind;
  48. #elif !defined(MAC)
  49. #include <unistd.h>
  50. #endif
  51.  
  52. #include "p_intern.h"
  53. #include "p_font.h"
  54.  
  55. void
  56. add_entry(PDF *p, FILE *out, pdf_font font, pdf_encoding enc)
  57. {
  58.     int        i;
  59.  
  60.     fprintf(out, "{");
  61.     fprintf(out, "\t\"%s\",\t\t\t/* FontName */\n", font.name);
  62.     fprintf(out, "\t%s,\t\t\t/* encoding */\n",
  63.     font.encoding == builtin ?
  64.         "builtin" :
  65.         p->encodings[font.encoding]->apiname);
  66.     fprintf(out, "\t0,\t\t\t\t/* used on page */\n");
  67.     fprintf(out, "\t0,\t\t\t\t/* embed */\n");
  68.     fprintf(out, "\tNULL,\t\t\t\t/* font file */\n");
  69.     fprintf(out, "\t0L,\t\t\t\t/* id */\n");
  70.     fprintf(out, "\t%luL,\t\t\t/* flags */\n", font.flags);
  71.     fprintf(out, "\tType1,\t\t\t\t/* font type */\n");
  72.  
  73.     if (font.ttname)
  74.     fprintf(out, "\t\"%s\",\t\t\t\t/* TT name */\n", font.ttname);
  75.     else
  76.     fprintf(out, "\tNULL,\t\t\t\t/* TT name */\n");
  77.  
  78.     fprintf(out, "\t\"%s\",\n", font.encodingScheme);
  79.  
  80.     fprintf(out, "\tcc_none,\n");
  81.  
  82.     fprintf(out, "\t(float) %2.1f,\t\t\t/* ItalicAngle */\n", font.italicAngle);
  83.     fprintf(out, "\t%d,\t\t\t\t/* isFixedPitch */\n", font.isFixedPitch);
  84.     fprintf(out, "\t%d,\t\t\t\t/* llx */\n", (int) font.llx);
  85.     fprintf(out, "\t%d,\t\t\t\t/* lly */\n", (int) font.lly);
  86.     fprintf(out, "\t%d,\t\t\t\t/* urx */\n", (int) font.urx);
  87.     fprintf(out, "\t%d,\t\t\t\t/* ury */\n", (int) font.ury);
  88.     fprintf(out, "\t%d,\t\t\t\t/* UnderlinePosition */\n",
  89.     font.underlinePosition);
  90.     fprintf(out, "\t%d,\t\t\t\t/* UnderlineThickness */\n",
  91.     font.underlineThickness);
  92.     fprintf(out, "\t%d,\t\t\t\t/* CapHeight */\n", font.capHeight);
  93.     fprintf(out, "\t%d,\t\t\t\t/* xHeight */\n", font.xHeight);
  94.     fprintf(out, "\t%d,\t\t\t\t/* Ascender */\n", font.ascender);
  95.     fprintf(out, "\t%d,\t\t\t\t/* Descender */\n", font.descender);
  96.     fprintf(out, "\t%d,\t\t\t\t/* StdVW */\n", font.StdVW);
  97.     fprintf(out, "\t%d,\t\t\t\t/* StdHW */\n", font.StdHW);
  98.     fprintf(out, "\t0,\n");
  99.     fprintf(out, "\t(CharMetricInfo *) NULL,\n");
  100.     fprintf(out, "\t0,\n");
  101.     fprintf(out, "\t(TrackKernData *) NULL,\n");
  102.     fprintf(out, "\t0,\n");
  103.     fprintf(out, "\t(PairKernData *) NULL,\n");
  104.     fprintf(out, "\t0,\n");
  105.     fprintf(out, "\t(CompCharData *) NULL,\n");
  106.  
  107.     fprintf(out, "\n{");
  108.  
  109.     for (i=0; i < 256; i++) {
  110.     fprintf(out, "\t%3d", font.widths[i]);
  111.     if (i == 255)
  112.         fprintf(out, " ");
  113.     else
  114.         fprintf(out, ",");
  115.  
  116.     if (i % 8 == 7)
  117.         fprintf(out, " /* 0x%02x */\n", i);
  118.     }
  119.  
  120.     fprintf(out, "}\n");
  121.     fprintf(out, "},\n");
  122.  
  123.     (void) pdf_cleanup_font_struct(p, &font);
  124. }
  125.  
  126. void
  127. usage(void)
  128. {
  129.     fprintf(stderr,
  130. "compile_metrics: Compile AFM, PFM, or TrueType data to C code for PDFlib\n");
  131.     fprintf(stderr, 
  132. "Usage: compile_metrics -h -e [winansi|macroman|ebcdic] [-o outfile] files...\n"
  133. );
  134.     exit(1);
  135. }
  136.  
  137. int
  138. main(int argc, char *argv[])
  139. {
  140.     int   opt;
  141.     char *filename, *outfilename = NULL;
  142.     int enc;
  143.     char *encname;
  144.     FILE *out = stdout;
  145.     PDF  *p;
  146.     pdf_font font;
  147.     size_t len;
  148.  
  149.     /* This is only a dummy to provide a PDF* for the auxiliary functions */
  150.     if ((p = PDF_new()) == NULL) {
  151.     fprintf(stderr, "Couldn't generate PDF object - aborting\n");
  152.     exit(99);
  153.     }
  154.  
  155.     PDF_open_file(p, "");
  156.  
  157.     /* By default, generate compiled metrics for the current platform.
  158.      * This may also be changed since the encoding is recorded in the
  159.      * generated C structs.
  160.      */
  161. #ifdef EBCDIC
  162.     enc = ebcdic;
  163.     encname = "ebcdic";
  164. #elif MAC
  165.     enc = macroman;
  166.     encname = "macroman";
  167. #else
  168.     enc = winansi;
  169.     encname = "winansi";
  170. #endif
  171.  
  172.     while ((opt = getopt(argc, argv, "e:ho:")) != -1)
  173.     switch (opt) {
  174.         case 'e':
  175.         if (!strcmp(optarg, "builtin")) {
  176.             enc = builtin;
  177.             encname = "builtin";
  178.         } else {
  179.             for (enc = 0; p->encodings[enc] != NULL; enc++) {
  180.             if (p->encodings[enc]->apiname &&
  181.                 !strcmp(optarg, p->encodings[enc]->apiname)) {
  182.                 encname = p->encodings[enc]->apiname;
  183.                 break;
  184.             }
  185.             }
  186.             if (p->encodings[enc] == NULL) {
  187.             fprintf(stderr, "Encoding %s not supported!\n", optarg);
  188.             exit(88);
  189.             }
  190.         }
  191.         break;
  192.  
  193.         case 'h':
  194.         usage();
  195.         break;
  196.  
  197.         case 'o':
  198.             outfilename = optarg;
  199.  
  200.         if (!strcmp(outfilename, "-")) {
  201.             out = stdout;
  202.         } else if ((out = fopen(outfilename, "w")) == NULL) {
  203.             fprintf(stderr, "Couldn't open output file %s!\n",
  204.                 outfilename);
  205.             exit(99);
  206.         }
  207.  
  208.         break;
  209.  
  210.         default:
  211.             break;
  212.     }
  213.  
  214.     if (optind < argc) {
  215.     fprintf(out, "\n");
  216.     } else
  217.     usage();
  218.  
  219.     while (optind < argc) {
  220.     filename = argv[optind++];
  221.     fprintf(stderr, "Reading %s, encoding = %s\n", filename, encname);
  222.  
  223.     len = strlen(filename);
  224.  
  225.     if (len >= 5 && (!strcmp(filename + len - 4, ".pfm") ||
  226.         !strcmp(filename + len - 4, ".PFM"))) {
  227.         /* parse PFM file */
  228.         if (!pdf_get_metrics_pfm(p, &font, NULL,  enc, filename)) {
  229.         fprintf(stderr, "Error parsing PFM file '%s' - skipped!\n",
  230.         filename);
  231.         } else {
  232.         fprintf(out,
  233.         "\n/* -------- Generated from %s -------- */\n",
  234.         filename);
  235.         add_entry(p, out, font, enc);
  236.         }
  237.     } else if (len >= 5 && (!strcmp(filename + len - 4, ".afm") ||
  238.         !strcmp(filename + len - 4, ".AFM"))) {
  239.         /* parse AFM file */
  240.         if (!pdf_get_metrics_afm(p, &font, NULL,  enc, filename)) {
  241.         fprintf(stderr, "Error parsing AFM file '%s' - skipped!\n",
  242.         filename);
  243.         } else {
  244.         fprintf(out,
  245.         "\n/* -------- Generated from %s -------- */\n",
  246.         filename);
  247.         add_entry(p, out, font, enc);
  248.         }
  249.     } else if (len >= 5 && (!strcmp(filename + len - 4, ".ttf") ||
  250.         !strcmp(filename + len - 4, ".TTF"))) {
  251.         /* parse TrueType file */
  252.         if (!pdf_get_metrics_tt(p, &font, NULL,  enc, filename, 0)) {
  253.         fprintf(stderr, "Error parsing TrueType file '%s' - skipped!\n",
  254.         filename);
  255.         } else {
  256.         fprintf(out,
  257.         "\n/* -------- Generated from %s -------- */\n",
  258.         filename);
  259.         add_entry(p, out, font, enc);
  260.         }
  261.     } else {
  262.         fprintf(stderr, "Unknown file name suffix in '%s' - skipped!\n",
  263.         filename);
  264.     }
  265.     }
  266.  
  267.     fclose(out);
  268.     exit(0);
  269. }
  270.