summaryrefslogtreecommitdiff
path: root/src/cairo-scaled-font-subsets.c
diff options
context:
space:
mode:
authorMarek Kasik <mkasik@redhat.com>2014-01-09 17:28:32 +0100
committerAdrian Johnson <ajohnson@redneon.com>2014-01-10 20:51:08 +1030
commit97f6e2005d9cbc9c9dd7cc21445df7c08e084c83 (patch)
tree02512a45a78a4a14f1598db2bf79ab636c790acb /src/cairo-scaled-font-subsets.c
parentf81b1406755a1e63f0c44996ca9ce3802b1877ba (diff)
downloadcairo-97f6e2005d9cbc9c9dd7cc21445df7c08e084c83.tar.gz
font: Generate PDFs with correct font names
Escape PostScript names of loaded fonts. These can not contain white spaces and delimiter characters when saving them to a PostScript file or a PDF file.
Diffstat (limited to 'src/cairo-scaled-font-subsets.c')
-rw-r--r--src/cairo-scaled-font-subsets.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/cairo-scaled-font-subsets.c b/src/cairo-scaled-font-subsets.c
index e78e0c283..212176183 100644
--- a/src/cairo-scaled-font-subsets.c
+++ b/src/cairo-scaled-font-subsets.c
@@ -1256,4 +1256,44 @@ CLEANUP_HASH:
return status;
}
+cairo_int_status_t
+_cairo_escape_ps_name (char **ps_name)
+{
+ cairo_status_t status = CAIRO_STATUS_SUCCESS;
+
+ /* Ensure PS name is a valid PDF/PS name object. In PDF names are
+ * treated as UTF8 and non ASCII bytes, ' ', and '#' are encoded
+ * as '#' followed by 2 hex digits that encode the byte. By also
+ * encoding the characters in the reserved string we ensure the
+ * name is also PS compatible. */
+ if (*ps_name) {
+ static const char *reserved = "()<>[]{}/%#\\";
+ char buf[128]; /* max name length is 127 bytes */
+ char *src = *ps_name;
+ char *dst = buf;
+
+ while (*src && dst < buf + 127) {
+ unsigned char c = *src;
+ if (c < 0x21 || c > 0x7e || strchr (reserved, c)) {
+ if (dst + 4 > buf + 127)
+ break;
+
+ snprintf (dst, 4, "#%02X", c);
+ src++;
+ dst += 3;
+ } else {
+ *dst++ = *src++;
+ }
+ }
+ *dst = 0;
+ free (*ps_name);
+ *ps_name = strdup (buf);
+ if (*ps_name == NULL) {
+ status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
+ }
+ }
+
+ return status;
+}
+
#endif /* CAIRO_HAS_FONT_SUBSET */