Wikipedia:WikiProject Chemistry/Structure drawing workgroup/Mysid's script

From Wikipedia, the free encyclopedia
#!/usr/bin/perl -i.old

# original source: https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Chemistry/Structure_drawing_workgroup/Mysid%27s_script

# Note: "-i.old" means original file will be renamed to <filename>.old
# Modify svg file from BKchem to work with librsvg, for use on Wikimedia.

use warnings;
use strict;

my $font_size = 24;

while (<>) {
    #Sans is the most general font definition we can use, and librsvg chokes
    #on Helvetica.
    s/helvetica/Sans/gi;
    if(m#<text[^>]+font-size="([0-9]+)pt"#){
	$font_size = $1;
    }
    # Convert relative font sizes to absolute font sizes
    s#(font-size=")([0-9]+)%(")#$1.($2*$font_size/100)."pt".$3#ge;
    # Replace baseline-shift="super" with a numeric baseline-shift
    if (/y="([\d\.]+)">.*<tspan baseline-shift="super"/) {
	my $vy = $1;
	my $oy = $1-4;
	s/baseline-shift="super"/y="$oy"/g;
	s#</tspan>([^<]+)<#</tspan><tspan y="$vy">$1</tspan><#g;
    }
    # Replace baseline-shift="sub" with a numeric baseline-shift
    if (/y="([\d\.]+)">.*<tspan baseline-shift="sub"/) {
	my $vy = $1;
	my $oy = $1+3.25;
	s/baseline-shift="sub"/y="$oy"/g;
	s#</tspan>([^<]+)<#</tspan><tspan y="$vy">$1</tspan><#g;
    }
    #write each line out after mangling it.
    print $_;
}