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