]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb.cc
* ttftool/util.c:
[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
17 char *
18 pfb2pfa (Byte const *pfb, int length)
19 {
20   char *out = new char[1];
21   int olen = 0;
22
23   Byte const *p = pfb;
24   while (p < pfb + length)
25     {
26       if (*p++ != 128)
27         break;
28
29       Byte type = *p++;
30       if (type == 3)
31         break ;
32
33       unsigned seglen =
34         p[0] | (p[1] << 8)
35         | (p[2] << 16) | (p[3] << 24);
36
37       p += 4;
38       if (type == 1)
39         {
40           out = (char*)realloc (out, olen + seglen + 1);
41           char *outp = out + olen ;
42           memcpy (outp, p, seglen);
43           olen += seglen;
44           p += seglen;
45         }
46       else if (type == 2)
47         {
48           unsigned outlength = (seglen * 2) + (seglen / 32) + 2;
49
50           out = (char*)realloc (out, olen + outlength + 1);
51
52           char *outp = out + olen;
53           for (int i = seglen; i--;)
54             {
55               sprintf (outp, "%02x", *p++);
56               outp += 2;
57               if (!(i % 32))
58                 {
59                   *outp ++ = '\n';
60                 }
61             }
62
63           olen = outp - out;
64         }
65
66     }
67   out[olen] = 0;
68
69   return out;
70 }
71
72 LY_DEFINE (ly_pfb_to_pfa, "ly:pfb->pfa",
73            1, 0, 0, (SCM pfb_file_name),
74            "Convert the contents of a PFB file to PFA."
75            )
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 {
97   SCM_ASSERT_TYPE (scm_is_string (ttf_file_name), ttf_file_name,
98                    SCM_ARG1, __FUNCTION__, "string");
99   
100   String file_name = ly_scm2string (ttf_file_name);
101
102   Memory_out_stream stream;
103   create_type42 (file_name.to_str0 (), stream.get_file ());
104   SCM asscm = scm_from_locale_stringn (stream.get_string (),
105                                        stream.get_length ());
106
107   return asscm;
108 }