From d54d09d050d51a1b5db542d9c4bb2e606a8b705c Mon Sep 17 00:00:00 2001 From: Simon Budig Date: Tue, 5 Jun 2007 14:26:42 +0000 Subject: [PATCH] plug-ins/common/tiff-load.c Implement the proposed scheme for storing 2007-06-05 Simon Budig * plug-ins/common/tiff-load.c * plug-ins/common/tiff-save.c: Implement the proposed scheme for storing international path names in TIFF files. svn path=/trunk/; revision=22712 --- ChangeLog | 6 ++++++ plug-ins/common/tiff-load.c | 38 ++++++++++++++++++++----------------- plug-ins/common/tiff-save.c | 38 ++++++++++++++++++++++++++++++++----- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 70d59f02ee..dea24d8308 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-06-05 Simon Budig + + * plug-ins/common/tiff-load.c + * plug-ins/common/tiff-save.c: Implement the proposed scheme for + storing international path names in TIFF files. + 2007-06-05 Sven Neumann * plug-ins/helpbrowser/dialog.c diff --git a/plug-ins/common/tiff-load.c b/plug-ins/common/tiff-load.c index 85bd5fd764..d501578fc9 100644 --- a/plug-ins/common/tiff-load.c +++ b/plug-ins/common/tiff-load.c @@ -1115,7 +1115,7 @@ static void load_paths (TIFF *tif, gint image) { guint16 id; - guint32 len, n_bytes, pos; + gsize len, n_bytes, pos; gchar *bytes, *name; guint32 *val32; guint16 *val16; @@ -1126,19 +1126,6 @@ load_paths (TIFF *tif, gint image) width = gimp_image_width (image); height = gimp_image_height (image); -#if 0 - /* TIFFTAG_CLIPPATH seems to be basically unused */ - - if (TIFFGetField (tif, TIFFTAG_CLIPPATH, &n_bytes, &bytes) && - TIFFGetField (tif, TIFFTAG_XCLIPPATHUNITS, &xclipunits) && - TIFFGetField (tif, TIFFTAG_YCLIPPATHUNITS, &yclipunits)) - { - /* Tiff clipping path */ - - g_printerr ("Tiff clipping path, %d - %d\n", xclipunits, yclipunits); - } -#endif - if (!TIFFGetField (tif, TIFFTAG_PHOTOSHOP, &n_bytes, &bytes)) return; @@ -1163,9 +1150,26 @@ load_paths (TIFF *tif, gint image) if (n_bytes - pos < len + 1) break; /* block not big enough */ - name = g_strndup (bytes + pos + 1, len); - if (!g_utf8_validate (name, -1, NULL)) - name = g_strdup ("imported path"); + /* + * do we have the UTF-marker? is it valid UTF-8? + * if so, we assume an utf-8 encoded name, otherwise we + * assume iso8859-1 + */ + name = bytes + pos + 1; + if (len >= 3 && + name[0] == '\xEF' && name[1] == '\xBB' && name[2] == '\xBF' && + g_utf8_validate (name, len - 3, NULL)) + { + name = g_strndup (name + 3, len - 3); + } + else + { + name = g_convert (name, len, "utf-8", "iso8859-1", NULL, NULL, NULL); + } + + if (!name) + name = g_strdup ("(imported path)"); + pos += len + 1; if (pos % 2) /* padding */ diff --git a/plug-ins/common/tiff-save.c b/plug-ins/common/tiff-save.c index 8bac545cb8..28f102bd47 100644 --- a/plug-ins/common/tiff-save.c +++ b/plug-ins/common/tiff-save.c @@ -465,18 +465,46 @@ save_paths (TIFF *tif, { GString *data; gchar *name, *nameend; - gint len, lenpos; + gsize len; + gint lenpos; gchar pointrecord[26] = { 0, }; + gchar *tmpname; + GError *err = NULL; data = g_string_new ("8BIM"); g_string_append_c (data, id / 256); g_string_append_c (data, id % 256); + /* + * - use iso8859-1 if possible + * - otherwise use UTF-8, prepended with \xef\xbb\xbf (Byte-Order-Mark) + */ name = gimp_vectors_get_name (vectors[v]); - len = g_utf8_strlen (name, 255); /* max. 255 chars in a pascal string */ - nameend = g_utf8_offset_to_pointer (name, len); - g_string_append_c (data, nameend - name); - g_string_append_len (data, name, nameend - name); + tmpname = g_convert (name, -1, "iso8859-1", "utf-8", NULL, &len, &err); + + if (tmpname && err == NULL) + { + g_string_append_c (data, MIN (len, 255)); + g_string_append_len (data, tmpname, MIN (len, 255)); + g_free (tmpname); + } + else + { + /* conversion failed, we fall back to UTF-8 */ + len = g_utf8_strlen (name, 255 - 3); /* need three marker-bytes */ + + nameend = g_utf8_offset_to_pointer (name, len); + len = nameend - name; /* in bytes */ + g_assert (len + 3 <= 255); + + g_string_append_c (data, len + 3); + g_string_append_len (data, "\xEF\xBB\xBF", 3); /* Unicode 0xfeff */ + g_string_append_len (data, name, len); + + if (tmpname) + g_free (tmpname); + } + if (data->len % 2) /* padding to even size */ g_string_append_c (data, 0); g_free (name);