anthonycaccese / art-book-next-es

A simple theme for Batocera + EmulationStation based around the look of a coffee table book
44 stars 11 forks source link

Update logo SVGs to include a `viewBox` so that they display properly in a web browser #3

Closed pkegg closed 2 years ago

pkegg commented 2 years ago

Details

Script

The bash script is as follows - requires inskscape. To run, create the script in the logos directory and run it with bash. Ex bash ./update-viewbox.sh. You should be able to run it multiple times and it won't hurt anything.

NOTE: this script requires linux (tested on Ubuntu 20.04). It probably could be adapted to run on a mac if needed/desired (would need changes like: sed -i bak instead of sed -i, etc, but should be pretty simple - just let me know)

#! /bin/bash
set -e

for file in *.svg; do
  echo "file: $file"
  if ! grep viewBox "$file"; then
    echo "adding default viewBox"
    sed -i 's|xmlns=|viewBox="" xmlns=|g' "$file"

  else
    echo "viewbox exists"

    # comment the below line (`continue`) to update .svg's with existing viewBox attributes
    continue

    #If a viewBox attribute starts somewhere other than 0,0, it will mess up the calculation.  So just remove a viewbox and recalculate
    echo "viewbox already set in $file - resetting"
    sed -i "s|viewBox=\"[0-9 ]*\"|viewBox=\"\"|g" "$file"
  fi

  RAW_LINE="$(inkscape --query-all "$file" 2>/dev/null | head -1)"
  X1="$(echo "$RAW_LINE" | cut -d',' -f2| awk '{print int($1+0.5)}' )"
  Y1="$(echo "$RAW_LINE" | cut -d',' -f3| awk '{print int($1+0.5)}')"
  X2="$(echo "$RAW_LINE" | cut -d',' -f4| awk '{print int($1+0.5)}')"
  Y2="$(echo "$RAW_LINE" | cut -d',' -f5| awk '{print int($1+0.5)}')"
  echo "raw: $RAW_LINE"
  echo "x1: $X1 y1: $Y1 x2: $X2 y2: $Y2"
  echo "updating viewBox"
  sed -i "s|viewBox=\"[0-9 ]*\"|viewBox=\"$X1 $Y1 $X2 $Y2\"|g" $file
done