Web friendly SVG export - pixel-perfect scaling
@jk
Submitted by Josef Kufner Link to original bug (#777276)
Description
When exporting to SVG using dia diagram.dia -e diagram.svg
command, I get diagram with dimensions in centimeters:
<svg width="37cm" height="37cm" viewBox="19 18 721 724" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="translate(-0.5,-0.5)" id="Pozadí">
...
Using such settings the diagram is always blurry as it is always scaled. When using the picture on web, it is more desirable to keep the image as it was drawn on screen using Dia.
To obtain pixel-perfect results, I use this:
<svg width="721" height="724" viewBox="19 18 721 724" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="translate(-0.5,-0.5)" id="Pozadí">
...
Translate by 0.5 px makes 1px line to be exactly 1px on screen. Use of pixels instead of centimeters results in default scale 1:1 on screen.
To make conversion automatic I use following script:
sed -i components.svg \
-e 's/\.[0-9]\+//g' \
-e 's/<svg width="[^"]\+" height="[^"]\+" viewBox="\([0-9]\+\) \([0-9]\+\) \([0-9]\+\) \([0-9]\+\)"/<svg style="width:\3px;height:\4px;" viewBox="\1 \2 \3 \4"/' \
-e 's/^ <g / <g transform="translate(-0.5,-0.5)" /'
It is far from perfect and surely not universal, but it does the trick in my case.
Version: 0.97.x