extract-vector-icon.sh 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/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
16
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

# 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'"]'`
50
icon=`xmllint "$source" --xpath '//*[@id="'$id'"]' --noblanks`
51 52 53 54 55 56 57
# 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;

58 59 60 61
# Add !important to any object with label "color-important".
icon=`echo $icon | sed 's/<\([^<>]*\)style="\([^"]*\)fill:\([^;"]*\)\([^"]*\)"\([^<>]*\)inkscape:label="color-important"\([^>]*\)>/<\1style="\2fill:\3 !important\4"\5\6>/'`
icon=`echo $icon | sed 's/<\([^<>]*\)inkscape:label="color-important"\([^>]*\)style="\([^"]*\)fill:\([^;"]*\)\([^"]*\)"\([^<>]*\)>/<\1\2style="\3fill:\4 !important\5"\6>/'`

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
# 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"