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