]> git.donarmstrong.com Git - lilypond.git/blob - ttftool/ttfps.c
* ttftool/util.c (syserror): use errno for better error reporting.
[lilypond.git] / ttftool / ttfps.c
1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
2
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include "types.h"
9 #include "proto.h"
10
11 #include "file-cookie.hh"
12  
13 static void endianness_test (void);
14 static void usage (char *);
15
16 int verbosity = 0;
17
18 void
19 create_type42 (const char *infile, void *out)
20 {
21   int fd, i;
22   struct OffsetTable ot;
23   struct HeadTable *ht;
24   struct PostTable *pt;
25   struct TableDirectoryEntry *td;
26   void *loca = NULL;
27   struct HheaTable *hhea = NULL;
28   struct Box *bbox = NULL;
29   longHorMetric *hmtx = NULL;
30   char **strings = NULL;
31   struct GlyphName *gnt = NULL;
32   struct KernEntry0 **ke;
33   int *nke;
34   int nglyphs, postType, nkern;
35   off_t headOff = 0, maxpOff = 0, postOff = 0, nameOff = 0,
36     locaOff = 0, glyfOff = 0, hheaOff = 0, hmtxOff = 0, kernOff = 0;
37
38   extern char *optarg;
39   extern int optind;
40   int c;
41
42   endianness_test ();
43
44   if ((fd = open (infile, O_RDONLY)) < 0)
45     syserror ("Error opening input file");
46
47   td = readDirectory (fd, &ot);
48   if (verbosity >= 2)
49     fprintf (stderr, "True type version %d.%u\n",
50              ot.version.mantissa, ot.version.fraction);
51
52   for (i = 0; i < ot.numTables; i++)
53     {
54       if (verbosity >= 2)
55         fprintf (stderr, "Found `%c%c%c%c' table\n",
56                  (char) (td[i].tag >> 24),
57                  (char) (td[i].tag >> 16) & 255,
58                  (char) (td[i].tag >> 8) & 255, (char) td[i].tag & 255);
59       switch (td[i].tag)
60         {
61         case MAKE_ULONG ('m', 'a', 'x', 'p'):
62           maxpOff = td[i].offset;
63           break;
64         case MAKE_ULONG ('h', 'e', 'a', 'd'):
65           headOff = td[i].offset;
66           break;
67         case MAKE_ULONG ('p', 'o', 's', 't'):
68           postOff = td[i].offset;
69           break;
70         case MAKE_ULONG ('n', 'a', 'm', 'e'):
71           nameOff = td[i].offset;
72           break;
73         case MAKE_ULONG ('l', 'o', 'c', 'a'):
74           locaOff = td[i].offset;
75           break;
76         case MAKE_ULONG ('g', 'l', 'y', 'f'):
77           glyfOff = td[i].offset;
78           break;
79         case MAKE_ULONG ('h', 'h', 'e', 'a'):
80           hheaOff = td[i].offset;
81           break;
82         case MAKE_ULONG ('h', 'm', 't', 'x'):
83           hmtxOff = td[i].offset;
84           break;
85         case MAKE_ULONG ('k', 'e', 'r', 'n'):
86           kernOff = td[i].offset;
87           break;
88         }
89     }
90   if (maxpOff == 0 || headOff == 0 || postOff == 0 || nameOff == 0)
91     ttf_error ("Incomplete TTF file\n");
92
93   if (verbosity >= 1)
94     fprintf (stderr, "Processing `maxp' table\n");
95   surely_lseek (fd, maxpOff, SEEK_SET);
96   nglyphs = readMaxpTable (fd);
97   if (verbosity >= 1)
98     fprintf (stderr, "  %d glyphs\n", nglyphs);
99
100   if (verbosity >= 1)
101     fprintf (stderr, "Processing `head' table\n");
102   surely_lseek (fd, headOff, SEEK_SET);
103   ht = mymalloc (sizeof (struct HeadTable));
104   readHeadTable (fd, ht);
105
106   if (verbosity >= 1)
107     fprintf (stderr, "Processing `post' table\n");
108   surely_lseek (fd, postOff, SEEK_SET);
109   pt = mymalloc (sizeof (struct PostTable));
110   postType = readPostTable (fd, nglyphs, pt, &gnt);
111
112   if (verbosity >= 1)
113     fprintf (stderr, "Processing `name' table\n");
114   surely_lseek (fd, nameOff, SEEK_SET);
115   strings = readNamingTable (fd);
116
117   if (verbosity >= 1)
118     fprintf (stderr, "Generating PS file\n");
119   printPSFont (out, ht, strings, nglyphs, postType, pt, gnt, fd);
120   lily_cookie_fclose (out);
121   if (verbosity >= 1)
122     fprintf (stderr, "Done.\n");
123   close (fd);
124 }
125
126
127 static void
128 endianness_test ()
129 {
130   union
131   {
132     BYTE b[4];
133     ULONG l;
134   } x;
135   ULONG v;
136
137   x.b[0] = 1;
138   x.b[1] = 2;
139   x.b[2] = 3;
140   x.b[3] = 4;
141
142   v = UL (x.l);
143
144   if (v != (((((1 << 8) + 2) << 8) + 3) << 8) + 4)
145     {
146       fprintf (stderr, "Code badly compiled for this architecture\n");
147       fprintf (stderr, "Please set SMALLENDIAN and recompile\n");
148       exit (2);
149     }
150 }