]> git.donarmstrong.com Git - lilypond.git/blob - lily/pdf-scheme.cc
add vcs lines to debian/control
[lilypond.git] / lily / pdf-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2011 Reinhold Kainhofer <reinhold@kainhofer.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <glib.h>
21 using namespace std;
22
23 #include "international.hh"
24 #include "warn.hh"
25 #include "lily-guile.hh"
26
27
28 LY_DEFINE (ly_encode_string_for_pdf, "ly:encode-string-for-pdf",
29            1, 0, 0, (SCM str),
30            "Encode the given string to either Latin1 (which is a subset of"
31            " the PDFDocEncoding) or if that's not possible to full UTF-16BE"
32            " with Byte-Order-Mark (BOM).")
33 {
34   LY_ASSERT_TYPE (scm_is_string, str, 1);
35   char *p = ly_scm2str0 (str);
36   char *g = NULL;
37   char const *charset="UTF-8"; // Input is ALWAYS UTF-8!
38   gsize bytes_written = 0;
39
40   /* First, try to convert to ISO-8859-1 (no encodings required). This will
41    * fail, if the string contains accented characters, so we do not check
42    * for errors. */
43   g = g_convert (p, -1, "ISO-8859-1", charset, 0, &bytes_written, 0);
44   /* If that fails, we have to resolve to full UTF-16BE */
45   if (!g) 
46     {
47       GError *e = NULL;
48       char *g_without_BOM = g_convert (p, -1,  "UTF-16BE", charset, 0, &bytes_written, &e);
49       if (e != NULL) 
50         {
51           warning (_f("Conversion of string `%s' to UTF-16be failed: %s", p, e->message));
52           g_error_free (e);
53         }
54       /* UTF-16BE allows/recommends a byte-order-mark (BOM) of two bytes 
55        * \xFE\xFF at the begin of the string. The pdfmark specification 
56        * requires it and depends on it to distinguish PdfDocEncoding from
57        * UTF-16BE. As g_convert does not automatically prepend this BOM 
58        * for UTF-16BE (only for UTF-16, which uses lower endian by default, 
59        * though), we have to prepend it manually. */
60       if (g_without_BOM) // conversion to UTF-16be might have failed (shouldn't!)
61         { 
62           g = new char[bytes_written+3];
63           char const *BOM = "\xFE\xFF";
64           strcpy (g, BOM);
65           memcpy (&g[2], g_without_BOM, bytes_written+1); // Copy string + \0
66           free (g_without_BOM);
67           bytes_written += 2;
68         }
69     }
70   free (p);
71
72   /* Convert back to SCM object and return it */
73   /* FIXME guile-2.0: With guile 2.0 the internal representation of a string
74    *                  has changed (char vector rather than binary bytes in 
75    *                  UTF-8). However, with guile 2.0, ly:encode-string-for-pdf
76    *                  is no longer needed and can be replaced by the new 
77    *                  (string->utf16 str 'big)
78    */
79   if (g) 
80     return scm_take_str (g, bytes_written); // scm_take_str eventually frees g!
81   else
82     return str;
83 }