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