User Tools

Site Tools


software:inkscape_scripts

inkscape scripts

This page contains some scripts which can be used with inkscape.

make path objects

As can be seen in the preamble at the code below, importing inkscape .svg files in libre office or converting them to pdf causes problems: Libre office chops off part of texts and svg to pdf converted files do not show dashed lines correctly. This script helps converting text and dashed line objects to paths.

#!/bin/bash
# This script converts all .svg files in a directory to
# .svg files which have their strokes and text converted
# to path.
# Conversion to path is necessary if files are imported
# in libre office writer or converted to pdf.
# Without conversion, files have incorrect dashed lines
# and text appears to be partly chopped off sometimes.
# 
# The script also converts all processed .svg files to
# .pdf files and merges them into one .pdf file.
#
# This script expects inkscape and ghostscript to be
# installed.
#
# Author: Marc Nijdam
# Date: 8th of May 2016
 
 
# Directory containing .svg files:
INPD="$HOME/documents/inkscape/project1/"
# Directory in ${INPD} where to write the converted files to:
SFX="proxy"
# File name for merged pdf files:
OUTF="all_merged_svg_files.pdf"
# Filter for files
SLCT="*.svg"
 
# Further initialization
OUTD="${INPD}${SFX}/"
FILES=""
CMND=""
 
if [ -d ${INPD}${SFX} ]
then
  cd ${INPD}${SFX} || exit 1
  rm -f ${SLCT}
  cd ${INPD}
  for f in ${INPD}${SLCT}
  do
    TMP=$(basename $f)
    DNM=$(dirname $f)
    NF=${TMP%.*}_${SFX}${SLCT#"*"}
    echo "Copying ${TMP} to ${NF} and analyzing file..."
    cp ${f} "${DNM}/${SFX}/${NF}"
 
    # Extract the remaining part after the first occurrence of 'font-style',
    # then extract the part between double quotes after 'id='
    idt=$(sed '1,/style=\"font-style/d' "${DNM}/${SFX}/${NF}" | sed -n '/id=/{p;q;}' | sed 's/.*"\(.*\)"[^"]*$/\1/')
    if [ -n "$idt" ]; then
      echo "   First text object ID found: ${idt}"
      CMND=" --select=$idt --verb EditSelectSameObjectType --verb ObjectToPath"
    else
      echo "   Did not found any text object ID at all."        
    fi
 
    echo -n "   Scanning for path object IDs"
    # Copy the svg file into variable
    svg="$(cat ${DNM}/${SFX}/${NF})"
 
    # Strip everything until after the first appearance of '<path':
    # grep $svg first to prevent the subsequent substring removal to freeze
    # in case of embedded images in svg files.
    svg=$(echo $svg | grep -oP '<path.*')
    svg="${svg#*<path}"
 
    # As long as the string is not empty, analyse the current 'path' tag.
    # When analysed, strip everything until after the first appearance of '<path'.
    # If there is no remaining '<path' in the string, then empty the string and
    # continue processing.
    while [ -n "$svg" ]; do
      # $svg starts with <path, then chop off to be shortest />
      # http://wiki.bash-hackers.org/syntax/pe#substring_removal
      pid=${svg%%/>*}
      # if the string contains sodipodi and the stroke-dasharray:[0-9] text, then
      # extract the part between double quotes after 'id='
      mln=$(echo $pid | grep sodipodi | grep -E "stroke-dasharray:[0-9]" | grep -Po 'id="\K.*?(?=")')
      if [ -n "$mln" ]; then
        CMND="$CMND --select=$mln --verb=StrokeToPath"
        echo -n "X"
      else
        echo -n "."
      fi
 
      # check if $svg still contains '<path', if yes, cut off everything until after the next first <path:
      if [[ $svg == *"<path"* ]]; then
        # Strip everything until after the first appearance of '<path':
        # grep $svg first to prevent the subsequent substring removal to freeze
        # in case of embedded images in svg files.
        svg=$(echo $svg | grep -oP '<path.*')
        svg="${svg#*<path}"
      else
        svg=""
      fi
    done
 
    if [ -n "$CMND" ]; then
      echo -e -n "\n   Converting to path..."
      /usr/bin/inkscape $CMND --verb FileSave --verb FileQuit ${DNM}/${SFX}/${NF} &> /dev/null
      echo " finished."
    else
      echo -e -n "\n   No path or text ID found, skipping path conversion."
    fi
    /usr/bin/inkscape -f "${DNM}/${SFX}/${NF}" -A "${DNM}/${SFX}/${NF}".pdf
    FILES="${FILES} ${DNM}/${SFX}/${NF}.pdf"
  done
  echo -n "Creating ${INPD}${OUTF}, containing all svg files..."
  /usr/bin/gs -dBATCH -sPAPERSIZE=a4 -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=${INPD}${OUTF} $FILES
  rm $FILES
  echo " done."
else
  echo -e "Please create a placeholder directory '${SFX}' where\nthe converted files can be stored, then restart script..."
fi
software/inkscape_scripts.txt · Last modified: 2016/05/11 21:47 by admin