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, BufferAction.__init__(self, name, buffer, expr, elem, minus, source,
fromExpr) fromExpr)
self.iter = iter # Name of the iterator variable used in the each loop self.iter = iter # Name of the iterator variable used in the each loop
def do(self): def do(self):
context = self.buffer.env.context context = self.buffer.env.context
# Check self.exprResult type # Check self.exprResult type
@ -195,7 +196,8 @@ class ForAction(BufferAction):
context[self.iter] = hiddenVariable context[self.iter] = hiddenVariable
else: else:
if self.exprResult: 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): class NullAction(BufferAction):
'''Action that does nothing. Used in conjunction with a "from" clause, it '''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 ' \ PDF_TO_IMG_ERROR = 'A PDF file could not be converted into images. Please ' \
'ensure that Ghostscript (gs) is installed on your ' \ 'ensure that Ghostscript (gs) is installed on your ' \
'system and the "gs" program is in the path.' '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: class DocImporter:
@ -186,7 +188,11 @@ def getSize(filePath, fileType):
if len(buf) == 5: if len(buf) == 5:
# else: invalid/corrupted GIF (bad header) # else: invalid/corrupted GIF (bad header)
x, y, u = struct.unpack("<HHB", buf) 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): class ImageImporter(DocImporter):
'''This class allows to import into the ODT result an image stored '''This class allows to import into the ODT result an image stored
@ -262,13 +268,14 @@ class ImageImporter(DocImporter):
self.sizeUnit = sizeUnit self.sizeUnit = sizeUnit
# Put CSS attributes from p_style in a dict. # Put CSS attributes from p_style in a dict.
self.cssAttrs = {} self.cssAttrs = {}
for attr in style.split(';'): if style:
if not attr.strip(): continue for attr in style.split(';'):
name, value = attr.strip().split(':') if not attr.strip(): continue
value = value.strip() name, value = attr.strip().split(':')
if value.endswith('px'): value = value[:-2] value = value.strip()
if value.isdigit(): value=int(value) if value.endswith('px'): value = value[:-2]
self.cssAttrs[name.strip()] = value if value.isdigit(): value=int(value)
self.cssAttrs[name.strip()] = value
def run(self): def run(self):
# Some shorcuts for the used xml namespaces # Some shorcuts for the used xml namespaces
@ -281,6 +288,16 @@ class ImageImporter(DocImporter):
i = self.importPath.rfind(self.pictFolder) i = self.importPath.rfind(self.pictFolder)
imagePath = self.importPath[i+1:].replace('\\', '/') imagePath = self.importPath[i+1:].replace('\\', '/')
self.fileNames[imagePath] = self.at 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. # Retrieve image size from self.size.
width = height = None width = height = None
if self.size: if self.size:

View file

@ -266,7 +266,7 @@ class Renderer:
return ifTrue return ifTrue
return ifFalse return ifFalse
imageFormats = ('png', 'jpeg', 'jpg', 'gif') imageFormats = ('png', 'jpeg', 'jpg', 'gif', 'svg')
ooFormats = ('odt',) ooFormats = ('odt',)
def importDocument(self, content=None, at=None, format=None, def importDocument(self, content=None, at=None, format=None,
anchor='as-char', wrapInPara=True, size=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', def __init__(self, app, out, appVersion='0.1.0',
pythonVersions=('2.6', '2.7'), 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. # app is the path to the Python package to Debianize.
self.app = app self.app = app
self.appName = os.path.basename(app) self.appName = os.path.basename(app)