]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb.cc
Nitpick run.
[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                 *outp++ = '\n';
62             }
63
64           olen = outp - out;
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   SCM_ASSERT_TYPE (scm_is_string (pfb_file_name), pfb_file_name,
77                    SCM_ARG1, __FUNCTION__, "string");
78
79   String file_name = ly_scm2string (pfb_file_name);
80   int len;
81
82   if (be_verbose_global)
83     progress_indication ("[" + file_name);
84
85   char *str = gulp_file (file_name, &len);
86   char *pfa = pfb2pfa ((Byte *)str, len);
87
88   SCM pfa_scm = scm_makfrom0str (pfa);
89   free (pfa);
90   delete str;
91   if (be_verbose_global)
92     progress_indication ("]");
93
94   return pfa_scm;
95 }
96
97 LY_DEFINE (ly_otf_to_cff, "ly:otf->cff",
98            1, 0, 0, (SCM otf_file_name),
99            "Convert the contents of a OTF file to CFF file, returning it as "
100            " a string.")
101 {
102   SCM_ASSERT_TYPE (scm_is_string (otf_file_name), otf_file_name,
103                    SCM_ARG1, __FUNCTION__, "string");
104
105   String file_name = ly_scm2string (otf_file_name);
106   if (be_verbose_global)
107     progress_indication ("[" + file_name);
108
109   FT_Face face = open_ft_face (file_name);
110   String table = get_otf_table (face, "CFF ");
111
112   SCM asscm = scm_from_locale_stringn ((char *) table.get_bytes (),
113                                        table.length ());
114
115   if (be_verbose_global)
116     progress_indication ("]");
117
118   return asscm;
119 }
120