[librsvg: 9/13] Add a -s / --stylesheet option to rsvg-convert



commit 52ea6bd57a9080a6994814cfef11e82845c7db57
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Jan 24 20:47:28 2020 -0600

    Add a -s / --stylesheet option to rsvg-convert
    
    This takes a filename as input, like --stylesheet=foo.css

 rsvg-convert.1 |  3 +++
 rsvg-convert.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
---
diff --git a/rsvg-convert.1 b/rsvg-convert.1
index 9f515504..ddb828a6 100644
--- a/rsvg-convert.1
+++ b/rsvg-convert.1
@@ -48,6 +48,9 @@ preserved.
 .I "\-b \-\-background-color [black, white, #abccee, #aaa...]"
 Specify the background color.  If unspecified, none is used as the default.
 .TP
+.I "\-s \-\-stylesheet filename"
+Filename of a CSS stylesheet.
+.TP
 .I "\-\-base-uri uri"
 Specify the base URI for SVG files.  If unspecified, none is used as the default.
 .TP
diff --git a/rsvg-convert.c b/rsvg-convert.c
index ba044287..f57706e0 100644
--- a/rsvg-convert.c
+++ b/rsvg-convert.c
@@ -218,6 +218,7 @@ main (int argc, char **argv)
     int bVersion = 0;
     char *format = NULL;
     char *output = NULL;
+    char *stylesheet = NULL;
     char *export_id = NULL;
     int keep_aspect_ratio = FALSE;
     guint32 background_color = 0;
@@ -243,6 +244,9 @@ main (int argc, char **argv)
     double unscaled_width, unscaled_height;
     int scaled_width, scaled_height;
 
+    char *stylesheet_data = NULL;
+    gsize stylesheet_data_len = 0;
+
     char buffer[25];
     char *endptr;
     char *source_date_epoch;
@@ -279,6 +283,7 @@ main (int argc, char **argv)
          N_("whether to preserve the aspect ratio [optional; defaults to FALSE]"), NULL},
         {"background-color", 'b', 0, G_OPTION_ARG_STRING, &background_color_str,
          N_("set the background color [optional; defaults to None]"), N_("[black, white, #abccee, 
#aaa...]")},
+        {"stylesheet", 's', 0, G_OPTION_ARG_FILENAME, &stylesheet, N_("Filename of CSS stylesheet"), NULL},
         {"unlimited", 'u', 0, G_OPTION_ARG_NONE, &unlimited, N_("Allow huge SVG files"), NULL},
         {"keep-image-data", 0, 0, G_OPTION_ARG_NONE, &keep_image_data, N_("Keep image data"), NULL},
         {"no-keep-image-data", 0, 0, G_OPTION_ARG_NONE, &no_keep_image_data, N_("Don't keep image data"), 
NULL},
@@ -306,6 +311,14 @@ main (int argc, char **argv)
         return 0;
     }
 
+    if (stylesheet != NULL) {
+        error = NULL;
+        if (!g_file_get_contents (stylesheet, &stylesheet_data, &stylesheet_data_len, &error)) {
+            g_printerr (_("Error reading stylesheet: %s\n"), error->message);
+            exit (1);
+        }
+    }
+
     if (output != NULL) {
         output_file = fopen (output, "wb");
         if (!output_file) {
@@ -401,6 +414,13 @@ main (int argc, char **argv)
 
         g_assert (rsvg != NULL);
 
+        if (stylesheet_data != NULL) {
+            if (!rsvg_handle_set_stylesheet (rsvg, stylesheet_data, stylesheet_data_len, &error)) {
+                g_printerr (_("Error in stylesheet: %s\n"), error->message);
+                exit (1);
+            }
+        }
+
         rsvg_handle_set_dpi_x_y (rsvg, dpi_x, dpi_y);
 
         export_lookup_id = get_lookup_id_from_command_line (export_id);


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