appy.pod: allow to import SVG files (requires installing imagemagick).

This commit is contained in:
Gaetan Delannay 2012-01-24 17:04:40 +01:00
parent a1b048153f
commit 298ed34b5b
4 changed files with 30 additions and 11 deletions

View file

@ -127,6 +127,7 @@ class ForAction(BufferAction):
BufferAction.__init__(self, name, buffer, expr, elem, minus, source,
fromExpr)
self.iter = iter # Name of the iterator variable used in the each loop
def do(self):
context = self.buffer.env.context
# Check self.exprResult type
@ -195,7 +196,8 @@ class ForAction(BufferAction):
context[self.iter] = hiddenVariable
else:
if self.exprResult:
del context[self.iter]
if self.iter in context: # May not be the case on error.
del context[self.iter]
class NullAction(BufferAction):
'''Action that does nothing. Used in conjunction with a "from" clause, it

View file

@ -29,6 +29,8 @@ FILE_NOT_FOUND = "'%s' does not exist or is not a file."
PDF_TO_IMG_ERROR = 'A PDF file could not be converted into images. Please ' \
'ensure that Ghostscript (gs) is installed on your ' \
'system and the "gs" program is in the path.'
CONVERT_ERROR = 'Program "convert", from imagemagick, must be installed and ' \
'in the path for converting a SVG file into a PNG file.'
# ------------------------------------------------------------------------------
class DocImporter:
@ -186,7 +188,11 @@ def getSize(filePath, fileType):
if len(buf) == 5:
# else: invalid/corrupted GIF (bad header)
x, y, u = struct.unpack("<HHB", buf)
return float(x)/pxToCm, float(y)/pxToCm
f.close()
if x and y:
return float(x)/pxToCm, float(y)/pxToCm
else:
return x, y
class ImageImporter(DocImporter):
'''This class allows to import into the ODT result an image stored
@ -262,13 +268,14 @@ class ImageImporter(DocImporter):
self.sizeUnit = sizeUnit
# Put CSS attributes from p_style in a dict.
self.cssAttrs = {}
for attr in style.split(';'):
if not attr.strip(): continue
name, value = attr.strip().split(':')
value = value.strip()
if value.endswith('px'): value = value[:-2]
if value.isdigit(): value=int(value)
self.cssAttrs[name.strip()] = value
if style:
for attr in style.split(';'):
if not attr.strip(): continue
name, value = attr.strip().split(':')
value = value.strip()
if value.endswith('px'): value = value[:-2]
if value.isdigit(): value=int(value)
self.cssAttrs[name.strip()] = value
def run(self):
# Some shorcuts for the used xml namespaces
@ -281,6 +288,16 @@ class ImageImporter(DocImporter):
i = self.importPath.rfind(self.pictFolder)
imagePath = self.importPath[i+1:].replace('\\', '/')
self.fileNames[imagePath] = self.at
# In the case of SVG files, perform an image conversion to PNG
if imagePath.endswith('.svg'):
newImportPath = os.path.splitext(self.importPath)[0] + '.png'
err= os.system('convert "%s" "%s"'% (self.importPath,newImportPath))
if err:
raise Exception(CONVERT_ERROR)
os.remove(self.importPath)
self.importPath = newImportPath
imagePath = os.path.splitext(imagePath)[0] + '.png'
self.format = 'png'
# Retrieve image size from self.size.
width = height = None
if self.size:

View file

@ -266,7 +266,7 @@ class Renderer:
return ifTrue
return ifFalse
imageFormats = ('png', 'jpeg', 'jpg', 'gif')
imageFormats = ('png', 'jpeg', 'jpg', 'gif', 'svg')
ooFormats = ('odt',)
def importDocument(self, content=None, at=None, format=None,
anchor='as-char', wrapInPara=True, size=None,

View file

@ -21,7 +21,7 @@ class Debianizer:
def __init__(self, app, out, appVersion='0.1.0',
pythonVersions=('2.6', '2.7'),
depends=('zope2.12', 'openoffice.org')):
depends=('zope2.12', 'openoffice.org', 'imagemagick')):
# app is the path to the Python package to Debianize.
self.app = app
self.appName = os.path.basename(app)