[librsvg] Use mime types instead of content types



commit a22a60d54517b83882d02e8c0e058a8cabaa47b9
Author: Christian Persch <chpe gnome org>
Date:   Sat Jan 28 18:32:21 2012 +0100

    Use mime types instead of content types
    
    Instead of converting back and forth, just use the mime types
    directly.

 rsvg-base.c   |   16 ++++++------
 rsvg-image.c  |    9 ++-----
 rsvg-io.c     |   71 +++++++++++++++++++++++++++++++-------------------------
 rsvg-io.h     |    4 +-
 rsvg-styles.c |   22 ++++++++---------
 5 files changed, 62 insertions(+), 60 deletions(-)
---
diff --git a/rsvg-base.c b/rsvg-base.c
index 29bbccd..8d55a27 100644
--- a/rsvg-base.c
+++ b/rsvg-base.c
@@ -889,20 +889,20 @@ rsvg_processing_instruction (void *ctx, const xmlChar * target, const xmlChar *
                     if (value && value[0]) {
                         guint8 *style_data;
                         gsize style_data_len;
-                        char *content_type = NULL, *css_content_type;
+                        char *mime_type = NULL;
 
                         style_data = _rsvg_handle_acquire_data (handle,
                                                                 value,
-                                                                &content_type,
+                                                                &mime_type,
                                                                 &style_data_len,
                                                                 NULL);
-                        if (style_data && content_type) {
-                            css_content_type = g_content_type_from_mime_type ("text/css");
-                            if (g_content_type_is_a (content_type, css_content_type))
-                                rsvg_parse_cssbuffer (handle, (char *) style_data, style_data_len);
-                            g_free (css_content_type);
-                            g_free (content_type);
+                        if (style_data && 
+                            mime_type &&
+                            strcmp (mime_type, "text/css") == 0) {
+                            rsvg_parse_cssbuffer (handle, (char *) style_data, style_data_len);
                         }
+
+                        g_free (mime_type);
                         g_free (style_data);
                     }
                 }
diff --git a/rsvg-image.c b/rsvg-image.c
index b655974..e225a47 100644
--- a/rsvg-image.c
+++ b/rsvg-image.c
@@ -43,21 +43,19 @@ rsvg_cairo_surface_new_from_href (RsvgHandle *handle,
 {
     guint8 *data;
     gsize data_len;
-    char *content_type = NULL, *mime_type;
+    char *mime_type = NULL;
     GdkPixbufLoader *loader;
     GdkPixbuf *pixbuf = NULL;
     int res;
     cairo_surface_t *surface;
 
-    data = _rsvg_handle_acquire_data (handle, href, &content_type, &data_len, error);
+    data = _rsvg_handle_acquire_data (handle, href, &mime_type, &data_len, error);
     if (data == NULL)
         return NULL;
 
-    if (content_type) {
-        mime_type = g_content_type_get_mime_type (content_type);
+    if (mime_type) {
         loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error);
         g_free (mime_type);
-        g_free (content_type);
     } else {
         loader = gdk_pixbuf_loader_new ();
     }
@@ -67,7 +65,6 @@ rsvg_cairo_surface_new_from_href (RsvgHandle *handle,
         return NULL;
     }
 
-
     res = gdk_pixbuf_loader_write (loader, data, data_len, error);
     g_free (data);
 
diff --git a/rsvg-io.c b/rsvg-io.c
index e8a24ac..2cf0cdb 100644
--- a/rsvg-io.c
+++ b/rsvg-io.c
@@ -68,19 +68,20 @@ uri_decoded_copy (const char *part,
 static guint8 *
 rsvg_acquire_data_data (const char *uri,
                         const char *base_uri, 
-                        char **out_content_type,
+                        char **out_mime_type,
                         gsize *out_len,
                         GError **error)
 {
     const char *comma, *start, *end;
-    char *content_type = NULL;
+    char *mime_type;
     char *data;
     gsize data_len;
     gboolean base64 = FALSE;
 
     g_assert (out_len != NULL);
-
     g_assert (g_str_has_prefix (uri, "data:"));
+
+    mime_type = NULL;
     start = uri + 5;
     comma = strchr (start, ',');
 
@@ -95,7 +96,7 @@ rsvg_acquire_data_data (const char *uri,
         }
 
         if (end != start) {
-            content_type = uri_decoded_copy (start, end - start);
+            mime_type = uri_decoded_copy (start, end - start);
         }
     }
 
@@ -114,13 +115,10 @@ rsvg_acquire_data_data (const char *uri,
         data_len = 0;
     }
 
-    if (out_content_type) {
-        if (content_type)
-            *out_content_type = g_content_type_from_mime_type (content_type);
-        else
-            *out_content_type = NULL;
-    }
-    g_free (content_type);
+    if (out_mime_type)
+        *out_mime_type = mime_type;
+    else
+        g_free (mime_type);
 
     *out_len = data_len;
     return data;
@@ -157,7 +155,7 @@ rsvg_get_file_path (const gchar * filename, const gchar * base_uri)
 static guint8 *
 rsvg_acquire_file_data (const char *filename,
                         const char *base_uri,
-                        char **out_content_type,
+                        char **out_mime_type,
                         gsize *out_len,
                         GCancellable *cancellable,
                         GError **error)
@@ -166,6 +164,7 @@ rsvg_acquire_file_data (const char *filename,
     gchar *path, *data;
     GInputStream *stream;
     gsize len;
+    char *content_type;
     gboolean res;
 
     rsvg_return_val_if_fail (filename != NULL, NULL, error);
@@ -180,8 +179,10 @@ rsvg_acquire_file_data (const char *filename,
         return NULL;
     }
 
-    if (out_content_type) {
-        *out_content_type = g_content_type_guess (path, data, len, NULL);
+    if (out_mime_type &&
+        (content_type = g_content_type_guess (path, data, len, NULL))) {
+        *out_mime_type = g_content_type_get_mime_type (content_type);
+        g_free (content_type);
     }
 
     g_free (path);
@@ -193,7 +194,7 @@ rsvg_acquire_file_data (const char *filename,
 static GInputStream *
 rsvg_acquire_gvfs_stream (const char *uri, 
                           const char *base_uri, 
-                          char **out_content_type,
+                          char **out_mime_type,
                           GCancellable *cancellable,
                           GError **error)
 {
@@ -224,19 +225,22 @@ rsvg_acquire_gvfs_stream (const char *uri,
         return NULL;
     }
 
-    if (out_content_type) {
+    if (out_mime_type) {
         GFileInfo *file_info;
+        const char *content_type;
 
         file_info = g_file_input_stream_query_info (stream, 
                                                     G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                                                     cancellable,
                                                     NULL /* error */);
-        if (file_info) {
-            *out_content_type = g_strdup (g_file_info_get_content_type (file_info));
+        if (file_info &&
+            (content_type = g_file_info_get_content_type (file_info)))
+            *out_mime_type = g_content_type_get_mime_type (content_type);
+        else
+            *out_mime_type = NULL;
+
+        if (file_info)
             g_object_unref (file_info);
-        } else {
-            *out_content_type = NULL;
-        }
     }
 
     return G_INPUT_STREAM (stream);
@@ -245,7 +249,7 @@ rsvg_acquire_gvfs_stream (const char *uri,
 static guint8 *
 rsvg_acquire_gvfs_data (const char *uri,
                         const char *base_uri,
-                        char **out_content_type,
+                        char **out_mime_type,
                         gsize *out_len,
                         GCancellable *cancellable,
                         GError **error)
@@ -255,6 +259,7 @@ rsvg_acquire_gvfs_data (const char *uri,
     GError *err;
     gchar *data;
     gsize len;
+    char *content_type;
     gboolean res;
 
     file = g_file_new_for_uri (uri);
@@ -280,8 +285,10 @@ rsvg_acquire_gvfs_data (const char *uri,
         return NULL;
     }
 
-    if (out_content_type) {
-        *out_content_type = g_content_type_guess (uri, data, len, NULL);
+    if (out_mime_type &&
+        (content_type = g_content_type_guess (uri, data, len, NULL))) {
+        *out_mime_type = g_content_type_get_mime_type (content_type);
+        g_free (content_type);
     }
 
     *out_len = len;
@@ -291,7 +298,7 @@ rsvg_acquire_gvfs_data (const char *uri,
 guint8 *
 _rsvg_io_acquire_data (const char *href, 
                        const char *base_uri, 
-                       char **content_type,
+                       char **mime_type,
                        gsize *len,
                        GCancellable *cancellable,
                        GError **error)
@@ -309,12 +316,12 @@ _rsvg_io_acquire_data (const char *href,
         len = &llen;
 
     if (strncmp (href, "data:", 5) == 0)
-      return rsvg_acquire_data_data (href, NULL, content_type, len, error);
+      return rsvg_acquire_data_data (href, NULL, mime_type, len, error);
 
-    if ((data = rsvg_acquire_file_data (href, base_uri, content_type, len, cancellable, NULL)))
+    if ((data = rsvg_acquire_file_data (href, base_uri, mime_type, len, cancellable, NULL)))
       return data;
 
-    if ((data = rsvg_acquire_gvfs_data (href, base_uri, content_type, len, cancellable, error)))
+    if ((data = rsvg_acquire_gvfs_data (href, base_uri, mime_type, len, cancellable, error)))
       return data;
 
     return NULL;
@@ -323,7 +330,7 @@ _rsvg_io_acquire_data (const char *href,
 GInputStream *
 _rsvg_io_acquire_stream (const char *href, 
                          const char *base_uri, 
-                         char **content_type,
+                         char **mime_type,
                          GCancellable *cancellable,
                          GError **error)
 {
@@ -338,16 +345,16 @@ _rsvg_io_acquire_stream (const char *href,
     }
 
     if (strncmp (href, "data:", 5) == 0) {
-        if (!(data = rsvg_acquire_data_data (href, NULL, content_type, &len, error)))
+        if (!(data = rsvg_acquire_data_data (href, NULL, mime_type, &len, error)))
             return NULL;
 
         return g_memory_input_stream_new_from_data (data, len, (GDestroyNotify) g_free);
     }
 
-    if ((data = rsvg_acquire_file_data (href, base_uri, content_type, &len, cancellable, NULL)))
+    if ((data = rsvg_acquire_file_data (href, base_uri, mime_type, &len, cancellable, NULL)))
       return g_memory_input_stream_new_from_data (data, len, (GDestroyNotify) g_free);
 
-    if ((stream = rsvg_acquire_gvfs_stream (href, base_uri, content_type, cancellable, error)))
+    if ((stream = rsvg_acquire_gvfs_stream (href, base_uri, mime_type, cancellable, error)))
       return stream;
 
     return NULL;
diff --git a/rsvg-io.h b/rsvg-io.h
index 1f21469..43b8eff 100644
--- a/rsvg-io.h
+++ b/rsvg-io.h
@@ -28,14 +28,14 @@
 
 guint8* _rsvg_io_acquire_data (const char *uri,
                                const char *base_uri,
-                               char **content_type,
+                               char **mime_type,
                                gsize *len,
                                GCancellable *cancellable,
                                GError **error);
 
 GInputStream *_rsvg_io_acquire_stream (const char *uri,
                                        const char *base_uri,
-                                       char **content_type,
+                                       char **mime_type,
                                        GCancellable *cancellable,
                                        GError **error);
 
diff --git a/rsvg-styles.c b/rsvg-styles.c
index 6ceb627..5eab4f5 100644
--- a/rsvg-styles.c
+++ b/rsvg-styles.c
@@ -1164,30 +1164,28 @@ ccss_import_style (CRDocHandler * a_this,
     CSSUserData *user_data = (CSSUserData *) a_this->app_data;
     guint8 *stylesheet_data;
     gsize stylesheet_data_len;
-    char *content_type = NULL, *css_content_type;
+    char *mime_type = NULL;
 
     if (a_uri == NULL)
         return;
 
     stylesheet_data = _rsvg_handle_acquire_data (user_data->ctx,
                                                  (gchar *) cr_string_peek_raw_str (a_uri),
-                                                 &content_type,
+                                                 &mime_type,
                                                  &stylesheet_data_len,
                                                  NULL);
-    if (stylesheet_data == NULL)
+    if (stylesheet_data == NULL || 
+        mime_type == NULL || 
+        strcmp (mime_type, "text/css") != 0) {
+        g_free (stylesheet_data);
+        g_free (mime_type);
         return;
-
-    if (content_type) {
-        css_content_type = g_content_type_from_mime_type ("text/css");
-        if (g_content_type_is_a (content_type, css_content_type)) {
-            rsvg_parse_cssbuffer (user_data->ctx, (const char *) stylesheet_data,
-                                  stylesheet_data_len);
-        }
-        g_free (css_content_type);
-        g_free (content_type);
     }
 
+    rsvg_parse_cssbuffer (user_data->ctx, (const char *) stylesheet_data,
+                          stylesheet_data_len);
     g_free (stylesheet_data);
+    g_free (mime_type);
 }
 
 /* Parse an SVG transform string into an affine matrix. Reference: SVG



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]