first commit
This commit is contained in:
parent
f2c51c5042
commit
1ae917e207
3 changed files with 170 additions and 0 deletions
26
README.md
Normal file
26
README.md
Normal file
|
@ -0,0 +1,26 @@
|
|||
# svg-to-dia
|
||||
|
||||
[Dia](http://dia-installer.de/) is an open-source diagram editor.
|
||||
It supports [SVG pictograms](http://dia-installer.de/shapes/index.html).
|
||||
**svg-to-dia** converts ordinary SVG in the format supported by Dia.
|
||||
|
||||
# How to use
|
||||
|
||||
- Prepare files:
|
||||
```
|
||||
svg-to-dia --name my-shapes my-file1.svg my-file2.svg my-file3.svg
|
||||
```
|
||||
the above command will create a sheets/ and a shapes/ folders.
|
||||
|
||||
- Move the generated files to dia's folder
|
||||
```
|
||||
mv sheets/* ~/.dia/sheets
|
||||
mv shapes/* ~/.dia/shapes
|
||||
```
|
||||
|
||||
- Don't forget to restart dia
|
||||
- If you intend to publish, make sure to edit the `.sheet` files to add human-readable descriptions (multiple languages if you can)
|
||||
- ... and to edit the `.shape` files to add better coordinates of attachment points
|
||||
|
||||
# License
|
||||
svg-to-dia is released in the public domain, see [UNLICENSE file](UNLICENSE).
|
24
UNLICENSE
Normal file
24
UNLICENSE
Normal file
|
@ -0,0 +1,24 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
120
svg-to-dia.py
Executable file
120
svg-to-dia.py
Executable file
|
@ -0,0 +1,120 @@
|
|||
#!/usr/bin/env python3
|
||||
# This is free and unencumbered software released into the public domain.
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import xml.etree.ElementTree as ET
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import getpass
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
|
||||
@dataclass
|
||||
class Sheet:
|
||||
name: str
|
||||
author: str
|
||||
description: str
|
||||
objects: dict
|
||||
|
||||
|
||||
def build_sheet(sheet):
|
||||
xsheet = ET.Element(
|
||||
'sheet',
|
||||
attrib={'xmlns': 'http://www.lysator.liu.se/~alla/dia/dia-sheet-ns'}
|
||||
)
|
||||
if sheet.name:
|
||||
ET.SubElement(xsheet, 'name').text = sheet.name
|
||||
if sheet.author:
|
||||
ET.SubElement(xsheet, 'created_by').text = sheet.author
|
||||
if sheet.description:
|
||||
ET.SubElement(xsheet, 'description').text = sheet.description
|
||||
|
||||
xcontents = ET.SubElement(xsheet, 'contents')
|
||||
for oname, odescription in sheet.objects.items():
|
||||
xobj = ET.SubElement(xcontents, 'object')
|
||||
xobj.attrib['name'] = oname
|
||||
ET.SubElement(xobj, 'description').text = odescription
|
||||
return xsheet
|
||||
|
||||
|
||||
def get_float(s):
|
||||
return float(re.match(r'\d+([.]\d*)?', s)[0])
|
||||
|
||||
|
||||
def build_shape(svg_path):
|
||||
orig_xsvg = ET.parse(str(svg_path)).getroot()
|
||||
|
||||
xshape = ET.Element('shape', attrib={'xmlns': 'http://www.daa.com.au/~james/dia-shape-ns'})
|
||||
ET.SubElement(xshape, 'name').text = svg_path.stem
|
||||
ET.SubElement(xshape, 'icon').text = f"{svg_path.stem}.png"
|
||||
|
||||
width = int(get_float(orig_xsvg.attrib['width']))
|
||||
height = int(get_float(orig_xsvg.attrib['height']))
|
||||
xconns = ET.SubElement(xshape, 'connections')
|
||||
points = [
|
||||
(0, 0),
|
||||
(0, width // 2),
|
||||
(0, width),
|
||||
(height // 2, 0),
|
||||
(height, 0),
|
||||
]
|
||||
for x, y in points:
|
||||
ET.SubElement(xconns, 'point', attrib={'x': str(x), 'y': str(y)})
|
||||
|
||||
ET.SubElement(xshape, 'aspectratio', attrib={'type': 'fixed'})
|
||||
xshape.append(orig_xsvg)
|
||||
|
||||
return xshape
|
||||
|
||||
|
||||
def main():
|
||||
aparser = ArgumentParser()
|
||||
aparser.add_argument('--name', default='TODO')
|
||||
aparser.add_argument('svg_file', nargs='+')
|
||||
args = aparser.parse_args()
|
||||
|
||||
psheets = Path('sheets')
|
||||
psheets.mkdir(exist_ok=True)
|
||||
pshapes = Path('shapes')
|
||||
pshapes.mkdir(exist_ok=True)
|
||||
|
||||
sheet = Sheet(
|
||||
name=args.name,
|
||||
author=getpass.getuser(),
|
||||
description=f"TODO: fill {args.name} description",
|
||||
objects={}
|
||||
)
|
||||
|
||||
for svg_file in args.svg_file:
|
||||
svg_file = Path(svg_file)
|
||||
|
||||
xshape = build_shape(svg_file)
|
||||
# ET.indent(xshape)
|
||||
tshape = ET.tostring(xshape, encoding='unicode')
|
||||
shape_path = pshapes.joinpath(f"{svg_file.stem}.shape")
|
||||
shape_path.write_text(tshape)
|
||||
print(f"wrote {shape_path}")
|
||||
|
||||
png_file = f'{pshapes}/{svg_file.stem}.png'
|
||||
subprocess.check_call([
|
||||
'convert',
|
||||
str(svg_file),
|
||||
'-resize',
|
||||
'64x64',
|
||||
png_file,
|
||||
])
|
||||
print(f"wrote {png_file}")
|
||||
|
||||
sheet.objects[svg_file.stem] = f"TODO: fill description for {svg_file.stem}"
|
||||
|
||||
xsheet = build_sheet(sheet)
|
||||
# ET.indent(xsheet)
|
||||
tsheet = ET.tostring(xsheet, encoding="unicode")
|
||||
sheet_path = psheets.joinpath(f"{args.name}.sheet")
|
||||
sheet_path.write_text(tsheet)
|
||||
print(f"wrote {sheet_path}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in a new issue