]> git.donarmstrong.com Git - lilypond.git/blob - ttftool/ps.c
* ttftool/util.c:
[lilypond.git] / ttftool / ps.c
1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
2
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <malloc.h>
7 #include "types.h"
8 #include "proto.h"
9
10 #include "libc-extension.hh"
11
12 #define CHUNKSIZE 65534
13
14 #define NAMEOF(i) \
15         ((i)==0?\
16          ".notdef":\
17          ((postType==2)?\
18           ((gnt[i].type==0)?\
19            (gnt[i].name.index==0?NULL:macGlyphEncoding[gnt[i].name.index]):\
20            gnt[i].name.name):\
21           ((i)<258?macGlyphEncoding[i]:NULL)))
22
23
24 void
25 printPSFont (FILE * out, struct HeadTable *ht,
26              char **strings, int nglyphs, int postType,
27              struct PostTable *pt, struct GlyphName *gnt, int fd)
28 {
29   printPSHeader (out, ht, strings, pt);
30   printPSData (out, fd);
31   printPSTrailer (out, nglyphs, postType, gnt);
32 }
33
34 void
35 printPSHeader (FILE * out, struct HeadTable *ht,
36                char **strings, struct PostTable *pt)
37 {
38   fprintf (out, "%%!PS-TrueTypeFont\n");
39   if (pt->maxMemType42)
40     fprintf (out, "%%%%VMUsage: %ld %ld\n", pt->minMemType42,
41              pt->maxMemType42);
42   fprintf (out, "%d dict begin\n", 11);
43   fprintf (out, "/FontName /%s def\n", strings[6] ? strings[6] : "Unknown");
44   fprintf (out, "/Encoding StandardEncoding def\n");
45   fprintf (out, "/PaintType 0 def\n/FontMatrix [1 0 0 1 0 0] def\n");
46   fprintf (out, "/FontBBox [%ld %ld %ld %ld] def\n",
47            ht->xMin * 1000L / ht->unitsPerEm,
48            ht->yMin * 1000L / ht->unitsPerEm,
49            ht->xMax * 1000L / ht->unitsPerEm,
50            ht->yMax * 1000L / ht->unitsPerEm);
51   fprintf (out, "/FontType 42 def\n");
52   fprintf (out, "/FontInfo 8 dict dup begin\n");
53   fprintf (out, "/version (%d.%d) def\n",
54            ht->fontRevision.mantissa, ht->fontRevision.fraction);
55   if (strings[0])
56     {
57       fprintf (out, "/Notice (");
58       fputpss (strings[0], out);
59       fprintf (out, ") def\n");
60     }
61   if (strings[4])
62     {
63       fprintf (out, "/FullName (");
64       fputpss (strings[4], out);
65       fprintf (out, ") def\n");
66     }
67   if (strings[1])
68     {
69       fprintf (out, "/FamilyName (");
70       fputpss (strings[1], out);
71       fprintf (out, ") def\n");
72     }
73   fprintf (out, "/isFixedPitch %s def\n",
74            pt->isFixedPitch ? "true" : "false");
75   fprintf (out, "/UnderlinePosition %ld def\n",
76            pt->underlinePosition * 1000L / ht->unitsPerEm);
77   fprintf (out, "/UnderlineThickness %ld def\n",
78            pt->underlineThickness * 1000L / ht->unitsPerEm);
79   fprintf (out, "end readonly def\n");
80 }
81
82 void
83 printPSData (FILE * out, int fd)
84 {
85   static char xdigits[] = "0123456789ABCDEF";
86
87   unsigned char *buffer;
88   int i, j;
89
90   surely_lseek (fd, 0, SEEK_SET);
91
92   buffer = mymalloc (CHUNKSIZE);
93
94   fprintf (out, "/sfnts [");
95   for (;;)
96     {
97       i = read (fd, buffer, CHUNKSIZE);
98       if (i == 0)
99         break;
100       fprintf (out, "\n<");
101       for (j = 0; j < i; j++)
102         {
103           if (j != 0 && j % 36 == 0)
104             putc ('\n', out);
105           /* fprintf(out,"%02X",(int)buffer[j]) is too slow */
106           putc (xdigits[(buffer[j] & 0xF0) >> 4], out);
107           putc (xdigits[buffer[j] & 0x0F], out);
108         }
109       fprintf (out, "00>");     /* Adobe bug? */
110       if (i < CHUNKSIZE)
111         break;
112     }
113   fprintf (out, "\n] def\n");
114   free (buffer);
115 }
116
117 void
118 printPSTrailer (FILE * out, int nglyphs, int postType, struct GlyphName *gnt)
119 {
120   int i, n;
121   char *name;
122
123   fprintf (out, "/CharStrings %d dict dup begin\n", nglyphs);
124   switch (postType)
125     {
126     case 2:
127       for (n = i = 0; i < nglyphs; i++)
128         {
129           if (n != 0 && n % 4 == 0)
130             fprintf (out, "\n");
131           name = NAMEOF (i);
132           if (name)
133             {
134               fprintf (out, "/%s %d def ", name, i);
135               n++;
136             }
137         }
138       break;
139     default:
140       if (postType != 1)
141         {
142           if (verbosity > -2)
143             fprintf (stderr,
144                      "No glyph name table; assuming MacGlyphEncoding\n");
145         }
146       for (i = 0; i < 258 && i < nglyphs; i++)
147         {
148           fprintf (out, "/%s %d def ", macGlyphEncoding[i], i);
149           if (i != 0 && i % 4 == 0)
150             fprintf (out, "\n");
151         }
152       break;
153     }
154   fprintf (out, "end readonly def\n");
155   fprintf (out, "FontName currentdict end definefont pop\n");
156 }