- if (!g) {
- char *g_without_BOM = g_convert (p, -1, "UTF-16BE", charset, 0, &bytes_written, 0);
- /* prepend the BOM manually, g_convert doesn't do it! */
- g = new char[bytes_written+3];
- g[0] = (char)254;
- g[1] = (char)255;
- memcpy (&g[2], g_without_BOM, bytes_written+1); // Copy string + \0
- free (g_without_BOM);
- bytes_written += 2;
- }
+ if (!g)
+ {
+ GError *e = NULL;
+ char *g_without_BOM = g_convert (p, -1, "UTF-16BE", charset, 0, &bytes_written, &e);
+ if (e != NULL)
+ {
+ warning (_f ("Conversion of string `%s' to UTF-16be failed: %s", p, e->message));
+ g_error_free (e);
+ }
+ /* UTF-16BE allows/recommends a byte-order-mark (BOM) of two bytes
+ * \xFE\xFF at the begin of the string. The pdfmark specification
+ * requires it and depends on it to distinguish PdfDocEncoding from
+ * UTF-16BE. As g_convert does not automatically prepend this BOM
+ * for UTF-16BE (only for UTF-16, which uses lower endian by default,
+ * though), we have to prepend it manually. */
+ if (g_without_BOM) // conversion to UTF-16be might have failed (shouldn't!)
+ {
+ g = (char *)malloc ( sizeof (char) * (bytes_written + 3));
+ char const *BOM = "\xFE\xFF";
+ strcpy (g, BOM);
+ memcpy (&g[2], g_without_BOM, bytes_written + 1); // Copy string + \0
+ g_free (g_without_BOM);
+ bytes_written += 2;
+ }
+ }