Skip to content

Instantly share code, notes, and snippets.

@ganadist
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganadist/99ea17cf86cd726cb6a0 to your computer and use it in GitHub Desktop.
Save ganadist/99ea17cf86cd726cb6a0 to your computer and use it in GitHub Desktop.
cairo clock svg convert script
#!/usr/bin/env python
import cairo
from gi.repository import Rsvg
scales = {
'antique': 1.26,
'default': 1.1,
'fdo': 1.25,
'funky': 1.2,
'glassy': 1.0,
'gremlin': 1.0,
'ipulse': 1.2,
'radium': 1.19,
'silvia': 1.15,
'simple': 1.27,
'tango': 1.27,
'zen': 1.27,
}
def convert_rsvg(theme, filename, outfile, size):
basename = os.path.basename(filename)
scale = scales[theme]
width, height = size, size
handle = Rsvg.Handle.new_from_file(filename)
d = handle.get_dimensions()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surface)
cr.translate((1 - scale) * width /2.0,
(1 - scale) * height /2.0)
cr.save()
cr.scale(scale * width/d.width, scale * height/d.height)
if 'hand' in basename:
cr.translate(d.width/2, d.height/2)
handle.render_cairo(cr)
cr.restore()
surface.write_to_png(outfile)
if __name__ == '__main__':
import sys, os, glob
source = sys.argv[1]
out = sys.argv[2]
scale = int(sys.argv[3])
out = os.path.join(out, 'res', 'drawable-hdpi')
if not os.path.isdir(out):
os.makedirs(out)
for filename in glob.glob(os.path.join(source, '*/*.svg')):
basename = filename[len(source):]
if basename.startswith('/'):
basename = basename[1:]
theme, basename = basename.split('/')
if theme.endswith('24'): continue
basename = basename.replace('.svg', '.png')
outname = theme.replace('-', '_') + '_' + basename.replace('-', '_')
outname = os.path.join(out, outname)
convert_rsvg(theme, filename, outname, scale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment