]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb.cc
* lily/include/lily-guile.hh: compatibility glue for 1.6
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   
8 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "string.hh"
14 #include "lily-guile.hh"
15 #include "source-file.hh"
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_path),
74           "Convert the contents of a PFB file to PFA."
75           )
76 {
77   SCM_ASSERT_TYPE(scm_is_string (pfb_path), pfb_path,
78                   SCM_ARG1, __FUNCTION__, "string");
79
80   String path = ly_scm2string (pfb_path);
81   int len ; 
82   char *str = gulp_file (path, &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