]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb.cc
(LY_DEFINE): show file name when loading PFB.
[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 #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 LY_DEFINE (ly_ttf_to_pfa, "ly:ttf->pfa",
101            1, 0, 0, (SCM ttf_file_name),
102            "Convert the contents of a TTF file to Type42 PFA, returning it as "
103            " a string.")
104 {
105   SCM_ASSERT_TYPE (scm_is_string (ttf_file_name), ttf_file_name,
106                    SCM_ARG1, __FUNCTION__, "string");
107
108   String file_name = ly_scm2string (ttf_file_name);
109   if (be_verbose_global)
110     progress_indication ("[" + file_name);
111   
112   
113   Memory_out_stream stream;
114   create_type42 (file_name.to_str0 (), stream.get_file ());
115   SCM asscm = scm_from_locale_stringn (stream.get_string (),
116                                        stream.get_length ());
117
118   if (be_verbose_global)
119     progress_indication ("]");
120   
121   return asscm;
122 }
123
124
125
126 LY_DEFINE (ly_otf_to_cff, "ly:otf->cff",
127            1, 0, 0, (SCM otf_file_name),
128            "Convert the contents of a OTF file to CFF file, returning it as "
129            " a string.")
130 {
131   SCM_ASSERT_TYPE (scm_is_string (otf_file_name), otf_file_name,
132                    SCM_ARG1, __FUNCTION__, "string");
133
134   String file_name = ly_scm2string (otf_file_name);
135   if (be_verbose_global)
136     progress_indication ("[" + file_name);
137
138   FT_Face face = open_ft_face (file_name);
139   String table = get_otf_table (face, "CFF ");
140
141   SCM asscm = scm_from_locale_stringn ((char*) table.get_bytes (),
142                                        table.length ());
143
144   if (be_verbose_global)
145     progress_indication ("]");
146   
147   return asscm;
148 }