From b3f201486360ad2ffeb7d1ac24d14972d850f8ca Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Mon, 30 Aug 2010 01:53:59 -0300 Subject: [PATCH] Split gitsch2ppm into its constituents. - scripts/gitsch2ps: generate PS files for KiCad schematics in git - scripts/normalizeschps: normalize eeschema Postscript - scripts/schps2ppm: generate PPM files from normalized Eeschema Postscript - scripts/gitsch2ppm: wrapper and cache manager for the above scripts --- scripts/gitsch2ppm | 98 ++++++++++-------------------------------- scripts/gitsch2ps | 81 ++++++++++++++++++++++++++++++++++ scripts/normalizeschps | 59 +++++++++++++++++++++++++ scripts/schps2ppm | 53 +++++++++++++++++++++++ 4 files changed, 216 insertions(+), 75 deletions(-) create mode 100755 scripts/gitsch2ps create mode 100755 scripts/normalizeschps create mode 100755 scripts/schps2ppm diff --git a/scripts/gitsch2ppm b/scripts/gitsch2ppm index e761680..221e2fd 100755 --- a/scripts/gitsch2ppm +++ b/scripts/gitsch2ppm @@ -12,36 +12,6 @@ # -RES=1280x850 -LINEWIDTH=120 - - -ps2ppm() -{ - X=`echo $RES | sed 's/x.*//'` - Y=`echo $RES | sed 's/.*x//'` - IRES=${Y}x$X - res=`expr 72 \* $X / 800` - - ( cat <"${1%.ps}.ppm" -} - - usage() { cat <&2 @@ -59,7 +29,9 @@ EOF cache=false keep=false -sanitize=true +res= +sanitize= +width= while true; do case "$1" in -c) cache=true @@ -67,13 +39,12 @@ while true; do -k) keep=true shift;; -r) [ -z "$2" ] && usage - RES="$2" + res="-r $2" shift 2;; - -S) sanitize=`PATH="$PATH":\`dirname "$0"\` which sanitize-profile` - [ "$sanitize" = "${sanitize#/}" ] && sanitize=`pwd`/"$sanitize" + -S) sanitize=-S shift;; -w) [ -z "$2" ] && usage - LINEWIDTH="$2" + width="-r $2" shift 2;; -*) usage;; @@ -82,53 +53,30 @@ while true; do esac done -[ ! -z "$3" -a -z "$5" ] || usage -dir="$1" -schem="$2" -sdir=`dirname "$schem"` -if [ -z "$4" ]; then - commit=HEAD - outdir="$3" -else - commit="$3" - outdir="$4" -fi +tmp=`pwd`/_gitsch2ppm +eval outdir=\${$#} -[ "$dir" != "${dir#/}" ] || dir=`pwd`/$dir - -[ "$commit" != HEAD -o -f "$dir/$schem" ] || usage -[ -d "$dir/.git" ] || usage - -tmp="$dir/../_gitsch2ppm" -sch="$tmp/$sdir" +# with thanks to http://www.faqs.org/faqs/unix-faq/faq/part2/section-12.html +argv= +while [ $# -gt 1 ]; do + argv="$argv \"$1\"" + shift +done +eval set x "$argv" +shift if ! $cache; then rm -rf "$tmp" rm -rf "$outdir" - - git clone -s -n "$dir/.git" "$tmp" || exit - ( cd "$tmp" && git checkout -q "$commit"; ) || exit - - if [ ! -f "$tmp/$schem" ]; then - echo "$schem not found (checked out into $tmp)" 1>&2 - exit 1 - fi - - ( - cd "$sch" || exit - rm -f *.ps *.ppm - $sanitize "$tmp/${schem%.sch}.pro" || - exit - eeschema --plot "$tmp/$schem" - ) || exit + gitsch2ps $sanitize "$@" "$tmp" + for n in "$tmp"/*.ps; do + normalizeschps $width "$n" + done fi -for n in "$sch"/*.ps; do - ps2ppm "$n" +mkdir -p "$outdir" +for n in "$tmp"/*.ps; do + schps2ppm $res "$n" "$outdir/`basename \"${n%.ps}.ppm\"`" done -mkdir -p "$outdir" - -mv "$sch"/*.ppm "$outdir" - $keep || rm -rf "$tmp" diff --git a/scripts/gitsch2ps b/scripts/gitsch2ps new file mode 100755 index 0000000..6245fe0 --- /dev/null +++ b/scripts/gitsch2ps @@ -0,0 +1,81 @@ +#!/bin/sh +# +# gitsch2ps - Generate PS files for KiCad schematics in git +# +# Written 2010 by Werner Almesberger +# Copyright 2010 Werner Almesberger +# +# 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 2 of the License, or +# (at your option) any later version. +# + + +usage() +{ + cat <&2 +usage: $0 [options] top-dir top-schem [commit] outdir + + -S sanitize the KiCad profile +EOF + exit 1 +} + + +sanitize=true +while true; do + case "$1" in + -S) sanitize=`PATH="$PATH":\`dirname "$0"\` which sanitize-profile` + [ "$sanitize" = "${sanitize#/}" ] && sanitize=`pwd`/"$sanitize" + shift;; + -*) + usage;; + *) + break;; + esac +done + +[ ! -z "$3" -a -z "$5" ] || usage +dir="$1" +schem="$2" +sdir=`dirname "$schem"` +if [ -z "$4" ]; then + commit=HEAD + outdir="$3" +else + commit="$3" + outdir="$4" +fi + +[ "$dir" != "${dir#/}" ] || dir=`pwd`/$dir + +[ "$commit" != HEAD -o -f "$dir/$schem" ] || usage +[ -d "$dir/.git" ] || usage + +tmp="$dir/../_gitsch2ps" +sch="$tmp/$sdir" + +rm -rf "$tmp" +rm -rf "$outdir" + +git clone -s -n "$dir/.git" "$tmp" || exit +( cd "$tmp" && git checkout -q "$commit"; ) || exit + +if [ ! -f "$tmp/$schem" ]; then + echo "$schem not found (checked out into $tmp)" 1>&2 + exit 1 +fi + +( + cd "$sch" || exit + rm -f *.ps + $sanitize "$tmp/${schem%.sch}.pro" || + exit + eeschema --plot "$tmp/$schem" +) || exit + +mkdir -p "$outdir" +mv "$sch"/*.ps "$outdir" + +rm -rf "$tmp" diff --git a/scripts/normalizeschps b/scripts/normalizeschps new file mode 100755 index 0000000..86601ad --- /dev/null +++ b/scripts/normalizeschps @@ -0,0 +1,59 @@ +#!/bin/sh +# +# normalizeschps - Normalize eeschema Postscript +# +# Written 2010 by Werner Almesberger +# Copyright 2010 Werner Almesberger +# +# 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 2 of the License, or +# (at your option) any later version. +# + + +LINEWIDTH=120 + + +usage() +{ + cat <&2 +usage: $0 [options] file ... + + -w points Postscript line width (default: $LINEWIDTH) +EOF + exit 1 +} + + +while true; do + case "$1" in + -w) [ -z "$2" ] && usage + LINEWIDTH="$2" + shift 2;; + -*) + usage;; + *) + break;; + esac +done + +for n in "$@"; do + sed -i ' + 1c%!PS-Adobe-3.0\ + currentdict /DidNormalize known not { \ + /setlinewidth { '$LINEWIDTH' 2 copy lt { exch } if pop \ + setlinewidth } bind def \ + /rectfill { rectstroke } bind def \ + /DidNormalize true def \ + } if \ + gsave + /%%DocumentMedia: A4.*/a-20 -10 translate + /%%DocumentMedia: A3.*/{s/A3/A4/;a-20 -10 translate 0.70 dup scale + } + /%%DocumentMedia: A2.*/{s/A2/A4/;a-18 -12 translate 0.49 dup scale + } + $agrestore' "$n" +done + +# /%%Orientation: Landscape/d' "$n" diff --git a/scripts/schps2ppm b/scripts/schps2ppm new file mode 100755 index 0000000..d402dfb --- /dev/null +++ b/scripts/schps2ppm @@ -0,0 +1,53 @@ +#!/bin/sh +# +# schps2ppm - Generate PPM files from normalized Eeschema Postscript +# +# Written 2010 by Werner Almesberger +# Copyright 2010 Werner Almesberger +# +# 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 2 of the License, or +# (at your option) any later version. +# + + +RES=1280x850 + + +usage() +{ + cat <&2 +usage: $0 [options] [file.ps [file.ppm]] + + -r XxY image resolution (default: $RES) +EOF + exit 1 +} + + +while true; do + case "$1" in + -r) [ -z "$2" ] && usage + RES="$2" + shift 2;; + -*) + usage;; + *) + break;; + esac +done + +[ ! -z "$3" ] && usage +in=${1:--} +out=${2:-/dev/stdout} + +X=`echo $RES | sed 's/x.*//'` +Y=`echo $RES | sed 's/.*x//'` +IRES=${Y}x$X +res=`expr 72 \* $X / 800` + +cat "$in" | + gs -sDEVICE=ppmraw -sOutputFile=- -g$IRES -r$res \ + -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q - | + pnmflip -r270 >"$out"