You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
5.6 KiB
Python
182 lines
5.6 KiB
Python
# The clock style module
|
|
|
|
import os, re, gzip, tarfile, tempfile, shutil
|
|
|
|
|
|
from AppLibFinder import AppLibFinder
|
|
|
|
appLibFinder = AppLibFinder()
|
|
appLibFinder.check()
|
|
|
|
from StyleInfo import StyleInfo
|
|
|
|
|
|
# style xml filename
|
|
XML_FILENAME = 'style.xml'
|
|
|
|
|
|
class Style:
|
|
def __init__(self, path):
|
|
self.path = path
|
|
self.info = StyleInfo()
|
|
|
|
def load(self, name):
|
|
#if os.path.exists(filename):
|
|
self.name = name
|
|
self.title = name
|
|
# try and load in the images for this style
|
|
if os.path.exists(self.path + XML_FILENAME):
|
|
info = StyleInfo()
|
|
info.load(self.path + XML_FILENAME)
|
|
self.images = {'Clock': info.clock.image,
|
|
'Hour': info.hands['Hour'].image,
|
|
'Minute': info.hands['Minute'].image,
|
|
'Second': info.hands['Second'].image
|
|
}
|
|
else:
|
|
# if the xml file cannot load then setup the default basic style
|
|
self.images = {'Clock': 'Clock.png',
|
|
'Hour': 'Hour.png',
|
|
'Minute': 'Minute.png',
|
|
'Second': 'Second.png'
|
|
}
|
|
self.loadSizes()
|
|
|
|
def loadSizes(self):
|
|
# search the folders for valid sizes
|
|
self.sizes = []
|
|
files = os.listdir(self.path)
|
|
for file in files:
|
|
match = re.match('(\d+)x(\d+)', file)
|
|
if match != None:
|
|
self.sizes.append((int(match.group(1)), int(match.group(2))))
|
|
self.sizes.sort()
|
|
|
|
def getImageFile(self, imageName, imageSize):
|
|
# get the full path and filename for this style, based on the size required
|
|
# for this image name.
|
|
# The SVG resize method will automatically create a folder with the nearest
|
|
# size available
|
|
filename = ''
|
|
try:
|
|
filename = self.images[imageName]
|
|
except:
|
|
pass
|
|
if filename == '':
|
|
return filename
|
|
# now go through all of the available sizes looking for the nearest size
|
|
size = (100, 100)
|
|
# calculate the nearest size to the requested size
|
|
imageSize = self.calcSVGSize(imageSize)
|
|
# now loop around the sizes available and find the one
|
|
for size in self.sizes:
|
|
# check to see if the size has valid files
|
|
if self.isImageFolderValid(size):
|
|
# if the size is found or a bigger size is found then return the file
|
|
if size == imageSize or size > imageSize:
|
|
return self.path + ('/%dx%d/' % size ) + filename
|
|
|
|
# if none found and we need to return the largest size available
|
|
if imageSize > size:
|
|
return self.path + ('/%dx%d/' % size ) + filename
|
|
|
|
# if no sizes are found try to return the default 100x100 or the files in the style folder
|
|
if os.path.exists(self.path + '/100x100'):
|
|
return self.path + '/100x100/' + filename
|
|
else:
|
|
# if all else fails then return the filename with no size folder
|
|
return self.path + '/' + filename
|
|
|
|
def calcSVGSize(self, size, grain = 100):
|
|
size = ( int(round(float(size[0]) / grain) * grain), int(round(float(size[1]) / grain) * grain) )
|
|
return size
|
|
|
|
def resizeSVG(self, size):
|
|
global appLibFinder
|
|
# use Inkscape to resize the images
|
|
# do we have the apps to do this?
|
|
if not appLibFinder.IS_SVG_RENDERING:
|
|
return
|
|
# check to see if we have any svg files
|
|
if not os.path.exists(self.path + '/svg'):
|
|
return
|
|
if size <= (0, 0):
|
|
return
|
|
size = self.calcSVGSize(size)
|
|
# we leave the default sizes alone
|
|
if size[0] <= 200 or size[1] <= 200:
|
|
return
|
|
print('resizing SVG to', size)
|
|
# calc the new size folder in the style
|
|
toFolder = self.path + '/' + ( '%dx%d' % size)
|
|
# check to see if the size folder exists, if not then create it
|
|
if not os.path.exists(toFolder):
|
|
os.mkdir(toFolder)
|
|
# now iterate through all of the files for this style and resize them using Inkscape
|
|
for filename in self.images.values():
|
|
fileInfo = os.path.splitext(filename)
|
|
self.svg2png(self.path + '/svg/' + fileInfo[0] + '.svg', toFolder + '/' + filename, size)
|
|
|
|
# now reload the size into memory for use by this style
|
|
self.loadSizes()
|
|
|
|
def needToResizeSVG(self, size):
|
|
global appLibFinder
|
|
# method returns true if we need to generate a new series of sizes from SVG
|
|
# see if we have Inkscape or rsvg to redraw png files using svg
|
|
if not appLibFinder.IS_SVG_RENDERING:
|
|
return False
|
|
# check to see if we have any svg files
|
|
if not os.path.exists(self.path + '/svg'):
|
|
return False
|
|
if size <= (0, 0):
|
|
return False
|
|
size = self.calcSVGSize(size)
|
|
# we leave the default sizes alone if it is within size
|
|
if size[0] <= 200 or size[1] <= 200:
|
|
return False
|
|
# return True if the image folder needs rebuilding
|
|
return not self.isImageFolderValid(size)
|
|
|
|
def isImageFolderValid(self, size):
|
|
toFolder = self.path + '/' + ( '%dx%d' % size)
|
|
# check to see if the size folder exists in the style
|
|
if os.path.exists(toFolder):
|
|
# now go through all of th files and see if they already exist
|
|
for filename in self.images.values():
|
|
if not os.path.exists(toFolder + '/' + filename):
|
|
# if a file does not exist then we need to resize
|
|
return False
|
|
else:
|
|
# folder does not exist
|
|
return False
|
|
#folder exists and all of the files are present
|
|
return True
|
|
|
|
def svg2png(self, svgFile, pngFile, size):
|
|
global appLibFinder
|
|
# command to call Inkscape or rsvg to generate png files at different sizes
|
|
if os.path.exists(pngFile):
|
|
os.unlink(pngFile)
|
|
command = ''
|
|
if appLibFinder.IS_INKSCAPE_LOADED:
|
|
command = 'inkscape -C -d 92 '
|
|
command += '-w %d ' % size[0]
|
|
command += '-h %d ' % size[1]
|
|
command += svgFile + ' '
|
|
command += '-o ' + pngFile + ' '
|
|
elif appLibFinder.IS_RSVG_LOADED:
|
|
command = 'rsvg -d 92 -p 92 '
|
|
command += '-w %d ' % size[0]
|
|
command += '-h %d ' % size[1]
|
|
command += svgFile + ' '
|
|
command += pngFile + ' '
|
|
if command != '':
|
|
print(command)
|
|
os.system(command)
|
|
|
|
def hasSVG(self):
|
|
# return True if this style has svg files
|
|
return os.path.exists(self.path + '/svg')
|
|
|