tools: scripts to extract a single SVG icon from a source file.

SVG sources contains many icons and each icon is extracted by its id.
This commit is contained in:
Jehan 2016-02-14 00:27:46 +01:00
parent 1427b2c795
commit b0af146ea3
3 changed files with 214 additions and 2 deletions

View File

@ -3,7 +3,9 @@
libgimpbase = $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la
if ENABLE_VECTOR_ICONS
invert_svg = invert-svg
svg_icon_tools = invert-svg compute-svg-viewbox
else
svg_icon_tools =
endif
if WITH_PDBGEN
@ -28,7 +30,7 @@ bin_PROGRAMS = gimptool-2.0
endif
noinst_PROGRAMS = test-clipboard $(invert_svg)
noinst_PROGRAMS = test-clipboard $(svg_icon_tools)
EXTRA_PROGRAMS = \
kernelgen
@ -53,6 +55,14 @@ invert_svg_SOURCES = invert-svg.c
invert_svg_LDADD = \
$(GLIB_LIBS) \
$(GIO_LIBS)
compute_svg_viewbox_SOURCES = compute-svg-viewbox.c
compute_svg_viewbox_CFLAGS = \
$(SVG_CFLAGS)
compute_svg_viewbox_LDADD = \
$(SVG_LIBS)
endif
AM_CPPFLAGS = \

View File

@ -0,0 +1,85 @@
/* compute-svg-viewbox.c
* Copyright (C) 2016 Jehan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <librsvg/rsvg.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
RsvgHandle *handle;
RsvgPositionData position_data;
RsvgDimensionData dimension;
gchar *endptr;
gchar *path;
gchar *id;
gint prev_x;
gint prev_y;
if (argc != 5)
{
fprintf (stderr, "Usage: compute-svg-viewbox path object_id x y\n");
return 1;
}
prev_x = strtol (argv[3], &endptr, 10);
if (endptr == argv[3])
{
fprintf (stderr, "Error: the third parameter must be an integer\n");
return 1;
}
prev_y = strtol (argv[4], &endptr, 10);
if (endptr == argv[4])
{
fprintf (stderr, "Error: the fourth parameter must be an integer\n");
return 1;
}
path = argv[1];
handle = rsvg_handle_new_from_file (path, NULL);
if (! handle)
{
fprintf (stderr, "Error: wrong path \"%s\".\n", path);
return 1;
}
id = g_strdup_printf("#%s", argv[2]);
if (! rsvg_handle_has_sub (handle, id))
{
fprintf (stderr, "Error: the id \"%s\" does not exist.\n", id);
g_object_unref (handle);
g_free (id);
return 1;
}
rsvg_handle_get_position_sub (handle, &position_data, id);
rsvg_handle_get_dimensions_sub (handle, &dimension, id);
if (dimension.width != dimension.height)
{
/* Right now, we are constraining all objects into square objects. */
fprintf (stderr, "WARNING: object \"%s\" has unexpected size %dx%d [pos: (%d, %d)].\n",
id, dimension.width, dimension.height,
position_data.x, position_data.y);
}
printf("viewBox=\"%d %d %d %d\"",
position_data.x + prev_x, position_data.y + prev_y,
dimension.width, dimension.height);
g_object_unref (handle);
g_free (id);
return 0;
}

117
tools/extract-vector-icon.sh Executable file
View File

@ -0,0 +1,117 @@
#!/bin/sh
# extract-vector-icon.sh
# Copyright (C) 2016 Jehan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script generates a new SVG file by extracting a single object by
# its id, from a source SVG, and updating the viewBox (canvas) size and
# position.
usage ()
{
printf "Usage: extract-vector-icon.sh source icon-name [width height]\n"
printf "Create the file 'icon-name.svg' from the \`source\` SVG.\n"
}
if [ "$#" != 2 ]; then
if [ "$#" != 4 ]; then
usage
exit 1
fi
fi
# The script is run from $(top_builddir)/icons/Symbolic/
compute_viewbox="$(pwd)/../../tools/compute-svg-viewbox"
source="$1"
id="$2"
if [ "$#" == 4 ]; then
# The expected display width/height for the image.
width="$3"
height="$4"
else
# We base the design of our scalable icons on 16x16 pixels.
width="16"
height="16"
fi
# Extract the icon code.
#icon=`xmllint "$source" --xpath '//*[local-name()="g" and @id="'$id'"]'`
icon=`xmllint "$source" --xpath '//*[@id="'$id'"]'`
# Get rid of any transform on the top node to help librsvg.
#icon=`echo $icon | sed 's/^\(<[^>]*\) transform="[^"]*"/\1/'`
if [ $? -ne 0 ]; then
>&2 echo "extract-vector-icon.sh: object id \"$id\" not found in \"$source\" ";
exit 1;
fi;
# The typical namespaces declared at start of a SVG made with Inkscape.
# Since we are not sure of what namespace will use the object XML, and
# since we don't want to end up with invalid XML, we just keep them all
# declared here.
svg_start='<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created by GIMP build. -->
<svg
xmlns:osb="http://www.openswatchbook.org/uri/2009/osb"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
version="1.1"
'
# Grab the defined color palette.
defs=`xmllint "$source" --xpath '//*[local-name()="defs"]'`
# Create a temporary SVG file with the main information.
svg_temp="`mktemp ./${id}-XXXX.svg`"
echo "$svg_start>$defs$icon</svg>" > $svg_temp
x=0
y=0
# In case the source SVG has a viewBox not starting at (0, 0), get
# the current origin coordinates.
#viewBox=`xmllint $svg_temp --xpath '/*[local-name()="svg"]/@viewBox'`
#if [ $? -eq 0 ]; then
# x=`echo $viewBox| sed 's/ *viewBox *= *"\([0-9]\+\) .*$/\1/'`
# y=`echo $viewBox| sed 's/ *viewBox *= *"[0-9]\+ \+\([0-9]\+\).*$/\1/'`
#fi;
# Compute the viewBox that we want to set to our generated SVG.
viewBox=`$compute_viewbox "$svg_temp" "$id" $x $y`
if [ $? -ne 0 ]; then
>&2 echo "extract-vector-icon.sh: error running \`$compute_viewbox "$tmp" "$id" $x $y\`.";
rm -f $svg_temp
exit 1;
fi;
rm -f $svg_temp
# The finale SVG file with properly set viewBox.
svg="$svg_start $viewBox"
if [ "$#" == 5 ]; then
svg="$svg
width=\"$width\"
height=\"$height\""
fi
svg="$svg>
<title>$id</title>
$defs
$icon
</svg>"
echo "$svg"