]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/commitdiff
example.c,Makefile.am: begin adding GdkPixbufModule parts.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 13:05:00 +0000 (09:05 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 13:05:00 +0000 (09:05 -0400)
Makefile.am
example.c

index e79da672051d5cc0228569a521e6c6c82e208fe2..c1e0a7d2dabd2ca5c3f4b43a1f83072da1e8e749 100644 (file)
@@ -2,7 +2,8 @@ bin_PROGRAMS = example
 example_CPPFLAGS = $(CAIRO_CFLAGS) \
                    $(GDKPIXBUF_CFLAGS) \
                    $(GTK4_CFLAGS) \
-                   $(LIBSVGTINY_CFLAGS)
+                   $(LIBSVGTINY_CFLAGS) \
+                   -DGDK_PIXBUF_ENABLE_BACKEND
 example_LDADD = $(CAIRO_LIBS) \
                 $(GDKPIXBUF_LIBS) \
                 $(GTK4_LIBS) \
index 74a66581f04bf04733795b8adc3ab9967a6c5da5..7c69182f1e0e914aa84b1e2b22ab97ffe0f96fc1 100644 (file)
--- a/example.c
+++ b/example.c
@@ -305,3 +305,85 @@ int main(int argc, char** argv) {
 
   return 0;
 }
+
+
+static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError **error) {
+  diagram_t* diagram;
+  cairo_t* cr = 0;
+
+  GdkPixbuf* pb;
+
+  diagram = svgtiny_diagram_from_file(fp, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
+  if (!diagram) {
+    return NULL;
+  }
+
+  cr = cairo_context_from_diagram(diagram);
+  if (!cr) {
+    svgtiny_free(diagram);
+    return NULL;
+  }
+
+  /* We're using the diagram width and height and not the viewport
+   * width/height for the image. This has the potential to create an
+   * image with a different size and aspect ratio than the viewport,
+   * but since our viewport is entirely made-up... I don't know. This
+   * relies on libsvgtiny being good at scaling/stretching/etc an SVG
+   * that may only have partial width/height/viewBox information.
+   */
+  pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
+                                  0,
+                                  0,
+                                  diagram->width,
+                                  diagram->height);
+
+
+  if (!pb) {
+    fprintf(stderr, "gdk_pixbuf_get_from_surface failed!\n");
+  }
+
+  return pb;
+}
+
+
+G_MODULE_EXPORT void fill_vtable(GdkPixbufModule* module);
+void fill_vtable(GdkPixbufModule* module) {
+  module->load = gdk_pixbuf_from_svg_file_stream;
+}
+
+G_MODULE_EXPORT void fill_info(GdkPixbufFormat *info);
+void fill_info(GdkPixbufFormat* info) {
+  /* Borrowed from librsvg-2.40.21 */
+  static const GdkPixbufModulePattern signature[] = {
+    {  " <svg",  "*    ", 100 },
+    {  " <!DOCTYPE svg",  "*             ", 100 },
+    { NULL, NULL, 0 }
+  };
+
+  /* I'm not sure if we should support gzipped svg here? */
+  static const gchar *mime_types[] = {
+    "image/svg+xml",
+    "image/svg",
+    "image/svg-xml",
+    "image/vnd.adobe.svg+xml",
+    "text/xml-svg",
+    "image/svg+xml-compressed",
+    NULL
+  };
+
+  static const gchar *extensions[] = {
+    "svg",
+    "svgz",
+    "svg.gz",
+    NULL
+  };
+
+  info->name        = "svg";
+  info->signature   = (GdkPixbufModulePattern*) signature;
+  info->description = "Scalable Vector Graphics";
+  info->mime_types  = (gchar**) mime_types;
+  info->extensions  = (gchar**) extensions;
+  info->flags       = GDK_PIXBUF_FORMAT_SCALABLE;
+  info->license     = "AGPL3";
+}
+