]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb.cc
* lily/pfb.cc (LY_DEFINE): add cast.
[lilypond.git] / lily / pfb.cc
1 /*
2   pfb.cc -- implement pfb conversion.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <cstdlib>
10 #include <cstdio>
11 #include <cstring>
12
13 #include "source-file.hh"
14 #include "memory-stream.hh"
15 #include "ttftool.h"
16 #include "open-type-font.hh"
17
18 char *
19 pfb2pfa (Byte const *pfb, int length)
20 {
21   char *out = new char[1];
22   int olen = 0;
23
24   Byte const *p = pfb;
25   while (p < pfb + length)
26     {
27       if (*p++ != 128)
28         break;
29
30       Byte type = *p++;
31       if (type == 3)
32         break;
33
34       unsigned seglen
35         = p[0] | (p[1] << 8)
36         | (p[2] << 16) | (p[3] << 24);
37
38       p += 4;
39       if (type == 1)
40         {
41           out = (char *)realloc (out, olen + seglen + 1);
42           char *outp = out + olen;
43           memcpy (outp, p, seglen);
44           olen += seglen;
45           p += seglen;
46         }
47       else if (type == 2)
48         {
49           unsigned outlength = (seglen * 2) + (seglen / 32) + 2;
50
51           out = (char *)realloc (out, olen + outlength + 1);
52
53           char *outp = out + olen;
54           for (int i = seglen; i--;)
55             {
56               sprintf (outp, "%02x", *p++);
57               outp += 2;
58               if (! (i % 32))
59                 {
60                   *outp++ = '\n';
61                 }
62             }
63
64           olen = outp - out;
65         }
66
67     }
68   out[olen] = 0;
69
70   return out;
71 }
72
73 LY_DEFINE (ly_pfb_to_pfa, "ly:pfb->pfa",
74            1, 0, 0, (SCM pfb_file_name),
75            "Convert the contents of a PFB file to PFA.")
76 {
77   SCM_ASSERT_TYPE (scm_is_string (pfb_file_name), pfb_file_name,
78                    SCM_ARG1, __FUNCTION__, "string");
79
80   String file_name = ly_scm2string (pfb_file_name);
81   int len;
82   char *str = gulp_file (file_name, &len);
83   char *pfa = pfb2pfa ((Byte *)str, len);
84
85   SCM pfa_scm = scm_makfrom0str (pfa);
86   free (pfa);
87   delete str;
88   return pfa_scm;
89 }
90
91 LY_DEFINE (ly_ttf_to_pfa, "ly:ttf->pfa",
92            1, 0, 0, (SCM ttf_file_name),
93            "Convert the contents of a TTF file to Type42 PFA, returning it as "
94            " a string.")
95 {
96   SCM_ASSERT_TYPE (scm_is_string (ttf_file_name), ttf_file_name,
97                    SCM_ARG1, __FUNCTION__, "string");
98
99   String file_name = ly_scm2string (ttf_file_name);
100
101   Memory_out_stream stream;
102   create_type42 (file_name.to_str0 (), stream.get_file ());
103   SCM asscm = scm_from_locale_stringn (stream.get_string (),
104                                        stream.get_length ());
105
106   return asscm;
107 }
108
109
110
111 LY_DEFINE (ly_otf_to_cff, "ly:otf->cff",
112            1, 0, 0, (SCM otf_file_name),
113            "Convert the contents of a OTF file to CFF file, returning it as "
114            " a string.")
115 {
116   SCM_ASSERT_TYPE (scm_is_string (otf_file_name), otf_file_name,
117                    SCM_ARG1, __FUNCTION__, "string");
118
119   String file_name = ly_scm2string (otf_file_name);
120
121   FT_Face face = open_ft_face (file_name);
122   String table = get_otf_table (face, "CFF ");
123
124   SCM asscm = scm_from_locale_stringn ((char*) table.get_bytes (),
125                                        table.length ());
126
127   return asscm;
128 }