]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb.cc
(LY_DEFINE): progress indication for opening ttf and
[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   char *str = gulp_file (file_name, &len);
85   char *pfa = pfb2pfa ((Byte *)str, len);
86
87   SCM pfa_scm = scm_makfrom0str (pfa);
88   free (pfa);
89   delete str;
90   return pfa_scm;
91 }
92
93 LY_DEFINE (ly_ttf_to_pfa, "ly:ttf->pfa",
94            1, 0, 0, (SCM ttf_file_name),
95            "Convert the contents of a TTF file to Type42 PFA, returning it as "
96            " a string.")
97 {
98   SCM_ASSERT_TYPE (scm_is_string (ttf_file_name), ttf_file_name,
99                    SCM_ARG1, __FUNCTION__, "string");
100
101   String file_name = ly_scm2string (ttf_file_name);
102   if (be_verbose_global)
103     progress_indication ("[" + file_name);
104   
105   
106   Memory_out_stream stream;
107   create_type42 (file_name.to_str0 (), stream.get_file ());
108   SCM asscm = scm_from_locale_stringn (stream.get_string (),
109                                        stream.get_length ());
110
111   if (be_verbose_global)
112     progress_indication ("]");
113   
114   return asscm;
115 }
116
117
118
119 LY_DEFINE (ly_otf_to_cff, "ly:otf->cff",
120            1, 0, 0, (SCM otf_file_name),
121            "Convert the contents of a OTF file to CFF file, returning it as "
122            " a string.")
123 {
124   SCM_ASSERT_TYPE (scm_is_string (otf_file_name), otf_file_name,
125                    SCM_ARG1, __FUNCTION__, "string");
126
127   String file_name = ly_scm2string (otf_file_name);
128   if (be_verbose_global)
129     progress_indication ("[" + file_name);
130
131   FT_Face face = open_ft_face (file_name);
132   String table = get_otf_table (face, "CFF ");
133
134   SCM asscm = scm_from_locale_stringn ((char*) table.get_bytes (),
135                                        table.length ());
136
137   if (be_verbose_global)
138     progress_indication ("]");
139   
140   return asscm;
141 }