]> git.donarmstrong.com Git - lilypond.git/blob - lily/pfb-scheme.cc
e718a13bf079f36c89708aebf65ddbcf1aa98611
[lilypond.git] / lily / pfb-scheme.cc
1
2 #include "international.hh"
3 #include "program-option.hh"
4 #include "source-file.hh"
5 #include "memory-stream.hh"
6 #include "open-type-font.hh"
7 #include "main.hh"
8 #include "warn.hh"
9
10 LY_DEFINE (ly_type1_2_pfa, "ly:type1->pfa",
11            1, 0, 0, (SCM type1_file_name),
12            "Convert the contents of a Type@tie{}1 font in PFB format"
13            " to PFA format.  If the file is already in PFA format,"
14            " pass through it.")
15 {
16   LY_ASSERT_TYPE (scm_is_string, type1_file_name, 1);
17
18   string file_name = ly_scm2string (type1_file_name);
19
20   debug_output ("[" + file_name); // start message on a new line
21
22   vector<char> type1_string = gulp_file (file_name, 0);
23   SCM pfa_scm;
24
25   if ((Byte) type1_string[0] == 0x80)
26     {
27       /* The file is in PFB format. Convert it to PFA format. */
28       char *pfa = pfb2pfa ((Byte *) &type1_string[0],
29                            (int) type1_string.size ());
30       pfa_scm = scm_from_latin1_string (pfa);
31       free (pfa);
32     }
33   else
34     {
35       /* The file is in PFA format. Pass it through. */
36       type1_string.push_back(0);
37       pfa_scm = scm_from_latin1_string (&type1_string[0]);
38     }
39
40   debug_output ("]", false);
41
42   return pfa_scm;
43 }
44
45 LY_DEFINE (ly_otf_2_cff, "ly:otf->cff",
46            1, 1, 0, (SCM otf_file_name, SCM idx),
47            "Convert the contents of an OTF file to a CFF file,"
48            " returning it as a string.  The optional"
49            " @var{idx} argument is useful for OpenType/CFF collections (OTC)"
50            " only; it specifies the font index within the OTC.  The default"
51            " value of @var{idx} is@tie{}0.")
52 {
53   LY_ASSERT_TYPE (scm_is_string, otf_file_name, 1);
54
55   int i = 0;
56   if (!SCM_UNBNDP (idx))
57     {
58       LY_ASSERT_TYPE (scm_is_integer, idx, 2);
59       i = scm_to_int (idx);
60       if (i < 0)
61         {
62           warning (_ ("font index must be non-negative, using index 0"));
63           i = 0;
64         }
65     }
66
67   string file_name = ly_scm2string (otf_file_name);
68   debug_output ("[" + file_name); // start message on a new line
69
70   FT_Face face;
71   /* check whether font index is valid */
72   if (i > 0)
73     {
74       face = open_ft_face (file_name, -1);
75       if (i >= face->num_faces)
76         {
77           warning (_f ("font index %d too large for font `%s', using index 0",
78                        i, file_name.c_str ()));
79           i = 0;
80         }
81       FT_Done_Face (face);
82     }
83
84   face = open_ft_face (file_name, i);
85   string table = get_otf_table (face, "CFF ");
86
87   SCM asscm = scm_from_latin1_stringn ((char *) table.data (),
88                                        table.length ());
89   FT_Done_Face (face);
90
91   debug_output ("]", false);
92
93   return asscm;
94 }