2012-11-03

How to Fit ViewBox to Area of Drawing on SVG

  1. Delete attribute(width and height), from root element (the tag name is svg) of SVG document. Then save it as tmp.svg. notice: This process is needed to calculate position and area size of drawing correctly.
  2. With inkscape command, get position (X and Y) and area size (W and H) of drawing from tmp.svg. To do this, execute command "inkscape -X tmp.svg", in the same way for Y, W and H. See "inkscape --help"
  3. Change viewBox attribute to "X Y W H". The root element has this attribute.
here is an example:
#!/bin/zsh

INKSCAPE=$(whence inkscape)
if [ -z "${INKSCAPE}" ]; then
  echo "Command inkscape not found." > /dev/stderr
  exit 1
fi
CSHARP=$(whence csharp)
if [ -z "${CSHARP}" ]; then
  echo "Command csharp not found." > /dev/stderr
  exit 1
fi

ID=
INPUT=
while [ -n "$1" ]; do
  case $1 in
    -I)
      ID="$2"
      shift
    ;;
    *)
      INPUT="$1"
    ;;
  esac
  shift
done

if [ -z "${INPUT}" ]; then
  echo "No input file." > /dev/stderr
  exit 1
fi
if [ ! -f "${INPUT}" ]; then
  echo "File '${INPUT}' is not found." > /dev/stderr
  exit 1
fi

OPTS=()
if [ -n "${ID}" ]; then
  OPTS=(${OPTS} -I ${ID})
fi

TMPFILE=$(tempfile).svg
cat <<EOT | ${CSHARP} -reference:System.Xml.Linq > ${TMPFILE}
using System;
using System.Xml.Linq;

var doc = XDocument.Load(@"${INPUT}");
var width = doc.Root.Attribute(XName.Get("width"));
if (width != null) width.Remove();
var height = doc.Root.Attribute(XName.Get("height"));
if (height != null) height.Remove();

doc.Save(Console.Out);
EOT

X=$(${INKSCAPE} ${OPTS} -X ${TMPFILE})
Y=$(${INKSCAPE} ${OPTS} -Y ${TMPFILE})
W=$(${INKSCAPE} ${OPTS} -W ${TMPFILE})
H=$(${INKSCAPE} ${OPTS} -H ${TMPFILE})

cat <<EOT | ${CSHARP} -reference:System.Xml.Linq
using System;
using System.Xml.Linq;

var doc = XDocument.Load(@"${TMPFILE}");
var viewBoxValue = @"${X} ${Y} ${W} ${H}";

var viewBox = doc.Root.Attribute(XName.Get("viewBox"));
if (viewBox == null) doc.Root.Add(new XAttribute(XName.Get("viewBox"), viewBoxValue));
if (viewBox != null) viewBox.SetValue(viewBoxValue);

doc.Save(Console.Out);
EOT

2012-10-31

Today's Oneliner: Numbering Files

mkdir renamed
ls -1 *(.) | sort | awk '{ count++; printf "cp %s renamed/%04d\n",$1,count; }' | zsh
  1. get the list of files in the current directory
  2. sort the list with sort command by the required order: reversed, numeric, ignoring case, ...
  3. generate a shell script, and execute it.

2012-10-23

Disabling Auto-Indent for Current File

:setl noai nocin nosi inde=

Today's Oneliner

 % sudo emerge -uDN --keep-going @world

 * IMPORTANT: 7 news items need reading for repository 'gentoo'.
 * Use eselect news to read news items.

Calculating dependencies \

!!! Problem resolving dependencies for net-nds/openldap from @selected
... done!

!!! The ebuild selected to satisfy "net-nds/openldap" has unmet requirements.
- net-nds/openldap-2.4.33::gentoo USE="berkdb crypt cxx ipv6 (multilib) ssl
  syslog tcpd -debug -experim
passwd"

  The following REQUIRED_USE flag constraints are unsatisfied:
    cxx? ( sasl )

(dependency required by "@selected" [set])
(dependency required by "@world" [argument])
 % sudo equery d net-nds/openldap
 * These packages depend on net-nds/openldap:
app-admin/sudo-1.8.6_p3 (ldap ? >=net-nds/openldap-2.1.30-r1)
app-crypt/gnupg-2.0.19 (ldap ? net-nds/openldap)
app-text/acroread-9.5.1-r1 (ldap ? net-nds/openldap)
dev-libs/apr-util-1.4.1 (ldap ? =net-nds/openldap-2*)
dev-libs/cyrus-sasl-2.1.25-r3 (openldap ? net-nds/openldap)
gnome-base/gconf-3.2.5 (ldap ? net-nds/openldap)
kde-base/kdepimlibs-4.9.2 (ldap ? net-nds/openldap)
net-misc/curl-7.26.0 (ldap ? net-nds/openldap)
net-misc/openssh-6.1_p1 (ldap ? net-nds/openldap)
 % sudo USE="-ldap" emerge -1 $(equery -C d net-nds/openldap | awk '{ print "="$1}')
...snip...
 % sudo emerge -c net-nds/openldap
 % sudo emerge -uDN --keep-going @world

2012-09-01

Shown on Heading, not in Table of Contents

Issue

By the reason on layouting, sometimes there is "somestuff" I should write in a heading command such as \section. It is also shown in the table of contents by \addcontentsline used by \section. But "somestuff" should be enabled only on the heading, not the table of contents. How can I solve that?

Solution

In the document preemble (or style file):
\makeatletter

\let\if@notinaddcontentsline\iftrue
\let\@oldaddcontentsline\addcontentsline
\def\dontaddcontentsline#1{%
  \if@notinaddcontentsline #1 \fi}
\def\addcontentsline#1#2#3{%
  \let\if@notinaddcontentsline\iffalse  
  \@oldaddcontentsline{#1}{#2}{#3}
  \let\if@notinaddcontentsline\iftrue}

\makeatother
Then, write my heading:
\section{foobar\dontaddcontentsline{\\}baz}

2012-07-27

Failed to emerge =x11-drivers/xf86-video-vmware-12.0.2 with >=x11-base/xorg-server-1.12.99.902

The new ebuild (x11-drivers/xf86-video-vmware-12.0.2-r1) has been provided since Aug 8th, so this problem has already been resolved. Now, we can emerge it.